Change to let media_stream_devices_controller.cc decide if a gUM request fail if...
[chromium-blink-merge.git] / components / domain_reliability / test_util.cc
blobcfb1f3106958fb4a15b1b37b4ee5cc3f7cfe1783
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);
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());
32 if (running_)
33 ++callback_sequence_number_;
34 running_ = true;
35 user_task_ = user_task;
36 time_->AddTask(delay,
37 base::Bind(&MockTimer::OnDelayPassed,
38 weak_factory_.GetWeakPtr(),
39 callback_sequence_number_));
42 virtual void Stop() OVERRIDE {
43 if (running_) {
44 ++callback_sequence_number_;
45 running_ = false;
49 virtual bool IsRunning() OVERRIDE { return running_; }
51 private:
52 void OnDelayPassed(int expected_callback_sequence_number) {
53 if (callback_sequence_number_ != expected_callback_sequence_number)
54 return;
56 DCHECK(running_);
57 running_ = false;
59 // Grab user task in case it re-entrantly starts the timer again.
60 base::Closure task_to_run = user_task_;
61 user_task_.Reset();
62 task_to_run.Run();
65 MockTime* time_;
66 bool running_;
67 int callback_sequence_number_;
68 base::Closure user_task_;
69 base::WeakPtrFactory<MockTimer> weak_factory_;
72 } // namespace
74 TestCallback::TestCallback()
75 : callback_(base::Bind(&TestCallback::OnCalled,
76 base::Unretained(this))),
77 called_(false) {}
79 TestCallback::~TestCallback() {}
81 void TestCallback::OnCalled() {
82 EXPECT_FALSE(called_);
83 called_ = true;
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);
97 MockTime::MockTime()
98 : now_(base::TimeTicks::Now()),
99 epoch_(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);
122 now_ = key.time;
123 VLOG(1) << "Advancing mock time: task at T=" << elapsed_sec() << "s";
125 task.Run();
128 DCHECK(now_ <= target);
129 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