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"
15 class URLRequestStatus
;
18 namespace domain_reliability
{
20 // A simple test callback that remembers whether it's been called.
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_
; }
34 base::Closure callback_
;
38 class MockUploader
: public DomainReliabilityUploader
{
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
;
55 UploadRequestCallback callback_
;
58 class MockTime
: public MockableTime
{
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.
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
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
);
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.
85 TaskKey(base::TimeTicks time
, int sequence_number
)
87 sequence_number(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(); }
109 base::TimeTicks now_ticks_
;
110 base::TimeTicks epoch_ticks_
;
111 int task_sequence_number_
;
115 } // namespace domain_reliability
117 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_