blob: 0c3ceb60ce02aa2bef5114f2a1063eec62f288e7 [file] [log] [blame]
Dan Sinclair5553d8b2018-01-03 09:44:28 -05001// 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
17namespace {
18
19const char* g_exe_path = nullptr;
20
21#ifdef PDF_ENABLE_V8
22#ifdef V8_USE_EXTERNAL_STARTUP_DATA
23v8::StartupData* g_v8_natives = nullptr;
24v8::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.
31class Environment : public testing::Environment {
32 public:
33 void SetUp() override {
34#ifdef PDF_ENABLE_V8
35#ifdef V8_USE_EXTERNAL_STARTUP_DATA
36 if (g_v8_natives && g_v8_snapshot) {
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000037 platform_ = InitializeV8ForPDFiumWithStartupData(
38 g_exe_path, std::string(), nullptr, nullptr);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050039 } else {
40 g_v8_natives = new v8::StartupData;
41 g_v8_snapshot = new v8::StartupData;
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000042 platform_ = InitializeV8ForPDFiumWithStartupData(
43 g_exe_path, std::string(), g_v8_natives, g_v8_snapshot);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050044 }
45#else
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000046 platform_ = InitializeV8ForPDFium(g_exe_path);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050047#endif // V8_USE_EXTERNAL_STARTUP_DATA
48#endif // FPDF_ENABLE_V8
49 }
50
51 void TearDown() override {
52#ifdef PDF_ENABLE_V8
53 v8::V8::ShutdownPlatform();
Dan Sinclair5553d8b2018-01-03 09:44:28 -050054#endif // PDF_ENABLE_V8
55 }
56
57 private:
58#ifdef PDF_ENABLE_V8
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000059 std::unique_ptr<v8::Platform> platform_;
Dan Sinclair5553d8b2018-01-03 09:44:28 -050060#endif // PDF_ENABLE_V8
61};
62
63Environment* env_ = nullptr;
64
65} // namespace
66
67// Can't use gtest-provided main since we need to stash the path to the
68// executable in order to find the external V8 binary data files.
69int main(int argc, char** argv) {
70 g_exe_path = argv[0];
71
72 FXMEM_InitializePartitionAlloc();
73
74 env_ = new Environment();
75 // The env will be deleted by gtest.
76 AddGlobalTestEnvironment(env_);
77
78 testing::InitGoogleTest(&argc, argv);
79 testing::InitGoogleMock(&argc, argv);
80
81 int ret_val = RUN_ALL_TESTS();
82
83#ifdef PDF_ENABLE_V8
84#ifdef V8_USE_EXTERNAL_STARTUP_DATA
85 if (g_v8_natives)
86 free(const_cast<char*>(g_v8_natives->data));
87 if (g_v8_snapshot)
88 free(const_cast<char*>(g_v8_snapshot->data));
89#endif // V8_USE_EXTERNAL_STARTUP_DATA
90#endif // PDF_ENABLE_V8
91
92 return ret_val;
93}