| OLD | NEW |
| 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" |
| 12 #include "base/callback_helpers.h" |
| 10 #include "base/location.h" | 13 #include "base/location.h" |
| 11 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 12 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 13 | 16 |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 17 namespace content { | 20 namespace content { |
| 18 namespace { | 21 namespace { |
| 19 | 22 |
| 20 using base::Closure; | 23 using base::Closure; |
| 21 using testing::_; | 24 using testing::_; |
| 22 using testing::Assign; | 25 using testing::Assign; |
| 23 using testing::Invoke; | 26 using testing::Invoke; |
| 24 using testing::WithArg; | 27 using testing::WithArg; |
| 25 | 28 |
| 26 int observer_calls = 0; | 29 int observer_calls = 0; |
| 27 int task_count = 0; | 30 int task_count = 0; |
| 28 int observer_result; | 31 int observer_result; |
| 29 base::Closure task; | 32 base::Closure task; |
| 30 | 33 |
| 31 // I couldn't get gMock's SaveArg to compile, hence had to save the argument | 34 // I couldn't get gMock's SaveArg to compile, hence had to save the argument |
| 32 // this way | 35 // this way |
| 33 bool SaveTaskArg(const Closure& arg) { | 36 bool SaveTaskArg(base::Closure* arg) { |
| 34 task = arg; | 37 task = std::move(*arg); |
| 35 return true; | 38 return true; |
| 36 } | 39 } |
| 37 | 40 |
| 38 void Observer(int result) { | 41 void Observer(int result) { |
| 39 observer_calls++; | 42 observer_calls++; |
| 40 observer_result = result; | 43 observer_result = result; |
| 41 } | 44 } |
| 42 | 45 |
| 43 class StartupTaskRunnerTest : public testing::Test { | 46 class StartupTaskRunnerTest : public testing::Test { |
| 44 public: | 47 public: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 // We can't use the real message loop, even if we want to, since doing so on | 80 // 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 | 81 // 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 | 82 // as a content_shell test; but content_shell startup invokes the class we are |
| 80 // trying to test. | 83 // trying to test. |
| 81 // | 84 // |
| 82 // The mocks are not directly in TaskRunnerProxy because reference counted | 85 // The mocks are not directly in TaskRunnerProxy because reference counted |
| 83 // objects seem to confuse the mocking framework | 86 // objects seem to confuse the mocking framework |
| 84 | 87 |
| 85 class MockTaskRunner { | 88 class MockTaskRunner { |
| 86 public: | 89 public: |
| 87 MOCK_METHOD3( | 90 MOCK_METHOD3(PostDelayedTask, |
| 88 PostDelayedTask, | 91 bool(const tracked_objects::Location&, |
| 89 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta)); | 92 base::Closure*, |
| 90 MOCK_METHOD3( | 93 base::TimeDelta)); |
| 91 PostNonNestableDelayedTask, | 94 MOCK_METHOD3(PostNonNestableDelayedTask, |
| 92 bool(const tracked_objects::Location&, const Closure&, base::TimeDelta)); | 95 bool(const tracked_objects::Location&, |
| 96 base::Closure*, |
| 97 base::TimeDelta)); |
| 98 |
| 99 bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 100 base::Closure task, |
| 101 base::TimeDelta delay) { |
| 102 return PostDelayedTask(from_here, &task, delay); |
| 103 } |
| 104 |
| 105 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 106 base::Closure task, |
| 107 base::TimeDelta delay) { |
| 108 return PostNonNestableDelayedTask(from_here, &task, delay); |
| 109 } |
| 93 }; | 110 }; |
| 94 | 111 |
| 95 class TaskRunnerProxy : public base::SingleThreadTaskRunner { | 112 class TaskRunnerProxy : public base::SingleThreadTaskRunner { |
| 96 public: | 113 public: |
| 97 TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {} | 114 TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {} |
| 98 bool RunsTasksOnCurrentThread() const override { return true; } | 115 bool RunsTasksOnCurrentThread() const override { return true; } |
| 99 bool PostDelayedTask(const tracked_objects::Location& location, | 116 bool PostDelayedTask(const tracked_objects::Location& location, |
| 100 const Closure& closure, | 117 base::Closure closure, |
| 101 base::TimeDelta delta) override { | 118 base::TimeDelta delta) override { |
| 102 return mock_->PostDelayedTask(location, closure, delta); | 119 return mock_->PostDelayedTask(location, std::move(closure), delta); |
| 103 } | 120 } |
| 104 bool PostNonNestableDelayedTask(const tracked_objects::Location& location, | 121 bool PostNonNestableDelayedTask(const tracked_objects::Location& location, |
| 105 const Closure& closure, | 122 base::Closure closure, |
| 106 base::TimeDelta delta) override { | 123 base::TimeDelta delta) override { |
| 107 return mock_->PostNonNestableDelayedTask(location, closure, delta); | 124 return mock_->PostNonNestableDelayedTask(location, std::move(closure), |
| 125 delta); |
| 108 } | 126 } |
| 109 | 127 |
| 110 private: | 128 private: |
| 111 MockTaskRunner* mock_; | 129 MockTaskRunner* mock_; |
| 112 ~TaskRunnerProxy() override {} | 130 ~TaskRunnerProxy() override {} |
| 113 }; | 131 }; |
| 114 | 132 |
| 115 TEST_F(StartupTaskRunnerTest, SynchronousExecution) { | 133 TEST_F(StartupTaskRunnerTest, SynchronousExecution) { |
| 116 MockTaskRunner mock_runner; | 134 MockTaskRunner mock_runner; |
| 117 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); | 135 scoped_refptr<TaskRunnerProxy> proxy = new TaskRunnerProxy(&mock_runner); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 runner.StartRunningTasksAsync(); | 266 runner.StartRunningTasksAsync(); |
| 249 | 267 |
| 250 // No tasks should have run yet, since we the message loop hasn't run. | 268 // No tasks should have run yet, since we the message loop hasn't run. |
| 251 EXPECT_EQ(GetLastTask(), 0); | 269 EXPECT_EQ(GetLastTask(), 0); |
| 252 | 270 |
| 253 // Fake the actual message loop. Each time a task is run a new task should | 271 // 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 | 272 // 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), | 273 // at most 3 times (once for each task plus possibly once for the observer), |
| 256 // the "4" is a backstop. | 274 // the "4" is a backstop. |
| 257 for (int i = 0; i < 4 && observer_calls == 0; i++) { | 275 for (int i = 0; i < 4 && observer_calls == 0; i++) { |
| 258 task.Run(); | 276 std::move(task).Run(); |
| 259 EXPECT_EQ(i + 1, GetLastTask()); | 277 EXPECT_EQ(i + 1, GetLastTask()); |
| 260 } | 278 } |
| 261 EXPECT_EQ(task_count, 2); | 279 EXPECT_EQ(task_count, 2); |
| 262 EXPECT_EQ(observer_calls, 1); | 280 EXPECT_EQ(observer_calls, 1); |
| 263 EXPECT_EQ(observer_result, 0); | 281 EXPECT_EQ(observer_result, 0); |
| 264 | 282 |
| 265 // Check that running synchronously now doesn't do anything | 283 // Check that running synchronously now doesn't do anything |
| 266 | 284 |
| 267 runner.RunAllTasksNow(); | 285 runner.RunAllTasksNow(); |
| 268 EXPECT_EQ(task_count, 2); | 286 EXPECT_EQ(task_count, 2); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 295 runner.StartRunningTasksAsync(); | 313 runner.StartRunningTasksAsync(); |
| 296 | 314 |
| 297 // No tasks should have run yet, since we the message loop hasn't run. | 315 // No tasks should have run yet, since we the message loop hasn't run. |
| 298 EXPECT_EQ(GetLastTask(), 0); | 316 EXPECT_EQ(GetLastTask(), 0); |
| 299 | 317 |
| 300 // Fake the actual message loop. Each time a task is run a new task should | 318 // 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 | 319 // 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 | 320 // at most twice (once for the failed task plus possibly once for the |
| 303 // observer), the "4" is a backstop. | 321 // observer), the "4" is a backstop. |
| 304 for (int i = 0; i < 4 && observer_calls == 0; i++) { | 322 for (int i = 0; i < 4 && observer_calls == 0; i++) { |
| 305 task.Run(); | 323 std::move(task).Run(); |
| 306 } | 324 } |
| 307 EXPECT_EQ(GetLastTask(), 3); | 325 EXPECT_EQ(GetLastTask(), 3); |
| 308 EXPECT_EQ(task_count, 1); | 326 EXPECT_EQ(task_count, 1); |
| 309 | 327 |
| 310 EXPECT_EQ(observer_calls, 1); | 328 EXPECT_EQ(observer_calls, 1); |
| 311 EXPECT_EQ(observer_result, 1); | 329 EXPECT_EQ(observer_result, 1); |
| 312 | 330 |
| 313 // Check that running synchronously now doesn't do anything | 331 // Check that running synchronously now doesn't do anything |
| 314 runner.RunAllTasksNow(); | 332 runner.RunAllTasksNow(); |
| 315 EXPECT_EQ(observer_calls, 1); | 333 EXPECT_EQ(observer_calls, 1); |
| 316 EXPECT_EQ(task_count, 1); | 334 EXPECT_EQ(task_count, 1); |
| 317 } | 335 } |
| 318 } // namespace | 336 } // namespace |
| 319 } // namespace content | 337 } // namespace content |
| OLD | NEW |