OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
tbansal1
2017/07/12 21:58:45
2017
RyanSturm
2017/07/17 18:48:30
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/page_load_metrics/observers/previews_ukm_observer.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/metrics/metrics_hashes.h" | |
9 #include "base/optional.h" | |
10 #include "chrome/browser/loader/chrome_navigation_data.h" | |
11 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_ test_harness.h" | |
12 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" | |
13 #include "chrome/browser/previews/previews_infobar_delegate.h" | |
14 #include "chrome/test/base/testing_browser_process.h" | |
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h" | |
16 #include "components/ukm/test_ukm_recorder.h" | |
17 #include "components/ukm/ukm_source.h" | |
18 | |
19 namespace previews { | |
20 | |
21 namespace { | |
22 | |
23 const char kDefaultTestUrl[] = "https://www.google.com/"; | |
24 | |
25 data_reduction_proxy::DataReductionProxyData* DataForNavigationHandle( | |
26 content::WebContents* web_contents, | |
27 content::NavigationHandle* navigation_handle) { | |
28 ChromeNavigationData* chrome_navigation_data = new ChromeNavigationData(); | |
29 content::WebContentsTester::For(web_contents) | |
30 ->SetNavigationData(navigation_handle, | |
31 base::WrapUnique(chrome_navigation_data)); | |
32 data_reduction_proxy::DataReductionProxyData* data = | |
33 new data_reduction_proxy::DataReductionProxyData(); | |
34 chrome_navigation_data->SetDataReductionProxyData(base::WrapUnique(data)); | |
35 | |
36 return data; | |
37 } | |
38 | |
39 class TestPreviewsUKMObserver : public PreviewsUKMObserver { | |
40 public: | |
41 TestPreviewsUKMObserver(content::WebContents* web_contents, | |
42 bool data_reduction_proxy_used, | |
43 bool lite_page_received) | |
44 : web_contents_(web_contents), | |
45 data_reduction_proxy_used_(data_reduction_proxy_used), | |
46 lite_page_received_(lite_page_received) {} | |
47 | |
48 ~TestPreviewsUKMObserver() override {} | |
49 | |
50 // page_load_metrics::PageLoadMetricsObserver implementation: | |
51 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, | |
52 ukm::SourceId source_id) override { | |
53 data_reduction_proxy::DataReductionProxyData* data = | |
54 DataForNavigationHandle(web_contents_, navigation_handle); | |
55 data->set_used_data_reduction_proxy(data_reduction_proxy_used_); | |
56 data->set_request_url(GURL(kDefaultTestUrl)); | |
57 data->set_lite_page_received(lite_page_received_); | |
58 return PreviewsUKMObserver::OnCommit(navigation_handle, source_id); | |
59 } | |
60 | |
61 private: | |
62 content::WebContents* web_contents_; | |
63 bool data_reduction_proxy_used_; | |
64 bool lite_page_received_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(TestPreviewsUKMObserver); | |
67 }; | |
68 | |
69 class PreviewsUKMObserverTest | |
70 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | |
71 public: | |
72 PreviewsUKMObserverTest() {} | |
73 ~PreviewsUKMObserverTest() override {} | |
74 | |
75 void RunTest(bool data_reduction_proxy_used, bool lite_page_received) { | |
76 data_reduction_proxy_used_ = data_reduction_proxy_used; | |
77 lite_page_received_ = lite_page_received; | |
78 NavigateAndCommit(GURL(kDefaultTestUrl)); | |
79 } | |
80 | |
81 void ValidateUKM(bool server_lofi_expected, | |
82 bool client_lofi_expected, | |
83 bool lite_page_expected, | |
84 bool opt_out_expected) { | |
85 const ukm::UkmSource* source = | |
86 test_ukm_recorder().GetSourceForUrl(kDefaultTestUrl); | |
87 if (!server_lofi_expected && !client_lofi_expected && !lite_page_expected && | |
88 !opt_out_expected) { | |
89 EXPECT_EQ(0u, test_ukm_recorder().entries_count()); | |
90 return; | |
91 } | |
92 | |
93 EXPECT_EQ(server_lofi_expected, test_ukm_recorder().HasMetric( | |
94 *source, "Previews", "server_lofi")); | |
95 EXPECT_EQ(client_lofi_expected, test_ukm_recorder().HasMetric( | |
96 *source, "Previews", "client_lofi")); | |
97 EXPECT_EQ(lite_page_expected, | |
98 test_ukm_recorder().HasMetric(*source, "Previews", "lite_page")); | |
99 EXPECT_EQ(opt_out_expected, | |
100 test_ukm_recorder().HasMetric(*source, "Previews", "opt_out")); | |
101 } | |
102 | |
103 protected: | |
104 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | |
105 tracker->AddObserver(base::MakeUnique<TestPreviewsUKMObserver>( | |
106 web_contents(), data_reduction_proxy_used_, lite_page_received_)); | |
107 // Data is only added to the first navigation after RunTest(). | |
108 data_reduction_proxy_used_ = false; | |
109 lite_page_received_ = false; | |
110 } | |
111 | |
112 private: | |
113 bool data_reduction_proxy_used_ = false; | |
114 bool lite_page_received_ = false; | |
115 | |
116 DISALLOW_COPY_AND_ASSIGN(PreviewsUKMObserverTest); | |
117 }; | |
118 | |
119 TEST_F(PreviewsUKMObserverTest, NoPreviewSeen) { | |
120 RunTest(false /* data_reduction_proxy_used */, | |
121 false /* lite_page_received */); | |
122 NavigateToUntrackedUrl(); | |
123 | |
124 ValidateUKM(false /* server_lofi_expected */, | |
125 false /* client_lofi_expected */, false /* lite_page_expected */, | |
126 false /* opt_out_expected */); | |
127 } | |
128 | |
129 TEST_F(PreviewsUKMObserverTest, UntrackedPreviewTypeOptOut) { | |
130 RunTest(false /* data_reduction_proxy_used */, | |
131 false /* lite_page_received */); | |
132 observer()->BroadcastEventToObservers( | |
133 PreviewsInfoBarDelegate::OptOutEventKey()); | |
134 NavigateToUntrackedUrl(); | |
135 | |
136 // Opt out should not be added sicne we don't track this type. | |
137 ValidateUKM(false /* server_lofi_expected */, | |
138 false /* client_lofi_expected */, false /* lite_page_expected */, | |
139 false /* opt_out_expected */); | |
140 } | |
141 | |
142 TEST_F(PreviewsUKMObserverTest, LitePageSeen) { | |
143 RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */); | |
144 | |
145 NavigateToUntrackedUrl(); | |
146 | |
147 ValidateUKM(false /* server_lofi_expected */, | |
148 false /* client_lofi_expected */, true /* lite_page_expected */, | |
149 false /* opt_out_expected */); | |
150 } | |
151 | |
152 TEST_F(PreviewsUKMObserverTest, LitePageOptOut) { | |
153 RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */); | |
154 | |
155 observer()->BroadcastEventToObservers( | |
156 PreviewsInfoBarDelegate::OptOutEventKey()); | |
157 NavigateToUntrackedUrl(); | |
158 | |
159 ValidateUKM(false /* server_lofi_expected */, | |
160 false /* client_lofi_expected */, true /* lite_page_expected */, | |
161 true /* opt_out_expected */); | |
162 } | |
163 | |
164 TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) { | |
165 RunTest(false /* data_reduction_proxy_used */, | |
166 false /* lite_page_received */); | |
167 | |
168 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
169 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
170 data->set_client_lofi_requested(true); | |
171 | |
172 // Prepare 3 resources of varying size and configurations, 2 of which have | |
173 // client LoFi set. | |
174 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
175 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
176 1024 * 40 /* raw_body_bytes */, | |
177 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
178 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
179 // Uncached non-proxied request. | |
180 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
181 1024 * 40 /* raw_body_bytes */, | |
182 1024 * 40 /* original_network_content_length */, | |
183 nullptr /* data_reduction_proxy_data */, | |
184 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
185 }; | |
186 | |
187 for (const auto& request : resources) | |
188 SimulateLoadedResource(request); | |
189 | |
190 NavigateToUntrackedUrl(); | |
191 | |
192 ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */, | |
193 false /* lite_page_expected */, false /* opt_out_expected */); | |
194 } | |
195 | |
196 TEST_F(PreviewsUKMObserverTest, ClientLoFiOptOut) { | |
197 RunTest(false /* data_reduction_proxy_used */, | |
198 false /* lite_page_received */); | |
199 | |
200 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
201 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
202 data->set_client_lofi_requested(true); | |
203 | |
204 // Prepare 3 resources of varying size and configurations, 2 of which have | |
205 // client LoFi set. | |
206 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
207 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
208 1024 * 40 /* raw_body_bytes */, | |
209 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
210 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
211 // Uncached non-proxied request. | |
212 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
213 1024 * 40 /* raw_body_bytes */, | |
214 1024 * 40 /* original_network_content_length */, | |
215 nullptr /* data_reduction_proxy_data */, | |
216 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
217 }; | |
218 | |
219 for (const auto& request : resources) | |
220 SimulateLoadedResource(request); | |
221 observer()->BroadcastEventToObservers( | |
222 PreviewsInfoBarDelegate::OptOutEventKey()); | |
223 NavigateToUntrackedUrl(); | |
224 | |
225 ValidateUKM(false /* server_lofi_expected */, true /* client_lofi_expected */, | |
226 false /* lite_page_expected */, true /* opt_out_expected */); | |
227 } | |
228 | |
229 TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) { | |
230 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
231 | |
232 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
233 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
234 data->set_used_data_reduction_proxy(true); | |
235 data->set_lofi_received(true); | |
236 | |
237 // Prepare 3 resources of varying size and configurations, 2 of which have | |
238 // client LoFi set. | |
239 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
240 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
241 1024 * 40 /* raw_body_bytes */, | |
242 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
243 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
244 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
245 1024 * 40 /* raw_body_bytes */, | |
246 1024 * 40 /* original_network_content_length */, | |
247 nullptr /* data_reduction_proxy_data */, | |
248 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
249 }; | |
250 | |
251 for (const auto& request : resources) | |
252 SimulateLoadedResource(request); | |
253 | |
254 NavigateToUntrackedUrl(); | |
255 | |
256 ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */, | |
257 false /* lite_page_expected */, false /* opt_out_expected */); | |
258 } | |
259 | |
260 TEST_F(PreviewsUKMObserverTest, ServerLoFiOptOut) { | |
261 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
262 | |
263 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
264 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
265 data->set_used_data_reduction_proxy(true); | |
266 data->set_lofi_received(true); | |
267 | |
268 // Prepare 3 resources of varying size and configurations, 2 of which have | |
269 // client LoFi set. | |
270 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
271 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
272 1024 * 40 /* raw_body_bytes */, | |
273 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
274 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
275 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
276 1024 * 40 /* raw_body_bytes */, | |
277 1024 * 40 /* original_network_content_length */, | |
278 nullptr /* data_reduction_proxy_data */, | |
279 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
280 }; | |
281 | |
282 for (const auto& request : resources) | |
283 SimulateLoadedResource(request); | |
284 | |
285 observer()->BroadcastEventToObservers( | |
286 PreviewsInfoBarDelegate::OptOutEventKey()); | |
287 NavigateToUntrackedUrl(); | |
288 | |
289 ValidateUKM(true /* server_lofi_expected */, false /* client_lofi_expected */, | |
290 false /* lite_page_expected */, true /* opt_out_expected */); | |
291 } | |
292 | |
293 TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) { | |
294 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
295 | |
296 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 = | |
297 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
298 data1->set_used_data_reduction_proxy(true); | |
299 data1->set_lofi_received(true); | |
300 | |
301 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data2 = | |
302 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
303 data2->set_used_data_reduction_proxy(true); | |
304 data2->set_client_lofi_requested(true); | |
305 | |
306 // Prepare 4 resources of varying size and configurations, 1 has Client LoFi, | |
307 // 1 has Server LoFi. | |
308 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
309 // Uncached proxied request with .1 compression ratio. | |
310 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
311 1024 * 40 /* raw_body_bytes */, | |
312 1024 * 40 * 10 /* original_network_content_length */, std::move(data1), | |
313 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
314 // Uncached proxied request with .5 compression ratio. | |
315 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
316 1024 * 40 /* raw_body_bytes */, | |
317 1024 * 40 * 5 /* original_network_content_length */, std::move(data2), | |
318 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
319 }; | |
320 | |
321 for (const auto& request : resources) | |
322 SimulateLoadedResource(request); | |
323 | |
324 NavigateToUntrackedUrl(); | |
325 ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */, | |
326 false /* lite_page_expected */, false /* opt_out_expected */); | |
327 } | |
328 | |
329 TEST_F(PreviewsUKMObserverTest, BothLoFiOptOut) { | |
330 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
331 | |
332 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 = | |
333 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
334 data1->set_used_data_reduction_proxy(true); | |
335 data1->set_lofi_received(true); | |
336 | |
337 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data2 = | |
338 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
339 data2->set_used_data_reduction_proxy(true); | |
340 data2->set_client_lofi_requested(true); | |
341 | |
342 // Prepare 4 resources of varying size and configurations, 1 has Client LoFi, | |
343 // 1 has Server LoFi. | |
344 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
345 // Uncached proxied request with .1 compression ratio. | |
346 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
347 1024 * 40 /* raw_body_bytes */, | |
348 1024 * 40 * 10 /* original_network_content_length */, std::move(data1), | |
349 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
350 // Uncached proxied request with .5 compression ratio. | |
351 {GURL(kDefaultTestUrl), net::HostPortPair(), -1, false /*was_cached*/, | |
352 1024 * 40 /* raw_body_bytes */, | |
353 1024 * 40 * 5 /* original_network_content_length */, std::move(data2), | |
354 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
355 }; | |
356 | |
357 for (const auto& request : resources) | |
358 SimulateLoadedResource(request); | |
359 observer()->BroadcastEventToObservers( | |
360 PreviewsInfoBarDelegate::OptOutEventKey()); | |
361 NavigateToUntrackedUrl(); | |
362 ValidateUKM(true /* server_lofi_expected */, true /* client_lofi_expected */, | |
363 false /* lite_page_expected */, true /* opt_out_expected */); | |
364 } | |
365 | |
366 } // namespace | |
367 | |
368 } // namespace previews | |
OLD | NEW |