Add missing dependency to chrome_java_test_support GN target.
[chromium-blink-merge.git] / components / domain_reliability / test_util.h
blob7018c82dfd9ac471f6a63498f8f9c282bb723ba1
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 ~MockUploader() override;
51 virtual bool discard_uploads() const;
53 // DomainReliabilityUploader implementation:
54 void UploadReport(const std::string& report_json,
55 const GURL& upload_url,
56 const UploadCallback& callback) override;
58 void set_discard_uploads(bool discard_uploads) override;
60 private:
61 UploadRequestCallback callback_;
62 bool discard_uploads_;
65 class MockTime : public MockableTime {
66 public:
67 MockTime();
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.
72 ~MockTime() override;
74 // MockableTime implementation:
75 base::Time Now() override;
76 base::TimeTicks NowTicks() override;
77 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
81 // execute!)
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);
88 private:
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.
91 struct TaskKey {
92 TaskKey(base::TimeTicks time, int sequence_number)
93 : time(time),
94 sequence_number(sequence_number) {}
96 base::TimeTicks time;
97 int 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(); }
115 base::Time now_;
116 base::TimeTicks now_ticks_;
117 base::TimeTicks epoch_ticks_;
118 int task_sequence_number_;
119 TaskMap tasks_;
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_