Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / domain_reliability / test_util.h
blob4c968eded8561964941e1dca9e23e0d520535ebc
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_
8 #include "base/callback.h"
9 #include "components/domain_reliability/monitor.h"
10 #include "components/domain_reliability/uploader.h"
11 #include "components/domain_reliability/util.h"
12 #include "net/base/host_port_pair.h"
14 namespace net {
15 class URLRequestStatus;
16 } // namespace net
18 namespace domain_reliability {
20 // A simple test callback that remembers whether it's been called.
21 class TestCallback {
22 public:
23 TestCallback();
24 ~TestCallback();
26 // Returns a callback that can be called only once.
27 const base::Closure& callback() const { return callback_; }
28 // Returns whether the callback returned by |callback()| has been called.
29 bool called() const { return called_; }
31 private:
32 void OnCalled();
34 base::Closure callback_;
35 bool called_;
38 class MockUploader : public DomainReliabilityUploader {
39 public:
40 typedef base::Callback<void(const std::string& report_json,
41 const GURL& upload_url,
42 const UploadCallback& upload_callback)>
43 UploadRequestCallback;
45 MockUploader(const UploadRequestCallback& callback);
47 virtual ~MockUploader();
49 // DomainReliabilityUploader implementation:
50 virtual void UploadReport(const std::string& report_json,
51 const GURL& upload_url,
52 const UploadCallback& callback) OVERRIDE;
54 private:
55 UploadRequestCallback callback_;
58 class MockTime : public MockableTime {
59 public:
60 MockTime();
62 // N.B.: Tasks (and therefore Timers) scheduled to run in the future will
63 // never be run if MockTime is destroyed before the mock time is advanced
64 // to their scheduled time.
65 virtual ~MockTime();
67 // MockableTime implementation:
68 virtual base::Time Now() OVERRIDE;
69 virtual base::TimeTicks NowTicks() OVERRIDE;
70 virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
72 // Pretends that |delta| has passed, and runs tasks that would've happened
73 // during that interval (with |Now()| returning proper values while they
74 // execute!)
75 void Advance(base::TimeDelta delta);
77 // Queues |task| to be run after |delay|. (Lighter-weight than mocking an
78 // entire message pump.)
79 void AddTask(base::TimeDelta delay, const base::Closure& task);
81 private:
82 // Key used to store tasks in the task map. Includes the time the task should
83 // run and a sequence number to disambiguate tasks with the same time.
84 struct TaskKey {
85 TaskKey(base::TimeTicks time, int sequence_number)
86 : time(time),
87 sequence_number(sequence_number) {}
89 base::TimeTicks time;
90 int sequence_number;
93 // Comparator for TaskKey; sorts by time, then by sequence number.
94 struct TaskKeyCompare {
95 bool operator() (const TaskKey& lhs, const TaskKey& rhs) const {
96 return lhs.time < rhs.time ||
97 (lhs.time == rhs.time &&
98 lhs.sequence_number < rhs.sequence_number);
102 typedef std::map<TaskKey, base::Closure, TaskKeyCompare> TaskMap;
104 void AdvanceToInternal(base::TimeTicks target_ticks);
106 int elapsed_sec() { return (now_ticks_ - epoch_ticks_).InSeconds(); }
108 base::Time now_;
109 base::TimeTicks now_ticks_;
110 base::TimeTicks epoch_ticks_;
111 int task_sequence_number_;
112 TaskMap tasks_;
115 } // namespace domain_reliability
117 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_