1 // Copyright 2018 PDFium Authors. All rights reserved.
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 <memory>
6 #include <string>
7
8 #include "core/fxcrt/fx_memory.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 #ifdef PDF_ENABLE_V8
13 #include "testing/v8_initializer.h"
14 #include "v8/include/v8-platform.h"
15 #include "v8/include/v8.h"
16 #endif // PDF_ENABLE_v8
17
18 namespace {
19
20 const char* g_exe_path = nullptr;
21
22 #ifdef PDF_ENABLE_V8
23 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
24 v8::StartupData* g_v8_snapshot = nullptr;
25 #endif // V8_USE_EXTERNAL_STARTUP_DATA
26 #endif // PDF_ENABLE_V8
27
28 // The loading time of the CFGAS_FontMgr is linear in the number of times it is
29 // loaded. So, if a test suite has a lot of tests that need a font manager they
30 // can end up executing very, very slowly.
31 class Environment final : public testing::Environment {
32 public:
SetUp()33 void SetUp() override {
34 #ifdef PDF_ENABLE_V8
35 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
36 if (g_v8_snapshot) {
37 platform_ = InitializeV8ForPDFiumWithStartupData(g_exe_path,
38 std::string(), nullptr);
39 } else {
40 g_v8_snapshot = new v8::StartupData;
41 platform_ = InitializeV8ForPDFiumWithStartupData(
42 g_exe_path, std::string(), g_v8_snapshot);
43 }
44 #else
45 platform_ = InitializeV8ForPDFium(g_exe_path);
46 #endif // V8_USE_EXTERNAL_STARTUP_DATA
47 #endif // FPDF_ENABLE_V8
48 }
49
TearDown()50 void TearDown() override {
51 #ifdef PDF_ENABLE_V8
52 v8::V8::ShutdownPlatform();
53 #endif // PDF_ENABLE_V8
54 }
55
56 private:
57 #ifdef PDF_ENABLE_V8
58 std::unique_ptr<v8::Platform> platform_;
59 #endif // PDF_ENABLE_V8
60 };
61
62 Environment* env_ = nullptr;
63
64 } // namespace
65
66 // Can't use gtest-provided main since we need to stash the path to the
67 // executable in order to find the external V8 binary data files.
main(int argc,char ** argv)68 int main(int argc, char** argv) {
69 g_exe_path = argv[0];
70
71 FXMEM_InitializePartitionAlloc();
72
73 env_ = new Environment();
74 // The env will be deleted by gtest.
75 AddGlobalTestEnvironment(env_);
76
77 testing::InitGoogleTest(&argc, argv);
78 testing::InitGoogleMock(&argc, argv);
79
80 int ret_val = RUN_ALL_TESTS();
81
82 #ifdef PDF_ENABLE_V8
83 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
84 if (g_v8_snapshot)
85 free(const_cast<char*>(g_v8_snapshot->data));
86 #endif // V8_USE_EXTERNAL_STARTUP_DATA
87 #endif // PDF_ENABLE_V8
88
89 return ret_val;
90 }
91