blob: 34dfb063976fc57725893fa3df8593bf3ef84be4 [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"
Dan Sinclair5553d8b2018-01-03 09:44:28 -050011
12#ifdef PDF_ENABLE_V8
Lei Zhang72e8b692019-02-05 19:16:52 +000013#include "testing/v8_initializer.h"
Dan Sinclair5553d8b2018-01-03 09:44:28 -050014#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
Dan Sinclair5553d8b2018-01-03 09:44:28 -050024v8::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.
Tom Sepez55865452018-08-27 20:18:04 +000031class Environment final : public testing::Environment {
Dan Sinclair5553d8b2018-01-03 09:44:28 -050032 public:
33 void SetUp() override {
34#ifdef PDF_ENABLE_V8
35#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Lei Zhanga8e8baf2019-10-08 17:53:04 +000036 if (g_v8_snapshot) {
37 platform_ = InitializeV8ForPDFiumWithStartupData(g_exe_path,
38 std::string(), nullptr);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050039 } else {
Dan Sinclair5553d8b2018-01-03 09:44:28 -050040 g_v8_snapshot = new v8::StartupData;
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000041 platform_ = InitializeV8ForPDFiumWithStartupData(
Lei Zhanga8e8baf2019-10-08 17:53:04 +000042 g_exe_path, std::string(), g_v8_snapshot);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050043 }
44#else
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000045 platform_ = InitializeV8ForPDFium(g_exe_path);
Dan Sinclair5553d8b2018-01-03 09:44:28 -050046#endif // V8_USE_EXTERNAL_STARTUP_DATA
47#endif // FPDF_ENABLE_V8
48 }
49
50 void TearDown() override {
51#ifdef PDF_ENABLE_V8
52 v8::V8::ShutdownPlatform();
Dan Sinclair5553d8b2018-01-03 09:44:28 -050053#endif // PDF_ENABLE_V8
54 }
55
56 private:
57#ifdef PDF_ENABLE_V8
Lei Zhangb0fb8cc2018-02-08 14:01:32 +000058 std::unique_ptr<v8::Platform> platform_;
Dan Sinclair5553d8b2018-01-03 09:44:28 -050059#endif // PDF_ENABLE_V8
60};
61
62Environment* 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.
68int 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
Dan Sinclair5553d8b2018-01-03 09:44:28 -050084 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}