1 // Copyright (c) 2015 The Chromium 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 #ifndef THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_BINDING_FOR_TESTING_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_BINDING_FOR_TESTING_H_ 7 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h" 10 #include "third_party/blink/renderer/platform/bindings/exception_state.h" 11 #include "third_party/blink/renderer/platform/bindings/script_state.h" 12 #include "third_party/blink/renderer/platform/weborigin/kurl.h" 13 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" 14 #include "third_party/blink/renderer/platform/wtf/forward.h" 15 #include "v8/include/v8.h" 16 17 namespace blink { 18 19 class Document; 20 class DummyPageHolder; 21 class ExecutionContext; 22 class LocalDOMWindow; 23 class LocalFrame; 24 class KURL; 25 class Page; 26 27 class V8TestingScope { 28 STACK_ALLOCATED(); 29 30 public: 31 // TODO(keishi): Define CreateDummyPageHolder in DummyPageHolder. 32 static std::unique_ptr<DummyPageHolder> CreateDummyPageHolder( 33 const KURL& url); 34 explicit V8TestingScope(const KURL& url = KURL()); 35 ScriptState* GetScriptState() const; 36 ExecutionContext* GetExecutionContext() const; 37 v8::Isolate* GetIsolate() const; 38 v8::Local<v8::Context> GetContext() const; 39 ExceptionState& GetExceptionState(); 40 Page& GetPage(); 41 LocalFrame& GetFrame(); 42 LocalDOMWindow& GetWindow(); 43 Document& GetDocument(); 44 ~V8TestingScope(); 45 46 private: 47 std::unique_ptr<DummyPageHolder> holder_; 48 v8::HandleScope handle_scope_; 49 v8::Local<v8::Context> context_; 50 v8::Context::Scope context_scope_; 51 v8::TryCatch try_catch_; 52 DummyExceptionStateForTesting exception_state_; 53 }; 54 55 // Similar to other ToV8 helpers in to_v8_for_core.h. 56 template <typename T> ToV8(V8TestingScope * scope,T value)57v8::Local<v8::Value> ToV8(V8TestingScope* scope, T value) { 58 return blink::ToV8(value, scope->GetContext()->Global(), scope->GetIsolate()); 59 } 60 61 // Test supporting different kinds of GCs. 62 class BindingTestSupportingGC : public testing::Test { 63 public: SetIsolate(v8::Isolate * isolate)64 void SetIsolate(v8::Isolate* isolate) { 65 CHECK(isolate); 66 CHECK_EQ(isolate, ThreadState::Current()->GetIsolate()); 67 isolate_ = isolate; 68 } GetIsolate()69 v8::Isolate* GetIsolate() const { return isolate_; } 70 PreciselyCollectGarbage()71 void PreciselyCollectGarbage() { 72 ThreadState::Current()->CollectAllGarbageForTesting(); 73 } 74 RunV8MinorGC()75 void RunV8MinorGC() { 76 isolate_->RequestGarbageCollectionForTesting( 77 v8::Isolate::GarbageCollectionType::kMinorGarbageCollection); 78 } 79 80 void RunV8FullGC(v8::EmbedderHeapTracer::EmbedderStackState stack_state = 81 v8::EmbedderHeapTracer::EmbedderStackState::kEmpty) { 82 ThreadState::Current()->CollectAllGarbageForTesting( 83 stack_state == v8::EmbedderHeapTracer::EmbedderStackState::kEmpty 84 ? BlinkGC::kNoHeapPointersOnStack 85 : BlinkGC::kHeapPointersOnStack); 86 } 87 88 private: 89 v8::Isolate* isolate_; 90 }; 91 92 } // namespace blink 93 94 #endif // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_BINDING_FOR_TESTING_H_ 95