blob: e2e199cb0a440089d6e99a55b6db9c86f3e8543f [file] [log] [blame]
Alex Deymoef3955a2015-06-09 10:14:01 -07001// 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#ifndef LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_
6#define LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_
7
8#include <functional>
9#include <map>
10#include <queue>
11#include <utility>
12#include <vector>
13
Alex Deymo106edd12015-06-18 20:15:11 -070014#include <base/location.h>
Alex Deymoef3955a2015-06-09 10:14:01 -070015#include <base/test/simple_test_clock.h>
16#include <base/time/time.h>
17
18#include <chromeos/chromeos_export.h>
19#include <chromeos/message_loops/message_loop.h>
20
21namespace chromeos {
22
23// The FakeMessageLoop implements a message loop that doesn't block or wait for
24// time based tasks to be ready. The tasks are executed in the order they should
25// be executed in a real message loop implementation, but the time is advanced
26// to the time when the first task should be executed instead of blocking.
27// To keep a consistent notion of time for other classes, FakeMessageLoop
28// optionally updates a SimpleTestClock instance when it needs to advance the
29// clock.
30// This message loop implementation is useful for unittests.
31class CHROMEOS_EXPORT FakeMessageLoop : public MessageLoop {
32 public:
33 // Create a FakeMessageLoop optionally using a SimpleTestClock to update the
34 // time when Run() or RunOnce(true) are called and should block.
35 explicit FakeMessageLoop(base::SimpleTestClock* clock);
36 ~FakeMessageLoop() override = default;
37
Alex Deymo106edd12015-06-18 20:15:11 -070038 MessageLoop::TaskId PostDelayedTask(
39 const tracked_objects::Location& from_here,
40 const base::Closure &task,
41 base::TimeDelta delay) override;
42 using MessageLoop::PostDelayedTask;
Alex Deymoef3955a2015-06-09 10:14:01 -070043 bool CancelTask(TaskId task_id) override;
44 bool RunOnce(bool may_block) override;
45
46 // FakeMessageLoop methods:
47
48 // Return whether there are peding tasks. Useful to check that no
49 // callbacks were leaked.
50 bool PendingTasks();
51
52 private:
53 // Using std::greater<> for the priority_queue means that the top() of the
54 // queue is the lowest (earliest) time, and for the same time, the smallest
55 // TaskId. This determines the order in which the tasks will be fired.
56 std::priority_queue<
57 std::pair<base::Time, MessageLoop::TaskId>,
58 std::vector<std::pair<base::Time, MessageLoop::TaskId>>,
59 std::greater<std::pair<base::Time, MessageLoop::TaskId>>> fire_order_;
Alex Deymo106edd12015-06-18 20:15:11 -070060 std::map<MessageLoop::TaskId,
61 std::pair<tracked_objects::Location, base::Closure>> tasks_;
Alex Deymoef3955a2015-06-09 10:14:01 -070062
63 base::SimpleTestClock* test_clock_ = nullptr;
64 base::Time current_time_ = base::Time::FromDoubleT(1246996800.);
65
66 MessageLoop::TaskId last_id_ = kTaskIdNull;
67
68 DISALLOW_COPY_AND_ASSIGN(FakeMessageLoop);
69};
70
71} // namespace chromeos
72
73#endif // LIBCHROMEOS_CHROMEOS_MESSAGE_LOOPS_FAKE_MESSAGE_LOOP_H_