summaryrefslogtreecommitdiffstats
path: root/cc/test/ordered_simple_task_runner_unittest.cc
blob: 3fbfd3b73ccff069f1ac7b58bf259254ec6a56e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/cancelable_callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/test/test_pending_task.h"
#include "cc/test/ordered_simple_task_runner.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {

class OrderedSimpleTaskRunnerTest : public testing::Test {
 public:
  OrderedSimpleTaskRunnerTest() {
    task_runner_ = new OrderedSimpleTaskRunner;
  }
  virtual ~OrderedSimpleTaskRunnerTest() {}

 protected:
  void CreateAndPostTask(int task_num, base::TimeDelta delay) {
    base::Closure test_task = base::Bind(&OrderedSimpleTaskRunnerTest::Task,
                                         base::Unretained(this),
                                         task_num);
    task_runner_->PostDelayedTask(FROM_HERE, test_task, delay);
  }

  void RunAndCheckResult(const std::string expected_result) {
    task_runner_->RunPendingTasks();
    EXPECT_EQ(expected_result, executed_tasks_);
  }

 private:
  std::string executed_tasks_;
  scoped_refptr<OrderedSimpleTaskRunner> task_runner_;

  void Task(int task_num) {
    if (!executed_tasks_.empty())
      executed_tasks_ += " ";
    executed_tasks_ += base::StringPrintf("%d", task_num);
  }

  DISALLOW_COPY_AND_ASSIGN(OrderedSimpleTaskRunnerTest);
};

TEST_F(OrderedSimpleTaskRunnerTest, BasicOrderingTest) {
  CreateAndPostTask(1, base::TimeDelta());
  CreateAndPostTask(2, base::TimeDelta());
  CreateAndPostTask(3, base::TimeDelta());

  RunAndCheckResult("1 2 3");
}

TEST_F(OrderedSimpleTaskRunnerTest, OrderingTestWithDelayedTasks) {
  CreateAndPostTask(1, base::TimeDelta());
  CreateAndPostTask(2, base::TimeDelta::FromMilliseconds(15));
  CreateAndPostTask(3, base::TimeDelta());
  CreateAndPostTask(4, base::TimeDelta::FromMilliseconds(8));

  RunAndCheckResult("1 3 4 2");
}

}  // namespace cc