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 "net/reporting/reporting_cache.h" | 17 #include "net/reporting/reporting_cache.h" |
18 #include "net/reporting/reporting_client.h" | 18 #include "net/reporting/reporting_client.h" |
19 #include "net/reporting/reporting_context.h" | 19 #include "net/reporting/reporting_context.h" |
20 #include "net/reporting/reporting_delegate.h" | |
21 #include "net/reporting/reporting_delivery_agent.h" | 20 #include "net/reporting/reporting_delivery_agent.h" |
22 #include "net/reporting/reporting_garbage_collector.h" | 21 #include "net/reporting/reporting_garbage_collector.h" |
23 #include "net/reporting/reporting_persister.h" | 22 #include "net/reporting/reporting_persister.h" |
24 #include "net/reporting/reporting_policy.h" | 23 #include "net/reporting/reporting_policy.h" |
25 #include "net/reporting/reporting_uploader.h" | 24 #include "net/reporting/reporting_uploader.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "url/gurl.h" | 26 #include "url/gurl.h" |
28 #include "url/origin.h" | 27 #include "url/origin.h" |
29 | 28 |
30 namespace net { | 29 namespace net { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 const GURL& endpoint) { | 83 const GURL& endpoint) { |
85 std::vector<const ReportingClient*> clients; | 84 std::vector<const ReportingClient*> clients; |
86 cache->GetClients(&clients); | 85 cache->GetClients(&clients); |
87 for (const ReportingClient* client : clients) { | 86 for (const ReportingClient* client : clients) { |
88 if (client->origin == origin && client->endpoint == endpoint) | 87 if (client->origin == origin && client->endpoint == endpoint) |
89 return client; | 88 return client; |
90 } | 89 } |
91 return nullptr; | 90 return nullptr; |
92 } | 91 } |
93 | 92 |
94 TestReportingDelegate::TestReportingDelegate() {} | |
95 TestReportingDelegate::~TestReportingDelegate() {} | |
96 | |
97 void TestReportingDelegate::PersistData( | |
98 std::unique_ptr<const base::Value> persisted_data) { | |
99 persisted_data_ = std::move(persisted_data); | |
100 } | |
101 | |
102 std::unique_ptr<const base::Value> TestReportingDelegate::GetPersistedData() { | |
103 if (!persisted_data_) | |
104 return std::unique_ptr<const base::Value>(); | |
105 return persisted_data_->CreateDeepCopy(); | |
106 } | |
107 | |
108 TestReportingUploader::PendingUpload::~PendingUpload() {} | 93 TestReportingUploader::PendingUpload::~PendingUpload() {} |
109 TestReportingUploader::PendingUpload::PendingUpload() {} | 94 TestReportingUploader::PendingUpload::PendingUpload() {} |
110 | 95 |
111 TestReportingUploader::TestReportingUploader() {} | 96 TestReportingUploader::TestReportingUploader() {} |
112 TestReportingUploader::~TestReportingUploader() {} | 97 TestReportingUploader::~TestReportingUploader() {} |
113 | 98 |
114 void TestReportingUploader::StartUpload(const GURL& url, | 99 void TestReportingUploader::StartUpload(const GURL& url, |
115 const std::string& json, | 100 const std::string& json, |
116 const Callback& callback) { | 101 const Callback& callback) { |
117 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( | 102 pending_uploads_.push_back(base::MakeUnique<PendingUploadImpl>( |
118 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); | 103 url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_))); |
119 } | 104 } |
120 | 105 |
121 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) | 106 TestReportingContext::TestReportingContext(const ReportingPolicy& policy) |
122 : ReportingContext(policy, | 107 : ReportingContext(policy, |
123 base::MakeUnique<TestReportingDelegate>(), | |
124 base::MakeUnique<base::SimpleTestClock>(), | 108 base::MakeUnique<base::SimpleTestClock>(), |
125 base::MakeUnique<base::SimpleTestTickClock>(), | 109 base::MakeUnique<base::SimpleTestTickClock>(), |
126 base::MakeUnique<TestReportingUploader>()), | 110 base::MakeUnique<TestReportingUploader>()), |
127 delivery_timer_(new base::MockTimer(/* retain_user_task= */ false, | 111 delivery_timer_(new base::MockTimer(/* retain_user_task= */ false, |
128 /* is_repeating= */ false)), | 112 /* is_repeating= */ false)), |
129 persistence_timer_(new base::MockTimer(/* retain_user_task= */ false, | |
130 /* is_repeating= */ false)), | |
131 garbage_collection_timer_( | 113 garbage_collection_timer_( |
132 new base::MockTimer(/* retain_user_task= */ false, | 114 new base::MockTimer(/* retain_user_task= */ false, |
133 /* is_repeating= */ false)) { | 115 /* is_repeating= */ false)) { |
134 persister()->SetTimerForTesting(base::WrapUnique(persistence_timer_)); | |
135 garbage_collector()->SetTimerForTesting( | 116 garbage_collector()->SetTimerForTesting( |
136 base::WrapUnique(garbage_collection_timer_)); | 117 base::WrapUnique(garbage_collection_timer_)); |
137 delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_)); | 118 delivery_agent()->SetTimerForTesting(base::WrapUnique(delivery_timer_)); |
138 } | 119 } |
139 | 120 |
140 TestReportingContext::~TestReportingContext() { | 121 TestReportingContext::~TestReportingContext() { |
141 delivery_timer_ = nullptr; | 122 delivery_timer_ = nullptr; |
142 persistence_timer_ = nullptr; | |
143 garbage_collection_timer_ = nullptr; | 123 garbage_collection_timer_ = nullptr; |
144 } | 124 } |
145 | 125 |
146 ReportingTestBase::ReportingTestBase() { | 126 ReportingTestBase::ReportingTestBase() { |
147 // For tests, disable jitter. | 127 // For tests, disable jitter. |
148 ReportingPolicy policy; | 128 ReportingPolicy policy; |
149 policy.endpoint_backoff_policy.jitter_factor = 0.0; | 129 policy.endpoint_backoff_policy.jitter_factor = 0.0; |
150 | 130 |
151 CreateAndInitializeContext(policy, std::unique_ptr<const base::Value>(), | 131 CreateContext(policy, base::Time::Now(), base::TimeTicks::Now()); |
152 base::Time::Now(), base::TimeTicks::Now()); | |
153 } | 132 } |
154 | 133 |
155 ReportingTestBase::~ReportingTestBase() {} | 134 ReportingTestBase::~ReportingTestBase() {} |
156 | 135 |
157 void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { | 136 void ReportingTestBase::UsePolicy(const ReportingPolicy& new_policy) { |
158 CreateAndInitializeContext(new_policy, delegate()->GetPersistedData(), | 137 CreateContext(new_policy, clock()->Now(), tick_clock()->NowTicks()); |
159 clock()->Now(), tick_clock()->NowTicks()); | |
160 } | 138 } |
161 | 139 |
162 void ReportingTestBase::SimulateRestart(base::TimeDelta delta, | 140 void ReportingTestBase::SimulateRestart(base::TimeDelta delta, |
163 base::TimeDelta delta_ticks) { | 141 base::TimeDelta delta_ticks) { |
164 CreateAndInitializeContext(policy(), delegate()->GetPersistedData(), | 142 CreateContext(policy(), clock()->Now() + delta, |
165 clock()->Now() + delta, | 143 tick_clock()->NowTicks() + delta_ticks); |
166 tick_clock()->NowTicks() + delta_ticks); | |
167 } | 144 } |
168 | 145 |
169 void ReportingTestBase::CreateAndInitializeContext( | 146 void ReportingTestBase::CreateContext(const ReportingPolicy& policy, |
170 const ReportingPolicy& policy, | 147 base::Time now, |
171 std::unique_ptr<const base::Value> persisted_data, | 148 base::TimeTicks now_ticks) { |
172 base::Time now, | |
173 base::TimeTicks now_ticks) { | |
174 context_ = base::MakeUnique<TestReportingContext>(policy); | 149 context_ = base::MakeUnique<TestReportingContext>(policy); |
175 delegate()->PersistData(std::move(persisted_data)); | |
176 clock()->SetNow(now); | 150 clock()->SetNow(now); |
177 tick_clock()->SetNowTicks(now_ticks); | 151 tick_clock()->SetNowTicks(now_ticks); |
178 context_->Initialize(); | |
179 } | 152 } |
180 | 153 |
181 base::TimeTicks ReportingTestBase::yesterday() { | 154 base::TimeTicks ReportingTestBase::yesterday() { |
182 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); | 155 return tick_clock()->NowTicks() - base::TimeDelta::FromDays(1); |
183 } | 156 } |
184 | 157 |
185 base::TimeTicks ReportingTestBase::now() { | 158 base::TimeTicks ReportingTestBase::now() { |
186 return tick_clock()->NowTicks(); | 159 return tick_clock()->NowTicks(); |
187 } | 160 } |
188 | 161 |
189 base::TimeTicks ReportingTestBase::tomorrow() { | 162 base::TimeTicks ReportingTestBase::tomorrow() { |
190 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); | 163 return tick_clock()->NowTicks() + base::TimeDelta::FromDays(1); |
191 } | 164 } |
192 | 165 |
193 } // namespace net | 166 } // namespace net |
OLD | NEW |