Don't preload rarely seen large images
[chromium-blink-merge.git] / base / test / task_runner_test_template.h
blobee2b876671ffbe642413d4b487e4b87ff1a46131
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
3 // found in the LICENSE file.
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
7 // implementation.
8 //
9 // Say your class is called MyTaskRunner. Then you need to define a
10 // class called MyTaskRunnerTestDelegate in my_task_runner_unittest.cc
11 // like this:
13 // class MyTaskRunnerTestDelegate {
14 // public:
15 // // Tasks posted to the task runner after this and before
16 // // StopTaskRunner() is called is called should run successfully.
17 // void StartTaskRunner() {
18 // ...
19 // }
21 // // Should return the task runner implementation. Only called
22 // // after StartTaskRunner and before StopTaskRunner.
23 // scoped_refptr<MyTaskRunner> GetTaskRunner() {
24 // ...
25 // }
27 // // Stop the task runner and make sure all tasks posted before
28 // // this is called are run. Caveat: delayed tasks are not run,
29 // they're simply deleted.
30 // void StopTaskRunner() {
31 // ...
32 // }
33 // };
35 // The TaskRunnerTest test harness will have a member variable of
36 // this delegate type and will call its functions in the various
37 // tests.
39 // Then you simply #include this file as well as gtest.h and add the
40 // following statement to my_task_runner_unittest.cc:
42 // INSTANTIATE_TYPED_TEST_CASE_P(
43 // MyTaskRunner, TaskRunnerTest, MyTaskRunnerTestDelegate);
45 // Easy!
47 #ifndef BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
48 #define BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_
50 #include <cstddef>
51 #include <map>
53 #include "base/basictypes.h"
54 #include "base/bind.h"
55 #include "base/callback.h"
56 #include "base/location.h"
57 #include "base/memory/ref_counted.h"
58 #include "base/single_thread_task_runner.h"
59 #include "base/synchronization/condition_variable.h"
60 #include "base/synchronization/lock.h"
61 #include "base/task_runner.h"
62 #include "base/threading/thread.h"
63 #include "base/tracked_objects.h"
64 #include "testing/gtest/include/gtest/gtest.h"
66 namespace base {
68 namespace internal {
70 // Utility class that keeps track of how many times particular tasks
71 // are run.
72 class TaskTracker : public RefCountedThreadSafe<TaskTracker> {
73 public:
74 TaskTracker();
76 // Returns a closure that runs the given task and increments the run
77 // count of |i| by one. |task| may be null. It is guaranteed that
78 // only one task wrapped by a given tracker will be run at a time.
79 Closure WrapTask(const Closure& task, int i);
81 std::map<int, int> GetTaskRunCounts() const;
83 // Returns after the tracker observes a total of |count| task completions.
84 void WaitForCompletedTasks(int count);
86 private:
87 friend class RefCountedThreadSafe<TaskTracker>;
89 ~TaskTracker();
91 void RunTask(const Closure& task, int i);
93 mutable Lock lock_;
94 std::map<int, int> task_run_counts_;
95 int task_runs_;
96 ConditionVariable task_runs_cv_;
98 DISALLOW_COPY_AND_ASSIGN(TaskTracker);
101 } // namespace internal
103 template <typename TaskRunnerTestDelegate>
104 class TaskRunnerTest : public testing::Test {
105 protected:
106 TaskRunnerTest() : task_tracker_(new internal::TaskTracker()) {}
108 const scoped_refptr<internal::TaskTracker> task_tracker_;
109 TaskRunnerTestDelegate delegate_;
112 TYPED_TEST_CASE_P(TaskRunnerTest);
114 // We can't really test much, since TaskRunner provides very few
115 // guarantees.
117 // Post a bunch of tasks to the task runner. They should all
118 // complete.
119 TYPED_TEST_P(TaskRunnerTest, Basic) {
120 std::map<int, int> expected_task_run_counts;
122 this->delegate_.StartTaskRunner();
123 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
124 // Post each ith task i+1 times.
125 for (int i = 0; i < 20; ++i) {
126 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
127 for (int j = 0; j < i + 1; ++j) {
128 task_runner->PostTask(FROM_HERE, ith_task);
129 ++expected_task_run_counts[i];
132 this->delegate_.StopTaskRunner();
134 EXPECT_EQ(expected_task_run_counts,
135 this->task_tracker_->GetTaskRunCounts());
138 // Post a bunch of delayed tasks to the task runner. They should all
139 // complete.
140 TYPED_TEST_P(TaskRunnerTest, Delayed) {
141 std::map<int, int> expected_task_run_counts;
142 int expected_total_tasks = 0;
144 this->delegate_.StartTaskRunner();
145 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
146 // Post each ith task i+1 times with delays from 0-i.
147 for (int i = 0; i < 20; ++i) {
148 const Closure& ith_task = this->task_tracker_->WrapTask(Closure(), i);
149 for (int j = 0; j < i + 1; ++j) {
150 task_runner->PostDelayedTask(
151 FROM_HERE, ith_task, base::TimeDelta::FromMilliseconds(j));
152 ++expected_task_run_counts[i];
153 ++expected_total_tasks;
156 this->task_tracker_->WaitForCompletedTasks(expected_total_tasks);
157 this->delegate_.StopTaskRunner();
159 EXPECT_EQ(expected_task_run_counts,
160 this->task_tracker_->GetTaskRunCounts());
163 namespace internal {
165 // Calls RunsTasksOnCurrentThread() on |task_runner| and expects it to
166 // equal |expected_value|.
167 void ExpectRunsTasksOnCurrentThread(
168 bool expected_value,
169 const scoped_refptr<TaskRunner>& task_runner);
171 } // namespace internal
173 // Post a bunch of tasks to the task runner as well as to a separate
174 // thread, each checking the value of RunsTasksOnCurrentThread(),
175 // which should return true for the tasks posted on the task runner
176 // and false for the tasks posted on the separate thread.
177 TYPED_TEST_P(TaskRunnerTest, RunsTasksOnCurrentThread) {
178 std::map<int, int> expected_task_run_counts;
180 Thread thread("Non-task-runner thread");
181 ASSERT_TRUE(thread.Start());
182 this->delegate_.StartTaskRunner();
184 scoped_refptr<TaskRunner> task_runner = this->delegate_.GetTaskRunner();
185 // Post each ith task i+1 times on the task runner and i+1 times on
186 // the non-task-runner thread.
187 for (int i = 0; i < 20; ++i) {
188 const Closure& ith_task_runner_task =
189 this->task_tracker_->WrapTask(
190 Bind(&internal::ExpectRunsTasksOnCurrentThread,
191 true, task_runner),
193 const Closure& ith_non_task_runner_task =
194 this->task_tracker_->WrapTask(
195 Bind(&internal::ExpectRunsTasksOnCurrentThread,
196 false, task_runner),
198 for (int j = 0; j < i + 1; ++j) {
199 task_runner->PostTask(FROM_HERE, ith_task_runner_task);
200 thread.task_runner()->PostTask(FROM_HERE, ith_non_task_runner_task);
201 expected_task_run_counts[i] += 2;
205 this->delegate_.StopTaskRunner();
206 thread.Stop();
208 EXPECT_EQ(expected_task_run_counts,
209 this->task_tracker_->GetTaskRunCounts());
212 REGISTER_TYPED_TEST_CASE_P(
213 TaskRunnerTest, Basic, Delayed, RunsTasksOnCurrentThread);
215 } // namespace base
217 #endif // BASE_TEST_TASK_RUNNER_TEST_TEMPLATE_H_