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 #include "components/domain_reliability/test_util.h"
8 #include "net/url_request/url_request_status.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace domain_reliability
{
15 class MockTimer
: public MockableTime::Timer
{
17 MockTimer(MockTime
* time
)
20 callback_sequence_number_(0),
25 virtual ~MockTimer() {}
27 // MockableTime::Timer implementation:
28 virtual void Start(const tracked_objects::Location
& posted_from
,
29 base::TimeDelta delay
,
30 const base::Closure
& user_task
) OVERRIDE
{
31 DCHECK(!user_task
.is_null());
34 ++callback_sequence_number_
;
36 user_task_
= user_task
;
38 base::Bind(&MockTimer::OnDelayPassed
,
39 weak_factory_
.GetWeakPtr(),
40 callback_sequence_number_
));
43 virtual void Stop() OVERRIDE
{
45 ++callback_sequence_number_
;
50 virtual bool IsRunning() OVERRIDE
{ return running_
; }
53 void OnDelayPassed(int expected_callback_sequence_number
) {
54 if (callback_sequence_number_
!= expected_callback_sequence_number
)
60 // Grab user task in case it re-entrantly starts the timer again.
61 base::Closure task_to_run
= user_task_
;
68 int callback_sequence_number_
;
69 base::Closure user_task_
;
70 base::WeakPtrFactory
<MockTimer
> weak_factory_
;
75 TestCallback::TestCallback()
76 : callback_(base::Bind(&TestCallback::OnCalled
,
77 base::Unretained(this))),
80 TestCallback::~TestCallback() {}
82 void TestCallback::OnCalled() {
83 EXPECT_FALSE(called_
);
87 MockUploader::MockUploader(const UploadRequestCallback
& callback
)
88 : callback_(callback
) {}
90 MockUploader::~MockUploader() {}
92 void MockUploader::UploadReport(const std::string
& report_json
,
93 const GURL
& upload_url
,
94 const UploadCallback
& callback
) {
95 callback_
.Run(report_json
, upload_url
, callback
);
99 : now_(base::Time::Now()),
100 now_ticks_(base::TimeTicks::Now()),
101 epoch_ticks_(now_ticks_
),
102 task_sequence_number_(0) {
103 VLOG(1) << "Creating mock time: T=" << elapsed_sec() << "s";
106 MockTime::~MockTime() {}
108 base::Time
MockTime::Now() { return now_
; }
109 base::TimeTicks
MockTime::NowTicks() { return now_ticks_
; }
111 scoped_ptr
<MockableTime::Timer
> MockTime::CreateTimer() {
112 return scoped_ptr
<MockableTime::Timer
>(new MockTimer(this));
115 void MockTime::Advance(base::TimeDelta delta
) {
116 base::TimeTicks target_ticks
= now_ticks_
+ delta
;
118 while (!tasks_
.empty() && tasks_
.begin()->first
.time
<= target_ticks
) {
119 TaskKey key
= tasks_
.begin()->first
;
120 base::Closure task
= tasks_
.begin()->second
;
121 tasks_
.erase(tasks_
.begin());
123 DCHECK(now_ticks_
<= key
.time
);
124 DCHECK(key
.time
<= target_ticks
);
125 AdvanceToInternal(key
.time
);
126 VLOG(1) << "Advancing mock time: task at T=" << elapsed_sec() << "s";
131 DCHECK(now_ticks_
<= target_ticks
);
132 AdvanceToInternal(target_ticks
);
133 VLOG(1) << "Advanced mock time: T=" << elapsed_sec() << "s";
136 void MockTime::AddTask(base::TimeDelta delay
, const base::Closure
& task
) {
137 tasks_
[TaskKey(now_ticks_
+ delay
, task_sequence_number_
++)] = task
;
140 void MockTime::AdvanceToInternal(base::TimeTicks target_ticks
) {
141 base::TimeDelta delta
= target_ticks
- now_ticks_
;
146 } // namespace domain_reliability