OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/scheduler/CancellableTaskFactory.h" | 5 #include "platform/scheduler/CancellableTaskFactory.h" |
6 | 6 |
7 #include "platform/heap/Handle.h" | 7 #include "platform/heap/Handle.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "wtf/PtrUtil.h" |
| 10 #include <memory> |
9 | 11 |
10 namespace blink { | 12 namespace blink { |
11 | 13 |
12 namespace { | 14 namespace { |
13 | 15 |
14 class TestCancellableTaskFactory final : public CancellableTaskFactory { | 16 class TestCancellableTaskFactory final : public CancellableTaskFactory { |
15 public: | 17 public: |
16 explicit TestCancellableTaskFactory(std::unique_ptr<SameThreadClosure> closu
re) | 18 explicit TestCancellableTaskFactory(std::unique_ptr<SameThreadClosure> closu
re) |
17 : CancellableTaskFactory(std::move(closure)) | 19 : CancellableTaskFactory(std::move(closure)) |
18 { | 20 { |
19 } | 21 } |
20 }; | 22 }; |
21 | 23 |
22 } // namespace | 24 } // namespace |
23 | 25 |
24 using CancellableTaskFactoryTest = testing::Test; | 26 using CancellableTaskFactoryTest = testing::Test; |
25 | 27 |
26 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated) | 28 TEST_F(CancellableTaskFactoryTest, IsPending_TaskNotCreated) |
27 { | 29 { |
28 TestCancellableTaskFactory factory(nullptr); | 30 TestCancellableTaskFactory factory(nullptr); |
29 | 31 |
30 EXPECT_FALSE(factory.isPending()); | 32 EXPECT_FALSE(factory.isPending()); |
31 } | 33 } |
32 | 34 |
33 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated) | 35 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreated) |
34 { | 36 { |
35 TestCancellableTaskFactory factory(nullptr); | 37 TestCancellableTaskFactory factory(nullptr); |
36 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 38 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea
te()); |
37 | 39 |
38 EXPECT_TRUE(factory.isPending()); | 40 EXPECT_TRUE(factory.isPending()); |
39 } | 41 } |
40 | 42 |
41 void EmptyFn() | 43 void EmptyFn() |
42 { | 44 { |
43 } | 45 } |
44 | 46 |
45 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun) | 47 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndRun) |
46 { | 48 { |
47 TestCancellableTaskFactory factory(WTF::bind(&EmptyFn)); | 49 TestCancellableTaskFactory factory(WTF::bind(&EmptyFn)); |
48 { | 50 { |
49 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 51 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAnd
Create()); |
50 task->run(); | 52 task->run(); |
51 } | 53 } |
52 | 54 |
53 EXPECT_FALSE(factory.isPending()); | 55 EXPECT_FALSE(factory.isPending()); |
54 } | 56 } |
55 | 57 |
56 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed) | 58 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndDestroyed) |
57 { | 59 { |
58 TestCancellableTaskFactory factory(nullptr); | 60 TestCancellableTaskFactory factory(nullptr); |
59 delete factory.cancelAndCreate(); | 61 delete factory.cancelAndCreate(); |
60 | 62 |
61 EXPECT_FALSE(factory.isPending()); | 63 EXPECT_FALSE(factory.isPending()); |
62 } | 64 } |
63 | 65 |
64 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled) | 66 TEST_F(CancellableTaskFactoryTest, IsPending_TaskCreatedAndCancelled) |
65 { | 67 { |
66 TestCancellableTaskFactory factory(nullptr); | 68 TestCancellableTaskFactory factory(nullptr); |
67 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 69 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea
te()); |
68 factory.cancel(); | 70 factory.cancel(); |
69 | 71 |
70 EXPECT_FALSE(factory.isPending()); | 72 EXPECT_FALSE(factory.isPending()); |
71 } | 73 } |
72 | 74 |
73 class TestClass { | 75 class TestClass { |
74 public: | 76 public: |
75 OwnPtr<CancellableTaskFactory> m_factory; | 77 std::unique_ptr<CancellableTaskFactory> m_factory; |
76 | 78 |
77 TestClass() | 79 TestClass() |
78 : m_factory(CancellableTaskFactory::create(this, &TestClass::TestFn)) | 80 : m_factory(CancellableTaskFactory::create(this, &TestClass::TestFn)) |
79 { | 81 { |
80 } | 82 } |
81 | 83 |
82 void TestFn() | 84 void TestFn() |
83 { | 85 { |
84 EXPECT_FALSE(m_factory->isPending()); | 86 EXPECT_FALSE(m_factory->isPending()); |
85 } | 87 } |
86 }; | 88 }; |
87 | 89 |
88 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) | 90 TEST_F(CancellableTaskFactoryTest, IsPending_InCallback) |
89 { | 91 { |
90 TestClass testClass; | 92 TestClass testClass; |
91 OwnPtr<WebTaskRunner::Task> task = adoptPtr(testClass.m_factory->cancelAndCr
eate()); | 93 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(testClass.m_factory->
cancelAndCreate()); |
92 task->run(); | 94 task->run(); |
93 } | 95 } |
94 | 96 |
95 void AddOne(int* ptr) | 97 void AddOne(int* ptr) |
96 { | 98 { |
97 *ptr += 1; | 99 *ptr += 1; |
98 } | 100 } |
99 | 101 |
100 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) | 102 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) |
101 { | 103 { |
102 int executionCount = 0; | 104 int executionCount = 0; |
103 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 105 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
104 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 106 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea
te()); |
105 task->run(); | 107 task->run(); |
106 | 108 |
107 EXPECT_EQ(1, executionCount); | 109 EXPECT_EQ(1, executionCount); |
108 } | 110 } |
109 | 111 |
110 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) | 112 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) |
111 { | 113 { |
112 int executionCount = 0; | 114 int executionCount = 0; |
113 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 115 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
114 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 116 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea
te()); |
115 task->run(); | 117 task->run(); |
116 task->run(); | 118 task->run(); |
117 task->run(); | 119 task->run(); |
118 task->run(); | 120 task->run(); |
119 | 121 |
120 EXPECT_EQ(1, executionCount); | 122 EXPECT_EQ(1, executionCount); |
121 } | 123 } |
122 | 124 |
123 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) | 125 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) |
124 { | 126 { |
125 int executionCount = 0; | 127 int executionCount = 0; |
126 OwnPtr<WebTaskRunner::Task> task; | 128 std::unique_ptr<WebTaskRunner::Task> task; |
127 { | 129 { |
128 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 130 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
129 task = adoptPtr(factory.cancelAndCreate()); | 131 task = wrapUnique(factory.cancelAndCreate()); |
130 } | 132 } |
131 task->run(); | 133 task->run(); |
132 | 134 |
133 EXPECT_EQ(0, executionCount); | 135 EXPECT_EQ(0, executionCount); |
134 } | 136 } |
135 | 137 |
136 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) | 138 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) |
137 { | 139 { |
138 int executionCount = 0; | 140 int executionCount = 0; |
139 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 141 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
140 | 142 |
141 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate()); | 143 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre
ate()); |
142 taskA->run(); | 144 taskA->run(); |
143 EXPECT_EQ(1, executionCount); | 145 EXPECT_EQ(1, executionCount); |
144 | 146 |
145 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate()); | 147 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre
ate()); |
146 taskB->run(); | 148 taskB->run(); |
147 EXPECT_EQ(2, executionCount); | 149 EXPECT_EQ(2, executionCount); |
148 | 150 |
149 OwnPtr<WebTaskRunner::Task> taskC = adoptPtr(factory.cancelAndCreate()); | 151 std::unique_ptr<WebTaskRunner::Task> taskC = wrapUnique(factory.cancelAndCre
ate()); |
150 taskC->run(); | 152 taskC->run(); |
151 EXPECT_EQ(3, executionCount); | 153 EXPECT_EQ(3, executionCount); |
152 } | 154 } |
153 | 155 |
154 TEST_F(CancellableTaskFactoryTest, Cancel) | 156 TEST_F(CancellableTaskFactoryTest, Cancel) |
155 { | 157 { |
156 int executionCount = 0; | 158 int executionCount = 0; |
157 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 159 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
158 OwnPtr<WebTaskRunner::Task> task = adoptPtr(factory.cancelAndCreate()); | 160 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea
te()); |
159 factory.cancel(); | 161 factory.cancel(); |
160 task->run(); | 162 task->run(); |
161 | 163 |
162 EXPECT_EQ(0, executionCount); | 164 EXPECT_EQ(0, executionCount); |
163 } | 165 } |
164 | 166 |
165 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) | 167 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) |
166 { | 168 { |
167 int executionCount = 0; | 169 int executionCount = 0; |
168 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); | 170 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); |
169 | 171 |
170 OwnPtr<WebTaskRunner::Task> taskA = adoptPtr(factory.cancelAndCreate()); | 172 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre
ate()); |
171 OwnPtr<WebTaskRunner::Task> taskB = adoptPtr(factory.cancelAndCreate()); | 173 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre
ate()); |
172 | 174 |
173 taskA->run(); | 175 taskA->run(); |
174 EXPECT_EQ(0, executionCount); | 176 EXPECT_EQ(0, executionCount); |
175 | 177 |
176 taskB->run(); | 178 taskB->run(); |
177 EXPECT_EQ(1, executionCount); | 179 EXPECT_EQ(1, executionCount); |
178 } | 180 } |
179 | 181 |
180 namespace { | 182 namespace { |
181 | 183 |
(...skipping 12 matching lines...) Expand all Loading... |
194 void run() | 196 void run() |
195 { | 197 { |
196 s_invoked++; | 198 s_invoked++; |
197 } | 199 } |
198 | 200 |
199 DEFINE_INLINE_TRACE() { } | 201 DEFINE_INLINE_TRACE() { } |
200 | 202 |
201 static int s_destructed; | 203 static int s_destructed; |
202 static int s_invoked; | 204 static int s_invoked; |
203 | 205 |
204 OwnPtr<CancellableTaskFactory> m_factory; | 206 std::unique_ptr<CancellableTaskFactory> m_factory; |
205 }; | 207 }; |
206 | 208 |
207 int GCObject::s_destructed = 0; | 209 int GCObject::s_destructed = 0; |
208 int GCObject::s_invoked = 0; | 210 int GCObject::s_invoked = 0; |
209 | 211 |
210 } // namespace | 212 } // namespace |
211 | 213 |
212 TEST(CancellableTaskFactoryTest, GarbageCollectedWeak) | 214 TEST(CancellableTaskFactoryTest, GarbageCollectedWeak) |
213 { | 215 { |
214 GCObject* object = new GCObject(); | 216 GCObject* object = new GCObject(); |
215 OwnPtr<WebTaskRunner::Task> task = adoptPtr(object->m_factory->cancelAndCrea
te()); | 217 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(object->m_factory->ca
ncelAndCreate()); |
216 object = nullptr; | 218 object = nullptr; |
217 ThreadHeap::collectAllGarbage(); | 219 ThreadHeap::collectAllGarbage(); |
218 task->run(); | 220 task->run(); |
219 // The owning object will have been GCed and the task will have | 221 // The owning object will have been GCed and the task will have |
220 // lost its weak reference. Verify that it wasn't invoked. | 222 // lost its weak reference. Verify that it wasn't invoked. |
221 EXPECT_EQ(0, GCObject::s_invoked); | 223 EXPECT_EQ(0, GCObject::s_invoked); |
222 | 224 |
223 // ..and just to make sure |object| was indeed destructed. | 225 // ..and just to make sure |object| was indeed destructed. |
224 EXPECT_EQ(1, GCObject::s_destructed); | 226 EXPECT_EQ(1, GCObject::s_destructed); |
225 } | 227 } |
226 | 228 |
227 } // namespace blink | 229 } // namespace blink |
OLD | NEW |