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 "chrome/browser/page_load_metrics/observers/service_worker_page_load_me
trics_observer.h" | 5 #include "chrome/browser/page_load_metrics/observers/service_worker_page_load_me
trics_observer.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_
test_harness.h" | 8 #include "chrome/browser/page_load_metrics/observers/page_load_metrics_observer_
test_harness.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 | 11 |
12 const char kDefaultTestUrl[] = "https://google.com"; | 12 const char kDefaultTestUrl[] = "https://google.com"; |
13 const char kInboxTestUrl[] = "https://inbox.google.com/test"; | 13 const char kInboxTestUrl[] = "https://inbox.google.com/test"; |
14 | 14 |
15 } // namespace | 15 } // namespace |
16 | 16 |
17 class ServiceWorkerPageLoadMetricsObserverTest | 17 class ServiceWorkerPageLoadMetricsObserverTest |
18 : public page_load_metrics::PageLoadMetricsObserverTestHarness { | 18 : public page_load_metrics::PageLoadMetricsObserverTestHarness { |
19 protected: | 19 protected: |
20 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { | 20 void RegisterObservers(page_load_metrics::PageLoadTracker* tracker) override { |
21 tracker->AddObserver( | 21 tracker->AddObserver( |
22 base::MakeUnique<ServiceWorkerPageLoadMetricsObserver>()); | 22 base::MakeUnique<ServiceWorkerPageLoadMetricsObserver>()); |
23 } | 23 } |
24 | 24 |
25 void SimulateTimingWithoutPaint() { | 25 void SimulateTimingWithoutPaint() { |
26 page_load_metrics::PageLoadTiming timing; | 26 page_load_metrics::mojom::PageLoadTiming timing; |
| 27 page_load_metrics::InitPageLoadTimingForTest(&timing); |
27 timing.navigation_start = base::Time::FromDoubleT(1); | 28 timing.navigation_start = base::Time::FromDoubleT(1); |
28 SimulateTimingUpdate(timing); | 29 SimulateTimingUpdate(timing); |
29 } | 30 } |
30 | 31 |
31 void AssertNoServiceWorkerHistogramsLogged() { | 32 void AssertNoServiceWorkerHistogramsLogged() { |
32 histogram_tester().ExpectTotalCount( | 33 histogram_tester().ExpectTotalCount( |
33 internal::kHistogramServiceWorkerFirstContentfulPaint, 0); | 34 internal::kHistogramServiceWorkerFirstContentfulPaint, 0); |
34 histogram_tester().ExpectTotalCount( | 35 histogram_tester().ExpectTotalCount( |
35 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); | 36 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); |
36 histogram_tester().ExpectTotalCount( | 37 histogram_tester().ExpectTotalCount( |
(...skipping 13 matching lines...) Expand all Loading... |
50 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, 0); | 51 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, 0); |
51 histogram_tester().ExpectTotalCount( | 52 histogram_tester().ExpectTotalCount( |
52 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, | 53 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, |
53 0); | 54 0); |
54 histogram_tester().ExpectTotalCount( | 55 histogram_tester().ExpectTotalCount( |
55 internal::kHistogramServiceWorkerDomContentLoadedInbox, 0); | 56 internal::kHistogramServiceWorkerDomContentLoadedInbox, 0); |
56 histogram_tester().ExpectTotalCount( | 57 histogram_tester().ExpectTotalCount( |
57 internal::kHistogramServiceWorkerLoadInbox, 0); | 58 internal::kHistogramServiceWorkerLoadInbox, 0); |
58 } | 59 } |
59 | 60 |
60 void InitializeTestPageLoadTiming(page_load_metrics::PageLoadTiming* timing) { | 61 void InitializeTestPageLoadTiming( |
| 62 page_load_metrics::mojom::PageLoadTiming* timing) { |
| 63 page_load_metrics::InitPageLoadTimingForTest(timing); |
61 timing->navigation_start = base::Time::FromDoubleT(1); | 64 timing->navigation_start = base::Time::FromDoubleT(1); |
62 timing->parse_timing.parse_start = base::TimeDelta::FromMilliseconds(100); | 65 timing->parse_timing->parse_start = base::TimeDelta::FromMilliseconds(100); |
63 timing->paint_timing.first_contentful_paint = | 66 timing->paint_timing->first_contentful_paint = |
64 base::TimeDelta::FromMilliseconds(300); | 67 base::TimeDelta::FromMilliseconds(300); |
65 timing->document_timing.dom_content_loaded_event_start = | 68 timing->document_timing->dom_content_loaded_event_start = |
66 base::TimeDelta::FromMilliseconds(600); | 69 base::TimeDelta::FromMilliseconds(600); |
67 timing->document_timing.load_event_start = | 70 timing->document_timing->load_event_start = |
68 base::TimeDelta::FromMilliseconds(1000); | 71 base::TimeDelta::FromMilliseconds(1000); |
69 PopulateRequiredTimingFields(timing); | 72 PopulateRequiredTimingFields(timing); |
70 } | 73 } |
71 }; | 74 }; |
72 | 75 |
73 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, NoMetrics) { | 76 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, NoMetrics) { |
74 AssertNoServiceWorkerHistogramsLogged(); | 77 AssertNoServiceWorkerHistogramsLogged(); |
75 AssertNoInboxHistogramsLogged(); | 78 AssertNoInboxHistogramsLogged(); |
76 } | 79 } |
77 | 80 |
78 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, NoServiceWorker) { | 81 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, NoServiceWorker) { |
79 page_load_metrics::PageLoadTiming timing; | 82 page_load_metrics::mojom::PageLoadTiming timing; |
80 InitializeTestPageLoadTiming(&timing); | 83 InitializeTestPageLoadTiming(&timing); |
81 | 84 |
82 NavigateAndCommit(GURL(kDefaultTestUrl)); | 85 NavigateAndCommit(GURL(kDefaultTestUrl)); |
83 SimulateTimingUpdate(timing); | 86 SimulateTimingUpdate(timing); |
84 | 87 |
85 AssertNoServiceWorkerHistogramsLogged(); | 88 AssertNoServiceWorkerHistogramsLogged(); |
86 AssertNoInboxHistogramsLogged(); | 89 AssertNoInboxHistogramsLogged(); |
87 } | 90 } |
88 | 91 |
89 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, WithServiceWorker) { | 92 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, WithServiceWorker) { |
90 page_load_metrics::PageLoadTiming timing; | 93 page_load_metrics::mojom::PageLoadTiming timing; |
91 InitializeTestPageLoadTiming(&timing); | 94 InitializeTestPageLoadTiming(&timing); |
92 | 95 |
93 NavigateAndCommit(GURL(kDefaultTestUrl)); | 96 NavigateAndCommit(GURL(kDefaultTestUrl)); |
94 page_load_metrics::PageLoadMetadata metadata; | 97 page_load_metrics::mojom::PageLoadMetadata metadata; |
95 metadata.behavior_flags |= | 98 metadata.behavior_flags |= |
96 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; | 99 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; |
97 SimulateTimingAndMetadataUpdate(timing, metadata); | 100 SimulateTimingAndMetadataUpdate(timing, metadata); |
98 | 101 |
99 histogram_tester().ExpectTotalCount( | 102 histogram_tester().ExpectTotalCount( |
100 internal::kHistogramServiceWorkerFirstContentfulPaint, 1); | 103 internal::kHistogramServiceWorkerFirstContentfulPaint, 1); |
101 histogram_tester().ExpectBucketCount( | 104 histogram_tester().ExpectBucketCount( |
102 internal::kHistogramServiceWorkerFirstContentfulPaint, | 105 internal::kHistogramServiceWorkerFirstContentfulPaint, |
103 timing.paint_timing.first_contentful_paint.value().InMilliseconds(), 1); | 106 timing.paint_timing->first_contentful_paint.value().InMilliseconds(), 1); |
104 | 107 |
105 histogram_tester().ExpectTotalCount( | 108 histogram_tester().ExpectTotalCount( |
106 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); | 109 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); |
107 | 110 |
108 histogram_tester().ExpectTotalCount( | 111 histogram_tester().ExpectTotalCount( |
109 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 1); | 112 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 1); |
110 histogram_tester().ExpectBucketCount( | 113 histogram_tester().ExpectBucketCount( |
111 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, | 114 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, |
112 (timing.paint_timing.first_contentful_paint.value() - | 115 (timing.paint_timing->first_contentful_paint.value() - |
113 timing.parse_timing.parse_start.value()) | 116 timing.parse_timing->parse_start.value()) |
114 .InMilliseconds(), | 117 .InMilliseconds(), |
115 1); | 118 1); |
116 | 119 |
117 histogram_tester().ExpectTotalCount( | 120 histogram_tester().ExpectTotalCount( |
118 internal::kHistogramServiceWorkerDomContentLoaded, 1); | 121 internal::kHistogramServiceWorkerDomContentLoaded, 1); |
119 histogram_tester().ExpectBucketCount( | 122 histogram_tester().ExpectBucketCount( |
120 internal::kHistogramServiceWorkerDomContentLoaded, | 123 internal::kHistogramServiceWorkerDomContentLoaded, |
121 timing.document_timing.dom_content_loaded_event_start.value() | 124 timing.document_timing->dom_content_loaded_event_start.value() |
122 .InMilliseconds(), | 125 .InMilliseconds(), |
123 1); | 126 1); |
124 | 127 |
125 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 1); | 128 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 1); |
126 histogram_tester().ExpectBucketCount( | 129 histogram_tester().ExpectBucketCount( |
127 internal::kHistogramServiceWorkerLoad, | 130 internal::kHistogramServiceWorkerLoad, |
128 timing.document_timing.load_event_start.value().InMilliseconds(), 1); | 131 timing.document_timing->load_event_start.value().InMilliseconds(), 1); |
129 | 132 |
130 histogram_tester().ExpectTotalCount( | 133 histogram_tester().ExpectTotalCount( |
131 internal::kHistogramServiceWorkerParseStart, 1); | 134 internal::kHistogramServiceWorkerParseStart, 1); |
132 | 135 |
133 AssertNoInboxHistogramsLogged(); | 136 AssertNoInboxHistogramsLogged(); |
134 } | 137 } |
135 | 138 |
136 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, WithServiceWorkerBackground) { | 139 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, WithServiceWorkerBackground) { |
137 page_load_metrics::PageLoadTiming timing; | 140 page_load_metrics::mojom::PageLoadTiming timing; |
| 141 page_load_metrics::InitPageLoadTimingForTest(&timing); |
138 PopulateRequiredTimingFields(&timing); | 142 PopulateRequiredTimingFields(&timing); |
139 | 143 |
140 page_load_metrics::PageLoadMetadata metadata; | 144 page_load_metrics::mojom::PageLoadMetadata metadata; |
141 metadata.behavior_flags |= | 145 metadata.behavior_flags |= |
142 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; | 146 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; |
143 | 147 |
144 NavigateAndCommit(GURL(kDefaultTestUrl)); | 148 NavigateAndCommit(GURL(kDefaultTestUrl)); |
145 SimulateTimingAndMetadataUpdate(timing, metadata); | 149 SimulateTimingAndMetadataUpdate(timing, metadata); |
146 | 150 |
147 // Background the tab, then forground it. | 151 // Background the tab, then forground it. |
148 web_contents()->WasHidden(); | 152 web_contents()->WasHidden(); |
149 web_contents()->WasShown(); | 153 web_contents()->WasShown(); |
150 | 154 |
151 InitializeTestPageLoadTiming(&timing); | 155 InitializeTestPageLoadTiming(&timing); |
152 SimulateTimingAndMetadataUpdate(timing, metadata); | 156 SimulateTimingAndMetadataUpdate(timing, metadata); |
153 | 157 |
154 histogram_tester().ExpectTotalCount( | 158 histogram_tester().ExpectTotalCount( |
155 internal::kHistogramServiceWorkerFirstContentfulPaint, 0); | 159 internal::kHistogramServiceWorkerFirstContentfulPaint, 0); |
156 histogram_tester().ExpectTotalCount( | 160 histogram_tester().ExpectTotalCount( |
157 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 1); | 161 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 1); |
158 histogram_tester().ExpectBucketCount( | 162 histogram_tester().ExpectBucketCount( |
159 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, | 163 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, |
160 timing.paint_timing.first_contentful_paint.value().InMilliseconds(), 1); | 164 timing.paint_timing->first_contentful_paint.value().InMilliseconds(), 1); |
161 histogram_tester().ExpectTotalCount( | 165 histogram_tester().ExpectTotalCount( |
162 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 0); | 166 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 0); |
163 histogram_tester().ExpectTotalCount( | 167 histogram_tester().ExpectTotalCount( |
164 internal::kHistogramServiceWorkerDomContentLoaded, 0); | 168 internal::kHistogramServiceWorkerDomContentLoaded, 0); |
165 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 0); | 169 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 0); |
166 // TODO(crbug.com/686590): The following expectation fails on Win7 Tests | 170 // TODO(crbug.com/686590): The following expectation fails on Win7 Tests |
167 // (dbg)(1) builder, so is disabled for the time being. | 171 // (dbg)(1) builder, so is disabled for the time being. |
168 // histogram_tester().ExpectTotalCount( | 172 // histogram_tester().ExpectTotalCount( |
169 // internal::kBackgroundHistogramServiceWorkerParseStart, 1); | 173 // internal::kBackgroundHistogramServiceWorkerParseStart, 1); |
170 | 174 |
171 AssertNoInboxHistogramsLogged(); | 175 AssertNoInboxHistogramsLogged(); |
172 } | 176 } |
173 | 177 |
174 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, InboxSite) { | 178 TEST_F(ServiceWorkerPageLoadMetricsObserverTest, InboxSite) { |
175 page_load_metrics::PageLoadTiming timing; | 179 page_load_metrics::mojom::PageLoadTiming timing; |
176 InitializeTestPageLoadTiming(&timing); | 180 InitializeTestPageLoadTiming(&timing); |
177 | 181 |
178 NavigateAndCommit(GURL(kInboxTestUrl)); | 182 NavigateAndCommit(GURL(kInboxTestUrl)); |
179 page_load_metrics::PageLoadMetadata metadata; | 183 page_load_metrics::mojom::PageLoadMetadata metadata; |
180 metadata.behavior_flags |= | 184 metadata.behavior_flags |= |
181 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; | 185 blink::WebLoadingBehaviorFlag::kWebLoadingBehaviorServiceWorkerControlled; |
182 SimulateTimingAndMetadataUpdate(timing, metadata); | 186 SimulateTimingAndMetadataUpdate(timing, metadata); |
183 | 187 |
184 histogram_tester().ExpectTotalCount( | 188 histogram_tester().ExpectTotalCount( |
185 internal::kHistogramServiceWorkerFirstContentfulPaint, 1); | 189 internal::kHistogramServiceWorkerFirstContentfulPaint, 1); |
186 histogram_tester().ExpectBucketCount( | 190 histogram_tester().ExpectBucketCount( |
187 internal::kHistogramServiceWorkerFirstContentfulPaint, | 191 internal::kHistogramServiceWorkerFirstContentfulPaint, |
188 timing.paint_timing.first_contentful_paint.value().InMilliseconds(), 1); | 192 timing.paint_timing->first_contentful_paint.value().InMilliseconds(), 1); |
189 histogram_tester().ExpectTotalCount( | 193 histogram_tester().ExpectTotalCount( |
190 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, 1); | 194 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, 1); |
191 histogram_tester().ExpectBucketCount( | 195 histogram_tester().ExpectBucketCount( |
192 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, | 196 internal::kHistogramServiceWorkerFirstContentfulPaintInbox, |
193 timing.paint_timing.first_contentful_paint.value().InMilliseconds(), 1); | 197 timing.paint_timing->first_contentful_paint.value().InMilliseconds(), 1); |
194 | 198 |
195 histogram_tester().ExpectTotalCount( | 199 histogram_tester().ExpectTotalCount( |
196 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); | 200 internal::kBackgroundHistogramServiceWorkerFirstContentfulPaint, 0); |
197 | 201 |
198 histogram_tester().ExpectTotalCount( | 202 histogram_tester().ExpectTotalCount( |
199 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 1); | 203 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, 1); |
200 histogram_tester().ExpectBucketCount( | 204 histogram_tester().ExpectBucketCount( |
201 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, | 205 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaint, |
202 (timing.paint_timing.first_contentful_paint.value() - | 206 (timing.paint_timing->first_contentful_paint.value() - |
203 timing.parse_timing.parse_start.value()) | 207 timing.parse_timing->parse_start.value()) |
204 .InMilliseconds(), | 208 .InMilliseconds(), |
205 1); | 209 1); |
206 histogram_tester().ExpectTotalCount( | 210 histogram_tester().ExpectTotalCount( |
207 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, | 211 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, |
208 1); | 212 1); |
209 histogram_tester().ExpectBucketCount( | 213 histogram_tester().ExpectBucketCount( |
210 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, | 214 internal::kHistogramServiceWorkerParseStartToFirstContentfulPaintInbox, |
211 (timing.paint_timing.first_contentful_paint.value() - | 215 (timing.paint_timing->first_contentful_paint.value() - |
212 timing.parse_timing.parse_start.value()) | 216 timing.parse_timing->parse_start.value()) |
213 .InMilliseconds(), | 217 .InMilliseconds(), |
214 1); | 218 1); |
215 | 219 |
216 histogram_tester().ExpectTotalCount( | 220 histogram_tester().ExpectTotalCount( |
217 internal::kHistogramServiceWorkerDomContentLoaded, 1); | 221 internal::kHistogramServiceWorkerDomContentLoaded, 1); |
218 histogram_tester().ExpectBucketCount( | 222 histogram_tester().ExpectBucketCount( |
219 internal::kHistogramServiceWorkerDomContentLoaded, | 223 internal::kHistogramServiceWorkerDomContentLoaded, |
220 timing.document_timing.dom_content_loaded_event_start.value() | 224 timing.document_timing->dom_content_loaded_event_start.value() |
221 .InMilliseconds(), | 225 .InMilliseconds(), |
222 1); | 226 1); |
223 histogram_tester().ExpectTotalCount( | 227 histogram_tester().ExpectTotalCount( |
224 internal::kHistogramServiceWorkerDomContentLoadedInbox, 1); | 228 internal::kHistogramServiceWorkerDomContentLoadedInbox, 1); |
225 histogram_tester().ExpectBucketCount( | 229 histogram_tester().ExpectBucketCount( |
226 internal::kHistogramServiceWorkerDomContentLoadedInbox, | 230 internal::kHistogramServiceWorkerDomContentLoadedInbox, |
227 timing.document_timing.dom_content_loaded_event_start.value() | 231 timing.document_timing->dom_content_loaded_event_start.value() |
228 .InMilliseconds(), | 232 .InMilliseconds(), |
229 1); | 233 1); |
230 | 234 |
231 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 1); | 235 histogram_tester().ExpectTotalCount(internal::kHistogramServiceWorkerLoad, 1); |
232 histogram_tester().ExpectBucketCount( | 236 histogram_tester().ExpectBucketCount( |
233 internal::kHistogramServiceWorkerLoad, | 237 internal::kHistogramServiceWorkerLoad, |
234 timing.document_timing.load_event_start.value().InMilliseconds(), 1); | 238 timing.document_timing->load_event_start.value().InMilliseconds(), 1); |
235 histogram_tester().ExpectTotalCount( | 239 histogram_tester().ExpectTotalCount( |
236 internal::kHistogramServiceWorkerLoadInbox, 1); | 240 internal::kHistogramServiceWorkerLoadInbox, 1); |
237 histogram_tester().ExpectBucketCount( | 241 histogram_tester().ExpectBucketCount( |
238 internal::kHistogramServiceWorkerLoadInbox, | 242 internal::kHistogramServiceWorkerLoadInbox, |
239 timing.document_timing.load_event_start.value().InMilliseconds(), 1); | 243 timing.document_timing->load_event_start.value().InMilliseconds(), 1); |
240 histogram_tester().ExpectTotalCount( | 244 histogram_tester().ExpectTotalCount( |
241 internal::kHistogramServiceWorkerParseStart, 1); | 245 internal::kHistogramServiceWorkerParseStart, 1); |
242 } | 246 } |
OLD | NEW |