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 16 namespace net { 17 class URLRequestStatus; 18 } // namespace net 19 20 namespace domain_reliability { 21 22 // A simple test callback that remembers whether it's been called. 23 class 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 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; 46 47 MockUploader(const UploadRequestCallback& callback); 48 49 virtual ~MockUploader(); 50 51 // DomainReliabilityUploader implementation: 52 virtual void UploadReport(const std::string& report_json, 53 const GURL& upload_url, 54 const UploadCallback& callback) OVERRIDE; 55 56 private: 57 UploadRequestCallback callback_; 58 }; 59 60 class MockTime : public MockableTime { 61 public: 62 MockTime(); 63 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(); 68 69 // MockableTime implementation: 70 virtual base::Time Now() OVERRIDE; 71 virtual base::TimeTicks NowTicks() OVERRIDE; 72 virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE; 73 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); 78 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); 82 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) {} 90 91 base::TimeTicks time; 92 int sequence_number; 93 }; 94 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); 101 } 102 }; 103 104 typedef std::map<TaskKey, base::Closure, TaskKeyCompare> TaskMap; 105 106 void AdvanceToInternal(base::TimeTicks target_ticks); 107 108 int elapsed_sec() { return (now_ticks_ - epoch_ticks_).InSeconds(); } 109 110 base::Time now_; 111 base::TimeTicks now_ticks_; 112 base::TimeTicks epoch_ticks_; 113 int task_sequence_number_; 114 TaskMap tasks_; 115 }; 116 117 scoped_ptr<const DomainReliabilityConfig> MakeTestConfig(); 118 DomainReliabilityScheduler::Params MakeTestSchedulerParams(); 119 120 } // namespace domain_reliability 121 122 #endif // COMPONENTS_DOMAIN_RELIABILITY_TEST_UTIL_H_ 123