diff options
-rw-r--r-- | BUILD.gn | 39 | ||||
-rw-r--r-- | pdfium.gyp | 33 | ||||
-rw-r--r-- | samples/BUILD.gn | 7 | ||||
-rw-r--r-- | samples/pdfium_test.cc | 121 | ||||
-rw-r--r-- | samples/samples.gyp | 1 | ||||
-rw-r--r-- | testing/embedder_test.cpp | 118 | ||||
-rw-r--r-- | testing/test_support.cpp | 140 | ||||
-rw-r--r-- | testing/test_support.h | 44 |
8 files changed, 271 insertions, 232 deletions
@@ -133,6 +133,39 @@ static_library("pdfium") { } } +source_set("test_support") { + testonly = true + sources = [ + "testing/fx_string_testhelpers.cpp", + "testing/fx_string_testhelpers.h", + "testing/test_support.cpp", + "testing/test_support.h", + ] + deps = [ + "//testing/gmock", + "//testing/gtest", + ":pdfium", + ] + include_dirs = [ + "." + ] + if (pdf_enable_v8) { + deps += [ + "//v8", + "//v8:v8_libplatform", + ] + include_dirs += [ + "//v8", + "//v8/include", + ] + } + configs -= [ "//build/config/compiler:chromium_code" ] + configs += [ + ":pdfium_config", + "//build/config/compiler:no_chromium_code", + ] +} + # Targets below this are only visible within this file (and to the # top-level gn_visibility target used to help gn_all build everything). visibility = [ @@ -731,14 +764,13 @@ test("pdfium_unittests") { "core/src/fxcrt/fx_basic_wstring_unittest.cpp", "core/src/fxcrt/fx_bidi_unittest.cpp", "core/src/fxcrt/fx_system_unittest.cpp", - "testing/fx_string_testhelpers.cpp", - "testing/fx_string_testhelpers.h", "third_party/base/nonstd_unique_ptr_unittest.cpp", ] deps = [ "//testing/gtest", "//testing/gtest:gtest_main", ":pdfium", + ":test_support", ] include_dirs = [ "." ] configs -= [ "//build/config/compiler:chromium_code" ] @@ -763,13 +795,12 @@ test("pdfium_embeddertests") { "testing/embedder_test.h", "testing/embedder_test_mock_delegate.h", "testing/embedder_test_timer_handling_delegate.h", - "testing/fx_string_testhelpers.cpp", - "testing/fx_string_testhelpers.h", ] deps = [ "//testing/gmock", "//testing/gtest", ":pdfium", + ":test_support", ] include_dirs = [ "." diff --git a/pdfium.gyp b/pdfium.gyp index 6ea3f4ab07..2c1117277d 100644 --- a/pdfium.gyp +++ b/pdfium.gyp @@ -721,6 +721,7 @@ '<(DEPTH)/testing/gtest.gyp:gtest_main', '<(DEPTH)/testing/gtest.gyp:gtest', 'pdfium', + 'test_support', ], 'include_dirs': [ '<(DEPTH)' @@ -744,6 +745,7 @@ '<(DEPTH)/testing/gmock.gyp:gmock', '<(DEPTH)/testing/gtest.gyp:gtest', 'pdfium', + 'test_support', ], 'include_dirs': [ '<(DEPTH)', @@ -762,8 +764,6 @@ 'testing/embedder_test.h', 'testing/embedder_test_mock_delegate.h', 'testing/embedder_test_timer_handling_delegate.h', - 'testing/fx_string_testhelpers.cpp', - 'testing/fx_string_testhelpers.h', ], 'conditions': [ ['pdf_enable_v8==1', { @@ -781,5 +781,34 @@ }], ], }, + { + 'target_name': 'test_support', + 'type': 'static_library', + 'dependencies': [ + '<(DEPTH)/testing/gmock.gyp:gmock', + '<(DEPTH)/testing/gtest.gyp:gtest', + ], + 'include_dirs': [ + '<(DEPTH)', + ], + 'sources': [ + 'testing/fx_string_testhelpers.cpp', + 'testing/fx_string_testhelpers.h', + 'testing/test_support.cpp', + 'testing/test_support.h', + ], + 'conditions': [ + ['pdf_enable_v8==1', { + 'include_dirs': [ + '<(DEPTH)/v8', + '<(DEPTH)/v8/include', + ], + 'dependencies': [ + '<(DEPTH)/v8/tools/gyp/v8.gyp:v8', + '<(DEPTH)/v8/tools/gyp/v8.gyp:v8_libplatform', + ], + }], + ], + }, ], } diff --git a/samples/BUILD.gn b/samples/BUILD.gn index 5749de90f1..f3f6bc1a10 100644 --- a/samples/BUILD.gn +++ b/samples/BUILD.gn @@ -5,6 +5,7 @@ import("../pdfium.gni") group("samples") { + testonly = true deps = [ ":pdfium_test", ":pdfium_diff", @@ -25,12 +26,14 @@ config("pdfium_samples_config") { } executable("pdfium_test") { + testonly = true sources = [ "image_diff_png.cc", "pdfium_test.cc", ] deps = [ - "//third_party/pdfium", + "//third_party/pdfium:pdfium", + "//third_party/pdfium:test_support", # Regardless of whether the library ships against system freetype, # always link this binary against the bundled one for consistency # of results across platforms. @@ -50,6 +53,7 @@ executable("pdfium_test") { } executable("pdfium_diff") { + testonly = true sources = [ "image_diff.cc", "image_diff_png.cc", @@ -67,6 +71,7 @@ executable("pdfium_diff") { } static_library("fx_lpng") { + testonly = true sources = [ "fx_lpng/include/fx_png.h", "fx_lpng/src/fx_png.c", diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc index f15e19c75b..10504251c7 100644 --- a/samples/pdfium_test.cc +++ b/samples/pdfium_test.cc @@ -18,7 +18,9 @@ #include "../public/fpdf_formfill.h" #include "../public/fpdf_text.h" #include "../public/fpdfview.h" +#include "../testing/test_support.h" #include "image_diff_png.h" + #ifdef PDF_ENABLE_V8 #include "v8/include/libplatform/libplatform.h" #include "v8/include/v8.h" @@ -26,9 +28,6 @@ #ifdef _WIN32 #define snprintf _snprintf -#define PATH_SEPARATOR '\\' -#else -#define PATH_SEPARATOR '/' #endif enum OutputFormat { @@ -51,76 +50,6 @@ struct Options { std::string font_directory; }; -// Reads the entire contents of a file into a newly malloc'd buffer. -static char* GetFileContents(const char* filename, size_t* retlen) { - FILE* file = fopen(filename, "rb"); - if (!file) { - fprintf(stderr, "Failed to open: %s\n", filename); - return nullptr; - } - (void)fseek(file, 0, SEEK_END); - size_t file_length = ftell(file); - if (!file_length) { - (void)fclose(file); - return nullptr; - } - (void)fseek(file, 0, SEEK_SET); - char* buffer = static_cast<char*>(malloc(file_length)); - if (!buffer) { - (void)fclose(file); - return nullptr; - } - size_t bytes_read = fread(buffer, 1, file_length, file); - (void)fclose(file); - if (bytes_read != file_length) { - fprintf(stderr, "Failed to read: %s\n", filename); - free(buffer); - return nullptr; - } - *retlen = bytes_read; - return buffer; -} - -#ifdef PDF_ENABLE_V8 -#ifdef V8_USE_EXTERNAL_STARTUP_DATA -// Returns the full path for an external V8 data file based on either -// the currect exectuable path or an explicit override. -static std::string GetFullPathForSnapshotFile(const Options& options, - const std::string& filename) { - std::string result; - if (!options.bin_directory.empty()) { - result = options.bin_directory; - if (*options.bin_directory.rbegin() != PATH_SEPARATOR) { - result += PATH_SEPARATOR; - } - } else if (!options.exe_path.empty()) { - size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR); - if (last_separator != std::string::npos) { - result = options.exe_path.substr(0, last_separator + 1); - } - } - result += filename; - return result; -} - -// Reads an extenal V8 data file from the |options|-indicated location, -// returing true on success and false on error. -static bool GetExternalData(const Options& options, - const std::string& bin_filename, - v8::StartupData* result_data) { - std::string full_path = GetFullPathForSnapshotFile(options, bin_filename); - size_t data_length = 0; - char* data_buffer = GetFileContents(full_path.c_str(), &data_length); - if (!data_buffer) { - return false; - } - result_data->data = const_cast<const char*>(data_buffer); - result_data->raw_size = static_cast<int>(data_length); - return true; -} -#endif // V8_USE_EXTERNAL_STARTUP_DATA -#endif // PDF_ENABLE_V8 - static bool CheckDimensions(int stride, int width, int height) { if (stride < 0 || width < 0 || height < 0) return false; @@ -408,28 +337,6 @@ bool ParseCommandLine(const std::vector<std::string>& args, return true; } -class TestLoader { - public: - TestLoader(const char* pBuf, size_t len); - - const char* m_pBuf; - size_t m_Len; -}; - -TestLoader::TestLoader(const char* pBuf, size_t len) - : m_pBuf(pBuf), m_Len(len) { -} - -int GetBlock(void* param, - unsigned long pos, - unsigned char* pBuf, - unsigned long size) { - TestLoader* pLoader = static_cast<TestLoader*>(param); - if (pos + size < pos || pos + size > pLoader->m_Len) return 0; - memcpy(pBuf, pLoader->m_pBuf + pos, size); - return 1; -} - FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { return true; } @@ -453,11 +360,10 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len, form_callbacks.m_pJsPlatform = &platform_callbacks; TestLoader loader(pBuf, len); - FPDF_FILEACCESS file_access; memset(&file_access, '\0', sizeof(file_access)); file_access.m_FileLen = static_cast<unsigned long>(len); - file_access.m_GetBlock = GetBlock; + file_access.m_GetBlock = TestLoader::GetBlock; file_access.m_Param = &loader; FX_FILEAVAIL file_avail; @@ -602,25 +508,14 @@ int main(int argc, const char* argv[]) { } #ifdef PDF_ENABLE_V8 - v8::V8::InitializeICU(); - v8::Platform* platform = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(platform); - v8::V8::Initialize(); - - // By enabling predictable mode, V8 won't post any background tasks. - static const char predictable_flag[] = "--predictable"; - v8::V8::SetFlagsFromString(predictable_flag, - static_cast<int>(strlen(predictable_flag))); - + v8::Platform* platform; #ifdef V8_USE_EXTERNAL_STARTUP_DATA v8::StartupData natives; v8::StartupData snapshot; - if (!GetExternalData(options, "natives_blob.bin", &natives) || - !GetExternalData(options, "snapshot_blob.bin", &snapshot)) { - return 1; - } - v8::V8::SetNativesDataBlob(&natives); - v8::V8::SetSnapshotDataBlob(&snapshot); + InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives, + &snapshot, &platform); +#else // V8_USE_EXTERNAL_STARTUP_DATA + InitializeV8ForPDFium(&platform); #endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // PDF_ENABLE_V8 diff --git a/samples/samples.gyp b/samples/samples.gyp index 496a274da7..35b62b7fcf 100644 --- a/samples/samples.gyp +++ b/samples/samples.gyp @@ -34,6 +34,7 @@ 'dependencies': [ 'fx_lpng', '../pdfium.gyp:pdfium', + '../pdfium.gyp:test_support', # Regardless of whether the library ships against system freetype, # always link this binary against the bundled one for consistency # of results across platforms. diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp index 9d99084050..723bad7f8a 100644 --- a/testing/embedder_test.cpp +++ b/testing/embedder_test.cpp @@ -5,9 +5,6 @@ #include "embedder_test.h" #include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <list> #include <string> @@ -16,6 +13,7 @@ #include "../public/fpdf_text.h" #include "../public/fpdfview.h" +#include "test_support.h" #include "testing/gmock/include/gmock/gmock.h" #ifdef PDF_ENABLE_V8 @@ -23,103 +21,10 @@ #include "v8/include/v8.h" #endif // PDF_ENABLE_V8 -#ifdef _WIN32 -#define snprintf _snprintf -#define PATH_SEPARATOR '\\' -#else -#define PATH_SEPARATOR '/' -#endif - namespace { - const char* g_exe_path_ = nullptr; - -// Reads the entire contents of a file into a newly malloc'd buffer. -static char* GetFileContents(const char* filename, size_t* retlen) { - FILE* file = fopen(filename, "rb"); - if (!file) { - fprintf(stderr, "Failed to open: %s\n", filename); - return nullptr; - } - (void)fseek(file, 0, SEEK_END); - size_t file_length = ftell(file); - if (!file_length) { - return nullptr; - } - (void)fseek(file, 0, SEEK_SET); - char* buffer = (char*)malloc(file_length); - if (!buffer) { - return nullptr; - } - size_t bytes_read = fread(buffer, 1, file_length, file); - (void)fclose(file); - if (bytes_read != file_length) { - fprintf(stderr, "Failed to read: %s\n", filename); - free(buffer); - return nullptr; - } - *retlen = bytes_read; - return buffer; -} - -#ifdef PDF_ENABLE_V8 -#ifdef V8_USE_EXTERNAL_STARTUP_DATA -// Returns the full path for an external V8 data file based on either -// the currect exectuable path or an explicit override. -static std::string GetFullPathForSnapshotFile(const std::string& exe_path, - const std::string& filename) { - std::string result; - if (!exe_path.empty()) { - size_t last_separator = exe_path.rfind(PATH_SEPARATOR); - if (last_separator != std::string::npos) { - result = exe_path.substr(0, last_separator + 1); - } - } - result += filename; - return result; -} - -// Reads an extenal V8 data file from the |options|-indicated location, -// returing true on success and false on error. -static bool GetExternalData(const std::string& exe_path, - const std::string& filename, - v8::StartupData* result_data) { - std::string full_path = GetFullPathForSnapshotFile(exe_path, filename); - size_t data_length = 0; - char* data_buffer = GetFileContents(full_path.c_str(), &data_length); - if (!data_buffer) { - return false; - } - result_data->data = const_cast<const char*>(data_buffer); - result_data->raw_size = data_length; - return true; -} -#endif // V8_USE_EXTERNAL_STARTUP_DATA -#endif // PDF_ENABLE_V8 } // namespace -class TestLoader { - public: - TestLoader(const char* pBuf, size_t len); - - const char* m_pBuf; - size_t m_Len; -}; - -TestLoader::TestLoader(const char* pBuf, size_t len) - : m_pBuf(pBuf), m_Len(len) {} - -int Get_Block(void* param, - unsigned long pos, - unsigned char* pBuf, - unsigned long size) { - TestLoader* pLoader = (TestLoader*)param; - if (pos + size < pos || pos + size > pLoader->m_Len) - return 0; - memcpy(pBuf, pLoader->m_pBuf + pos, size); - return 1; -} - FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { return true; } @@ -146,22 +51,11 @@ EmbedderTest::~EmbedderTest() { void EmbedderTest::SetUp() { #ifdef PDF_ENABLE_V8 - v8::V8::InitializeICU(); - - platform_ = v8::platform::CreateDefaultPlatform(); - v8::V8::InitializePlatform(platform_); - v8::V8::Initialize(); - - // By enabling predictable mode, V8 won't post any background tasks. - const char predictable_flag[] = "--predictable"; - v8::V8::SetFlagsFromString(predictable_flag, - static_cast<int>(strlen(predictable_flag))); - #ifdef V8_USE_EXTERNAL_STARTUP_DATA - ASSERT_TRUE(GetExternalData(g_exe_path_, "natives_blob.bin", &natives_)); - ASSERT_TRUE(GetExternalData(g_exe_path_, "snapshot_blob.bin", &snapshot_)); - v8::V8::SetNativesDataBlob(&natives_); - v8::V8::SetSnapshotDataBlob(&snapshot_); + InitializeV8ForPDFium(g_exe_path_, std::string(), &natives_, &snapshot_, + &platform_); +#else + InitializeV8ForPDFium(&platform_); #endif // V8_USE_EXTERNAL_STARTUP_DATA #endif // FPDF_ENABLE_V8 @@ -205,7 +99,7 @@ bool EmbedderTest::OpenDocument(const std::string& filename) { loader_ = new TestLoader(file_contents_, file_length_); file_access_.m_FileLen = static_cast<unsigned long>(file_length_); - file_access_.m_GetBlock = Get_Block; + file_access_.m_GetBlock = TestLoader::GetBlock; file_access_.m_Param = loader_; file_avail_.version = 1; diff --git a/testing/test_support.cpp b/testing/test_support.cpp new file mode 100644 index 0000000000..611ba5d580 --- /dev/null +++ b/testing/test_support.cpp @@ -0,0 +1,140 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "test_support.h" + +#include <stdio.h> +#include <string.h> + +#ifdef _WIN32 +#define PATH_SEPARATOR '\\' +#else +#define PATH_SEPARATOR '/' +#endif + +namespace { + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA +// Returns the full path for an external V8 data file based on either +// the currect exectuable path or an explicit override. +std::string GetFullPathForSnapshotFile(const std::string& exe_path, + const std::string& bin_dir, + const std::string& filename) { + std::string result; + if (!bin_dir.empty()) { + result = bin_dir; + if (*bin_dir.rbegin() != PATH_SEPARATOR) { + result += PATH_SEPARATOR; + } + } else if (!exe_path.empty()) { + size_t last_separator = exe_path.rfind(PATH_SEPARATOR); + if (last_separator != std::string::npos) { + result = exe_path.substr(0, last_separator + 1); + } + } + result += filename; + return result; +} + +bool GetExternalData(const std::string& exe_path, + const std::string& bin_dir, + const std::string& filename, + v8::StartupData* result_data) { + std::string full_path = + GetFullPathForSnapshotFile(exe_path, bin_dir, filename); + size_t data_length = 0; + char* data_buffer = GetFileContents(full_path.c_str(), &data_length); + if (!data_buffer) { + return false; + } + result_data->data = const_cast<const char*>(data_buffer); + result_data->raw_size = data_length; + return true; +} +#endif // V8_USE_EXTERNAL_STARTUP_DATA + +void InitializeV8Common(v8::Platform** platform) { + v8::V8::InitializeICU(); + + *platform = v8::platform::CreateDefaultPlatform(); + v8::V8::InitializePlatform(*platform); + v8::V8::Initialize(); + + // By enabling predictable mode, V8 won't post any background tasks. + const char predictable_flag[] = "--predictable"; + v8::V8::SetFlagsFromString(predictable_flag, + static_cast<int>(strlen(predictable_flag))); +} +#endif // PDF_ENABLE_V8 + +} // namespace + +char* GetFileContents(const char* filename, size_t* retlen) { + FILE* file = fopen(filename, "rb"); + if (!file) { + fprintf(stderr, "Failed to open: %s\n", filename); + return nullptr; + } + (void)fseek(file, 0, SEEK_END); + size_t file_length = ftell(file); + if (!file_length) { + return nullptr; + } + (void)fseek(file, 0, SEEK_SET); + char* buffer = (char*)malloc(file_length); + if (!buffer) { + return nullptr; + } + size_t bytes_read = fread(buffer, 1, file_length, file); + (void)fclose(file); + if (bytes_read != file_length) { + fprintf(stderr, "Failed to read: %s\n", filename); + free(buffer); + return nullptr; + } + *retlen = bytes_read; + return buffer; +} + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA +bool InitializeV8ForPDFium(const std::string& exe_path, + const std::string& bin_dir, + v8::StartupData* natives_blob, + v8::StartupData* snapshot_blob, + v8::Platform** platform) { + InitializeV8Common(platform); + if (!GetExternalData(exe_path, bin_dir, "natives_blob.bin", natives_blob)) + return false; + if (!GetExternalData(exe_path, bin_dir, "snapshot_blob.bin", snapshot_blob)) + return false; + v8::V8::SetNativesDataBlob(natives_blob); + v8::V8::SetSnapshotDataBlob(snapshot_blob); + return true; +} +#else // V8_USE_EXTERNAL_STARTUP_DATA +bool InitializeV8ForPDFium(v8::Platform** platform) { + InitializeV8Common(platform); + return true; +} +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + +TestLoader::TestLoader(const char* pBuf, size_t len) + : m_pBuf(pBuf), m_Len(len) { +} + +// static +int TestLoader::GetBlock(void* param, + unsigned long pos, + unsigned char* pBuf, + unsigned long size) { + TestLoader* pLoader = static_cast<TestLoader*>(param); + if (pos + size < pos || pos + size > pLoader->m_Len) + return 0; + + memcpy(pBuf, pLoader->m_pBuf + pos, size); + return 1; +} diff --git a/testing/test_support.h b/testing/test_support.h new file mode 100644 index 0000000000..1018421ba7 --- /dev/null +++ b/testing/test_support.h @@ -0,0 +1,44 @@ +// Copyright 2015 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef TESTING_EMBEDDER_TEST_SUPPORT_H_ +#define TESTING_EMBEDDER_TEST_SUPPORT_H_ + +#include <stdlib.h> +#include <string> + +#ifdef PDF_ENABLE_V8 +#include "v8/include/libplatform/libplatform.h" +#include "v8/include/v8.h" +#endif + +// Reads the entire contents of a file into a newly malloc'd buffer. +char* GetFileContents(const char* filename, size_t* retlen); + +#ifdef PDF_ENABLE_V8 +#ifdef V8_USE_EXTERNAL_STARTUP_DATA +bool InitializeV8ForPDFium(const std::string& exe_path, + const std::string& bin_dir, + v8::StartupData* natives_blob, + v8::StartupData* snapshot_blob, + v8::Platform** platform); +#else // V8_USE_EXTERNAL_STARTUP_DATA +bool InitializeV8ForPDFium(v8::Platform** platform); +#endif // V8_USE_EXTERNAL_STARTUP_DATA +#endif // PDF_ENABLE_V8 + +class TestLoader { + public: + TestLoader(const char* pBuf, size_t len); + static int GetBlock(void* param, + unsigned long pos, + unsigned char* pBuf, + unsigned long size); + + private: + const char* const m_pBuf; + const size_t m_Len; +}; + +#endif // TESTING_EMBEDDER_TEST_SUPPORT_H_ |