Change to let media_stream_devices_controller.cc decide if a gUM request fail if...
[chromium-blink-merge.git] / components / domain_reliability / test_util.h
bloba1230734320c9bb50ce99408419ffc682bb0d335
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"
14 namespace net {
15 class URLRequestStatus;
16 } // namespace net
18 namespace domain_reliability {
20 // A simple test callback that remembers whether it's been called.
21 class TestCallback {
22 public:
23 TestCallback();
24 ~TestCallback();
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_; }
31 private:
32 void OnCalled();
34 base::Closure callback_;
35 bool called_;
38 class MockUploader : public DomainReliabilityUploader {
39 public:
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;
53 private:
54 UploadRequestCallback callback_;
57 class MockTime : public MockableTime {
58 public:
59 MockTime();
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.
63 virtual ~MockTime();
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
71 // execute!)
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);
78 private:
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.
81 struct TaskKey {
82 TaskKey(base::TimeTicks time, int sequence_number)
83 : time(time),
84 sequence_number(sequence_number) {}
86 base::TimeTicks time;
87 int 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_;
106 TaskMap tasks_;
109 } // namespace domain_reliability
111 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_