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.
4
5#ifndef COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_
6#define COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_
7
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"
15
16namespace net {
17class URLRequestStatus;
18}  // namespace net
19
20namespace domain_reliability {
21
22// A simple test callback that remembers whether it's been called.
23class TestCallback {
24 public:
25  TestCallback();
26  ~TestCallback();
27
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_; }
32
33 private:
34  void OnCalled();
35
36  base::Closure callback_;
37  bool called_;
38};
39
40class 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;
46
47  MockUploader(const UploadRequestCallback& callback);
48
49  virtual ~MockUploader();
50
51  virtual bool discard_uploads() const;
52
53  // DomainReliabilityUploader implementation:
54  virtual void UploadReport(const std::string& report_json,
55                            const GURL& upload_url,
56                            const UploadCallback& callback) OVERRIDE;
57
58  virtual void set_discard_uploads(bool discard_uploads) OVERRIDE;
59
60 private:
61  UploadRequestCallback callback_;
62  bool discard_uploads_;
63};
64
65class MockTime : public MockableTime {
66 public:
67  MockTime();
68
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  virtual ~MockTime();
73
74  // MockableTime implementation:
75  virtual base::Time Now() OVERRIDE;
76  virtual base::TimeTicks NowTicks() OVERRIDE;
77  virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
78
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);
83
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);
87
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) {}
95
96    base::TimeTicks time;
97    int sequence_number;
98  };
99
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);
106    }
107  };
108
109  typedef std::map<TaskKey, base::Closure, TaskKeyCompare> TaskMap;
110
111  void AdvanceToInternal(base::TimeTicks target_ticks);
112
113  int elapsed_sec() { return (now_ticks_ - epoch_ticks_).InSeconds(); }
114
115  base::Time now_;
116  base::TimeTicks now_ticks_;
117  base::TimeTicks epoch_ticks_;
118  int task_sequence_number_;
119  TaskMap tasks_;
120};
121
122scoped_ptr<const DomainReliabilityConfig> MakeTestConfig();
123scoped_ptr<const DomainReliabilityConfig> MakeTestConfigWithDomain(
124    const std::string& domain);
125DomainReliabilityScheduler::Params MakeTestSchedulerParams();
126
127}  // namespace domain_reliability
128
129#endif  // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_
130