| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 #include "core/dom/ExecutionContextTask.h" | |
| 6 | |
| 7 #include "platform/heap/Handle.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace blink { | |
| 11 | |
| 12 class GCObject : public GarbageCollectedFinalized<GCObject> { | |
| 13 public: | |
| 14 static int counter_; | |
| 15 GCObject() { ++counter_; } | |
| 16 ~GCObject() { --counter_; } | |
| 17 DEFINE_INLINE_TRACE() {} | |
| 18 void Run(GCObject*) {} | |
| 19 }; | |
| 20 | |
| 21 int GCObject::counter_ = 0; | |
| 22 | |
| 23 static void FunctionWithGarbageCollected(GCObject*) {} | |
| 24 | |
| 25 static void FunctionWithExecutionContext(GCObject*, ExecutionContext*) {} | |
| 26 | |
| 27 class CrossThreadTaskTest : public testing::Test { | |
| 28 protected: | |
| 29 void SetUp() override { GCObject::counter_ = 0; } | |
| 30 void TearDown() override { | |
| 31 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | |
| 32 BlinkGC::kGCWithSweep, | |
| 33 BlinkGC::kForcedGC); | |
| 34 ASSERT_EQ(0, GCObject::counter_); | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 TEST_F(CrossThreadTaskTest, CreateForGarbageCollectedMethod) { | |
| 39 std::unique_ptr<ExecutionContextTask> task1 = CreateCrossThreadTask( | |
| 40 &GCObject::Run, WrapCrossThreadPersistent(new GCObject), | |
| 41 WrapCrossThreadPersistent(new GCObject)); | |
| 42 std::unique_ptr<ExecutionContextTask> task2 = CreateCrossThreadTask( | |
| 43 &GCObject::Run, WrapCrossThreadPersistent(new GCObject), | |
| 44 WrapCrossThreadPersistent(new GCObject)); | |
| 45 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | |
| 46 BlinkGC::kGCWithSweep, | |
| 47 BlinkGC::kForcedGC); | |
| 48 EXPECT_EQ(4, GCObject::counter_); | |
| 49 } | |
| 50 | |
| 51 TEST_F(CrossThreadTaskTest, CreateForFunctionWithGarbageCollected) { | |
| 52 std::unique_ptr<ExecutionContextTask> task1 = CreateCrossThreadTask( | |
| 53 &FunctionWithGarbageCollected, WrapCrossThreadPersistent(new GCObject)); | |
| 54 std::unique_ptr<ExecutionContextTask> task2 = CreateCrossThreadTask( | |
| 55 &FunctionWithGarbageCollected, WrapCrossThreadPersistent(new GCObject)); | |
| 56 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | |
| 57 BlinkGC::kGCWithSweep, | |
| 58 BlinkGC::kForcedGC); | |
| 59 EXPECT_EQ(2, GCObject::counter_); | |
| 60 } | |
| 61 | |
| 62 TEST_F(CrossThreadTaskTest, CreateForFunctionWithExecutionContext) { | |
| 63 std::unique_ptr<ExecutionContextTask> task1 = CreateCrossThreadTask( | |
| 64 &FunctionWithExecutionContext, WrapCrossThreadPersistent(new GCObject)); | |
| 65 std::unique_ptr<ExecutionContextTask> task2 = CreateCrossThreadTask( | |
| 66 &FunctionWithExecutionContext, WrapCrossThreadPersistent(new GCObject)); | |
| 67 ThreadState::Current()->CollectGarbage(BlinkGC::kNoHeapPointersOnStack, | |
| 68 BlinkGC::kGCWithSweep, | |
| 69 BlinkGC::kForcedGC); | |
| 70 EXPECT_EQ(2, GCObject::counter_); | |
| 71 } | |
| 72 | |
| 73 } // namespace blink | |
| OLD | NEW |