OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" | 5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_ping
back_client.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include <list> |
9 #include <memory> | 10 #include <memory> |
10 #include <string> | 11 #include <string> |
11 | 12 |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/optional.h" | 16 #include "base/optional.h" |
| 17 #include "base/run_loop.h" |
16 #include "base/test/histogram_tester.h" | 18 #include "base/test/histogram_tester.h" |
17 #include "base/time/time.h" | 19 #include "base/time/time.h" |
18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | 20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_page_
load_timing.h" | 21 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_page_
load_timing.h" |
20 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc
hes.h" | 22 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc
hes.h" |
21 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.
h" | 23 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_util.
h" |
22 #include "components/data_reduction_proxy/proto/client_config.pb.h" | 24 #include "components/data_reduction_proxy/proto/client_config.pb.h" |
23 #include "components/data_reduction_proxy/proto/pageload_metrics.pb.h" | 25 #include "components/data_reduction_proxy/proto/pageload_metrics.pb.h" |
24 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
25 #include "net/nqe/effective_connection_type.h" | 27 #include "net/nqe/effective_connection_type.h" |
26 #include "net/url_request/test_url_fetcher_factory.h" | 28 #include "net/url_request/test_url_fetcher_factory.h" |
27 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
28 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
31 | 33 |
32 namespace data_reduction_proxy { | 34 namespace data_reduction_proxy { |
33 | 35 |
34 namespace { | 36 namespace { |
35 | 37 |
36 static const char kHistogramSucceeded[] = | 38 static const char kHistogramSucceeded[] = |
37 "DataReductionProxy.Pingback.Succeeded"; | 39 "DataReductionProxy.Pingback.Succeeded"; |
38 static const char kHistogramAttempted[] = | 40 static const char kHistogramAttempted[] = |
39 "DataReductionProxy.Pingback.Attempted"; | 41 "DataReductionProxy.Pingback.Attempted"; |
40 static const char kSessionKey[] = "fake-session"; | 42 static const char kSessionKey[] = "fake-session"; |
41 static const char kFakeURL[] = "http://www.google.com/"; | 43 static const char kFakeURL[] = "http://www.google.com/"; |
42 static const int64_t kBytes = 10000; | 44 static const int64_t kBytes = 10000; |
43 static const int64_t kBytesOriginal = 1000000; | 45 static const int64_t kBytesOriginal = 1000000; |
44 static const uint64_t kPageId = 1; | |
45 | 46 |
46 } // namespace | 47 } // namespace |
47 | 48 |
48 // Controls whether a pingback is sent or not. | 49 // Controls whether a pingback is sent or not. |
49 class TestDataReductionProxyPingbackClient | 50 class TestDataReductionProxyPingbackClient |
50 : public DataReductionProxyPingbackClient { | 51 : public DataReductionProxyPingbackClient { |
51 public: | 52 public: |
52 TestDataReductionProxyPingbackClient( | 53 TestDataReductionProxyPingbackClient( |
53 net::URLRequestContextGetter* url_request_context_getter) | 54 net::URLRequestContextGetter* url_request_context_getter) |
54 : DataReductionProxyPingbackClient(url_request_context_getter), | 55 : DataReductionProxyPingbackClient(url_request_context_getter), |
(...skipping 25 matching lines...) Expand all Loading... |
80 | 81 |
81 base::Time CurrentTime() const override { return current_time_; } | 82 base::Time CurrentTime() const override { return current_time_; } |
82 | 83 |
83 bool should_override_random_; | 84 bool should_override_random_; |
84 float override_value_; | 85 float override_value_; |
85 base::Time current_time_; | 86 base::Time current_time_; |
86 }; | 87 }; |
87 | 88 |
88 class DataReductionProxyPingbackClientTest : public testing::Test { | 89 class DataReductionProxyPingbackClientTest : public testing::Test { |
89 public: | 90 public: |
90 DataReductionProxyPingbackClientTest() | 91 DataReductionProxyPingbackClientTest() {} |
91 : timing_( | |
92 base::Time::FromJsTime(1500) /* navigation_start */, | |
93 base::Optional<base::TimeDelta>( | |
94 base::TimeDelta::FromMilliseconds(1600)) /* response_start */, | |
95 base::Optional<base::TimeDelta>( | |
96 base::TimeDelta::FromMilliseconds(1700)) /* load_event_start */, | |
97 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( | |
98 1800)) /* first_image_paint */, | |
99 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( | |
100 1900)) /* first_contentful_paint */, | |
101 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( | |
102 2000)) /* experimental_first_meaningful_paint */, | |
103 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( | |
104 100)) /* parse_blocked_on_script_load_duration */, | |
105 base::Optional<base::TimeDelta>( | |
106 base::TimeDelta::FromMilliseconds(2000)) /* parse_stop */, | |
107 kBytes /* network_bytes */, | |
108 kBytesOriginal /* original_network_bytes */) {} | |
109 | 92 |
110 TestDataReductionProxyPingbackClient* pingback_client() const { | 93 TestDataReductionProxyPingbackClient* pingback_client() const { |
111 return pingback_client_.get(); | 94 return pingback_client_.get(); |
112 } | 95 } |
113 | 96 |
114 void Init() { | 97 void Init() { |
115 request_context_getter_ = | 98 request_context_getter_ = |
116 new net::TestURLRequestContextGetter(message_loop_.task_runner()); | 99 new net::TestURLRequestContextGetter(message_loop_.task_runner()); |
117 pingback_client_ = base::WrapUnique<TestDataReductionProxyPingbackClient>( | 100 pingback_client_ = base::WrapUnique<TestDataReductionProxyPingbackClient>( |
118 new TestDataReductionProxyPingbackClient( | 101 new TestDataReductionProxyPingbackClient( |
119 request_context_getter_.get())); | 102 request_context_getter_.get())); |
| 103 page_id_ = 0u; |
120 } | 104 } |
121 | 105 |
122 void CreateAndSendPingback() { | 106 void CreateAndSendPingback(bool lofi_received, |
| 107 bool lite_page_received, |
| 108 bool app_background_occured, |
| 109 void* tab_key) { |
| 110 timing_ = base::MakeUnique<DataReductionProxyPageLoadTiming>( |
| 111 base::Time::FromJsTime(1500) /* navigation_start */, |
| 112 base::Optional<base::TimeDelta>( |
| 113 base::TimeDelta::FromMilliseconds(1600)) /* response_start */, |
| 114 base::Optional<base::TimeDelta>( |
| 115 base::TimeDelta::FromMilliseconds(1700)) /* load_event_start */, |
| 116 base::Optional<base::TimeDelta>( |
| 117 base::TimeDelta::FromMilliseconds(1800)) /* first_image_paint */, |
| 118 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( |
| 119 1900)) /* first_contentful_paint */, |
| 120 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( |
| 121 2000)) /* experimental_first_meaningful_paint */, |
| 122 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds( |
| 123 100)) /* parse_blocked_on_script_load_duration */, |
| 124 base::Optional<base::TimeDelta>( |
| 125 base::TimeDelta::FromMilliseconds(2000)) /* parse_stop */, |
| 126 kBytes /* network_bytes */, kBytesOriginal /* original_network_bytes */, |
| 127 app_background_occured /* app_background_occured */); |
| 128 |
123 DataReductionProxyData request_data; | 129 DataReductionProxyData request_data; |
124 request_data.set_session_key(kSessionKey); | 130 request_data.set_session_key(kSessionKey); |
125 request_data.set_request_url(GURL(kFakeURL)); | 131 request_data.set_request_url(GURL(kFakeURL)); |
126 request_data.set_effective_connection_type( | 132 request_data.set_effective_connection_type( |
127 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); | 133 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); |
128 request_data.set_page_id(kPageId); | 134 request_data.set_lofi_received(lofi_received); |
| 135 request_data.set_lite_page_received(lite_page_received); |
| 136 request_data.set_page_id(page_id_); |
129 factory()->set_remove_fetcher_on_delete(true); | 137 factory()->set_remove_fetcher_on_delete(true); |
130 pingback_client()->SendPingback(request_data, timing_); | 138 pingback_client()->SendPingback(request_data, *timing_, tab_key); |
| 139 page_id_++; |
131 } | 140 } |
132 | 141 |
133 net::TestURLFetcherFactory* factory() { return &factory_; } | 142 net::TestURLFetcherFactory* factory() { return &factory_; } |
134 | 143 |
135 const DataReductionProxyPageLoadTiming& timing() { return timing_; } | 144 const DataReductionProxyPageLoadTiming& timing() { return *timing_; } |
136 | 145 |
137 const base::HistogramTester& histogram_tester() { return histogram_tester_; } | 146 const base::HistogramTester& histogram_tester() { return histogram_tester_; } |
138 | 147 |
| 148 uint64_t page_id() const { return page_id_; } |
| 149 |
139 private: | 150 private: |
140 base::MessageLoopForIO message_loop_; | 151 base::MessageLoopForIO message_loop_; |
141 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 152 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
142 std::unique_ptr<TestDataReductionProxyPingbackClient> pingback_client_; | 153 std::unique_ptr<TestDataReductionProxyPingbackClient> pingback_client_; |
143 net::TestURLFetcherFactory factory_; | 154 net::TestURLFetcherFactory factory_; |
144 DataReductionProxyPageLoadTiming timing_; | 155 std::unique_ptr<DataReductionProxyPageLoadTiming> timing_; |
145 base::HistogramTester histogram_tester_; | 156 base::HistogramTester histogram_tester_; |
| 157 uint64_t page_id_; |
146 }; | 158 }; |
147 | 159 |
148 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { | 160 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { |
149 Init(); | 161 Init(); |
150 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 162 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
151 pingback_client()->OverrideRandom(true, 0.5f); | 163 pingback_client()->OverrideRandom(true, 0.5f); |
152 pingback_client()->SetPingbackReportingFraction(1.0f); | 164 pingback_client()->SetPingbackReportingFraction(1.0f); |
153 base::Time current_time = base::Time::UnixEpoch(); | 165 base::Time current_time = base::Time::UnixEpoch(); |
154 pingback_client()->set_current_time(current_time); | 166 pingback_client()->set_current_time(current_time); |
155 CreateAndSendPingback(); | 167 uint64_t data_page_id = page_id(); |
| 168 CreateAndSendPingback(false /* lofi_received */, |
| 169 false /* lite_page_received */, |
| 170 false /* app_background_occured */, nullptr); |
156 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 171 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
157 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 172 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
158 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); | 173 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
159 RecordPageloadMetricsRequest batched_request; | 174 RecordPageloadMetricsRequest batched_request; |
160 batched_request.ParseFromString(test_fetcher->upload_data()); | 175 batched_request.ParseFromString(test_fetcher->upload_data()); |
161 EXPECT_EQ(batched_request.pageloads_size(), 1); | 176 EXPECT_EQ(batched_request.pageloads_size(), 1); |
162 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime( | 177 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime( |
163 batched_request.metrics_sent_time())); | 178 batched_request.metrics_sent_time())); |
164 PageloadMetrics pageload_metrics = batched_request.pageloads(0); | 179 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
165 EXPECT_EQ( | 180 EXPECT_EQ( |
(...skipping 18 matching lines...) Expand all Loading... |
184 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), | 199 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), |
185 protobuf_parser::DurationToTimeDelta( | 200 protobuf_parser::DurationToTimeDelta( |
186 pageload_metrics.parse_blocked_on_script_load_duration())); | 201 pageload_metrics.parse_blocked_on_script_load_duration())); |
187 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( | 202 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( |
188 pageload_metrics.parse_stop())); | 203 pageload_metrics.parse_stop())); |
189 | 204 |
190 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); | 205 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); |
191 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); | 206 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); |
192 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); | 207 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); |
193 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); | 208 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); |
194 EXPECT_EQ(kPageId, pageload_metrics.page_id()); | 209 EXPECT_EQ(data_page_id, pageload_metrics.page_id()); |
| 210 |
| 211 EXPECT_EQ(PageloadMetrics_PreviewsType_NONE, |
| 212 pageload_metrics.previews_type()); |
| 213 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_UNKNOWN, |
| 214 pageload_metrics.previews_opt_out()); |
195 | 215 |
196 EXPECT_EQ( | 216 EXPECT_EQ( |
197 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLINE, | 217 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLINE, |
198 pageload_metrics.effective_connection_type()); | 218 pageload_metrics.effective_connection_type()); |
199 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 219 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
200 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); | 220 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); |
201 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 221 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
202 } | 222 } |
203 | 223 |
204 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoPingbacksBatchedContent) { | 224 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoPingbacksBatchedContent) { |
205 Init(); | 225 Init(); |
206 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 226 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
207 pingback_client()->OverrideRandom(true, 0.5f); | 227 pingback_client()->OverrideRandom(true, 0.5f); |
208 pingback_client()->SetPingbackReportingFraction(1.0f); | 228 pingback_client()->SetPingbackReportingFraction(1.0f); |
209 base::Time current_time = base::Time::UnixEpoch(); | 229 base::Time current_time = base::Time::UnixEpoch(); |
210 pingback_client()->set_current_time(current_time); | 230 pingback_client()->set_current_time(current_time); |
211 // First pingback | 231 // First pingback |
212 CreateAndSendPingback(); | 232 CreateAndSendPingback(false /* lofi_received */, |
| 233 false /* lite_page_received */, |
| 234 false /* app_background_occured */, nullptr); |
213 | 235 |
214 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 236 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
215 // Two more pingbacks batched together. | 237 // Two more pingbacks batched together. |
216 CreateAndSendPingback(); | 238 std::list<uint64_t> page_ids; |
| 239 page_ids.push_back(page_id()); |
| 240 CreateAndSendPingback(false /* lofi_received */, |
| 241 false /* lite_page_received */, |
| 242 false /* app_background_occured */, nullptr); |
217 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); | 243 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); |
218 CreateAndSendPingback(); | 244 page_ids.push_back(page_id()); |
| 245 CreateAndSendPingback(false /* lofi_received */, |
| 246 false /* lite_page_received */, |
| 247 false /* app_background_occured */, nullptr); |
219 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 3); | 248 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 3); |
220 | 249 |
221 // Ignore the first pingback. | 250 // Ignore the first pingback. |
222 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 251 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
223 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 252 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
224 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); | 253 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); |
225 | 254 |
226 // Check the state of the second pingback. | 255 // Check the state of the second pingback. |
227 test_fetcher = factory()->GetFetcherByID(0); | 256 test_fetcher = factory()->GetFetcherByID(0); |
228 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); | 257 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
(...skipping 28 matching lines...) Expand all Loading... |
257 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), | 286 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), |
258 protobuf_parser::DurationToTimeDelta( | 287 protobuf_parser::DurationToTimeDelta( |
259 pageload_metrics.parse_blocked_on_script_load_duration())); | 288 pageload_metrics.parse_blocked_on_script_load_duration())); |
260 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( | 289 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( |
261 pageload_metrics.parse_stop())); | 290 pageload_metrics.parse_stop())); |
262 | 291 |
263 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); | 292 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); |
264 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); | 293 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); |
265 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); | 294 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); |
266 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); | 295 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); |
267 EXPECT_EQ(kPageId, pageload_metrics.page_id()); | 296 EXPECT_EQ(page_ids.front(), pageload_metrics.page_id()); |
| 297 page_ids.pop_front(); |
268 EXPECT_EQ( | 298 EXPECT_EQ( |
269 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLIN
E, | 299 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLIN
E, |
270 pageload_metrics.effective_connection_type()); | 300 pageload_metrics.effective_connection_type()); |
271 } | 301 } |
272 | 302 |
273 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 303 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
274 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); | 304 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); |
275 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 305 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
276 } | 306 } |
277 | 307 |
278 TEST_F(DataReductionProxyPingbackClientTest, SendTwoPingbacks) { | 308 TEST_F(DataReductionProxyPingbackClientTest, SendTwoPingbacks) { |
279 Init(); | 309 Init(); |
280 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 310 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
281 pingback_client()->OverrideRandom(true, 0.5f); | 311 pingback_client()->OverrideRandom(true, 0.5f); |
282 pingback_client()->SetPingbackReportingFraction(1.0f); | 312 pingback_client()->SetPingbackReportingFraction(1.0f); |
283 CreateAndSendPingback(); | 313 CreateAndSendPingback(false /* lofi_received */, |
| 314 false /* lite_page_received */, |
| 315 false /* app_background_occured */, nullptr); |
284 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 316 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
285 CreateAndSendPingback(); | 317 CreateAndSendPingback(false /* lofi_received */, |
| 318 false /* lite_page_received */, |
| 319 false /* app_background_occured */, nullptr); |
286 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); | 320 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); |
287 | 321 |
288 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 322 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
289 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 323 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
290 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); | 324 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); |
291 EXPECT_TRUE(factory()->GetFetcherByID(0)); | 325 EXPECT_TRUE(factory()->GetFetcherByID(0)); |
292 test_fetcher = factory()->GetFetcherByID(0); | 326 test_fetcher = factory()->GetFetcherByID(0); |
293 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 327 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
294 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); | 328 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); |
295 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 329 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
296 histogram_tester().ExpectTotalCount(kHistogramAttempted, 2); | 330 histogram_tester().ExpectTotalCount(kHistogramAttempted, 2); |
297 } | 331 } |
298 | 332 |
299 TEST_F(DataReductionProxyPingbackClientTest, NoPingbackSent) { | 333 TEST_F(DataReductionProxyPingbackClientTest, NoPingbackSent) { |
300 Init(); | 334 Init(); |
301 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 335 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
302 pingback_client()->OverrideRandom(true, 0.5f); | 336 pingback_client()->OverrideRandom(true, 0.5f); |
303 pingback_client()->SetPingbackReportingFraction(0.0f); | 337 pingback_client()->SetPingbackReportingFraction(0.0f); |
304 CreateAndSendPingback(); | 338 CreateAndSendPingback(false /* lofi_received */, |
| 339 false /* lite_page_received */, |
| 340 false /* app_background_occured */, nullptr); |
305 histogram_tester().ExpectUniqueSample(kHistogramAttempted, false, 1); | 341 histogram_tester().ExpectUniqueSample(kHistogramAttempted, false, 1); |
306 histogram_tester().ExpectTotalCount(kHistogramSucceeded, 0); | 342 histogram_tester().ExpectTotalCount(kHistogramSucceeded, 0); |
307 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 343 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
308 } | 344 } |
309 | 345 |
310 TEST_F(DataReductionProxyPingbackClientTest, VerifyReportingBehvaior) { | 346 TEST_F(DataReductionProxyPingbackClientTest, VerifyReportingBehvaior) { |
311 Init(); | 347 Init(); |
312 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 348 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
313 | 349 |
314 // Verify that if the random number is less than the reporting fraction, the | 350 // Verify that if the random number is less than the reporting fraction, the |
315 // pingback is created. | 351 // pingback is created. |
316 pingback_client()->SetPingbackReportingFraction(0.5f); | 352 pingback_client()->SetPingbackReportingFraction(0.5f); |
317 pingback_client()->OverrideRandom(true, 0.4f); | 353 pingback_client()->OverrideRandom(true, 0.4f); |
318 CreateAndSendPingback(); | 354 CreateAndSendPingback(false /* lofi_received */, |
| 355 false /* lite_page_received */, |
| 356 false /* app_background_occured */, nullptr); |
319 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 357 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
320 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 358 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
321 EXPECT_TRUE(test_fetcher); | 359 EXPECT_TRUE(test_fetcher); |
322 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 360 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
323 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); | 361 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); |
324 | 362 |
325 // Verify that if the random number is greater than the reporting fraction, | 363 // Verify that if the random number is greater than the reporting fraction, |
326 // the pingback is not created. | 364 // the pingback is not created. |
327 pingback_client()->OverrideRandom(true, 0.6f); | 365 pingback_client()->OverrideRandom(true, 0.6f); |
328 CreateAndSendPingback(); | 366 CreateAndSendPingback(false /* lofi_received */, |
| 367 false /* lite_page_received */, |
| 368 false /* app_background_occured */, nullptr); |
329 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 1); | 369 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 1); |
330 test_fetcher = factory()->GetFetcherByID(0); | 370 test_fetcher = factory()->GetFetcherByID(0); |
331 EXPECT_FALSE(test_fetcher); | 371 EXPECT_FALSE(test_fetcher); |
332 | 372 |
333 // Verify that if the random number is equal to the reporting fraction, the | 373 // Verify that if the random number is equal to the reporting fraction, the |
334 // pingback is not created. Specifically, if the reporting fraction is zero, | 374 // pingback is not created. Specifically, if the reporting fraction is zero, |
335 // and the random number is zero, no pingback is sent. | 375 // and the random number is zero, no pingback is sent. |
336 pingback_client()->SetPingbackReportingFraction(0.0f); | 376 pingback_client()->SetPingbackReportingFraction(0.0f); |
337 pingback_client()->OverrideRandom(true, 0.0f); | 377 pingback_client()->OverrideRandom(true, 0.0f); |
338 CreateAndSendPingback(); | 378 CreateAndSendPingback(false /* lofi_received */, |
| 379 false /* lite_page_received */, |
| 380 false /* app_background_occured */, nullptr); |
339 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 2); | 381 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 2); |
340 test_fetcher = factory()->GetFetcherByID(0); | 382 test_fetcher = factory()->GetFetcherByID(0); |
341 EXPECT_FALSE(test_fetcher); | 383 EXPECT_FALSE(test_fetcher); |
342 | 384 |
343 // Verify that the command line flag forces a pingback. | 385 // Verify that the command line flag forces a pingback. |
344 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 386 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
345 data_reduction_proxy::switches::kEnableDataReductionProxyForcePingback); | 387 data_reduction_proxy::switches::kEnableDataReductionProxyForcePingback); |
346 pingback_client()->SetPingbackReportingFraction(0.0f); | 388 pingback_client()->SetPingbackReportingFraction(0.0f); |
347 pingback_client()->OverrideRandom(true, 1.0f); | 389 pingback_client()->OverrideRandom(true, 1.0f); |
348 CreateAndSendPingback(); | 390 CreateAndSendPingback(false /* lofi_received */, |
| 391 false /* lite_page_received */, |
| 392 false /* app_background_occured */, nullptr); |
349 histogram_tester().ExpectBucketCount(kHistogramAttempted, true, 2); | 393 histogram_tester().ExpectBucketCount(kHistogramAttempted, true, 2); |
350 test_fetcher = factory()->GetFetcherByID(0); | 394 test_fetcher = factory()->GetFetcherByID(0); |
351 EXPECT_TRUE(test_fetcher); | 395 EXPECT_TRUE(test_fetcher); |
352 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 396 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
353 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); | 397 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); |
354 } | 398 } |
355 | 399 |
356 TEST_F(DataReductionProxyPingbackClientTest, FailedPingback) { | 400 TEST_F(DataReductionProxyPingbackClientTest, FailedPingback) { |
357 Init(); | 401 Init(); |
358 EXPECT_FALSE(factory()->GetFetcherByID(0)); | 402 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
359 pingback_client()->OverrideRandom(true, 0.5f); | 403 pingback_client()->OverrideRandom(true, 0.5f); |
360 pingback_client()->SetPingbackReportingFraction(1.0f); | 404 pingback_client()->SetPingbackReportingFraction(1.0f); |
361 CreateAndSendPingback(); | 405 CreateAndSendPingback(false /* lofi_received */, |
| 406 false /* lite_page_received */, |
| 407 false /* app_background_occured */, nullptr); |
362 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); | 408 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
363 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); | 409 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
364 EXPECT_TRUE(test_fetcher); | 410 EXPECT_TRUE(test_fetcher); |
365 // Simulate a network error. | 411 // Simulate a network error. |
366 test_fetcher->set_status(net::URLRequestStatus( | 412 test_fetcher->set_status(net::URLRequestStatus( |
367 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); | 413 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); |
368 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); | 414 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
369 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); | 415 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); |
370 } | 416 } |
371 | 417 |
| 418 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentNoOptOut) { |
| 419 Init(); |
| 420 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 421 pingback_client()->OverrideRandom(true, 0.5f); |
| 422 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 423 base::Time current_time = base::Time::UnixEpoch(); |
| 424 pingback_client()->set_current_time(current_time); |
| 425 CreateAndSendPingback(true /* lofi_received */, |
| 426 false /* lite_page_received */, |
| 427 false /* app_background_occured */, nullptr); |
| 428 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 429 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 430 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 431 RecordPageloadMetricsRequest batched_request; |
| 432 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 433 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 434 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 435 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI, |
| 436 pageload_metrics.previews_type()); |
| 437 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_NON_OPT_OUT, |
| 438 pageload_metrics.previews_opt_out()); |
| 439 |
| 440 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 441 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 442 } |
| 443 |
| 444 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentOptOut) { |
| 445 Init(); |
| 446 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 447 pingback_client()->OverrideRandom(true, 0.5f); |
| 448 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 449 base::Time current_time = base::Time::UnixEpoch(); |
| 450 pingback_client()->set_current_time(current_time); |
| 451 pingback_client()->AddOptOut(nullptr, page_id()); |
| 452 CreateAndSendPingback(true /* lofi_received */, |
| 453 false /* lite_page_received */, |
| 454 false /* app_background_occured */, nullptr); |
| 455 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 456 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 457 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 458 RecordPageloadMetricsRequest batched_request; |
| 459 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 460 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 461 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 462 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI, |
| 463 pageload_metrics.previews_type()); |
| 464 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT, |
| 465 pageload_metrics.previews_opt_out()); |
| 466 |
| 467 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 468 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 469 } |
| 470 |
| 471 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentBackground) { |
| 472 Init(); |
| 473 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 474 pingback_client()->OverrideRandom(true, 0.5f); |
| 475 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 476 base::Time current_time = base::Time::UnixEpoch(); |
| 477 pingback_client()->set_current_time(current_time); |
| 478 pingback_client()->AddOptOut(nullptr, page_id()); |
| 479 CreateAndSendPingback(true /* lofi_received */, |
| 480 false /* lite_page_received */, |
| 481 true /* app_background_occured */, nullptr); |
| 482 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 483 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 484 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 485 RecordPageloadMetricsRequest batched_request; |
| 486 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 487 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 488 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 489 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI, |
| 490 pageload_metrics.previews_type()); |
| 491 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_UNKNOWN, |
| 492 pageload_metrics.previews_opt_out()); |
| 493 |
| 494 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 495 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 496 } |
| 497 |
| 498 TEST_F(DataReductionProxyPingbackClientTest, VerifyLitePageContent) { |
| 499 Init(); |
| 500 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 501 pingback_client()->OverrideRandom(true, 0.5f); |
| 502 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 503 base::Time current_time = base::Time::UnixEpoch(); |
| 504 pingback_client()->set_current_time(current_time); |
| 505 pingback_client()->AddOptOut(nullptr, page_id()); |
| 506 CreateAndSendPingback(false /* lofi_received */, |
| 507 true /* lite_page_received */, |
| 508 false /* app_background_occured */, nullptr); |
| 509 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); |
| 510 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 511 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 512 RecordPageloadMetricsRequest batched_request; |
| 513 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 514 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 515 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 516 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE, |
| 517 pageload_metrics.previews_type()); |
| 518 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT, |
| 519 pageload_metrics.previews_opt_out()); |
| 520 |
| 521 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 522 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 523 } |
| 524 |
| 525 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoLitePagePingbacks) { |
| 526 Init(); |
| 527 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 528 pingback_client()->OverrideRandom(true, 0.5f); |
| 529 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 530 base::Time current_time = base::Time::UnixEpoch(); |
| 531 pingback_client()->set_current_time(current_time); |
| 532 pingback_client()->AddOptOut(nullptr, page_id()); |
| 533 CreateAndSendPingback(false /* lofi_received */, |
| 534 true /* lite_page_received */, |
| 535 false /* app_background_occured */, nullptr); |
| 536 pingback_client()->AddOptOut(static_cast<void*>(pingback_client()), |
| 537 page_id()); |
| 538 CreateAndSendPingback(false /* lofi_received */, |
| 539 true /* lite_page_received */, |
| 540 false /* app_background_occured */, |
| 541 static_cast<void*>(pingback_client())); |
| 542 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); |
| 543 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); |
| 544 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 545 RecordPageloadMetricsRequest batched_request; |
| 546 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 547 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 548 PageloadMetrics pageload_metrics = batched_request.pageloads(0); |
| 549 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE, |
| 550 pageload_metrics.previews_type()); |
| 551 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT, |
| 552 pageload_metrics.previews_opt_out()); |
| 553 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 554 test_fetcher = factory()->GetFetcherByID(0); |
| 555 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); |
| 556 batched_request.ParseFromString(test_fetcher->upload_data()); |
| 557 EXPECT_EQ(batched_request.pageloads_size(), 1); |
| 558 pageload_metrics = batched_request.pageloads(0); |
| 559 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE, |
| 560 pageload_metrics.previews_type()); |
| 561 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT, |
| 562 pageload_metrics.previews_opt_out()); |
| 563 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); |
| 564 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 565 } |
| 566 |
| 567 TEST_F(DataReductionProxyPingbackClientTest, VerifyClearingPendingLoads) { |
| 568 Init(); |
| 569 EXPECT_FALSE(factory()->GetFetcherByID(0)); |
| 570 pingback_client()->OverrideRandom(true, 0.5f); |
| 571 pingback_client()->SetPingbackReportingFraction(1.0f); |
| 572 base::Time current_time = base::Time::UnixEpoch(); |
| 573 pingback_client()->set_current_time(current_time); |
| 574 pingback_client()->AddOptOut(nullptr, page_id()); |
| 575 pingback_client()->ClearTabKeyAsync(nullptr); |
| 576 base::RunLoop().RunUntilIdle(); |
| 577 EXPECT_EQ(0u, pingback_client()->PendingTabLoadsForTesting()); |
| 578 } |
| 579 |
372 } // namespace data_reduction_proxy | 580 } // namespace data_reduction_proxy |
OLD | NEW |