1 // Copyright 2014 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.
7 #include "base/cancelable_callback.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/test/test_pending_task.h"
11 #include "cc/test/ordered_simple_task_runner.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class OrderedSimpleTaskRunnerTest
: public testing::Test
{
18 OrderedSimpleTaskRunnerTest() {
19 task_runner_
= new OrderedSimpleTaskRunner
;
21 virtual ~OrderedSimpleTaskRunnerTest() {}
24 void CreateAndPostTask(int task_num
, base::TimeDelta delay
) {
25 base::Closure test_task
= base::Bind(&OrderedSimpleTaskRunnerTest::Task
,
26 base::Unretained(this),
28 task_runner_
->PostDelayedTask(FROM_HERE
, test_task
, delay
);
31 void RunAndCheckResult(const std::string expected_result
) {
32 task_runner_
->RunPendingTasks();
33 EXPECT_EQ(expected_result
, executed_tasks_
);
37 std::string executed_tasks_
;
38 scoped_refptr
<OrderedSimpleTaskRunner
> task_runner_
;
40 void Task(int task_num
) {
41 if (!executed_tasks_
.empty())
42 executed_tasks_
+= " ";
43 executed_tasks_
+= base::StringPrintf("%d", task_num
);
46 DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunnerTest
);
49 TEST_F(OrderedSimpleTaskRunnerTest
, BasicOrderingTest
) {
50 CreateAndPostTask(1, base::TimeDelta());
51 CreateAndPostTask(2, base::TimeDelta());
52 CreateAndPostTask(3, base::TimeDelta());
54 RunAndCheckResult("1 2 3");
57 TEST_F(OrderedSimpleTaskRunnerTest
, OrderingTestWithDelayedTasks
) {
58 CreateAndPostTask(1, base::TimeDelta());
59 CreateAndPostTask(2, base::TimeDelta::FromMilliseconds(15));
60 CreateAndPostTask(3, base::TimeDelta());
61 CreateAndPostTask(4, base::TimeDelta::FromMilliseconds(8));
63 RunAndCheckResult("1 3 4 2");