| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This class defines tests that implementations of TaskRunner should | 5 // This class defines tests that implementations of TaskRunner should |
| 6 // pass in order to be conformant. Here's how you use it to test your | 6 // pass in order to be conformant. Here's how you use it to test your |
| 7 // implementation. | 7 // implementation. |
| 8 // | 8 // |
| 9 // Say your class is called MyTaskRunner. Then you need to define a | 9 // Say your class is called MyTaskRunner. Then you need to define a |
| 10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc | 10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // scoped_refptr<MyTaskRunner> GetTaskRunner() { | 23 // scoped_refptr<MyTaskRunner> GetTaskRunner() { |
| 24 // ... | 24 // ... |
| 25 // } | 25 // } |
| 26 // | 26 // |
| 27 // // Stop the task runner and make sure all tasks posted before | 27 // // Stop the task runner and make sure all tasks posted before |
| 28 // // this is called are run. Caveat: delayed tasks are not run, | 28 // // this is called are run. Caveat: delayed tasks are not run, |
| 29 // they're simply deleted. | 29 // they're simply deleted. |
| 30 // void StopTaskRunner() { | 30 // void StopTaskRunner() { |
| 31 // ... | 31 // ... |
| 32 // } | 32 // } |
| 33 // | |
| 34 // // Returns whether or not the task runner obeys non-zero delays. | |
| 35 // bool TaskRunnerHandlesNonZeroDelays() const { | |
| 36 // return true; | |
| 37 // } | |
| 38 // }; | 33 // }; |
| 39 // | 34 // |
| 40 // The TaskRunnerTest test harness will have a member variable of | 35 // The TaskRunnerTest test harness will have a member variable of |
| 41 // this delegate type and will call its functions in the various | 36 // this delegate type and will call its functions in the various |
| 42 // tests. | 37 // tests. |
| 43 // | 38 // |
| 44 // Then you simply #include this file as well as gtest.h and add the | 39 // Then you simply #include this file as well as gtest.h and add the |
| 45 // following statement to my_task_runner_unittest.cc: | 40 // following statement to my_task_runner_unittest.cc: |
| 46 // | 41 // |
| 47 // INSTANTIATE_TYPED_TEST_CASE_P( | 42 // INSTANTIATE_TYPED_TEST_CASE_P( |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 129 } |
| 135 this->delegate_.StopTaskRunner(); | 130 this->delegate_.StopTaskRunner(); |
| 136 | 131 |
| 137 EXPECT_EQ(expected_task_run_counts, | 132 EXPECT_EQ(expected_task_run_counts, |
| 138 this->task_tracker_->GetTaskRunCounts()); | 133 this->task_tracker_->GetTaskRunCounts()); |
| 139 } | 134 } |
| 140 | 135 |
| 141 // Post a bunch of delayed tasks to the task runner. They should all | 136 // Post a bunch of delayed tasks to the task runner. They should all |
| 142 // complete. | 137 // complete. |
| 143 TYPED_TEST_P(TaskRunnerTest, Delayed) { | 138 TYPED_TEST_P(TaskRunnerTest, Delayed) { |
| 144 if (!this->delegate_.TaskRunnerHandlesNonZeroDelays()) { | |
| 145 DLOG(INFO) << "This TaskRunner doesn't handle non-zero delays; skipping"; | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 std::map<int, int> expected_task_run_counts; | 139 std::map<int, int> expected_task_run_counts; |
| 150 int expected_total_tasks = 0; | 140 int expected_total_tasks = 0; |
| 151 | 141 |
| 152 this->delegate_.StartTaskRunner(); | 142 this->delegate_.StartTaskRunner(); |
| 153 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); | 143 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner(); |
| 154 // Post each ith task i+1 times with delays from 0-i. | 144 // Post each ith task i+1 times with delays from 0-i. |
| 155 for (int i = 0; i < 20; ++i) { | 145 for (int i = 0; i < 20; ++i) { |
| 156 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i); | 146 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i); |
| 157 for (int j = 0; j < i + 1; ++j) { | 147 for (int j = 0; j < i + 1; ++j) { |
| 158 task_runner->PostDelayedTask( | 148 task_runner->PostDelayedTask( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 EXPECT_EQ(expected_task_run_counts, | 206 EXPECT_EQ(expected_task_run_counts, |
| 217 this->task_tracker_->GetTaskRunCounts()); | 207 this->task_tracker_->GetTaskRunCounts()); |
| 218 } | 208 } |
| 219 | 209 |
| 220 REGISTER_TYPED_TEST_CASE_P( | 210 REGISTER_TYPED_TEST_CASE_P( |
| 221 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread); | 211 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread); |
| 222 | 212 |
| 223 } // namespace base | 213 } // namespace base |
| 224 | 214 |
| 225 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ | 215 #endif //#define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_ |
| OLD | NEW |