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
);
46 virtual ~MockUploader();
48 // DomainREliabilityUploader implementation:
49 virtual void UploadReport(const std::string
& report_json
,
50 const GURL
& upload_url
,
51 const UploadCallback
& callback
) OVERRIDE
;
54 UploadRequestCallback callback_
;
57 class MockTime
: public MockableTime
{
60 // N.B.: Tasks (and therefore Timers) scheduled to run in the future will
61 // never be run if MockTime is destroyed before the mock time is advanced
62 // to their scheduled time.
65 // MockableTime implementation:
66 virtual base::TimeTicks
Now() OVERRIDE
;
67 virtual scoped_ptr
<MockableTime::Timer
> CreateTimer() OVERRIDE
;
69 // Pretends that |delta| has passed, and runs tasks that would've happened
70 // during that interval (with |Now()| returning proper values while they
72 void Advance(base::TimeDelta delta
);
74 // Queues |task| to be run after |delay|. (Lighter-weight than mocking an
75 // entire message pump.)
76 void AddTask(base::TimeDelta delay
, const base::Closure
& task
);
79 // Key used to store tasks in the task map. Includes the time the task should
80 // run and a sequence number to disambiguate tasks with the same time.
82 TaskKey(base::TimeTicks time
, int sequence_number
)
84 sequence_number(sequence_number
) {}
90 // Comparator for TaskKey; sorts by time, then by sequence number.
91 struct TaskKeyCompare
{
92 bool operator() (const TaskKey
& lhs
, const TaskKey
& rhs
) const {
93 return lhs
.time
< rhs
.time
||
94 (lhs
.time
== rhs
.time
&&
95 lhs
.sequence_number
< rhs
.sequence_number
);
99 typedef std::map
<TaskKey
, base::Closure
, TaskKeyCompare
> TaskMap
;
101 int elapsed_sec() { return (now_
- epoch_
).InSeconds(); }
103 base::TimeTicks now_
;
104 base::TimeTicks epoch_
;
105 int task_sequence_number_
;
109 } // namespace domain_reliability
111 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_