Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 1 | // 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 Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 5 | #include <memory> |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 6 | #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 | |
| 18 | namespace { |
| 19 | |
| 20 | const char* g_exe_path = nullptr; |
| 21 | |
| 22 | #ifdef PDF_ENABLE_V8 |
| 23 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 24 | v8::StartupData* g_v8_natives = nullptr; |
| 25 | v8::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 Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 32 | class Environment final : public testing::Environment { |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 33 | 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 Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 38 | platform_ = InitializeV8ForPDFiumWithStartupData( |
| 39 | g_exe_path, std::string(), nullptr, nullptr); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 40 | } else { |
| 41 | g_v8_natives = new v8::StartupData; |
| 42 | g_v8_snapshot = new v8::StartupData; |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 43 | platform_ = InitializeV8ForPDFiumWithStartupData( |
| 44 | g_exe_path, std::string(), g_v8_natives, g_v8_snapshot); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 45 | } |
| 46 | #else |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 47 | platform_ = InitializeV8ForPDFium(g_exe_path); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 48 | #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 Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 55 | #endif // PDF_ENABLE_V8 |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 60 | std::unique_ptr<v8::Platform> platform_; |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 61 | #endif // PDF_ENABLE_V8 |
| 62 | }; |
| 63 | |
| 64 | Environment* 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. |
| 70 | int 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 | } |