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

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

Issue 2093603002: Wrap non-GCed raw pointer parameters of WTF::bind with WTF::unretained (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@unretained_wrapper
Patch Set: rebase Created 4 years, 5 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" 9 #include "wtf/PtrUtil.h"
10 #include <memory> 10 #include <memory>
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 95 }
96 96
97 void AddOne(int* ptr) 97 void AddOne(int* ptr)
98 { 98 {
99 *ptr += 1; 99 *ptr += 1;
100 } 100 }
101 101
102 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted) 102 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecuted)
103 { 103 {
104 int executionCount = 0; 104 int executionCount = 0;
105 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 105 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&execu tionCount)));
106 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te()); 106 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te());
107 task->run(); 107 task->run();
108 108
109 EXPECT_EQ(1, executionCount); 109 EXPECT_EQ(1, executionCount);
110 } 110 }
111 111
112 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce) 112 TEST_F(CancellableTaskFactoryTest, Run_ClosureIsExecutedOnlyOnce)
113 { 113 {
114 int executionCount = 0; 114 int executionCount = 0;
115 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 115 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&execu tionCount)));
116 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te()); 116 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te());
117 task->run(); 117 task->run();
118 task->run(); 118 task->run();
119 task->run(); 119 task->run();
120 task->run(); 120 task->run();
121 121
122 EXPECT_EQ(1, executionCount); 122 EXPECT_EQ(1, executionCount);
123 } 123 }
124 124
125 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution) 125 TEST_F(CancellableTaskFactoryTest, Run_FactoryDestructionPreventsExecution)
126 { 126 {
127 int executionCount = 0; 127 int executionCount = 0;
128 std::unique_ptr<WebTaskRunner::Task> task; 128 std::unique_ptr<WebTaskRunner::Task> task;
129 { 129 {
130 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 130 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&e xecutionCount)));
131 task = wrapUnique(factory.cancelAndCreate()); 131 task = wrapUnique(factory.cancelAndCreate());
132 } 132 }
133 task->run(); 133 task->run();
134 134
135 EXPECT_EQ(0, executionCount); 135 EXPECT_EQ(0, executionCount);
136 } 136 }
137 137
138 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence) 138 TEST_F(CancellableTaskFactoryTest, Run_TasksInSequence)
139 { 139 {
140 int executionCount = 0; 140 int executionCount = 0;
141 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 141 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&execu tionCount)));
142 142
143 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre ate()); 143 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre ate());
144 taskA->run(); 144 taskA->run();
145 EXPECT_EQ(1, executionCount); 145 EXPECT_EQ(1, executionCount);
146 146
147 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre ate()); 147 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre ate());
148 taskB->run(); 148 taskB->run();
149 EXPECT_EQ(2, executionCount); 149 EXPECT_EQ(2, executionCount);
150 150
151 std::unique_ptr<WebTaskRunner::Task> taskC = wrapUnique(factory.cancelAndCre ate()); 151 std::unique_ptr<WebTaskRunner::Task> taskC = wrapUnique(factory.cancelAndCre ate());
152 taskC->run(); 152 taskC->run();
153 EXPECT_EQ(3, executionCount); 153 EXPECT_EQ(3, executionCount);
154 } 154 }
155 155
156 TEST_F(CancellableTaskFactoryTest, Cancel) 156 TEST_F(CancellableTaskFactoryTest, Cancel)
157 { 157 {
158 int executionCount = 0; 158 int executionCount = 0;
159 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 159 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&execu tionCount)));
160 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te()); 160 std::unique_ptr<WebTaskRunner::Task> task = wrapUnique(factory.cancelAndCrea te());
161 factory.cancel(); 161 factory.cancel();
162 task->run(); 162 task->run();
163 163
164 EXPECT_EQ(0, executionCount); 164 EXPECT_EQ(0, executionCount);
165 } 165 }
166 166
167 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes) 167 TEST_F(CancellableTaskFactoryTest, CreatingANewTaskCancelsPreviousOnes)
168 { 168 {
169 int executionCount = 0; 169 int executionCount = 0;
170 TestCancellableTaskFactory factory(WTF::bind(&AddOne, &executionCount)); 170 TestCancellableTaskFactory factory(WTF::bind(&AddOne, WTF::unretained(&execu tionCount)));
171 171
172 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre ate()); 172 std::unique_ptr<WebTaskRunner::Task> taskA = wrapUnique(factory.cancelAndCre ate());
173 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre ate()); 173 std::unique_ptr<WebTaskRunner::Task> taskB = wrapUnique(factory.cancelAndCre ate());
174 174
175 taskA->run(); 175 taskA->run();
176 EXPECT_EQ(0, executionCount); 176 EXPECT_EQ(0, executionCount);
177 177
178 taskB->run(); 178 taskB->run();
179 EXPECT_EQ(1, executionCount); 179 EXPECT_EQ(1, executionCount);
180 } 180 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 task->run(); 220 task->run();
221 // 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
222 // lost its weak reference. Verify that it wasn't invoked. 222 // lost its weak reference. Verify that it wasn't invoked.
223 EXPECT_EQ(0, GCObject::s_invoked); 223 EXPECT_EQ(0, GCObject::s_invoked);
224 224
225 // ..and just to make sure |object| was indeed destructed. 225 // ..and just to make sure |object| was indeed destructed.
226 EXPECT_EQ(1, GCObject::s_destructed); 226 EXPECT_EQ(1, GCObject::s_destructed);
227 } 227 }
228 228
229 } // namespace blink 229 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698