OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/csspaint/PaintWorklet.h" | 5 #include "modules/csspaint/PaintWorklet.h" |
6 | 6 |
| 7 #include <memory> |
7 #include "bindings/core/v8/ScriptSourceCode.h" | 8 #include "bindings/core/v8/ScriptSourceCode.h" |
8 #include "bindings/core/v8/V8GCController.h" | 9 #include "bindings/core/v8/V8GCController.h" |
9 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" | 10 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
10 #include "core/frame/LocalFrame.h" | 11 #include "core/frame/LocalFrame.h" |
11 #include "core/testing/DummyPageHolder.h" | 12 #include "core/testing/DummyPageHolder.h" |
12 #include "modules/csspaint/CSSPaintDefinition.h" | 13 #include "modules/csspaint/CSSPaintDefinition.h" |
13 #include "modules/csspaint/PaintWorkletGlobalScope.h" | 14 #include "modules/csspaint/PaintWorkletGlobalScope.h" |
| 15 #include "modules/csspaint/PaintWorkletGlobalScopeProxy.h" |
14 #include "modules/csspaint/WindowPaintWorklet.h" | 16 #include "modules/csspaint/WindowPaintWorklet.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 #include <memory> | |
17 | 18 |
18 namespace blink { | 19 namespace blink { |
19 | 20 |
20 class PaintWorkletTest : public testing::Test { | 21 class PaintWorkletTest : public testing::Test { |
21 public: | 22 public: |
22 PaintWorkletTest() : page_(DummyPageHolder::Create()) {} | 23 PaintWorkletTest() : page_(DummyPageHolder::Create()) {} |
23 | 24 |
| 25 void SetUp() override { proxy_ = GetPaintWorklet()->CreateGlobalScope(); } |
| 26 |
24 PaintWorklet* GetPaintWorklet() { | 27 PaintWorklet* GetPaintWorklet() { |
25 return WindowPaintWorklet::From(*page_->GetFrame().DomWindow()) | 28 return WindowPaintWorklet::From(*page_->GetFrame().DomWindow()) |
26 .paintWorklet(); | 29 .paintWorklet(); |
27 } | 30 } |
28 | 31 |
29 protected: | 32 PaintWorkletGlobalScopeProxy* GetProxy() { |
| 33 return PaintWorkletGlobalScopeProxy::From(proxy_.get()); |
| 34 } |
| 35 |
| 36 void Terminate() { |
| 37 page_.reset(); |
| 38 proxy_->TerminateWorkletGlobalScope(); |
| 39 proxy_.reset(); |
| 40 } |
| 41 |
| 42 private: |
30 std::unique_ptr<DummyPageHolder> page_; | 43 std::unique_ptr<DummyPageHolder> page_; |
| 44 std::unique_ptr<WorkletGlobalScopeProxy> proxy_; |
31 }; | 45 }; |
32 | 46 |
33 TEST_F(PaintWorkletTest, GarbageCollectionOfCSSPaintDefinition) { | 47 TEST_F(PaintWorkletTest, GarbageCollectionOfCSSPaintDefinition) { |
34 PaintWorkletGlobalScopeProxy* proxy = PaintWorkletGlobalScopeProxy::From( | 48 PaintWorkletGlobalScope* global_scope = GetProxy()->global_scope(); |
35 GetPaintWorklet()->GetWorkletGlobalScopeProxy()); | |
36 PaintWorkletGlobalScope* global_scope = proxy->global_scope(); | |
37 global_scope->ScriptController()->Evaluate( | 49 global_scope->ScriptController()->Evaluate( |
38 ScriptSourceCode("registerPaint('foo', class { paint() { } });")); | 50 ScriptSourceCode("registerPaint('foo', class { paint() { } });")); |
39 | 51 |
40 CSSPaintDefinition* definition = global_scope->FindDefinition("foo"); | 52 CSSPaintDefinition* definition = global_scope->FindDefinition("foo"); |
41 DCHECK(definition); | 53 DCHECK(definition); |
42 | 54 |
43 v8::Isolate* isolate = | 55 v8::Isolate* isolate = |
44 global_scope->ScriptController()->GetScriptState()->GetIsolate(); | 56 global_scope->ScriptController()->GetScriptState()->GetIsolate(); |
45 DCHECK(isolate); | 57 DCHECK(isolate); |
46 | 58 |
47 // Set our ScopedPersistent to the paint function, and make weak. | 59 // Set our ScopedPersistent to the paint function, and make weak. |
48 ScopedPersistent<v8::Function> handle; | 60 ScopedPersistent<v8::Function> handle; |
49 { | 61 { |
50 v8::HandleScope handle_scope(isolate); | 62 v8::HandleScope handle_scope(isolate); |
51 handle.Set(isolate, definition->PaintFunctionForTesting(isolate)); | 63 handle.Set(isolate, definition->PaintFunctionForTesting(isolate)); |
52 handle.SetPhantom(); | 64 handle.SetPhantom(); |
53 } | 65 } |
54 DCHECK(!handle.IsEmpty()); | 66 DCHECK(!handle.IsEmpty()); |
55 DCHECK(handle.IsWeak()); | 67 DCHECK(handle.IsWeak()); |
56 | 68 |
57 // Run a GC, persistent shouldn't have been collected yet. | 69 // Run a GC, persistent shouldn't have been collected yet. |
58 ThreadState::Current()->CollectAllGarbage(); | 70 ThreadState::Current()->CollectAllGarbage(); |
59 V8GCController::CollectAllGarbageForTesting(isolate); | 71 V8GCController::CollectAllGarbageForTesting(isolate); |
60 DCHECK(!handle.IsEmpty()); | 72 DCHECK(!handle.IsEmpty()); |
61 | 73 |
62 // Delete the page & associated objects. | 74 // Delete the page & associated objects. |
63 page_.reset(); | 75 Terminate(); |
64 | 76 |
65 // Run a GC, the persistent should have been collected. | 77 // Run a GC, the persistent should have been collected. |
66 ThreadState::Current()->CollectAllGarbage(); | 78 ThreadState::Current()->CollectAllGarbage(); |
67 V8GCController::CollectAllGarbageForTesting(isolate); | 79 V8GCController::CollectAllGarbageForTesting(isolate); |
68 DCHECK(handle.IsEmpty()); | 80 DCHECK(handle.IsEmpty()); |
69 } | 81 } |
70 | 82 |
71 } // namespace blink | 83 } // namespace blink |
OLD | NEW |