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 | |
| 5 | #include <string> |
| 6 | |
| 7 | #include "core/fxcrt/fx_memory.h" |
| 8 | #include "testing/gmock/include/gmock/gmock.h" |
| 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "testing/test_support.h" |
| 11 | |
| 12 | #ifdef PDF_ENABLE_V8 |
| 13 | #include "v8/include/v8-platform.h" |
| 14 | #include "v8/include/v8.h" |
| 15 | #endif // PDF_ENABLE_v8 |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const char* g_exe_path = nullptr; |
| 20 | |
| 21 | #ifdef PDF_ENABLE_V8 |
| 22 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 23 | v8::StartupData* g_v8_natives = nullptr; |
| 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. |
| 31 | class Environment : public testing::Environment { |
| 32 | public: |
| 33 | void SetUp() override { |
| 34 | #ifdef PDF_ENABLE_V8 |
| 35 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 36 | if (g_v8_natives && g_v8_snapshot) { |
Lei Zhang | 3355f45 | 2018-02-05 21:13:35 +0000 | [diff] [blame] | 37 | platform_ = InitializeV8ForPDFiumWithStartupData( |
| 38 | g_exe_path, std::string(), nullptr, nullptr); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 39 | } else { |
| 40 | g_v8_natives = new v8::StartupData; |
| 41 | g_v8_snapshot = new v8::StartupData; |
Lei Zhang | 3355f45 | 2018-02-05 21:13:35 +0000 | [diff] [blame] | 42 | platform_ = InitializeV8ForPDFiumWithStartupData( |
| 43 | g_exe_path, std::string(), g_v8_natives, g_v8_snapshot); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 44 | } |
| 45 | #else |
Andreas Haas | 608e8dd | 2018-02-05 18:32:18 +0000 | [diff] [blame] | 46 | platform_ = InitializeV8ForPDFium(g_exe_path); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 47 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 48 | #endif // FPDF_ENABLE_V8 |
| 49 | } |
| 50 | |
| 51 | void TearDown() override { |
| 52 | #ifdef PDF_ENABLE_V8 |
| 53 | v8::V8::ShutdownPlatform(); |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 54 | #endif // PDF_ENABLE_V8 |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | #ifdef PDF_ENABLE_V8 |
Andreas Haas | 608e8dd | 2018-02-05 18:32:18 +0000 | [diff] [blame] | 59 | std::unique_ptr<v8::Platform> platform_; |
Dan Sinclair | 5553d8b | 2018-01-03 09:44:28 -0500 | [diff] [blame] | 60 | #endif // PDF_ENABLE_V8 |
| 61 | }; |
| 62 | |
| 63 | Environment* env_ = nullptr; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | // Can't use gtest-provided main since we need to stash the path to the |
| 68 | // executable in order to find the external V8 binary data files. |
| 69 | int main(int argc, char** argv) { |
| 70 | g_exe_path = argv[0]; |
| 71 | |
| 72 | FXMEM_InitializePartitionAlloc(); |
| 73 | |
| 74 | env_ = new Environment(); |
| 75 | // The env will be deleted by gtest. |
| 76 | AddGlobalTestEnvironment(env_); |
| 77 | |
| 78 | testing::InitGoogleTest(&argc, argv); |
| 79 | testing::InitGoogleMock(&argc, argv); |
| 80 | |
| 81 | int ret_val = RUN_ALL_TESTS(); |
| 82 | |
| 83 | #ifdef PDF_ENABLE_V8 |
| 84 | #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 85 | if (g_v8_natives) |
| 86 | free(const_cast<char*>(g_v8_natives->data)); |
| 87 | if (g_v8_snapshot) |
| 88 | free(const_cast<char*>(g_v8_snapshot->data)); |
| 89 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 90 | #endif // PDF_ENABLE_V8 |
| 91 | |
| 92 | return ret_val; |
| 93 | } |