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),
24 virtual ~MockTimer() {}
26 // MockableTime::Timer implementation:
27 virtual void Start(const tracked_objects::Location
& posted_from
,
28 base::TimeDelta delay
,
29 const base::Closure
& user_task
) OVERRIDE
{
30 DCHECK(!user_task
.is_null());
33 ++callback_sequence_number_
;
35 user_task_
= user_task
;
37 base::Bind(&MockTimer::OnDelayPassed
,
38 weak_factory_
.GetWeakPtr(),
39 callback_sequence_number_
));
42 virtual void Stop() OVERRIDE
{
44 ++callback_sequence_number_
;
49 virtual bool IsRunning() OVERRIDE
{ return running_
; }
52 void OnDelayPassed(int expected_callback_sequence_number
) {
53 if (callback_sequence_number_
!= expected_callback_sequence_number
)
59 // Grab user task in case it re-entrantly starts the timer again.
60 base::Closure task_to_run
= user_task_
;
67 int callback_sequence_number_
;
68 base::Closure user_task_
;
69 base::WeakPtrFactory
<MockTimer
> weak_factory_
;
74 TestCallback::TestCallback()
75 : callback_(base::Bind(&TestCallback::OnCalled
,
76 base::Unretained(this))),
79 TestCallback::~TestCallback() {}
81 void TestCallback::OnCalled() {
82 EXPECT_FALSE(called_
);
86 MockUploader::MockUploader(const UploadRequestCallback
& callback
)
87 : callback_(callback
) {}
89 MockUploader::~MockUploader() {}
91 void MockUploader::UploadReport(const std::string
& report_json
,
92 const GURL
& upload_url
,
93 const UploadCallback
& callback
) {
94 callback_
.Run(report_json
, upload_url
, callback
);
98 : now_(base::TimeTicks::Now()),
100 task_sequence_number_(0) {
101 VLOG(1) << "Creating mock time: T=" << elapsed_sec() << "s";
104 MockTime::~MockTime() {}
106 base::TimeTicks
MockTime::Now() { return now_
; }
108 scoped_ptr
<MockableTime::Timer
> MockTime::CreateTimer() {
109 return scoped_ptr
<MockableTime::Timer
>(new MockTimer(this));
112 void MockTime::Advance(base::TimeDelta delta
) {
113 base::TimeTicks target
= now_
+ delta
;
115 while (!tasks_
.empty() && tasks_
.begin()->first
.time
<= target
) {
116 TaskKey key
= tasks_
.begin()->first
;
117 base::Closure task
= tasks_
.begin()->second
;
118 tasks_
.erase(tasks_
.begin());
120 DCHECK(now_
<= key
.time
);
121 DCHECK(key
.time
<= target
);
123 VLOG(1) << "Advancing mock time: task at T=" << elapsed_sec() << "s";
128 DCHECK(now_
<= target
);
130 VLOG(1) << "Advanced mock time: T=" << elapsed_sec() << "s";
133 void MockTime::AddTask(base::TimeDelta delay
, const base::Closure
& task
) {
134 tasks_
[TaskKey(now_
+ delay
, task_sequence_number_
++)] = task
;
137 } // namespace domain_reliability