Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: third_party/WebKit/Source/platform/scheduler/CancellableTaskFactoryTest.cpp

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

Powered by Google App Engine
This is Rietveld 408576698