1 // Copyright 2020 The PDFium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TESTING_V8_TEST_ENVIRONMENT_H_ 6 #define TESTING_V8_TEST_ENVIRONMENT_H_ 7 8 #ifndef PDF_ENABLE_V8 9 #error "V8 must be enabled" 10 #endif // PDF_ENABLE_V8 11 12 #include <memory> 13 14 #include "fxjs/cfx_v8.h" 15 #include "fxjs/cfx_v8_array_buffer_allocator.h" 16 #include "testing/gtest/include/gtest/gtest.h" 17 18 namespace v8 { 19 class Isolate; 20 class Platform; 21 class StartupData; 22 } // namespace v8 23 24 class TestLoader; 25 26 class V8TestEnvironment : public testing::Environment { 27 public: 28 explicit V8TestEnvironment(const char* exe_path); 29 ~V8TestEnvironment() override; 30 31 // Note: GetInstance() does not create one if it does not exist, 32 // so the main program must explicitly add this enviroment. 33 static V8TestEnvironment* GetInstance(); 34 static void PumpPlatformMessageLoop(v8::Isolate* pIsolate); 35 36 // testing::Environment: 37 void SetUp() override; 38 void TearDown() override; 39 platform()40 v8::Platform* platform() const { return platform_.get(); } isolate()41 v8::Isolate* isolate() const { return isolate_.get(); } 42 43 private: 44 const char* const exe_path_; 45 std::unique_ptr<v8::StartupData> startup_data_; 46 std::unique_ptr<v8::Platform> platform_; 47 std::unique_ptr<CFX_V8ArrayBufferAllocator> array_buffer_allocator_; 48 std::unique_ptr<v8::Isolate, CFX_V8IsolateDeleter> isolate_; 49 }; 50 51 #endif // TESTING_V8_TEST_ENVIRONMENT_H_ 52