1 // Copyright 2015 The Chromium OS 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.
4 
5 #include <brillo/message_loops/fake_message_loop.h>
6 
7 #include <memory>
8 #include <vector>
9 
10 #include <base/bind.h>
11 #include <base/bind_helpers.h>
12 #include <base/location.h>
13 #include <base/test/simple_test_clock.h>
14 #include <gtest/gtest.h>
15 
16 #include <brillo/message_loops/message_loop.h>
17 
18 using base::BindOnce;
19 using base::Time;
20 using base::TimeDelta;
21 using std::vector;
22 
23 namespace brillo {
24 
25 using TaskId = MessageLoop::TaskId;
26 
27 class FakeMessageLoopTest : public ::testing::Test {
28  protected:
SetUp()29   void SetUp() override {
30     loop_.reset(new FakeMessageLoop(nullptr));
31     EXPECT_TRUE(loop_.get());
32   }
TearDown()33   void TearDown() override {
34     EXPECT_FALSE(loop_->PendingTasks());
35   }
36 
37   base::SimpleTestClock clock_;
38   std::unique_ptr<FakeMessageLoop> loop_;
39 };
40 
TEST_F(FakeMessageLoopTest,CancelTaskInvalidValuesTest)41 TEST_F(FakeMessageLoopTest, CancelTaskInvalidValuesTest) {
42   EXPECT_FALSE(loop_->CancelTask(MessageLoop::kTaskIdNull));
43   EXPECT_FALSE(loop_->CancelTask(1234));
44 }
45 
TEST_F(FakeMessageLoopTest,PostDelayedTaskRunsInOrder)46 TEST_F(FakeMessageLoopTest, PostDelayedTaskRunsInOrder) {
47   vector<int> order;
48   loop_->PostDelayedTask(
49       BindOnce([](vector<int>* order) { order->push_back(1); }, &order),
50       TimeDelta::FromSeconds(1));
51   loop_->PostDelayedTask(
52       BindOnce([](vector<int>* order) { order->push_back(4); }, &order),
53       TimeDelta::FromSeconds(4));
54   loop_->PostDelayedTask(
55       BindOnce([](vector<int>* order) { order->push_back(3); }, &order),
56       TimeDelta::FromSeconds(3));
57   loop_->PostDelayedTask(
58       BindOnce([](vector<int>* order) { order->push_back(2); }, &order),
59       TimeDelta::FromSeconds(2));
60   // Run until all the tasks are run.
61   loop_->Run();
62   EXPECT_EQ((vector<int>{1, 2, 3, 4}), order);
63 }
64 
TEST_F(FakeMessageLoopTest,PostDelayedTaskAdvancesTheTime)65 TEST_F(FakeMessageLoopTest, PostDelayedTaskAdvancesTheTime) {
66   Time start = Time::FromInternalValue(1000000);
67   clock_.SetNow(start);
68   loop_.reset(new FakeMessageLoop(&clock_));
69   loop_->PostDelayedTask(base::DoNothing(), TimeDelta::FromSeconds(1));
70   loop_->PostDelayedTask(base::DoNothing(), TimeDelta::FromSeconds(2));
71   EXPECT_FALSE(loop_->RunOnce(false));
72   // If the callback didn't run, the time shouldn't change.
73   EXPECT_EQ(start, clock_.Now());
74 
75   // If we run only one callback, the time should be set to the time that
76   // callack ran.
77   EXPECT_TRUE(loop_->RunOnce(true));
78   EXPECT_EQ(start + TimeDelta::FromSeconds(1), clock_.Now());
79 
80   // If the clock is advanced manually, we should be able to run the
81   // callback without blocking, since the firing time is in the past.
82   clock_.SetNow(start + TimeDelta::FromSeconds(3));
83   EXPECT_TRUE(loop_->RunOnce(false));
84   // The time should not change even if the callback is due in the past.
85   EXPECT_EQ(start + TimeDelta::FromSeconds(3), clock_.Now());
86 }
87 
TEST_F(FakeMessageLoopTest,PendingTasksTest)88 TEST_F(FakeMessageLoopTest, PendingTasksTest) {
89   loop_->PostDelayedTask(base::DoNothing(), TimeDelta::FromSeconds(1));
90   EXPECT_TRUE(loop_->PendingTasks());
91   loop_->Run();
92 }
93 
94 }  // namespace brillo
95