blob: 6c1aa1a22c43b2d8a6d2149c2a4a4f113b7adcca [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"
Lei Zhang99f5bbb2018-10-09 21:31:28 +000012#include "third_party/base/ptr_util.h"
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000013
Ryan Harrison4f21d772018-06-01 15:19:04 +000014#ifdef PDF_ENABLE_V8
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000015#include "v8/include/v8-platform.h"
16#include "v8/include/v8.h"
Ryan Harrison4f21d772018-06-01 15:19:04 +000017#endif // PDF_ENABLE_V8
Tom Sepez64ee2c32017-04-24 15:04:25 -070018
Ryan Harrisond60d7cb2018-06-01 15:28:54 +000019#ifdef PDF_ENABLE_XFA
Lei Zhang96d6b4d2018-01-11 14:31:41 +000020#include "core/fxge/cfx_fontmgr.h"
Dan Sinclair574d4402017-08-28 10:11:24 -040021#include "core/fxge/cfx_gemodule.h"
22#include "xfa/fgas/font/cfgas_fontmgr.h"
23#include "xfa/fgas/font/cfgas_gefont.h"
24
25namespace {
26
27// The loading time of the CFGAS_FontMgr is linear in the number of times it is
28// loaded. So, if a test suite has a lot of tests that need a font manager they
29// can end up executing very, very slowly.
Tom Sepez55865452018-08-27 20:18:04 +000030class Environment final : public testing::Environment {
Dan Sinclair574d4402017-08-28 10:11:24 -040031 public:
32 void SetUp() override {
33 // TODO(dsinclair): This font loading is slow. We should make a test font
34 // loader which loads up a single font we use in all tests.
35 CFX_GEModule::Get()->GetFontMgr()->SetSystemFontInfo(
Dan Sinclairf8af5652018-03-06 18:56:33 +000036 SystemFontInfoIface::CreateDefault(nullptr));
Dan Sinclair574d4402017-08-28 10:11:24 -040037
Dan Sinclair81f02f42017-09-26 12:02:16 -040038 font_mgr_ = pdfium::MakeUnique<CFGAS_FontMgr>();
39 if (!font_mgr_->EnumFonts())
40 font_mgr_ = nullptr;
Dan Sinclair574d4402017-08-28 10:11:24 -040041 }
42
43 void TearDown() override {
44 font_mgr_.reset();
Dan Sinclair574d4402017-08-28 10:11:24 -040045 }
46 CFGAS_FontMgr* FontManager() const { return font_mgr_.get(); }
47
48 private:
Dan Sinclair574d4402017-08-28 10:11:24 -040049 std::unique_ptr<CFGAS_FontMgr> font_mgr_;
50};
51
52Environment* env_ = nullptr;
53
54} // namespace
55
56CFGAS_FontMgr* GetGlobalFontManager() {
57 return env_->FontManager();
58}
59#endif // PDF_ENABLE_XFA
60
Tom Sepez64ee2c32017-04-24 15:04:25 -070061// Can't use gtest-provided main since we need to initialize partition
62// alloc before invoking any test.
63int main(int argc, char** argv) {
Dan Sinclairdbc3d3e2017-05-11 13:41:38 -040064 FXMEM_InitializePartitionAlloc();
Dan Sinclair574d4402017-08-28 10:11:24 -040065
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000066#ifdef PDF_ENABLE_V8
67 std::unique_ptr<v8::Platform> platform;
Nico Weber611431d2018-02-26 20:36:16 +000068#ifdef V8_USE_EXTERNAL_STARTUP_DATA
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000069 static v8::StartupData* natives = new v8::StartupData;
70 static v8::StartupData* snapshot = new v8::StartupData;
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000071 platform = InitializeV8ForPDFiumWithStartupData(argv[0], std::string(),
72 natives, snapshot);
73#else // V8_USE_EXTERNAL_STARTUP_DATA
74 platform = InitializeV8ForPDFium(argv[0]);
75#endif // V8_USE_EXTERNAL_STARTUP_DATA
76#endif // PDF_ENABLE_V8
77
Ryan Harrisond60d7cb2018-06-01 15:28:54 +000078#ifdef PDF_ENABLE_XFA
Dan Sinclair574d4402017-08-28 10:11:24 -040079 env_ = new Environment();
80 // The env will be deleted by gtest.
81 AddGlobalTestEnvironment(env_);
82#endif // PDF_ENABLE_XFA
83
Tom Sepez64ee2c32017-04-24 15:04:25 -070084 testing::InitGoogleTest(&argc, argv);
85 testing::InitGoogleMock(&argc, argv);
Tom Sepez3f8ee5e2018-02-09 18:26:09 +000086
87 int ret_val = RUN_ALL_TESTS();
88
89#ifdef PDF_ENABLE_V8
90 v8::V8::ShutdownPlatform();
91#endif // PDF_ENABLE_V8
92
93 return ret_val;
Tom Sepez64ee2c32017-04-24 15:04:25 -070094}