1 // Copyright 2020 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "testing/embedder_test_environment.h" 6 7 #include <ostream> 8 9 #include "core/fxcrt/fx_system.h" 10 #include "public/fpdfview.h" 11 #include "testing/command_line_helpers.h" 12 #include "third_party/base/check.h" 13 14 #ifdef PDF_ENABLE_V8 15 #include "testing/v8_test_environment.h" 16 #endif // PDF_ENABLE_V8 17 18 namespace { 19 20 EmbedderTestEnvironment* g_environment = nullptr; 21 22 } // namespace 23 EmbedderTestEnvironment()24EmbedderTestEnvironment::EmbedderTestEnvironment() 25 : renderer_type_(GetDefaultRendererType()) { 26 DCHECK(!g_environment); 27 g_environment = this; 28 } 29 ~EmbedderTestEnvironment()30EmbedderTestEnvironment::~EmbedderTestEnvironment() { 31 DCHECK(g_environment); 32 g_environment = nullptr; 33 } 34 35 // static GetInstance()36EmbedderTestEnvironment* EmbedderTestEnvironment::GetInstance() { 37 return g_environment; 38 } 39 SetUp()40void EmbedderTestEnvironment::SetUp() { 41 FPDF_LIBRARY_CONFIG config; 42 config.version = 4; 43 config.m_pUserFontPaths = nullptr; 44 config.m_v8EmbedderSlot = 0; 45 config.m_pPlatform = nullptr; 46 47 config.m_pUserFontPaths = test_fonts_.font_paths(); 48 49 #ifdef PDF_ENABLE_V8 50 config.m_pIsolate = V8TestEnvironment::GetInstance()->isolate(); 51 config.m_pPlatform = V8TestEnvironment::GetInstance()->platform(); 52 #else // PDF_ENABLE_V8 53 config.m_pIsolate = nullptr; 54 config.m_pPlatform = nullptr; 55 #endif // PDF_ENABLE_V8 56 config.m_RendererType = renderer_type_; 57 58 FPDF_InitLibraryWithConfig(&config); 59 60 test_fonts_.InstallFontMapper(); 61 } 62 TearDown()63void EmbedderTestEnvironment::TearDown() { 64 FPDF_DestroyLibrary(); 65 } 66 AddFlags(int argc,char ** argv)67void EmbedderTestEnvironment::AddFlags(int argc, char** argv) { 68 for (int i = 1; i < argc; ++i) 69 AddFlag(argv[i]); 70 } 71 AddFlag(const std::string & flag)72void EmbedderTestEnvironment::AddFlag(const std::string& flag) { 73 if (flag == "--write-pngs") { 74 write_pngs_ = true; 75 return; 76 } 77 #if defined(_SKIA_SUPPORT_) 78 std::string value; 79 if (ParseSwitchKeyValue(flag, "--use-renderer=", &value)) { 80 if (value == "agg") { 81 renderer_type_ = FPDF_RENDERERTYPE_AGG; 82 } else if (value == "skia") { 83 renderer_type_ = FPDF_RENDERERTYPE_SKIA; 84 } else { 85 std::cerr << "Invalid --use-renderer argument, value must be one of agg " 86 "or skia\n"; 87 } 88 return; 89 } 90 #endif // defined(_SKIA_SUPPORT_) 91 92 std::cerr << "Unknown flag: " << flag << "\n"; 93 } 94