OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #ifndef NET_TEST_FAKE_TIME_SYSTEM_H_ |
| 6 #define NET_TEST_FAKE_TIME_SYSTEM_H_ |
| 7 |
| 8 #include <queue> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/task_runner.h" |
| 12 #include "base/time/clock.h" |
| 13 |
| 14 namespace net { |
| 15 |
| 16 // This class implements both base::Clock and base::TaskRunner, and can |
| 17 // be used to simulate actual time in unit test for classes that both query |
| 18 // for the current time and shedule future tasks using a TaskRunner. |
| 19 |
| 20 class FakeTimeSystem : public base::Clock, public base::TaskRunner { |
| 21 public: |
| 22 class Task { |
| 23 public: |
| 24 Task(base::Time time, base::Closure task); |
| 25 ~Task(); |
| 26 |
| 27 bool operator<(const Task &other) const; |
| 28 |
| 29 const base::Closure& closure() const { return task_; } |
| 30 const base::Time time() const { return time_; } |
| 31 |
| 32 private: |
| 33 base::Closure task_; |
| 34 base::Time time_; |
| 35 }; |
| 36 |
| 37 FakeTimeSystem(); |
| 38 |
| 39 void SetNow(base::Time now); |
| 40 |
| 41 // Runs pending tasks until the queue is empty. Runs only tasks |
| 42 // that are supposed to be run after |now_|. |
| 43 void RunPendingTasks(); |
| 44 |
| 45 virtual base::Time Now() OVERRIDE; |
| 46 |
| 47 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
| 48 const base::Closure& task, |
| 49 base::TimeDelta delay) OVERRIDE; |
| 50 |
| 51 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
| 52 |
| 53 private: |
| 54 virtual ~FakeTimeSystem(); |
| 55 |
| 56 base::Time now_; |
| 57 std::priority_queue<Task> tasks_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(FakeTimeSystem); |
| 60 }; |
| 61 } |
| 62 |
| 63 #endif // NET_TEST_FAKE_TIME_SYSTEM_H_ |
OLD | NEW |