blob: 92990bff28edacc0971fe521e28aeaf9764c8332 [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
Tom Sepez55865452018-08-27 20:18:04 +00005#include <memory>
Dan Sinclair5553d8b2018-01-03 09:44:28 -05006#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#include "testing/test_support.h"
12
13#ifdef PDF_ENABLE_V8
14#include "v8/include/v8-platform.h"
15#include "v8/include/v8.h"
16#endif // PDF_ENABLE_v8
17
18namespace {
19
20const char* g_exe_path = nullptr;
21
22#ifdef PDF_ENABLE_V8
23#ifdef V8_USE_EXTERNAL_STARTUP_DATA
24v8::StartupData* g_v8_natives = nullptr;
25v8::StartupData* g_v8_snapshot = nullptr;
26#endif // V8_USE_EXTERNAL_STARTUP_DATA
27#endif // PDF_ENABLE_V8
28
29// The loading time of the CFGAS_FontMgr is linear in the number of times it is
30// loaded. So, if a test suite has a lot of tests that need a font manager they
31// can end up executing very, very slowly.
Tom Sepez55865452018-08-27 20:18:04 +000032class Environment final : public testing::Environment {
Dan Sinclair5553d8b2018-01-03 09:44:28 -050033 public:
34 void SetUp() override {
35#ifdef PDF_ENABLE_V8
36#ifdef V8_USE_EXTERNAL_STARTUP_DATA
37 if (g_v8_natives && g_v8_snapshot) {
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000038 platform_ = InitializeV8ForPDFiumWithStartupData(
39 g_exe_path, std::string(), nullptr, nullptr);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050040 } else {
41 g_v8_natives = new v8::StartupData;
42 g_v8_snapshot = new v8::StartupData;
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000043 platform_ = InitializeV8ForPDFiumWithStartupData(
44 g_exe_path, std::string(), g_v8_natives, g_v8_snapshot);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050045 }
46#else
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000047 platform_ = InitializeV8ForPDFium(g_exe_path);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050048#endif // V8_USE_EXTERNAL_STARTUP_DATA
49#endif // FPDF_ENABLE_V8
50 }
51
52 void TearDown() override {
53#ifdef PDF_ENABLE_V8
54 v8::V8::ShutdownPlatform();
Dan Sinclair5553d8b2018-01-03 09:44:28 -050055#endif // PDF_ENABLE_V8
56 }
57
58 private:
59#ifdef PDF_ENABLE_V8
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000060 std::unique_ptr<v8::Platform> platform_;
Dan Sinclair5553d8b2018-01-03 09:44:28 -050061#endif // PDF_ENABLE_V8
62};
63
64Environment* 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.
70int 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}