diff options
author | Tom Sepez <tsepez@chromium.org> | 2016-01-05 10:06:09 -0800 |
---|---|---|
committer | Tom Sepez <tsepez@chromium.org> | 2016-01-05 10:06:09 -0800 |
commit | ed34cdf99d5a4b33e57f81f9244a311f6fb86db3 (patch) | |
tree | ee5c9711c68cb3971ce058400a6f777324873e60 | |
parent | 5dac8047f02c937a5a3546a8cc5b352db2188d97 (diff) | |
download | pdfium-ed34cdf99d5a4b33e57f81f9244a311f6fb86db3.tar.xz |
Make FPDF_WIDESTRING work regardless of endianness.
Given the helper routines, use it in the find bookmark test
instead of just trusting another bookmarks title.
R=thestig@chromium.org
Review URL: https://codereview.chromium.org/1554363002 .
-rw-r--r-- | fpdfsdk/src/fpdfdoc_embeddertest.cpp | 23 | ||||
-rw-r--r-- | fpdfsdk/src/fpdftext_embeddertest.cpp | 16 | ||||
-rw-r--r-- | samples/pdfium_test.cc | 2 | ||||
-rw-r--r-- | testing/embedder_test_timer_handling_delegate.h | 4 | ||||
-rw-r--r-- | testing/test_support.cpp | 15 | ||||
-rw-r--r-- | testing/test_support.h | 6 |
6 files changed, 49 insertions, 17 deletions
diff --git a/fpdfsdk/src/fpdfdoc_embeddertest.cpp b/fpdfsdk/src/fpdfdoc_embeddertest.cpp index 38a366268c..260f25f309 100644 --- a/fpdfsdk/src/fpdfdoc_embeddertest.cpp +++ b/fpdfsdk/src/fpdfdoc_embeddertest.cpp @@ -7,6 +7,7 @@ #include "public/fpdfview.h" #include "testing/embedder_test.h" #include "testing/fx_string_testhelpers.h" +#include "testing/test_support.h" #include "testing/gtest/include/gtest/gtest.h" class FPDFDocEmbeddertest : public EmbedderTest {}; @@ -101,20 +102,28 @@ TEST_F(FPDFDocEmbeddertest, Bookmarks) { } TEST_F(FPDFDocEmbeddertest, FindBookmarks) { - // Open a file with two bookmarks, and extract the first. + // Open a file with two bookmarks. EXPECT_TRUE(OpenDocument("bookmarks.pdf")); - unsigned short buf[128]; - FPDF_BOOKMARK child = FPDFBookmark_GetFirstChild(document(), nullptr); + // Find the first one, based on its known title. + FPDF_WIDESTRING title = GetFPDFWideString(L"A Good Beginning"); + FPDF_BOOKMARK child = FPDFBookmark_Find(document(), title); EXPECT_NE(nullptr, child); + + // Check that the string matches. + unsigned short buf[128]; EXPECT_EQ(34, FPDFBookmark_GetTitle(child, buf, sizeof(buf))); EXPECT_EQ(CFX_WideString(L"A Good Beginning"), CFX_WideString::FromUTF16LE(buf, 16)); - // Find the same one again using the title. - EXPECT_EQ(child, FPDFBookmark_Find(document(), buf)); + // Check that it is them same as the one returned by GetFirstChild. + EXPECT_EQ(child, FPDFBookmark_GetFirstChild(document(), nullptr)); // Try to find one using a non-existent title. - buf[0] = 'X'; - EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), buf)); + FPDF_WIDESTRING bad_title = GetFPDFWideString(L"A BAD Beginning"); + EXPECT_EQ(nullptr, FPDFBookmark_Find(document(), bad_title)); + + // Alas, the typedef includes the "const". + free(const_cast<unsigned short*>(title)); + free(const_cast<unsigned short*>(bad_title)); } diff --git a/fpdfsdk/src/fpdftext_embeddertest.cpp b/fpdfsdk/src/fpdftext_embeddertest.cpp index 6c1ae4ceb8..3772686c88 100644 --- a/fpdfsdk/src/fpdftext_embeddertest.cpp +++ b/fpdfsdk/src/fpdftext_embeddertest.cpp @@ -5,6 +5,7 @@ #include "public/fpdf_text.h" #include "public/fpdfview.h" #include "testing/embedder_test.h" +#include "testing/test_support.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -143,11 +144,10 @@ TEST_F(FPDFTextEmbeddertest, TextSearch) { FPDF_TEXTPAGE textpage = FPDFText_LoadPage(page); EXPECT_NE(nullptr, textpage); - // Avoid issues with system wchar_t width vs. FPDF_WideString. - const unsigned short nope[] = {'n', 'o', 'p', 'e', '\0'}; - const unsigned short world[] = {'w', 'o', 'r', 'l', 'd', '\0'}; - const unsigned short world_caps[] = {'W', 'O', 'R', 'L', 'D', '\0'}; - const unsigned short world_substr[] = {'o', 'r', 'l', 'd', '\0'}; + FPDF_WIDESTRING nope = GetFPDFWideString(L"nope"); + FPDF_WIDESTRING world = GetFPDFWideString(L"world"); + FPDF_WIDESTRING world_caps = GetFPDFWideString(L"WORLD"); + FPDF_WIDESTRING world_substr = GetFPDFWideString(L"orld"); // No occurences of "nope" in test page. FPDF_SCHHANDLE search = FPDFText_FindStart(textpage, nope, 0, 0); @@ -239,6 +239,12 @@ TEST_F(FPDFTextEmbeddertest, TextSearch) { FPDFText_ClosePage(textpage); UnloadPage(page); + + // Alas, the typedef includes the "const". + free(const_cast<unsigned short*>(nope)); + free(const_cast<unsigned short*>(world)); + free(const_cast<unsigned short*>(world_caps)); + free(const_cast<unsigned short*>(world_substr)); } // Test that the page has characters despite a bad stream length. diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index 89430f6dfd..9816bb8a31 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -196,7 +196,7 @@ void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) { int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING, int, int) { - std::wstring platform_string = GetWideString(msg); + std::wstring platform_string = GetPlatformWString(msg); printf("Alert: %ls\n", platform_string.c_str()); return 0; } diff --git a/testing/embedder_test_timer_handling_delegate.h b/testing/embedder_test_timer_handling_delegate.h index 74e346e830..a704bd49df 100644 --- a/testing/embedder_test_timer_handling_delegate.h +++ b/testing/embedder_test_timer_handling_delegate.h @@ -21,8 +21,8 @@ class EmbedderTestTimerHandlingDelegate : public EmbedderTest::Delegate { int type_in, int icon_in) : type(type_in), icon(icon_in) { - message = GetWideString(message_in); - title = GetWideString(title_in); + message = GetPlatformWString(message_in); + title = GetPlatformWString(title_in); } std::wstring message; diff --git a/testing/test_support.cpp b/testing/test_support.cpp index f71f6f8b4f..c7ab10ee3c 100644 --- a/testing/test_support.cpp +++ b/testing/test_support.cpp @@ -98,7 +98,7 @@ char* GetFileContents(const char* filename, size_t* retlen) { return buffer; } -std::wstring GetWideString(FPDF_WIDESTRING wstr) { +std::wstring GetPlatformWString(FPDF_WIDESTRING wstr) { if (!wstr) return nullptr; @@ -114,6 +114,19 @@ std::wstring GetWideString(FPDF_WIDESTRING wstr) { return platform_string; } +FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr) { + size_t length = sizeof(uint16_t) * (wstr.length() + 1); + unsigned char* ptr = static_cast<unsigned char*>(malloc(length)); + size_t i = 0; + for (wchar_t w : wstr) { + ptr[i++] = w & 0xff; + ptr[i++] = (w >> 8) & 0xff; + } + ptr[i++] = 0; + ptr[i] = 0; + return reinterpret_cast<FPDF_WIDESTRING>(ptr); +} + #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA bool InitializeV8ForPDFium(const std::string& exe_path, diff --git a/testing/test_support.h b/testing/test_support.h index a2241144a4..d48d5596f4 100644 --- a/testing/test_support.h +++ b/testing/test_support.h @@ -19,7 +19,11 @@ char* GetFileContents(const char* filename, size_t* retlen); // Converts a FPDF_WIDESTRING to a std::wstring. // Deals with differences between UTF16LE and wchar_t. -std::wstring GetWideString(FPDF_WIDESTRING wstr); +std::wstring GetPlatformWString(const FPDF_WIDESTRING wstr); + +// Returns a newly mallocated FPDF_WIDESTRING (caller must free()). +// Deals with differences between UTF16LE and wchar_t. +FPDF_WIDESTRING GetFPDFWideString(const std::wstring& wstr); #ifdef PDF_ENABLE_V8 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |