Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / components / domain_reliability / test_util.h
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.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"
13
14 namespace net {
15 class URLRequestStatus;
16 }  // namespace net
17
18 namespace domain_reliability {
19
20 // A simple test callback that remembers whether it's been called.
21 class TestCallback {
22  public:
23   TestCallback();
24   ~TestCallback();
25
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_; }
30
31  private:
32   void OnCalled();
33
34   base::Closure callback_;
35   bool called_;
36 };
37
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;
44
45   MockUploader(const UploadRequestCallback& callback);
46   virtual ~MockUploader();
47
48   // DomainREliabilityUploader implementation:
49   virtual void UploadReport(const std::string& report_json,
50                             const GURL& upload_url,
51                             const UploadCallback& callback) OVERRIDE;
52
53  private:
54   UploadRequestCallback callback_;
55 };
56
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();
64
65   // MockableTime implementation:
66   virtual base::TimeTicks Now() OVERRIDE;
67   virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
68
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);
73
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);
77
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) {}
85
86     base::TimeTicks time;
87     int sequence_number;
88   };
89
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);
96     }
97   };
98
99   typedef std::map<TaskKey, base::Closure, TaskKeyCompare> TaskMap;
100
101   int elapsed_sec() { return (now_ - epoch_).InSeconds(); }
102
103   base::TimeTicks now_;
104   base::TimeTicks epoch_;
105   int task_sequence_number_;
106   TaskMap tasks_;
107 };
108
109 }  // namespace domain_reliability
110
111 #endif  // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_