Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / domain_reliability / test_util.cc
blob9bc5a7b031d6d76e3db10c15ba2ddef690ac94a9
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"
7 #include "base/bind.h"
8 #include "net/url_request/url_request_status.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace domain_reliability {
13 namespace {
15 class MockTimer : public MockableTime::Timer {
16 public:
17 MockTimer(MockTime* time)
18 : time_(time),
19 running_(false),
20 callback_sequence_number_(0),
21 weak_factory_(this) {
22 DCHECK(time);
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());
33 if (running_)
34 ++callback_sequence_number_;
35 running_ = true;
36 user_task_ = user_task;
37 time_->AddTask(delay,
38 base::Bind(&MockTimer::OnDelayPassed,
39 weak_factory_.GetWeakPtr(),
40 callback_sequence_number_));
43 virtual void Stop() OVERRIDE {
44 if (running_) {
45 ++callback_sequence_number_;
46 running_ = false;
50 virtual bool IsRunning() OVERRIDE { return running_; }
52 private:
53 void OnDelayPassed(int expected_callback_sequence_number) {
54 if (callback_sequence_number_ != expected_callback_sequence_number)
55 return;
57 DCHECK(running_);
58 running_ = false;
60 // Grab user task in case it re-entrantly starts the timer again.
61 base::Closure task_to_run = user_task_;
62 user_task_.Reset();
63 task_to_run.Run();
66 MockTime* time_;
67 bool running_;
68 int callback_sequence_number_;
69 base::Closure user_task_;
70 base::WeakPtrFactory<MockTimer> weak_factory_;
73 } // namespace
75 TestCallback::TestCallback()
76 : callback_(base::Bind(&TestCallback::OnCalled,
77 base::Unretained(this))),
78 called_(false) {}
80 TestCallback::~TestCallback() {}
82 void TestCallback::OnCalled() {
83 EXPECT_FALSE(called_);
84 called_ = true;
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);
98 MockTime::MockTime()
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";
128 task.Run();
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_;
142 now_ += delta;
143 now_ticks_ += delta;
146 } // namespace domain_reliability