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

Side by Side Diff: content/browser/startup_task_runner_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: . Created 3 years, 8 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/browser/startup_task_runner.h" 5 #include "content/browser/startup_task_runner.h"
6 6
7 #include <utility>
8
7 #include "base/bind.h" 9 #include "base/bind.h"
8 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
9 #include "base/callback.h" 11 #include "base/callback.h"
10 #include "base/location.h" 12 #include "base/location.h"
11 #include "base/run_loop.h" 13 #include "base/run_loop.h"
12 #include "base/task_runner.h" 14 #include "base/task_runner.h"
13 15
14 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 18
17 namespace content { 19 namespace content {
18 namespace { 20 namespace {
19 21
20 using base::Closure; 22 using base::Closure;
21 using testing::_; 23 using testing::_;
22 using testing::Assign; 24 using testing::Assign;
23 using testing::Invoke; 25 using testing::Invoke;
24 using testing::WithArg; 26 using testing::WithArg;
25 27
26 int observer_calls = 0; 28 int observer_calls = 0;
27 int task_count = 0; 29 int task_count = 0;
28 int observer_result; 30 int observer_result;
29 base::Closure task; 31 base::Closure task;
30 32
31 // I couldn't get gMock's SaveArg to compile, hence had to save the argument 33 // I couldn't get gMock's SaveArg to compile, hence had to save the argument
32 // this way 34 // this way
33 bool SaveTaskArg(const Closure& arg) { 35 bool SaveTaskArg(base::Closure* arg) {
gab 2017/03/28 15:29:51 Ouch, that was already an ugly hack and now it's e
tzik 2017/03/28 17:34:20 Done. Yes, //content is covered by kinuko's revie
34 task = arg; 36 task = std::move(*arg);
35 return true; 37 return true;
36 } 38 }
37 39
38 void Observer(int result) { 40 void Observer(int result) {
39 observer_calls++; 41 observer_calls++;
40 observer_result = result; 42 observer_result = result;
41 } 43 }
42 44
43 class StartupTaskRunnerTest : public testing::Test { 45 class StartupTaskRunnerTest : public testing::Test {
44 public: 46 public:
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 // We can't use the real message loop, even if we want to, since doing so on 79 // We can't use the real message loop, even if we want to, since doing so on
78 // Android requires a complex Java infrastructure. The test would have to built 80 // Android requires a complex Java infrastructure. The test would have to built
79 // as a content_shell test; but content_shell startup invokes the class we are 81 // as a content_shell test; but content_shell startup invokes the class we are
80 // trying to test. 82 // trying to test.
81 // 83 //
82 // The mocks are not directly in TaskRunnerProxy because reference counted 84 // The mocks are not directly in TaskRunnerProxy because reference counted
83 // objects seem to confuse the mocking framework 85 // objects seem to confuse the mocking framework
84 86
85 class MockTaskRunner { 87 class MockTaskRunner {
86 public: 88 public:
87 MOCK_METHOD3( 89 MOCK_METHOD3(PostDelayedTask,
88 PostDelayedTask, 90 bool(const tracked_objects::Location&,
89 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta)); 91 base::Closure*,
90 MOCK_METHOD3( 92 base::TimeDelta));
91 PostNonNestableDelayedTask, 93 MOCK_METHOD3(PostNonNestableDelayedTask,
92 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta)); 94 bool(const tracked_objects::Location&,
95 base::Closure*,
96 base::TimeDelta));
97
98 bool PostDelayedTask(const tracked_objects::Location& from_here,
99 base::Closure task,
100 base::TimeDelta delay) {
101 return PostDelayedTask(from_here, &task, delay);
102 }
103
104 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
105 base::Closure task,
106 base::TimeDelta delay) {
107 return PostNonNestableDelayedTask(from_here, &task, delay);
108 }
93 }; 109 };
94 110
95 class TaskRunnerProxy : public base::SingleThreadTaskRunner { 111 class TaskRunnerProxy : public base::SingleThreadTaskRunner {
96 public: 112 public:
97 TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {} 113 TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {}
98 bool RunsTasksOnCurrentThread() const override { return true; } 114 bool RunsTasksOnCurrentThread() const override { return true; }
99 bool PostDelayedTask(const tracked_objects::Location& location, 115 bool PostDelayedTask(const tracked_objects::Location& location,
100 const Closure& closure, 116 base::Closure closure,
101 base::TimeDelta delta) override { 117 base::TimeDelta delta) override {
102 return mock_->PostDelayedTask(location, closure, delta); 118 return mock_->PostDelayedTask(location, std::move(closure), delta);
103 } 119 }
104 bool PostNonNestableDelayedTask(const tracked_objects::Location& location, 120 bool PostNonNestableDelayedTask(const tracked_objects::Location& location,
105 const Closure& closure, 121 base::Closure closure,
106 base::TimeDelta delta) override { 122 base::TimeDelta delta) override {
107 return mock_->PostNonNestableDelayedTask(location, closure, delta); 123 return mock_->PostNonNestableDelayedTask(location, std::move(closure),
124 delta);
108 } 125 }
109 126
110 private: 127 private:
111 MockTaskRunner* mock_; 128 MockTaskRunner* mock_;
112 ~TaskRunnerProxy() override {} 129 ~TaskRunnerProxy() override {}
113 }; 130 };
114 131
115 TEST_F(StartupTaskRunnerTest, SynchronousExecution) { 132 TEST_F(StartupTaskRunnerTest, SynchronousExecution) {
116 MockTaskRunner mock_runner; 133 MockTaskRunner mock_runner;
117 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); 134 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 runner.StartRunningTasksAsync(); 265 runner.StartRunningTasksAsync();
249 266
250 // No tasks should have run yet, since we the message loop hasn't run. 267 // No tasks should have run yet, since we the message loop hasn't run.
251 EXPECT_EQ(GetLastTask(), 0); 268 EXPECT_EQ(GetLastTask(), 0);
252 269
253 // Fake the actual message loop. Each time a task is run a new task should 270 // Fake the actual message loop. Each time a task is run a new task should
254 // be added to the queue, hence updating "task". The loop should actually run 271 // be added to the queue, hence updating "task". The loop should actually run
255 // at most 3 times (once for each task plus possibly once for the observer), 272 // at most 3 times (once for each task plus possibly once for the observer),
256 // the "4" is a backstop. 273 // the "4" is a backstop.
257 for (int i = 0; i < 4 && observer_calls == 0; i++) { 274 for (int i = 0; i < 4 && observer_calls == 0; i++) {
258 task.Run(); 275 std::move(task).Run();
259 EXPECT_EQ(i + 1, GetLastTask()); 276 EXPECT_EQ(i + 1, GetLastTask());
260 } 277 }
261 EXPECT_EQ(task_count, 2); 278 EXPECT_EQ(task_count, 2);
262 EXPECT_EQ(observer_calls, 1); 279 EXPECT_EQ(observer_calls, 1);
263 EXPECT_EQ(observer_result, 0); 280 EXPECT_EQ(observer_result, 0);
264 281
265 // Check that running synchronously now doesn't do anything 282 // Check that running synchronously now doesn't do anything
266 283
267 runner.RunAllTasksNow(); 284 runner.RunAllTasksNow();
268 EXPECT_EQ(task_count, 2); 285 EXPECT_EQ(task_count, 2);
(...skipping 26 matching lines...) Expand all
295 runner.StartRunningTasksAsync(); 312 runner.StartRunningTasksAsync();
296 313
297 // No tasks should have run yet, since we the message loop hasn't run. 314 // No tasks should have run yet, since we the message loop hasn't run.
298 EXPECT_EQ(GetLastTask(), 0); 315 EXPECT_EQ(GetLastTask(), 0);
299 316
300 // Fake the actual message loop. Each time a task is run a new task should 317 // Fake the actual message loop. Each time a task is run a new task should
301 // be added to the queue, hence updating "task". The loop should actually run 318 // be added to the queue, hence updating "task". The loop should actually run
302 // at most twice (once for the failed task plus possibly once for the 319 // at most twice (once for the failed task plus possibly once for the
303 // observer), the "4" is a backstop. 320 // observer), the "4" is a backstop.
304 for (int i = 0; i < 4 && observer_calls == 0; i++) { 321 for (int i = 0; i < 4 && observer_calls == 0; i++) {
305 task.Run(); 322 std::move(task).Run();
306 } 323 }
307 EXPECT_EQ(GetLastTask(), 3); 324 EXPECT_EQ(GetLastTask(), 3);
308 EXPECT_EQ(task_count, 1); 325 EXPECT_EQ(task_count, 1);
309 326
310 EXPECT_EQ(observer_calls, 1); 327 EXPECT_EQ(observer_calls, 1);
311 EXPECT_EQ(observer_result, 1); 328 EXPECT_EQ(observer_result, 1);
312 329
313 // Check that running synchronously now doesn't do anything 330 // Check that running synchronously now doesn't do anything
314 runner.RunAllTasksNow(); 331 runner.RunAllTasksNow();
315 EXPECT_EQ(observer_calls, 1); 332 EXPECT_EQ(observer_calls, 1);
316 EXPECT_EQ(task_count, 1); 333 EXPECT_EQ(task_count, 1);
317 } 334 }
318 } // namespace 335 } // namespace
319 } // namespace content 336 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/dom_storage/dom_storage_task_runner.cc ('k') | content/child/worker_thread_registry.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698