• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
6 
7 #include "core/fxcrt/fx_memory.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/test_support.h"
11 
12 #ifdef PDF_ENABLE_V8
13 #include "v8/include/v8-platform.h"
14 #include "v8/include/v8.h"
15 #endif  // PDF_ENABLE_v8
16 
17 namespace {
18 
19 const char* g_exe_path = nullptr;
20 
21 #ifdef PDF_ENABLE_V8
22 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
23 v8::StartupData* g_v8_natives = nullptr;
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 : 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_natives && g_v8_snapshot) {
37       InitializeV8ForPDFium(g_exe_path, std::string(), nullptr, nullptr,
38                             &platform_);
39     } else {
40       g_v8_natives = new v8::StartupData;
41       g_v8_snapshot = new v8::StartupData;
42       InitializeV8ForPDFium(g_exe_path, std::string(), g_v8_natives,
43                             g_v8_snapshot, &platform_);
44     }
45 #else
46     InitializeV8ForPDFium(g_exe_path, &platform_);
47 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
48 #endif  // FPDF_ENABLE_V8
49   }
50 
TearDown()51   void TearDown() override {
52 #ifdef PDF_ENABLE_V8
53     v8::V8::ShutdownPlatform();
54     delete platform_;
55 #endif  // PDF_ENABLE_V8
56   }
57 
58  private:
59 #ifdef PDF_ENABLE_V8
60   v8::Platform* platform_;
61 #endif  // PDF_ENABLE_V8
62 };
63 
64 Environment* env_ = nullptr;
65 
66 }  // namespace
67 
68 // Can't use gtest-provided main since we need to stash the path to the
69 // executable in order to find the external V8 binary data files.
main(int argc,char ** argv)70 int main(int argc, char** argv) {
71   g_exe_path = argv[0];
72 
73   FXMEM_InitializePartitionAlloc();
74 
75   env_ = new Environment();
76   // The env will be deleted by gtest.
77   AddGlobalTestEnvironment(env_);
78 
79   testing::InitGoogleTest(&argc, argv);
80   testing::InitGoogleMock(&argc, argv);
81 
82   int ret_val = RUN_ALL_TESTS();
83 
84 #ifdef PDF_ENABLE_V8
85 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
86   if (g_v8_natives)
87     free(const_cast<char*>(g_v8_natives->data));
88   if (g_v8_snapshot)
89     free(const_cast<char*>(g_v8_snapshot->data));
90 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
91 #endif  // PDF_ENABLE_V8
92 
93   return ret_val;
94 }
95