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/check.h"
10 #include "core/fxcrt/fx_system.h"
11 #include "public/fpdfview.h"
12 #include "testing/command_line_helpers.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()24 EmbedderTestEnvironment::EmbedderTestEnvironment()
25 : renderer_type_(GetDefaultRendererType()) {
26 DCHECK(!g_environment);
27 g_environment = this;
28 }
29
~EmbedderTestEnvironment()30 EmbedderTestEnvironment::~EmbedderTestEnvironment() {
31 DCHECK(g_environment);
32 g_environment = nullptr;
33 }
34
35 // static
GetInstance()36 EmbedderTestEnvironment* EmbedderTestEnvironment::GetInstance() {
37 return g_environment;
38 }
39
SetUp()40 void EmbedderTestEnvironment::SetUp() {
41 FPDF_LIBRARY_CONFIG config = {
42 .version = 4,
43 .m_pUserFontPaths = test_fonts_.font_paths(),
44
45 #ifdef PDF_ENABLE_V8
46 .m_pIsolate = V8TestEnvironment::GetInstance()->isolate(),
47 .m_v8EmbedderSlot = 0,
48 .m_pPlatform = V8TestEnvironment::GetInstance()->platform(),
49 #else // PDF_ENABLE_V8
50 .m_pIsolate = nullptr,
51 .m_v8EmbedderSlot = 0,
52 .m_pPlatform = nullptr,
53 #endif // PDF_ENABLE_V8
54
55 .m_RendererType = renderer_type_,
56 };
57
58 FPDF_InitLibraryWithConfig(&config);
59
60 test_fonts_.InstallFontMapper();
61 }
62
TearDown()63 void EmbedderTestEnvironment::TearDown() {
64 FPDF_DestroyLibrary();
65 }
66
AddFlags(int argc,char ** argv)67 void 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)72 void EmbedderTestEnvironment::AddFlag(const std::string& flag) {
73 if (flag == "--write-pngs") {
74 write_pngs_ = true;
75 return;
76 }
77 #if defined(PDF_USE_SKIA)
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(PDF_USE_SKIA)
91
92 std::cerr << "Unknown flag: " << flag << "\n";
93 }
94