OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/reporting/reporting_test_util.h" | 5 #include "net/reporting/reporting_test_util.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
15 #include "base/test/simple_test_tick_clock.h" | 15 #include "base/test/simple_test_tick_clock.h" |
| 16 #include "base/timer/mock_timer.h" |
| 17 #include "base/timer/timer.h" |
16 #include "net/reporting/reporting_cache.h" | 18 #include "net/reporting/reporting_cache.h" |
17 #include "net/reporting/reporting_client.h" | 19 #include "net/reporting/reporting_client.h" |
18 #include "net/reporting/reporting_context.h" | 20 #include "net/reporting/reporting_context.h" |
19 #include "net/reporting/reporting_delegate.h" | 21 #include "net/reporting/reporting_delegate.h" |
| 22 #include "net/reporting/reporting_persister.h" |
20 #include "net/reporting/reporting_policy.h" | 23 #include "net/reporting/reporting_policy.h" |
21 #include "net/reporting/reporting_uploader.h" | 24 #include "net/reporting/reporting_uploader.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "url/gurl.h" | 26 #include "url/gurl.h" |
24 #include "url/origin.h" | 27 #include "url/origin.h" |
25 | 28 |
26 namespace net { | 29 namespace net { |
27 | 30 |
28 namespace { | 31 namespace { |
29 | 32 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const Callback& callback) { | 115 const Callback& callback) { |
113 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( | 116 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( |
114 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); | 117 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); |
115 } | 118 } |
116 | 119 |
117 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) | 120 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) |
118 : ReportingContext(policy, | 121 : ReportingContext(policy, |
119 base::MakeUnique<TestReportingDelegate>(), | 122 base::MakeUnique<TestReportingDelegate>(), |
120 base::MakeUnique<base::SimpleTestClock>(), | 123 base::MakeUnique<base::SimpleTestClock>(), |
121 base::MakeUnique<base::SimpleTestTickClock>(), | 124 base::MakeUnique<base::SimpleTestTickClock>(), |
122 base::MakeUnique<TestReportingUploader>()) {} | 125 base::MakeUnique<TestReportingUploader>()), |
| 126 persistence_timer_(new base::MockTimer(/* retain_user_task= */ false, |
| 127 /* is_repeating= */ false)) { |
| 128 persister()->SetTimerForTesting(base::WrapUnique(persistence_timer_)); |
| 129 } |
123 | 130 |
124 TestReportingContext::~TestReportingContext() {} | 131 TestReportingContext::~TestReportingContext() { |
| 132 persistence_timer_ = nullptr; |
| 133 } |
125 | 134 |
126 ReportingTestBase::ReportingTestBase() { | 135 ReportingTestBase::ReportingTestBase() { |
127 // For tests, disable jitter. | 136 // For tests, disable jitter. |
128 ReportingPolicy policy; | 137 ReportingPolicy policy; |
129 policy.endpoint_backoff_policy.jitter_factor = 0.0; | 138 policy.endpoint_backoff_policy.jitter_factor = 0.0; |
130 UsePolicy(policy); | 139 |
| 140 CreateAndInitializeContext(policy, std::unique_ptr<const base::Value>(), |
| 141 base::Time::Now(), base::TimeTicks::Now()); |
131 } | 142 } |
132 | 143 |
133 ReportingTestBase::~ReportingTestBase() {} | 144 ReportingTestBase::~ReportingTestBase() {} |
134 | 145 |
135 void ReportingTestBase::UsePolicy(const ReportingPolicy& policy) { | 146 void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { |
| 147 CreateAndInitializeContext(new_policy, delegate()->GetPersistedData(), |
| 148 clock()->Now(), tick_clock()->NowTicks()); |
| 149 } |
| 150 |
| 151 void ReportingTestBase::SimulateRestart(base::TimeDelta delta, |
| 152 base::TimeDelta delta_ticks) { |
| 153 CreateAndInitializeContext(policy(), delegate()->GetPersistedData(), |
| 154 clock()->Now() + delta, |
| 155 tick_clock()->NowTicks() + delta_ticks); |
| 156 } |
| 157 |
| 158 void ReportingTestBase::CreateAndInitializeContext( |
| 159 const ReportingPolicy& policy, |
| 160 std::unique_ptr<const base::Value> persisted_data, |
| 161 base::Time now, |
| 162 base::TimeTicks now_ticks) { |
136 context_ = base::MakeUnique<TestReportingContext>(policy); | 163 context_ = base::MakeUnique<TestReportingContext>(policy); |
| 164 delegate()->PersistData(std::move(persisted_data)); |
| 165 clock()->SetNow(now); |
| 166 tick_clock()->SetNowTicks(now_ticks); |
| 167 context_->Initialize(); |
137 } | 168 } |
138 | 169 |
139 base::TimeTicks ReportingTestBase::yesterday() { | 170 base::TimeTicks ReportingTestBase::yesterday() { |
140 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); | 171 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
141 } | 172 } |
142 | 173 |
143 base::TimeTicks ReportingTestBase::tomorrow() { | 174 base::TimeTicks ReportingTestBase::tomorrow() { |
144 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); | 175 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
145 } | 176 } |
146 | 177 |
147 } // namespace net | 178 } // namespace net |
OLD | NEW |