blob: 92dceefdb67e805f2814ccb714750dc9fa7e5322 [file] [log] [blame]
Tom Sepez64ee2c32017-04-24 15:04:25 -07001// Copyright 2017 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
Dan Sinclair574d4402017-08-28 10:11:24 -04005#include <memory>
Tom Sepez3f8ee5e2018-02-09 18:26:09 +00006#include <string>
Dan Sinclair574d4402017-08-28 10:11:24 -04007
Tom Sepez64ee2c32017-04-24 15:04:25 -07008#include "core/fxcrt/fx_memory.h"
9#include "testing/gmock/include/gmock/gmock.h"
10#include "testing/gtest/include/gtest/gtest.h"
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000011#include "testing/test_support.h"
12
Ryan Harrison4f21d772018-06-01 15:19:04 +000013#ifdef PDF_ENABLE_V8
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000014#include "v8/include/v8-platform.h"
15#include "v8/include/v8.h"
Ryan Harrison4f21d772018-06-01 15:19:04 +000016#endif // PDF_ENABLE_V8
Tom Sepez64ee2c32017-04-24 15:04:25 -070017
Dan Sinclair574d4402017-08-28 10:11:24 -040018#if PDF_ENABLE_XFA
Lei Zhang96d6b4d2018-01-11 14:31:41 +000019#include "core/fxge/cfx_fontmgr.h"
Dan Sinclair574d4402017-08-28 10:11:24 -040020#include "core/fxge/cfx_gemodule.h"
21#include "xfa/fgas/font/cfgas_fontmgr.h"
22#include "xfa/fgas/font/cfgas_gefont.h"
23
24namespace {
25
26// The loading time of the CFGAS_FontMgr is linear in the number of times it is
27// loaded. So, if a test suite has a lot of tests that need a font manager they
28// can end up executing very, very slowly.
29class Environment : public testing::Environment {
30 public:
31 void SetUp() override {
32 // TODO(dsinclair): This font loading is slow. We should make a test font
33 // loader which loads up a single font we use in all tests.
34 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
Dan Sinclairf8af5652018-03-06 18:56:33 +000035 SystemFontInfoIface::CreateDefault(nullptr));
Dan Sinclair574d4402017-08-28 10:11:24 -040036
Dan Sinclair81f02f42017-09-26 12:02:16 -040037 font_mgr_ = pdfium::MakeUnique<CFGAS_FontMgr>();
38 if (!font_mgr_->EnumFonts())
39 font_mgr_ = nullptr;
Dan Sinclair574d4402017-08-28 10:11:24 -040040 }
41
42 void TearDown() override {
43 font_mgr_.reset();
Dan Sinclair574d4402017-08-28 10:11:24 -040044 }
45 CFGAS_FontMgr* FontManager() const { return font_mgr_.get(); }
46
47 private:
Dan Sinclair574d4402017-08-28 10:11:24 -040048 std::unique_ptr<CFGAS_FontMgr> font_mgr_;
49};
50
51Environment* env_ = nullptr;
52
53} // namespace
54
55CFGAS_FontMgr* GetGlobalFontManager() {
56 return env_->FontManager();
57}
58#endif // PDF_ENABLE_XFA
59
Tom Sepez64ee2c32017-04-24 15:04:25 -070060// Can't use gtest-provided main since we need to initialize partition
61// alloc before invoking any test.
62int main(int argc, char** argv) {
Dan Sinclairdbc3d3e2017-05-11 13:41:38 -040063 FXMEM_InitializePartitionAlloc();
Dan Sinclair574d4402017-08-28 10:11:24 -040064
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000065#ifdef PDF_ENABLE_V8
66 std::unique_ptr<v8::Platform> platform;
Nico Weber611431d2018-02-26 20:36:16 +000067#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000068 static v8::StartupData* natives = new v8::StartupData;
69 static v8::StartupData* snapshot = new v8::StartupData;
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000070 platform = InitializeV8ForPDFiumWithStartupData(argv[0], std::string(),
71 natives, snapshot);
72#else // V8_USE_EXTERNAL_STARTUP_DATA
73 platform = InitializeV8ForPDFium(argv[0]);
74#endif // V8_USE_EXTERNAL_STARTUP_DATA
75#endif // PDF_ENABLE_V8
76
Dan Sinclair574d4402017-08-28 10:11:24 -040077#if PDF_ENABLE_XFA
78 env_ = new Environment();
79 // The env will be deleted by gtest.
80 AddGlobalTestEnvironment(env_);
81#endif // PDF_ENABLE_XFA
82
Tom Sepez64ee2c32017-04-24 15:04:25 -070083 testing::InitGoogleTest(&argc, argv);
84 testing::InitGoogleMock(&argc, argv);
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000085
86 int ret_val = RUN_ALL_TESTS();
87
88#ifdef PDF_ENABLE_V8
89 v8::V8::ShutdownPlatform();
90#endif // PDF_ENABLE_V8
91
92 return ret_val;
Tom Sepez64ee2c32017-04-24 15:04:25 -070093}