Support for process-wide incidents in the safe browsing incident reporting service.
[chromium-blink-merge.git] / components / domain_reliability / test_util.h
bloba4529dd90f5051e81b6a33058fcb62e66fc128af
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"
16 namespace net {
17 class URLRequestStatus;
18 } // namespace net
20 namespace domain_reliability {
22 // A simple test callback that remembers whether it's been called.
23 class TestCallback {
24 public:
25 TestCallback();
26 ~TestCallback();
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_; }
33 private:
34 void OnCalled();
36 base::Closure callback_;
37 bool called_;
40 class MockUploader : public DomainReliabilityUploader {
41 public:
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 // DomainReliabilityUploader implementation:
52 virtual void UploadReport(const std::string& report_json,
53 const GURL& upload_url,
54 const UploadCallback& callback) OVERRIDE;
56 private:
57 UploadRequestCallback callback_;
60 class MockTime : public MockableTime {
61 public:
62 MockTime();
64 // N.B.: Tasks (and therefore Timers) scheduled to run in the future will
65 // never be run if MockTime is destroyed before the mock time is advanced
66 // to their scheduled time.
67 virtual ~MockTime();
69 // MockableTime implementation:
70 virtual base::Time Now() OVERRIDE;
71 virtual base::TimeTicks NowTicks() OVERRIDE;
72 virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
74 // Pretends that |delta| has passed, and runs tasks that would've happened
75 // during that interval (with |Now()| returning proper values while they
76 // execute!)
77 void Advance(base::TimeDelta delta);
79 // Queues |task| to be run after |delay|. (Lighter-weight than mocking an
80 // entire message pump.)
81 void AddTask(base::TimeDelta delay, const base::Closure& task);
83 private:
84 // Key used to store tasks in the task map. Includes the time the task should
85 // run and a sequence number to disambiguate tasks with the same time.
86 struct TaskKey {
87 TaskKey(base::TimeTicks time, int sequence_number)
88 : time(time),
89 sequence_number(sequence_number) {}
91 base::TimeTicks time;
92 int sequence_number;
95 // Comparator for TaskKey; sorts by time, then by sequence number.
96 struct TaskKeyCompare {
97 bool operator() (const TaskKey& lhs, const TaskKey& rhs) const {
98 return lhs.time < rhs.time ||
99 (lhs.time == rhs.time &&
100 lhs.sequence_number < rhs.sequence_number);
104 typedef std::map<TaskKey, base::Closure, TaskKeyCompare> TaskMap;
106 void AdvanceToInternal(base::TimeTicks target_ticks);
108 int elapsed_sec() { return (now_ticks_ - epoch_ticks_).InSeconds(); }
110 base::Time now_;
111 base::TimeTicks now_ticks_;
112 base::TimeTicks epoch_ticks_;
113 int task_sequence_number_;
114 TaskMap tasks_;
117 scoped_ptr<const DomainReliabilityConfig> MakeTestConfig();
118 DomainReliabilityScheduler::Params MakeTestSchedulerParams();
120 } // namespace domain_reliability
122 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_