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" | 16 #include "base/timer/mock_timer.h" |
| 17 #include "base/timer/timer.h" |
17 #include "net/reporting/reporting_cache.h" | 18 #include "net/reporting/reporting_cache.h" |
18 #include "net/reporting/reporting_client.h" | 19 #include "net/reporting/reporting_client.h" |
19 #include "net/reporting/reporting_context.h" | 20 #include "net/reporting/reporting_context.h" |
20 #include "net/reporting/reporting_delegate.h" | 21 #include "net/reporting/reporting_delegate.h" |
21 #include "net/reporting/reporting_garbage_collector.h" | 22 #include "net/reporting/reporting_garbage_collector.h" |
| 23 #include "net/reporting/reporting_persister.h" |
22 #include "net/reporting/reporting_policy.h" | 24 #include "net/reporting/reporting_policy.h" |
23 #include "net/reporting/reporting_uploader.h" | 25 #include "net/reporting/reporting_uploader.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
26 #include "url/origin.h" | 28 #include "url/origin.h" |
27 | 29 |
28 namespace net { | 30 namespace net { |
29 | 31 |
30 namespace { | 32 namespace { |
31 | 33 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( | 117 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( |
116 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); | 118 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); |
117 } | 119 } |
118 | 120 |
119 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) | 121 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) |
120 : ReportingContext(policy, | 122 : ReportingContext(policy, |
121 base::MakeUnique<TestReportingDelegate>(), | 123 base::MakeUnique<TestReportingDelegate>(), |
122 base::MakeUnique<base::SimpleTestClock>(), | 124 base::MakeUnique<base::SimpleTestClock>(), |
123 base::MakeUnique<base::SimpleTestTickClock>(), | 125 base::MakeUnique<base::SimpleTestTickClock>(), |
124 base::MakeUnique<TestReportingUploader>()), | 126 base::MakeUnique<TestReportingUploader>()), |
| 127 persistence_timer_(new base::MockTimer(/* retain_user_task= */ false, |
| 128 /* is_repeating= */ false)), |
125 garbage_collection_timer_( | 129 garbage_collection_timer_( |
126 new base::MockTimer(/* retain_user_task= */ false, | 130 new base::MockTimer(/* retain_user_task= */ false, |
127 /* is_repeating= */ false)) { | 131 /* is_repeating= */ false)) { |
| 132 persister()->SetTimerForTesting(base::WrapUnique(persistence_timer_)); |
128 garbage_collector()->SetTimerForTesting( | 133 garbage_collector()->SetTimerForTesting( |
129 base::WrapUnique(garbage_collection_timer_)); | 134 base::WrapUnique(garbage_collection_timer_)); |
130 } | 135 } |
131 | 136 |
132 TestReportingContext::~TestReportingContext() { | 137 TestReportingContext::~TestReportingContext() { |
| 138 persistence_timer_ = nullptr; |
133 garbage_collection_timer_ = nullptr; | 139 garbage_collection_timer_ = nullptr; |
134 } | 140 } |
135 | 141 |
136 ReportingTestBase::ReportingTestBase() { | 142 ReportingTestBase::ReportingTestBase() { |
137 // For tests, disable jitter. | 143 // For tests, disable jitter. |
138 ReportingPolicy policy; | 144 ReportingPolicy policy; |
139 policy.endpoint_backoff_policy.jitter_factor = 0.0; | 145 policy.endpoint_backoff_policy.jitter_factor = 0.0; |
140 UsePolicy(policy); | 146 |
| 147 CreateAndInitializeContext(policy, std::unique_ptr<const base::Value>(), |
| 148 base::Time::Now(), base::TimeTicks::Now()); |
141 } | 149 } |
142 | 150 |
143 ReportingTestBase::~ReportingTestBase() {} | 151 ReportingTestBase::~ReportingTestBase() {} |
144 | 152 |
145 void ReportingTestBase::UsePolicy(const ReportingPolicy& policy) { | 153 void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { |
| 154 CreateAndInitializeContext(new_policy, delegate()->GetPersistedData(), |
| 155 clock()->Now(), tick_clock()->NowTicks()); |
| 156 } |
| 157 |
| 158 void ReportingTestBase::SimulateRestart(base::TimeDelta delta, |
| 159 base::TimeDelta delta_ticks) { |
| 160 CreateAndInitializeContext(policy(), delegate()->GetPersistedData(), |
| 161 clock()->Now() + delta, |
| 162 tick_clock()->NowTicks() + delta_ticks); |
| 163 } |
| 164 |
| 165 void ReportingTestBase::CreateAndInitializeContext( |
| 166 const ReportingPolicy& policy, |
| 167 std::unique_ptr<const base::Value> persisted_data, |
| 168 base::Time now, |
| 169 base::TimeTicks now_ticks) { |
146 context_ = base::MakeUnique<TestReportingContext>(policy); | 170 context_ = base::MakeUnique<TestReportingContext>(policy); |
| 171 delegate()->PersistData(std::move(persisted_data)); |
| 172 clock()->SetNow(now); |
| 173 tick_clock()->SetNowTicks(now_ticks); |
| 174 context_->Initialize(); |
147 } | 175 } |
148 | 176 |
149 base::TimeTicks ReportingTestBase::yesterday() { | 177 base::TimeTicks ReportingTestBase::yesterday() { |
150 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); | 178 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
151 } | 179 } |
152 | 180 |
153 base::TimeTicks ReportingTestBase::tomorrow() { | 181 base::TimeTicks ReportingTestBase::tomorrow() { |
154 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); | 182 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
155 } | 183 } |
156 | 184 |
157 } // namespace net | 185 } // namespace net |
OLD | NEW |