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 "platform/WebTaskRunner.h" | 5 #include "platform/WebTaskRunner.h" |
6 | 6 |
7 #include "platform/scheduler/test/fake_web_task_runner.h" | 7 #include "platform/scheduler/test/fake_web_task_runner.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace blink { | 10 namespace blink { |
11 namespace { | 11 namespace { |
12 | 12 |
13 void increment(int* x) { | 13 void increment(int* x) { |
14 ++*x; | 14 ++*x; |
15 } | 15 } |
16 | 16 |
17 void getIsActive(bool* isActive, TaskHandle* handle) { | 17 void getIsActive(bool* isActive, TaskHandle* handle) { |
18 *isActive = handle->isActive(); | 18 *isActive = handle->isActive(); |
19 } | 19 } |
20 | 20 |
| 21 class CancellationTestHelper { |
| 22 public: |
| 23 CancellationTestHelper() : m_weakPtrFactory(this) {} |
| 24 |
| 25 WeakPtr<CancellationTestHelper> createWeakPtr() { |
| 26 return m_weakPtrFactory.createWeakPtr(); |
| 27 } |
| 28 |
| 29 void revokeWeakPtrs() { m_weakPtrFactory.revokeAll(); } |
| 30 void incrementCounter() { ++m_counter; } |
| 31 int counter() const { return m_counter; } |
| 32 |
| 33 private: |
| 34 int m_counter = 0; |
| 35 WeakPtrFactory<CancellationTestHelper> m_weakPtrFactory; |
| 36 }; |
| 37 |
21 } // namespace | 38 } // namespace |
22 | 39 |
23 TEST(WebTaskRunnerTest, PostCancellableTaskTest) { | 40 TEST(WebTaskRunnerTest, PostCancellableTaskTest) { |
24 scheduler::FakeWebTaskRunner taskRunner; | 41 scheduler::FakeWebTaskRunner taskRunner; |
25 | 42 |
26 // Run without cancellation. | 43 // Run without cancellation. |
27 int count = 0; | 44 int count = 0; |
28 TaskHandle handle = taskRunner.postCancellableTask( | 45 TaskHandle handle = taskRunner.postCancellableTask( |
29 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); | 46 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
30 EXPECT_EQ(0, count); | 47 EXPECT_EQ(0, count); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 bool isActive = false; | 109 bool isActive = false; |
93 handle = taskRunner.postCancellableTask( | 110 handle = taskRunner.postCancellableTask( |
94 BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive), | 111 BLINK_FROM_HERE, WTF::bind(&getIsActive, WTF::unretained(&isActive), |
95 WTF::unretained(&handle))); | 112 WTF::unretained(&handle))); |
96 EXPECT_TRUE(handle.isActive()); | 113 EXPECT_TRUE(handle.isActive()); |
97 taskRunner.runUntilIdle(); | 114 taskRunner.runUntilIdle(); |
98 EXPECT_FALSE(isActive); | 115 EXPECT_FALSE(isActive); |
99 EXPECT_FALSE(handle.isActive()); | 116 EXPECT_FALSE(handle.isActive()); |
100 } | 117 } |
101 | 118 |
| 119 TEST(WebTaskRunnerTest, CancellationCheckerTest) { |
| 120 scheduler::FakeWebTaskRunner taskRunner; |
| 121 |
| 122 int count = 0; |
| 123 TaskHandle handle = taskRunner.postCancellableTask( |
| 124 BLINK_FROM_HERE, WTF::bind(&increment, WTF::unretained(&count))); |
| 125 EXPECT_EQ(0, count); |
| 126 |
| 127 // TaskHandle::isActive should detect the deletion of posted task. |
| 128 auto queue = taskRunner.takePendingTasks(); |
| 129 ASSERT_EQ(1u, queue.size()); |
| 130 EXPECT_FALSE(queue[0].IsCancelled()); |
| 131 EXPECT_TRUE(handle.isActive()); |
| 132 queue.clear(); |
| 133 EXPECT_FALSE(handle.isActive()); |
| 134 EXPECT_EQ(0, count); |
| 135 |
| 136 count = 0; |
| 137 CancellationTestHelper helper; |
| 138 handle = taskRunner.postCancellableTask( |
| 139 BLINK_FROM_HERE, WTF::bind(&CancellationTestHelper::incrementCounter, |
| 140 helper.createWeakPtr())); |
| 141 EXPECT_EQ(0, helper.counter()); |
| 142 |
| 143 // The cancellation of the posted task should be propagated to TaskHandle. |
| 144 queue = taskRunner.takePendingTasks(); |
| 145 ASSERT_EQ(1u, queue.size()); |
| 146 EXPECT_FALSE(queue[0].IsCancelled()); |
| 147 EXPECT_TRUE(handle.isActive()); |
| 148 helper.revokeWeakPtrs(); |
| 149 EXPECT_TRUE(queue[0].IsCancelled()); |
| 150 EXPECT_FALSE(handle.isActive()); |
| 151 } |
| 152 |
102 } // namespace blink | 153 } // namespace blink |
OLD | NEW |