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_forward.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "components/domain_reliability/config.h"
11 #include "components/domain_reliability/scheduler.h"
12 #include "components/domain_reliability/uploader.h"
13 #include "components/domain_reliability/util.h"
14 #include "net/base/host_port_pair.h"
17 class URLRequestStatus
;
20 namespace domain_reliability
{
22 // A simple test callback that remembers whether it's been called.
28 // Returns a callback that can be called only once.
29 const base::Closure
& callback() const { return callback_
; }
30 // Returns whether the callback returned by |callback()| has been called.
31 bool called() const { return called_
; }
36 base::Closure callback_
;
40 class MockUploader
: public DomainReliabilityUploader
{
42 typedef base::Callback
<void(const std::string
& report_json
,
43 const GURL
& upload_url
,
44 const UploadCallback
& upload_callback
)>
45 UploadRequestCallback
;
47 MockUploader(const UploadRequestCallback
& callback
);
49 virtual ~MockUploader();
51 virtual bool discard_uploads() const;
53 // DomainReliabilityUploader implementation:
54 virtual void UploadReport(const std::string
& report_json
,
55 const GURL
& upload_url
,
56 const UploadCallback
& callback
) OVERRIDE
;
58 virtual void set_discard_uploads(bool discard_uploads
) OVERRIDE
;
61 UploadRequestCallback callback_
;
62 bool discard_uploads_
;
65 class MockTime
: public MockableTime
{
69 // N.B.: Tasks (and therefore Timers) scheduled to run in the future will
70 // never be run if MockTime is destroyed before the mock time is advanced
71 // to their scheduled time.
74 // MockableTime implementation:
75 virtual base::Time
Now() OVERRIDE
;
76 virtual base::TimeTicks
NowTicks() OVERRIDE
;
77 virtual scoped_ptr
<MockableTime::Timer
> CreateTimer() OVERRIDE
;
79 // Pretends that |delta| has passed, and runs tasks that would've happened
80 // during that interval (with |Now()| returning proper values while they
82 void Advance(base::TimeDelta delta
);
84 // Queues |task| to be run after |delay|. (Lighter-weight than mocking an
85 // entire message pump.)
86 void AddTask(base::TimeDelta delay
, const base::Closure
& task
);
89 // Key used to store tasks in the task map. Includes the time the task should
90 // run and a sequence number to disambiguate tasks with the same time.
92 TaskKey(base::TimeTicks time
, int sequence_number
)
94 sequence_number(sequence_number
) {}
100 // Comparator for TaskKey; sorts by time, then by sequence number.
101 struct TaskKeyCompare
{
102 bool operator() (const TaskKey
& lhs
, const TaskKey
& rhs
) const {
103 return lhs
.time
< rhs
.time
||
104 (lhs
.time
== rhs
.time
&&
105 lhs
.sequence_number
< rhs
.sequence_number
);
109 typedef std::map
<TaskKey
, base::Closure
, TaskKeyCompare
> TaskMap
;
111 void AdvanceToInternal(base::TimeTicks target_ticks
);
113 int elapsed_sec() { return (now_ticks_
- epoch_ticks_
).InSeconds(); }
116 base::TimeTicks now_ticks_
;
117 base::TimeTicks epoch_ticks_
;
118 int task_sequence_number_
;
122 scoped_ptr
<const DomainReliabilityConfig
> MakeTestConfig();
123 scoped_ptr
<const DomainReliabilityConfig
> MakeTestConfigWithDomain(
124 const std::string
& domain
);
125 DomainReliabilityScheduler::Params
MakeTestSchedulerParams();
127 } // namespace domain_reliability
129 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_