OLD | NEW |
---|---|
(Empty) | |
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 | |
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/test/base/testing_browser_process.h" | |
14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data .h" | |
15 #include "components/ukm/test_ukm_recorder.h" | |
16 #include "components/ukm/ukm_source.h" | |
17 | |
18 namespace previews { | |
19 | |
20 namespace { | |
21 | |
22 const char kDefaultTestUrl[] = "https://www.google.com"; | |
23 | |
24 data_reduction_proxy::DataReductionProxyData* DataForNavigationHandle( | |
25 content::WebContents* web_contents, | |
26 content::NavigationHandle* navigation_handle) { | |
27 ChromeNavigationData* chrome_navigation_data = new ChromeNavigationData(); | |
28 content::WebContentsTester::For(web_contents) | |
29 ->SetNavigationData(navigation_handle, | |
30 base::WrapUnique(chrome_navigation_data)); | |
31 data_reduction_proxy::DataReductionProxyData* data = | |
32 new data_reduction_proxy::DataReductionProxyData(); | |
33 chrome_navigation_data->SetDataReductionProxyData(base::WrapUnique(data)); | |
34 | |
35 return data; | |
36 } | |
37 | |
38 class TestPreviewsUKMObserver : public PreviewsUKMObserver { | |
39 public: | |
40 TestPreviewsUKMObserver(content::WebContents* web_contents, | |
41 bool data_reduction_proxy_used, | |
42 bool lite_page_received) | |
43 : web_contents_(web_contents), | |
44 data_reduction_proxy_used_(data_reduction_proxy_used), | |
45 lite_page_received_(lite_page_received) {} | |
46 | |
47 ~TestPreviewsUKMObserver() override {} | |
48 | |
49 // page_load_metrics::PageLoadMetricsObserver implementation: | |
50 ObservePolicy OnCommit(content::NavigationHandle* navigation_handle, | |
51 ukm::SourceId source_id) override { | |
52 data_reduction_proxy::DataReductionProxyData* data = | |
53 DataForNavigationHandle(web_contents_, navigation_handle); | |
54 data->set_used_data_reduction_proxy(data_reduction_proxy_used_); | |
55 data->set_request_url(GURL(kDefaultTestUrl)); | |
56 data->set_lite_page_received(lite_page_received_); | |
57 return PreviewsUKMObserver::OnCommit(navigation_handle, source_id); | |
58 } | |
59 | |
60 private: | |
61 content::WebContents* web_contents_; | |
62 bool data_reduction_proxy_used_; | |
63 bool lite_page_received_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(TestPreviewsUKMObserver); | |
66 }; | |
67 | |
68 class PreviewsUKMObserverTest | |
69 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | |
70 public: | |
71 PreviewsUKMObserverTest() {} | |
72 ~PreviewsUKMObserverTest() override {} | |
73 | |
74 void SetUp() override { | |
75 page_load_metrics::PageLoadMetricsObserverTestHarness::SetUp(); | |
76 TestingBrowserProcess::GetGlobal()->SetUkmRecorder(&test_ukm_recorder_); | |
77 } | |
78 | |
79 void RunTest(bool data_reduction_proxy_used, bool lite_page_received) { | |
80 data_reduction_proxy_used_ = data_reduction_proxy_used; | |
81 lite_page_received_ = lite_page_received; | |
82 NavigateAndCommit(GURL(kDefaultTestUrl)); | |
83 } | |
84 | |
85 void ValidateUKM(int expected_value, ukm::SourceId source_id) { | |
86 ukm::mojom::UkmEntryPtr entry = GetMergedEntryForSourceID(source_id); | |
87 const ukm::mojom::UkmMetric* metric = | |
88 ukm::TestUkmRecorder::FindMetric(entry.get(), "previews_type"); | |
89 if (expected_value == PREVIEWS_UKM_NONE) { | |
90 EXPECT_FALSE(metric); | |
91 } else { | |
92 EXPECT_EQ(entry->source_id, source_id); | |
93 EXPECT_EQ(entry->event_hash, base::HashMetricName("Previews")); | |
94 EXPECT_EQ(expected_value, metric->value); | |
95 } | |
96 } | |
97 | |
98 size_t ukm_source_count() { return test_ukm_recorder_.sources_count(); } | |
Bryan McQuade
2017/07/03 16:56:52
I have a change in flight: https://chromium-review
RyanSturm
2017/07/11 21:46:41
Done.
| |
99 | |
100 size_t ukm_entry_count() { return test_ukm_recorder_.entries_count(); } | |
101 | |
102 const ukm::UkmSource* GetUkmSourceForUrl(const char* url) { | |
103 return test_ukm_recorder_.GetSourceForUrl(url); | |
104 } | |
105 | |
106 const ukm::mojom::UkmEntry* GetUkmEntry(size_t entry_index) { | |
107 return test_ukm_recorder_.GetEntry(entry_index); | |
108 } | |
109 | |
110 std::vector<const ukm::mojom::UkmEntry*> GetUkmEntriesForSourceID( | |
111 ukm::SourceId source_id) { | |
112 std::vector<const ukm::mojom::UkmEntry*> entries; | |
113 for (size_t i = 0; i < ukm_entry_count(); ++i) { | |
114 const ukm::mojom::UkmEntry* entry = GetUkmEntry(i); | |
115 if (entry->source_id == source_id) | |
116 entries.push_back(entry); | |
117 } | |
118 return entries; | |
119 } | |
120 | |
121 ukm::mojom::UkmEntryPtr GetMergedEntry( | |
122 const std::vector<const ukm::mojom::UkmEntry*>& entries) { | |
123 ukm::mojom::UkmEntryPtr merged_entry = ukm::mojom::UkmEntry::New(); | |
124 for (const auto* entry : entries) { | |
125 if (merged_entry->event_hash) { | |
126 EXPECT_EQ(merged_entry->source_id, entry->source_id); | |
127 EXPECT_EQ(merged_entry->event_hash, entry->event_hash); | |
128 } else { | |
129 merged_entry->event_hash = entry->event_hash; | |
130 merged_entry->source_id = entry->source_id; | |
131 } | |
132 for (const auto& metric : entry->metrics) { | |
133 merged_entry->metrics.emplace_back(metric->Clone()); | |
134 } | |
135 } | |
136 return merged_entry; | |
137 } | |
138 | |
139 ukm::mojom::UkmEntryPtr GetMergedEntryForSourceID(ukm::SourceId source_id) { | |
140 ukm::mojom::UkmEntryPtr entry = | |
141 GetMergedEntry(GetUkmEntriesForSourceID(source_id)); | |
142 return entry; | |
143 } | |
144 | |
145 protected: | |
146 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | |
147 tracker->AddObserver(base::MakeUnique<TestPreviewsUKMObserver>( | |
148 web_contents(), data_reduction_proxy_used_, lite_page_received_)); | |
149 // Data is only added to the first navigation after RunTest(). | |
150 data_reduction_proxy_used_ = false; | |
151 lite_page_received_ = false; | |
152 } | |
153 | |
154 private: | |
155 bool data_reduction_proxy_used_ = false; | |
156 bool lite_page_received_ = false; | |
157 | |
158 ukm::TestUkmRecorder test_ukm_recorder_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(PreviewsUKMObserverTest); | |
161 }; | |
162 | |
163 TEST_F(PreviewsUKMObserverTest, NoPreviewSeen) { | |
164 RunTest(false /* data_reduction_proxy_used */, | |
165 false /* lite_page_received */); | |
166 ukm::SourceId source_id = observer()->GetUKMSourceForCommittedLoad().value(); | |
167 NavigateToUntrackedUrl(); | |
168 | |
169 ValidateUKM(PREVIEWS_UKM_NONE, source_id); | |
170 } | |
171 | |
172 TEST_F(PreviewsUKMObserverTest, LitePageSeen) { | |
173 RunTest(true /* data_reduction_proxy_used */, true /* lite_page_received */); | |
174 | |
175 ukm::SourceId source_id = observer()->GetUKMSourceForCommittedLoad().value(); | |
176 | |
177 NavigateToUntrackedUrl(); | |
178 | |
179 ValidateUKM(PREVIEWS_UKM_LITE_PAGE, source_id); | |
180 } | |
181 | |
182 TEST_F(PreviewsUKMObserverTest, ClientLoFiSeen) { | |
183 RunTest(false /* data_reduction_proxy_used */, | |
184 false /* lite_page_received */); | |
185 ukm::SourceId source_id = observer()->GetUKMSourceForCommittedLoad().value(); | |
186 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
187 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
188 data->set_client_lofi_requested(true); | |
189 | |
190 // Prepare 3 resources of varying size and configurations, 2 of which have | |
191 // client LoFi set. | |
192 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
193 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
194 1024 * 40 /* raw_body_bytes */, | |
195 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
196 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
197 // Uncached non-proxied request. | |
198 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
199 1024 * 40 /* raw_body_bytes */, | |
200 1024 * 40 /* original_network_content_length */, | |
201 nullptr /* data_reduction_proxy_data */, | |
202 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
203 }; | |
204 | |
205 for (const auto& request : resources) | |
206 SimulateLoadedResource(request); | |
207 | |
208 NavigateToUntrackedUrl(); | |
209 | |
210 ValidateUKM(PREVIEWS_UKM_CLIENT_LOFI, source_id); | |
211 } | |
212 | |
213 TEST_F(PreviewsUKMObserverTest, ServerLoFiSeen) { | |
214 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
215 ukm::SourceId source_id = observer()->GetUKMSourceForCommittedLoad().value(); | |
216 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data = | |
217 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
218 data->set_used_data_reduction_proxy(true); | |
219 data->set_lofi_received(true); | |
220 | |
221 // Prepare 3 resources of varying size and configurations, 2 of which have | |
222 // client LoFi set. | |
223 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
224 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
225 1024 * 40 /* raw_body_bytes */, | |
226 1024 * 40 * 5 /* original_network_content_length */, std::move(data), | |
227 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
228 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
229 1024 * 40 /* raw_body_bytes */, | |
230 1024 * 40 /* original_network_content_length */, | |
231 nullptr /* data_reduction_proxy_data */, | |
232 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
233 }; | |
234 | |
235 for (const auto& request : resources) | |
236 SimulateLoadedResource(request); | |
237 | |
238 NavigateToUntrackedUrl(); | |
239 | |
240 ValidateUKM(PREVIEWS_UKM_SERVER_LOFI, source_id); | |
241 } | |
242 | |
243 TEST_F(PreviewsUKMObserverTest, BothLoFiSeen) { | |
244 RunTest(true /* data_reduction_proxy_used */, false /* lite_page_received */); | |
245 ukm::SourceId source_id = observer()->GetUKMSourceForCommittedLoad().value(); | |
246 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data1 = | |
247 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
248 data1->set_used_data_reduction_proxy(true); | |
249 data1->set_lofi_received(true); | |
250 | |
251 std::unique_ptr<data_reduction_proxy::DataReductionProxyData> data2 = | |
252 base::MakeUnique<data_reduction_proxy::DataReductionProxyData>(); | |
253 data2->set_used_data_reduction_proxy(true); | |
254 data2->set_client_lofi_requested(true); | |
255 | |
256 // Prepare 4 resources of varying size and configurations, 1 has Client LoFi, | |
257 // 1 has Server LoFi. | |
258 page_load_metrics::ExtraRequestCompleteInfo resources[] = { | |
259 // Uncached proxied request with .1 compression ratio. | |
260 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
261 1024 * 40 /* raw_body_bytes */, | |
262 1024 * 40 * 10 /* original_network_content_length */, std::move(data1), | |
263 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
264 // Uncached proxied request with .5 compression ratio. | |
265 {GURL(), net::HostPortPair(), -1, false /*was_cached*/, | |
266 1024 * 40 /* raw_body_bytes */, | |
267 1024 * 40 * 5 /* original_network_content_length */, std::move(data2), | |
268 content::ResourceType::RESOURCE_TYPE_IMAGE, 0}, | |
269 }; | |
270 | |
271 for (const auto& request : resources) | |
272 SimulateLoadedResource(request); | |
273 | |
274 NavigateToUntrackedUrl(); | |
275 ValidateUKM(PREVIEWS_UKM_SERVER_LOFI | PREVIEWS_UKM_CLIENT_LOFI, source_id); | |
276 } | |
277 | |
278 } // namespace | |
279 | |
280 } // namespace previews | |
OLD | NEW |