Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Side by Side Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_pingback_client_unittest.cc

Issue 2833523002: Adding opt out and previews type information to DRP pingback (Closed)
Patch Set: bengr comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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_occurred) {
109 timing_ = base::MakeUnique<DataReductionProxyPageLoadTiming>(
110 base::Time::FromJsTime(1500) /* navigation_start */,
111 base::Optional<base::TimeDelta>(
112 base::TimeDelta::FromMilliseconds(1600)) /* response_start */,
113 base::Optional<base::TimeDelta>(
114 base::TimeDelta::FromMilliseconds(1700)) /* load_event_start */,
115 base::Optional<base::TimeDelta>(
116 base::TimeDelta::FromMilliseconds(1800)) /* first_image_paint */,
117 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds(
118 1900)) /* first_contentful_paint */,
119 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds(
120 2000)) /* experimental_first_meaningful_paint */,
121 base::Optional<base::TimeDelta>(base::TimeDelta::FromMilliseconds(
122 100)) /* parse_blocked_on_script_load_duration */,
123 base::Optional<base::TimeDelta>(
124 base::TimeDelta::FromMilliseconds(2000)) /* parse_stop */,
125 kBytes /* network_bytes */, kBytesOriginal /* original_network_bytes */,
126 app_background_occurred /* app_background_occurred */);
127
123 DataReductionProxyData request_data; 128 DataReductionProxyData request_data;
124 request_data.set_session_key(kSessionKey); 129 request_data.set_session_key(kSessionKey);
125 request_data.set_request_url(GURL(kFakeURL)); 130 request_data.set_request_url(GURL(kFakeURL));
126 request_data.set_effective_connection_type( 131 request_data.set_effective_connection_type(
127 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE); 132 net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
128 request_data.set_page_id(kPageId); 133 request_data.set_lofi_received(lofi_received);
134 request_data.set_lite_page_received(lite_page_received);
135 request_data.set_page_id(page_id_);
129 factory()->set_remove_fetcher_on_delete(true); 136 factory()->set_remove_fetcher_on_delete(true);
130 pingback_client()->SendPingback(request_data, timing_); 137 pingback_client()->SendPingback(request_data, *timing_);
138 page_id_++;
131 } 139 }
132 140
133 net::TestURLFetcherFactory* factory() { return &factory_; } 141 net::TestURLFetcherFactory* factory() { return &factory_; }
134 142
135 const DataReductionProxyPageLoadTiming& timing() { return timing_; } 143 const DataReductionProxyPageLoadTiming& timing() { return *timing_; }
136 144
137 const base::HistogramTester& histogram_tester() { return histogram_tester_; } 145 const base::HistogramTester& histogram_tester() { return histogram_tester_; }
138 146
147 uint64_t page_id() const { return page_id_; }
148
139 private: 149 private:
140 base::MessageLoopForIO message_loop_; 150 base::MessageLoopForIO message_loop_;
141 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 151 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
142 std::unique_ptr<TestDataReductionProxyPingbackClient> pingback_client_; 152 std::unique_ptr<TestDataReductionProxyPingbackClient> pingback_client_;
143 net::TestURLFetcherFactory factory_; 153 net::TestURLFetcherFactory factory_;
144 DataReductionProxyPageLoadTiming timing_; 154 std::unique_ptr<DataReductionProxyPageLoadTiming> timing_;
145 base::HistogramTester histogram_tester_; 155 base::HistogramTester histogram_tester_;
156 uint64_t page_id_;
146 }; 157 };
147 158
148 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) { 159 TEST_F(DataReductionProxyPingbackClientTest, VerifyPingbackContent) {
149 Init(); 160 Init();
150 EXPECT_FALSE(factory()->GetFetcherByID(0)); 161 EXPECT_FALSE(factory()->GetFetcherByID(0));
151 pingback_client()->OverrideRandom(true, 0.5f); 162 pingback_client()->OverrideRandom(true, 0.5f);
152 pingback_client()->SetPingbackReportingFraction(1.0f); 163 pingback_client()->SetPingbackReportingFraction(1.0f);
153 base::Time current_time = base::Time::UnixEpoch(); 164 base::Time current_time = base::Time::UnixEpoch();
154 pingback_client()->set_current_time(current_time); 165 pingback_client()->set_current_time(current_time);
155 CreateAndSendPingback(); 166 uint64_t data_page_id = page_id();
167 CreateAndSendPingback(false /* lofi_received */,
168 false /* lite_page_received */,
169 false /* app_background_occurred */);
156 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); 170 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
157 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); 171 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
158 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); 172 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
159 RecordPageloadMetricsRequest batched_request; 173 RecordPageloadMetricsRequest batched_request;
160 batched_request.ParseFromString(test_fetcher->upload_data()); 174 batched_request.ParseFromString(test_fetcher->upload_data());
161 EXPECT_EQ(batched_request.pageloads_size(), 1); 175 EXPECT_EQ(batched_request.pageloads_size(), 1);
162 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime( 176 EXPECT_EQ(current_time, protobuf_parser::TimestampToTime(
163 batched_request.metrics_sent_time())); 177 batched_request.metrics_sent_time()));
164 PageloadMetrics pageload_metrics = batched_request.pageloads(0); 178 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
165 EXPECT_EQ( 179 EXPECT_EQ(
(...skipping 18 matching lines...) Expand all
184 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), 198 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(),
185 protobuf_parser::DurationToTimeDelta( 199 protobuf_parser::DurationToTimeDelta(
186 pageload_metrics.parse_blocked_on_script_load_duration())); 200 pageload_metrics.parse_blocked_on_script_load_duration()));
187 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( 201 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta(
188 pageload_metrics.parse_stop())); 202 pageload_metrics.parse_stop()));
189 203
190 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); 204 EXPECT_EQ(kSessionKey, pageload_metrics.session_key());
191 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); 205 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url());
192 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); 206 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes());
193 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); 207 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes());
194 EXPECT_EQ(kPageId, pageload_metrics.page_id()); 208 EXPECT_EQ(data_page_id, pageload_metrics.page_id());
209
210 EXPECT_EQ(PageloadMetrics_PreviewsType_NONE,
211 pageload_metrics.previews_type());
212 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_UNKNOWN,
213 pageload_metrics.previews_opt_out());
195 214
196 EXPECT_EQ( 215 EXPECT_EQ(
197 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLINE, 216 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLINE,
198 pageload_metrics.effective_connection_type()); 217 pageload_metrics.effective_connection_type());
199 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 218 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
200 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); 219 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1);
201 EXPECT_FALSE(factory()->GetFetcherByID(0)); 220 EXPECT_FALSE(factory()->GetFetcherByID(0));
202 } 221 }
203 222
204 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoPingbacksBatchedContent) { 223 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoPingbacksBatchedContent) {
205 Init(); 224 Init();
206 EXPECT_FALSE(factory()->GetFetcherByID(0)); 225 EXPECT_FALSE(factory()->GetFetcherByID(0));
207 pingback_client()->OverrideRandom(true, 0.5f); 226 pingback_client()->OverrideRandom(true, 0.5f);
208 pingback_client()->SetPingbackReportingFraction(1.0f); 227 pingback_client()->SetPingbackReportingFraction(1.0f);
209 base::Time current_time = base::Time::UnixEpoch(); 228 base::Time current_time = base::Time::UnixEpoch();
210 pingback_client()->set_current_time(current_time); 229 pingback_client()->set_current_time(current_time);
211 // First pingback 230 // First pingback
212 CreateAndSendPingback(); 231 CreateAndSendPingback(false /* lofi_received */,
232 false /* lite_page_received */,
233 false /* app_background_occurred */);
213 234
214 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); 235 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
215 // Two more pingbacks batched together. 236 // Two more pingbacks batched together.
216 CreateAndSendPingback(); 237 std::list<uint64_t> page_ids;
238 page_ids.push_back(page_id());
239 CreateAndSendPingback(false /* lofi_received */,
240 false /* lite_page_received */,
241 false /* app_background_occurred */);
217 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); 242 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2);
218 CreateAndSendPingback(); 243 page_ids.push_back(page_id());
244 CreateAndSendPingback(false /* lofi_received */,
245 false /* lite_page_received */,
246 false /* app_background_occurred */);
219 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 3); 247 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 3);
220 248
221 // Ignore the first pingback. 249 // Ignore the first pingback.
222 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); 250 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
223 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 251 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
224 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); 252 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1);
225 253
226 // Check the state of the second pingback. 254 // Check the state of the second pingback.
227 test_fetcher = factory()->GetFetcherByID(0); 255 test_fetcher = factory()->GetFetcherByID(0);
228 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf"); 256 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
(...skipping 28 matching lines...) Expand all
257 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(), 285 EXPECT_EQ(timing().parse_blocked_on_script_load_duration.value(),
258 protobuf_parser::DurationToTimeDelta( 286 protobuf_parser::DurationToTimeDelta(
259 pageload_metrics.parse_blocked_on_script_load_duration())); 287 pageload_metrics.parse_blocked_on_script_load_duration()));
260 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta( 288 EXPECT_EQ(timing().parse_stop.value(), protobuf_parser::DurationToTimeDelta(
261 pageload_metrics.parse_stop())); 289 pageload_metrics.parse_stop()));
262 290
263 EXPECT_EQ(kSessionKey, pageload_metrics.session_key()); 291 EXPECT_EQ(kSessionKey, pageload_metrics.session_key());
264 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url()); 292 EXPECT_EQ(kFakeURL, pageload_metrics.first_request_url());
265 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes()); 293 EXPECT_EQ(kBytes, pageload_metrics.compressed_page_size_bytes());
266 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes()); 294 EXPECT_EQ(kBytesOriginal, pageload_metrics.original_page_size_bytes());
267 EXPECT_EQ(kPageId, pageload_metrics.page_id()); 295 EXPECT_EQ(page_ids.front(), pageload_metrics.page_id());
296 page_ids.pop_front();
268 EXPECT_EQ( 297 EXPECT_EQ(
269 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLIN E, 298 PageloadMetrics_EffectiveConnectionType_EFFECTIVE_CONNECTION_TYPE_OFFLIN E,
270 pageload_metrics.effective_connection_type()); 299 pageload_metrics.effective_connection_type());
271 } 300 }
272 301
273 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 302 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
274 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); 303 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2);
275 EXPECT_FALSE(factory()->GetFetcherByID(0)); 304 EXPECT_FALSE(factory()->GetFetcherByID(0));
276 } 305 }
277 306
278 TEST_F(DataReductionProxyPingbackClientTest, SendTwoPingbacks) { 307 TEST_F(DataReductionProxyPingbackClientTest, SendTwoPingbacks) {
279 Init(); 308 Init();
280 EXPECT_FALSE(factory()->GetFetcherByID(0)); 309 EXPECT_FALSE(factory()->GetFetcherByID(0));
281 pingback_client()->OverrideRandom(true, 0.5f); 310 pingback_client()->OverrideRandom(true, 0.5f);
282 pingback_client()->SetPingbackReportingFraction(1.0f); 311 pingback_client()->SetPingbackReportingFraction(1.0f);
283 CreateAndSendPingback(); 312 CreateAndSendPingback(false /* lofi_received */,
313 false /* lite_page_received */,
314 false /* app_background_occurred */);
284 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); 315 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
285 CreateAndSendPingback(); 316 CreateAndSendPingback(false /* lofi_received */,
317 false /* lite_page_received */,
318 false /* app_background_occurred */);
286 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2); 319 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2);
287 320
288 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); 321 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
289 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 322 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
290 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); 323 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1);
291 EXPECT_TRUE(factory()->GetFetcherByID(0)); 324 EXPECT_TRUE(factory()->GetFetcherByID(0));
292 test_fetcher = factory()->GetFetcherByID(0); 325 test_fetcher = factory()->GetFetcherByID(0);
293 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 326 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
294 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); 327 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2);
295 EXPECT_FALSE(factory()->GetFetcherByID(0)); 328 EXPECT_FALSE(factory()->GetFetcherByID(0));
296 histogram_tester().ExpectTotalCount(kHistogramAttempted, 2); 329 histogram_tester().ExpectTotalCount(kHistogramAttempted, 2);
297 } 330 }
298 331
299 TEST_F(DataReductionProxyPingbackClientTest, NoPingbackSent) { 332 TEST_F(DataReductionProxyPingbackClientTest, NoPingbackSent) {
300 Init(); 333 Init();
301 EXPECT_FALSE(factory()->GetFetcherByID(0)); 334 EXPECT_FALSE(factory()->GetFetcherByID(0));
302 pingback_client()->OverrideRandom(true, 0.5f); 335 pingback_client()->OverrideRandom(true, 0.5f);
303 pingback_client()->SetPingbackReportingFraction(0.0f); 336 pingback_client()->SetPingbackReportingFraction(0.0f);
304 CreateAndSendPingback(); 337 CreateAndSendPingback(false /* lofi_received */,
338 false /* lite_page_received */,
339 false /* app_background_occurred */);
305 histogram_tester().ExpectUniqueSample(kHistogramAttempted, false, 1); 340 histogram_tester().ExpectUniqueSample(kHistogramAttempted, false, 1);
306 histogram_tester().ExpectTotalCount(kHistogramSucceeded, 0); 341 histogram_tester().ExpectTotalCount(kHistogramSucceeded, 0);
307 EXPECT_FALSE(factory()->GetFetcherByID(0)); 342 EXPECT_FALSE(factory()->GetFetcherByID(0));
308 } 343 }
309 344
310 TEST_F(DataReductionProxyPingbackClientTest, VerifyReportingBehvaior) { 345 TEST_F(DataReductionProxyPingbackClientTest, VerifyReportingBehvaior) {
311 Init(); 346 Init();
312 EXPECT_FALSE(factory()->GetFetcherByID(0)); 347 EXPECT_FALSE(factory()->GetFetcherByID(0));
313 348
314 // Verify that if the random number is less than the reporting fraction, the 349 // Verify that if the random number is less than the reporting fraction, the
315 // pingback is created. 350 // pingback is created.
316 pingback_client()->SetPingbackReportingFraction(0.5f); 351 pingback_client()->SetPingbackReportingFraction(0.5f);
317 pingback_client()->OverrideRandom(true, 0.4f); 352 pingback_client()->OverrideRandom(true, 0.4f);
318 CreateAndSendPingback(); 353 CreateAndSendPingback(false /* lofi_received */,
354 false /* lite_page_received */,
355 false /* app_background_occurred */);
319 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); 356 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
320 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); 357 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
321 EXPECT_TRUE(test_fetcher); 358 EXPECT_TRUE(test_fetcher);
322 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 359 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
323 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1); 360 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 1);
324 361
325 // Verify that if the random number is greater than the reporting fraction, 362 // Verify that if the random number is greater than the reporting fraction,
326 // the pingback is not created. 363 // the pingback is not created.
327 pingback_client()->OverrideRandom(true, 0.6f); 364 pingback_client()->OverrideRandom(true, 0.6f);
328 CreateAndSendPingback(); 365 CreateAndSendPingback(false /* lofi_received */,
366 false /* lite_page_received */,
367 false /* app_background_occurred */);
329 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 1); 368 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 1);
330 test_fetcher = factory()->GetFetcherByID(0); 369 test_fetcher = factory()->GetFetcherByID(0);
331 EXPECT_FALSE(test_fetcher); 370 EXPECT_FALSE(test_fetcher);
332 371
333 // Verify that if the random number is equal to the reporting fraction, the 372 // 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, 373 // pingback is not created. Specifically, if the reporting fraction is zero,
335 // and the random number is zero, no pingback is sent. 374 // and the random number is zero, no pingback is sent.
336 pingback_client()->SetPingbackReportingFraction(0.0f); 375 pingback_client()->SetPingbackReportingFraction(0.0f);
337 pingback_client()->OverrideRandom(true, 0.0f); 376 pingback_client()->OverrideRandom(true, 0.0f);
338 CreateAndSendPingback(); 377 CreateAndSendPingback(false /* lofi_received */,
378 false /* lite_page_received */,
379 false /* app_background_occurred */);
339 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 2); 380 histogram_tester().ExpectBucketCount(kHistogramAttempted, false, 2);
340 test_fetcher = factory()->GetFetcherByID(0); 381 test_fetcher = factory()->GetFetcherByID(0);
341 EXPECT_FALSE(test_fetcher); 382 EXPECT_FALSE(test_fetcher);
342 383
343 // Verify that the command line flag forces a pingback. 384 // Verify that the command line flag forces a pingback.
344 base::CommandLine::ForCurrentProcess()->AppendSwitch( 385 base::CommandLine::ForCurrentProcess()->AppendSwitch(
345 data_reduction_proxy::switches::kEnableDataReductionProxyForcePingback); 386 data_reduction_proxy::switches::kEnableDataReductionProxyForcePingback);
346 pingback_client()->SetPingbackReportingFraction(0.0f); 387 pingback_client()->SetPingbackReportingFraction(0.0f);
347 pingback_client()->OverrideRandom(true, 1.0f); 388 pingback_client()->OverrideRandom(true, 1.0f);
348 CreateAndSendPingback(); 389 CreateAndSendPingback(false /* lofi_received */,
390 false /* lite_page_received */,
391 false /* app_background_occurred */);
349 histogram_tester().ExpectBucketCount(kHistogramAttempted, true, 2); 392 histogram_tester().ExpectBucketCount(kHistogramAttempted, true, 2);
350 test_fetcher = factory()->GetFetcherByID(0); 393 test_fetcher = factory()->GetFetcherByID(0);
351 EXPECT_TRUE(test_fetcher); 394 EXPECT_TRUE(test_fetcher);
352 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 395 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
353 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2); 396 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, true, 2);
354 } 397 }
355 398
356 TEST_F(DataReductionProxyPingbackClientTest, FailedPingback) { 399 TEST_F(DataReductionProxyPingbackClientTest, FailedPingback) {
357 Init(); 400 Init();
358 EXPECT_FALSE(factory()->GetFetcherByID(0)); 401 EXPECT_FALSE(factory()->GetFetcherByID(0));
359 pingback_client()->OverrideRandom(true, 0.5f); 402 pingback_client()->OverrideRandom(true, 0.5f);
360 pingback_client()->SetPingbackReportingFraction(1.0f); 403 pingback_client()->SetPingbackReportingFraction(1.0f);
361 CreateAndSendPingback(); 404 CreateAndSendPingback(false /* lofi_received */,
405 false /* lite_page_received */,
406 false /* app_background_occurred */);
362 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1); 407 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
363 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0); 408 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
364 EXPECT_TRUE(test_fetcher); 409 EXPECT_TRUE(test_fetcher);
365 // Simulate a network error. 410 // Simulate a network error.
366 test_fetcher->set_status(net::URLRequestStatus( 411 test_fetcher->set_status(net::URLRequestStatus(
367 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS)); 412 net::URLRequestStatus::FAILED, net::ERR_INVALID_AUTH_CREDENTIALS));
368 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher); 413 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
369 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1); 414 histogram_tester().ExpectUniqueSample(kHistogramSucceeded, false, 1);
370 } 415 }
371 416
417 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentNoOptOut) {
418 Init();
419 EXPECT_FALSE(factory()->GetFetcherByID(0));
420 pingback_client()->OverrideRandom(true, 0.5f);
421 pingback_client()->SetPingbackReportingFraction(1.0f);
422 base::Time current_time = base::Time::UnixEpoch();
423 pingback_client()->set_current_time(current_time);
424 CreateAndSendPingback(true /* lofi_received */,
425 false /* lite_page_received */,
426 false /* app_background_occurred */);
427 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
428 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
429 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
430 RecordPageloadMetricsRequest batched_request;
431 batched_request.ParseFromString(test_fetcher->upload_data());
432 EXPECT_EQ(batched_request.pageloads_size(), 1);
433 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
434 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI,
435 pageload_metrics.previews_type());
436 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_NON_OPT_OUT,
437 pageload_metrics.previews_opt_out());
438
439 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
440 EXPECT_FALSE(factory()->GetFetcherByID(0));
441 }
442
443 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentOptOut) {
444 Init();
445 EXPECT_FALSE(factory()->GetFetcherByID(0));
446 pingback_client()->OverrideRandom(true, 0.5f);
447 pingback_client()->SetPingbackReportingFraction(1.0f);
448 base::Time current_time = base::Time::UnixEpoch();
449 pingback_client()->set_current_time(current_time);
450 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
451 CreateAndSendPingback(true /* lofi_received */,
452 false /* lite_page_received */,
453 false /* app_background_occurred */);
454 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
455 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
456 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
457 RecordPageloadMetricsRequest batched_request;
458 batched_request.ParseFromString(test_fetcher->upload_data());
459 EXPECT_EQ(batched_request.pageloads_size(), 1);
460 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
461 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI,
462 pageload_metrics.previews_type());
463 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT,
464 pageload_metrics.previews_opt_out());
465
466 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
467 EXPECT_FALSE(factory()->GetFetcherByID(0));
468 }
469
470 TEST_F(DataReductionProxyPingbackClientTest, VerifyLoFiContentBackground) {
471 Init();
472 EXPECT_FALSE(factory()->GetFetcherByID(0));
473 pingback_client()->OverrideRandom(true, 0.5f);
474 pingback_client()->SetPingbackReportingFraction(1.0f);
475 base::Time current_time = base::Time::UnixEpoch();
476 pingback_client()->set_current_time(current_time);
477 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
478 CreateAndSendPingback(true /* lofi_received */,
479 false /* lite_page_received */,
480 true /* app_background_occurred */);
481 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
482 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
483 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
484 RecordPageloadMetricsRequest batched_request;
485 batched_request.ParseFromString(test_fetcher->upload_data());
486 EXPECT_EQ(batched_request.pageloads_size(), 1);
487 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
488 EXPECT_EQ(PageloadMetrics_PreviewsType_LOFI,
489 pageload_metrics.previews_type());
490 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_UNKNOWN,
491 pageload_metrics.previews_opt_out());
492
493 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
494 EXPECT_FALSE(factory()->GetFetcherByID(0));
495 }
496
497 TEST_F(DataReductionProxyPingbackClientTest, VerifyLitePageContent) {
498 Init();
499 EXPECT_FALSE(factory()->GetFetcherByID(0));
500 pingback_client()->OverrideRandom(true, 0.5f);
501 pingback_client()->SetPingbackReportingFraction(1.0f);
502 base::Time current_time = base::Time::UnixEpoch();
503 pingback_client()->set_current_time(current_time);
504 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
505 CreateAndSendPingback(false /* lofi_received */,
506 true /* lite_page_received */,
507 false /* app_background_occurred */);
508 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 1);
509 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
510 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
511 RecordPageloadMetricsRequest batched_request;
512 batched_request.ParseFromString(test_fetcher->upload_data());
513 EXPECT_EQ(batched_request.pageloads_size(), 1);
514 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
515 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE,
516 pageload_metrics.previews_type());
517 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT,
518 pageload_metrics.previews_opt_out());
519
520 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
521 EXPECT_FALSE(factory()->GetFetcherByID(0));
522 }
523
524 TEST_F(DataReductionProxyPingbackClientTest, VerifyTwoLitePagePingbacks) {
525 Init();
526 EXPECT_FALSE(factory()->GetFetcherByID(0));
527 pingback_client()->OverrideRandom(true, 0.5f);
528 pingback_client()->SetPingbackReportingFraction(1.0f);
529 base::Time current_time = base::Time::UnixEpoch();
530 pingback_client()->set_current_time(current_time);
531 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
532 CreateAndSendPingback(false /* lofi_received */,
533 true /* lite_page_received */,
534 false /* app_background_occurred */);
535 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
536 CreateAndSendPingback(false /* lofi_received */,
537 true /* lite_page_received */,
538 false /* app_background_occurred */);
539 histogram_tester().ExpectUniqueSample(kHistogramAttempted, true, 2);
540 net::TestURLFetcher* test_fetcher = factory()->GetFetcherByID(0);
541 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
542 RecordPageloadMetricsRequest batched_request;
543 batched_request.ParseFromString(test_fetcher->upload_data());
544 EXPECT_EQ(batched_request.pageloads_size(), 1);
545 PageloadMetrics pageload_metrics = batched_request.pageloads(0);
546 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE,
547 pageload_metrics.previews_type());
548 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT,
549 pageload_metrics.previews_opt_out());
550 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
551 test_fetcher = factory()->GetFetcherByID(0);
552 EXPECT_EQ(test_fetcher->upload_content_type(), "application/x-protobuf");
553 batched_request.ParseFromString(test_fetcher->upload_data());
554 EXPECT_EQ(batched_request.pageloads_size(), 1);
555 pageload_metrics = batched_request.pageloads(0);
556 EXPECT_EQ(PageloadMetrics_PreviewsType_LITE_PAGE,
557 pageload_metrics.previews_type());
558 EXPECT_EQ(PageloadMetrics_PreviewsOptOut_OPT_OUT,
559 pageload_metrics.previews_opt_out());
560 test_fetcher->delegate()->OnURLFetchComplete(test_fetcher);
561 EXPECT_FALSE(factory()->GetFetcherByID(0));
562 }
563
564 TEST_F(DataReductionProxyPingbackClientTest, VerifyClearingPendingLoads) {
565 Init();
566 EXPECT_FALSE(factory()->GetFetcherByID(0));
567 pingback_client()->OverrideRandom(true, 0.5f);
568 pingback_client()->SetPingbackReportingFraction(1.0f);
569 base::Time current_time = base::Time::UnixEpoch();
570 pingback_client()->set_current_time(current_time);
571 pingback_client()->AddOptOut(NavigationID(page_id(), kSessionKey));
572 EXPECT_EQ(1u, pingback_client()->OptOutsSizeForTesting());
573 pingback_client()->ClearNavigationKeyAsync(
574 NavigationID(page_id(), kSessionKey));
575 base::RunLoop().RunUntilIdle();
576 EXPECT_EQ(0u, pingback_client()->OptOutsSizeForTesting());
577 }
578
372 } // namespace data_reduction_proxy 579 } // namespace data_reduction_proxy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698