Index: net/test/fake_time_system.h |
diff --git a/net/test/fake_time_system.h b/net/test/fake_time_system.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..30e1135c6c16957b63c26857fa713ed8da2ee0fd |
--- /dev/null |
+++ b/net/test/fake_time_system.h |
@@ -0,0 +1,63 @@ |
+// Copyright (c) 2013 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. |
+ |
+#ifndef NET_TEST_FAKE_TIME_SYSTEM_H_ |
+#define NET_TEST_FAKE_TIME_SYSTEM_H_ |
+ |
+#include <queue> |
+ |
+#include "base/callback.h" |
+#include "base/task_runner.h" |
+#include "base/time/clock.h" |
+ |
+namespace net { |
+ |
+// This class implements both base::Clock and base::TaskRunner, and can |
+// be used to simulate actual time in unit test for classes that both query |
+// for the current time and shedule future tasks using a TaskRunner. |
+ |
+class FakeTimeSystem : public base::Clock, public base::TaskRunner { |
+ public: |
+ class Task { |
+ public: |
+ Task(base::Time time, base::Closure task); |
+ ~Task(); |
+ |
+ bool operator<(const Task &other) const; |
+ |
+ const base::Closure& closure() const { return task_; } |
+ const base::Time time() const { return time_; } |
+ |
+ private: |
+ base::Closure task_; |
+ base::Time time_; |
+ }; |
+ |
+ FakeTimeSystem(); |
+ |
+ void SetNow(base::Time now); |
+ |
+ // Runs pending tasks until the queue is empty. Runs only tasks |
+ // that are supposed to be run after |now_|. |
+ void RunPendingTasks(); |
+ |
+ virtual base::Time Now() OVERRIDE; |
+ |
+ virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
+ const base::Closure& task, |
+ base::TimeDelta delay) OVERRIDE; |
+ |
+ virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
+ |
+ private: |
+ virtual ~FakeTimeSystem(); |
+ |
+ base::Time now_; |
+ std::priority_queue<Task> tasks_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(FakeTimeSystem); |
+}; |
+} |
+ |
+#endif // NET_TEST_FAKE_TIME_SYSTEM_H_ |