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" |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 11 | |
| 12 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | 72e8b69 | 2019-02-05 19:16:52 +0000 | [diff] [blame] | 13 | #include "testing/v8_initializer.h" |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 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 |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 24 | v8::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 Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 31 | class Environment final : public testing::Environment { |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 32 | public: |
| 33 | void SetUp() override { |
| 34 | #ifdef PDF_ENABLE_V8 |
| 35 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
Lei Zhang | a8e8baf | 2019-10-08 17:53:04 +0000 | [diff] [blame] | 36 | if (g_v8_snapshot) { |
| 37 | platform_ = InitializeV8ForPDFiumWithStartupData(g_exe_path, |
| 38 | std::string(), nullptr); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 39 | } else { |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 40 | g_v8_snapshot = new v8::StartupData; |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 41 | platform_ = InitializeV8ForPDFiumWithStartupData( |
Lei Zhang | a8e8baf | 2019-10-08 17:53:04 +0000 | [diff] [blame] | 42 | g_exe_path, std::string(), g_v8_snapshot); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 43 | } |
| 44 | #else |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 45 | platform_ = InitializeV8ForPDFium(g_exe_path); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 46 | #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 Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 53 | #endif // PDF_ENABLE_V8 |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | #ifdef PDF_ENABLE_V8 |
Lei Zhang | b0fb8cc | 2018-02-08 14:01:32 +0000 | [diff] [blame] | 58 | std::unique_ptr<v8::Platform> platform_; |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 59 | #endif // PDF_ENABLE_V8 |
| 60 | }; |
| 61 | |
| 62 | Environment* 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. |
| 68 | int 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 Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 84 | 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 | } |