OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/metrics_web_contents_observer.h" | 5 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 const char kDefaultTestUrl[] = "https://google.com/"; | 33 const char kDefaultTestUrl[] = "https://google.com/"; |
34 const char kDefaultTestUrlAnchor[] = "https://google.com/#samedocument"; | 34 const char kDefaultTestUrlAnchor[] = "https://google.com/#samedocument"; |
35 const char kDefaultTestUrl2[] = "https://whatever.com/"; | 35 const char kDefaultTestUrl2[] = "https://whatever.com/"; |
36 const char kFilteredStartUrl[] = "https://whatever.com/ignore-on-start"; | 36 const char kFilteredStartUrl[] = "https://whatever.com/ignore-on-start"; |
37 const char kFilteredCommitUrl[] = "https://whatever.com/ignore-on-commit"; | 37 const char kFilteredCommitUrl[] = "https://whatever.com/ignore-on-commit"; |
38 | 38 |
39 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the | 39 // Simple PageLoadMetricsObserver that copies observed PageLoadTimings into the |
40 // provided std::vector, so they can be analyzed by unit tests. | 40 // provided std::vector, so they can be analyzed by unit tests. |
41 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { | 41 class TestPageLoadMetricsObserver : public PageLoadMetricsObserver { |
42 public: | 42 public: |
43 TestPageLoadMetricsObserver(std::vector<PageLoadTiming>* updated_timings, | 43 TestPageLoadMetricsObserver( |
44 std::vector<PageLoadTiming>* complete_timings, | 44 std::vector<mojo::StructPtr<mojom::PageLoadTiming>>* updated_timings, |
45 std::vector<GURL>* observed_committed_urls) | 45 std::vector<mojo::StructPtr<mojom::PageLoadTiming>>* complete_timings, |
| 46 std::vector<GURL>* observed_committed_urls) |
46 : updated_timings_(updated_timings), | 47 : updated_timings_(updated_timings), |
47 complete_timings_(complete_timings), | 48 complete_timings_(complete_timings), |
48 observed_committed_urls_(observed_committed_urls) {} | 49 observed_committed_urls_(observed_committed_urls) {} |
49 | 50 |
50 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, | 51 ObservePolicy OnStart(content::NavigationHandle* navigation_handle, |
51 const GURL& currently_committed_url, | 52 const GURL& currently_committed_url, |
52 bool started_in_foreground) override { | 53 bool started_in_foreground) override { |
53 observed_committed_urls_->push_back(currently_committed_url); | 54 observed_committed_urls_->push_back(currently_committed_url); |
54 return CONTINUE_OBSERVING; | 55 return CONTINUE_OBSERVING; |
55 } | 56 } |
56 | 57 |
57 void OnTimingUpdate(const PageLoadTiming& timing, | 58 void OnTimingUpdate(const mojom::PageLoadTiming& timing, |
58 const PageLoadExtraInfo& extra_info) override { | 59 const PageLoadExtraInfo& extra_info) override { |
59 updated_timings_->push_back(timing); | 60 updated_timings_->push_back(timing.Clone()); |
60 } | 61 } |
61 | 62 |
62 void OnComplete(const PageLoadTiming& timing, | 63 void OnComplete(const mojom::PageLoadTiming& timing, |
63 const PageLoadExtraInfo& extra_info) override { | 64 const PageLoadExtraInfo& extra_info) override { |
64 complete_timings_->push_back(timing); | 65 complete_timings_->push_back(timing.Clone()); |
65 } | 66 } |
66 | 67 |
67 ObservePolicy FlushMetricsOnAppEnterBackground( | 68 ObservePolicy FlushMetricsOnAppEnterBackground( |
68 const PageLoadTiming& timing, | 69 const mojom::PageLoadTiming& timing, |
69 const PageLoadExtraInfo& extra_info) override { | 70 const PageLoadExtraInfo& extra_info) override { |
70 return STOP_OBSERVING; | 71 return STOP_OBSERVING; |
71 } | 72 } |
72 | 73 |
73 private: | 74 private: |
74 std::vector<PageLoadTiming>* const updated_timings_; | 75 std::vector<mojo::StructPtr<mojom::PageLoadTiming>>* const updated_timings_; |
75 std::vector<PageLoadTiming>* const complete_timings_; | 76 std::vector<mojo::StructPtr<mojom::PageLoadTiming>>* const complete_timings_; |
76 std::vector<GURL>* const observed_committed_urls_; | 77 std::vector<GURL>* const observed_committed_urls_; |
77 }; | 78 }; |
78 | 79 |
79 // Test PageLoadMetricsObserver that stops observing page loads with certain | 80 // Test PageLoadMetricsObserver that stops observing page loads with certain |
80 // substrings in the URL. | 81 // substrings in the URL. |
81 class FilteringPageLoadMetricsObserver : public PageLoadMetricsObserver { | 82 class FilteringPageLoadMetricsObserver : public PageLoadMetricsObserver { |
82 public: | 83 public: |
83 explicit FilteringPageLoadMetricsObserver( | 84 explicit FilteringPageLoadMetricsObserver( |
84 std::vector<GURL>* completed_filtered_urls) | 85 std::vector<GURL>* completed_filtered_urls) |
85 : completed_filtered_urls_(completed_filtered_urls) {} | 86 : completed_filtered_urls_(completed_filtered_urls) {} |
86 | 87 |
87 ObservePolicy OnStart(content::NavigationHandle* handle, | 88 ObservePolicy OnStart(content::NavigationHandle* handle, |
88 const GURL& currently_committed_url, | 89 const GURL& currently_committed_url, |
89 bool started_in_foreground) override { | 90 bool started_in_foreground) override { |
90 const bool should_ignore = | 91 const bool should_ignore = |
91 handle->GetURL().spec().find("ignore-on-start") != std::string::npos; | 92 handle->GetURL().spec().find("ignore-on-start") != std::string::npos; |
92 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; | 93 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; |
93 } | 94 } |
94 | 95 |
95 ObservePolicy OnCommit(content::NavigationHandle* handle) override { | 96 ObservePolicy OnCommit(content::NavigationHandle* handle) override { |
96 const bool should_ignore = | 97 const bool should_ignore = |
97 handle->GetURL().spec().find("ignore-on-commit") != std::string::npos; | 98 handle->GetURL().spec().find("ignore-on-commit") != std::string::npos; |
98 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; | 99 return should_ignore ? STOP_OBSERVING : CONTINUE_OBSERVING; |
99 } | 100 } |
100 | 101 |
101 void OnComplete(const PageLoadTiming& timing, | 102 void OnComplete(const mojom::PageLoadTiming& timing, |
102 const PageLoadExtraInfo& extra_info) override { | 103 const PageLoadExtraInfo& extra_info) override { |
103 completed_filtered_urls_->push_back(extra_info.url); | 104 completed_filtered_urls_->push_back(extra_info.url); |
104 } | 105 } |
105 | 106 |
106 private: | 107 private: |
107 std::vector<GURL>* const completed_filtered_urls_; | 108 std::vector<GURL>* const completed_filtered_urls_; |
108 }; | 109 }; |
109 | 110 |
110 class TestPageLoadMetricsEmbedderInterface | 111 class TestPageLoadMetricsEmbedderInterface |
111 : public PageLoadMetricsEmbedderInterface { | 112 : public PageLoadMetricsEmbedderInterface { |
112 public: | 113 public: |
113 TestPageLoadMetricsEmbedderInterface() : is_ntp_(false) {} | 114 TestPageLoadMetricsEmbedderInterface() : is_ntp_(false) {} |
114 | 115 |
115 bool IsNewTabPageUrl(const GURL& url) override { return is_ntp_; } | 116 bool IsNewTabPageUrl(const GURL& url) override { return is_ntp_; } |
116 void set_is_ntp(bool is_ntp) { is_ntp_ = is_ntp; } | 117 void set_is_ntp(bool is_ntp) { is_ntp_ = is_ntp; } |
117 void RegisterObservers(PageLoadTracker* tracker) override { | 118 void RegisterObservers(PageLoadTracker* tracker) override { |
118 tracker->AddObserver(base::MakeUnique<TestPageLoadMetricsObserver>( | 119 tracker->AddObserver(base::MakeUnique<TestPageLoadMetricsObserver>( |
119 &updated_timings_, &complete_timings_, &observed_committed_urls_)); | 120 &updated_timings_, &complete_timings_, &observed_committed_urls_)); |
120 tracker->AddObserver(base::MakeUnique<FilteringPageLoadMetricsObserver>( | 121 tracker->AddObserver(base::MakeUnique<FilteringPageLoadMetricsObserver>( |
121 &completed_filtered_urls_)); | 122 &completed_filtered_urls_)); |
122 } | 123 } |
123 const std::vector<PageLoadTiming>& updated_timings() const { | 124 const std::vector<mojo::StructPtr<mojom::PageLoadTiming>>& updated_timings() |
| 125 const { |
124 return updated_timings_; | 126 return updated_timings_; |
125 } | 127 } |
126 const std::vector<PageLoadTiming>& complete_timings() const { | 128 const std::vector<mojo::StructPtr<mojom::PageLoadTiming>>& complete_timings() |
| 129 const { |
127 return complete_timings_; | 130 return complete_timings_; |
128 } | 131 } |
129 | 132 |
130 // currently_committed_urls passed to OnStart(). | 133 // currently_committed_urls passed to OnStart(). |
131 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 134 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
132 return observed_committed_urls_; | 135 return observed_committed_urls_; |
133 } | 136 } |
134 | 137 |
135 // committed URLs passed to FilteringPageLoadMetricsObserver::OnComplete(). | 138 // committed URLs passed to FilteringPageLoadMetricsObserver::OnComplete(). |
136 const std::vector<GURL>& completed_filtered_urls() const { | 139 const std::vector<GURL>& completed_filtered_urls() const { |
137 return completed_filtered_urls_; | 140 return completed_filtered_urls_; |
138 } | 141 } |
139 | 142 |
140 private: | 143 private: |
141 std::vector<PageLoadTiming> updated_timings_; | 144 std::vector<mojo::StructPtr<mojom::PageLoadTiming>> updated_timings_; |
142 std::vector<PageLoadTiming> complete_timings_; | 145 std::vector<mojo::StructPtr<mojom::PageLoadTiming>> complete_timings_; |
143 std::vector<GURL> observed_committed_urls_; | 146 std::vector<GURL> observed_committed_urls_; |
144 std::vector<GURL> completed_filtered_urls_; | 147 std::vector<GURL> completed_filtered_urls_; |
145 bool is_ntp_; | 148 bool is_ntp_; |
146 }; | 149 }; |
147 | 150 |
148 } // namespace | 151 } // namespace |
149 | 152 |
150 class MetricsWebContentsObserverTest : public ChromeRenderViewHostTestHarness { | 153 class MetricsWebContentsObserverTest : public ChromeRenderViewHostTestHarness { |
151 public: | 154 public: |
152 MetricsWebContentsObserverTest() : num_errors_(0) {} | 155 MetricsWebContentsObserverTest() : num_errors_(0) {} |
153 | 156 |
154 void SetUp() override { | 157 void SetUp() override { |
155 ChromeRenderViewHostTestHarness::SetUp(); | 158 ChromeRenderViewHostTestHarness::SetUp(); |
156 AttachObserver(); | 159 AttachObserver(); |
157 } | 160 } |
158 | 161 |
159 void NavigateToUntrackedUrl() { | 162 void NavigateToUntrackedUrl() { |
160 content::WebContentsTester::For(web_contents()) | 163 content::WebContentsTester::For(web_contents()) |
161 ->NavigateAndCommit(GURL(url::kAboutBlankURL)); | 164 ->NavigateAndCommit(GURL(url::kAboutBlankURL)); |
162 } | 165 } |
163 | 166 |
164 void SimulateTimingUpdate(const PageLoadTiming& timing) { | 167 void SimulateTimingUpdate(const mojom::PageLoadTiming& timing) { |
165 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); | 168 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); |
166 } | 169 } |
167 | 170 |
168 void SimulateTimingUpdate(const PageLoadTiming& timing, | 171 void SimulateTimingUpdate(const mojom::PageLoadTiming& timing, |
169 content::RenderFrameHost* render_frame_host) { | 172 content::RenderFrameHost* render_frame_host) { |
170 observer_->OnTimingUpdated(render_frame_host, timing, PageLoadMetadata()); | 173 observer_->OnTimingUpdated(render_frame_host, timing, |
| 174 mojom::PageLoadMetadata()); |
171 } | 175 } |
172 | 176 |
173 void AttachObserver() { | 177 void AttachObserver() { |
174 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); | 178 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); |
175 // Owned by the web_contents. Tests must be careful not to call | 179 // Owned by the web_contents. Tests must be careful not to call |
176 // SimulateTimingUpdate after they call DeleteContents() without also | 180 // SimulateTimingUpdate after they call DeleteContents() without also |
177 // calling AttachObserver() again. Otherwise they will use-after-free the | 181 // calling AttachObserver() again. Otherwise they will use-after-free the |
178 // observer_. | 182 // observer_. |
179 observer_ = MetricsWebContentsObserver::CreateForWebContents( | 183 observer_ = MetricsWebContentsObserver::CreateForWebContents( |
180 web_contents(), base::WrapUnique(embedder_interface_)); | 184 web_contents(), base::WrapUnique(embedder_interface_)); |
181 observer_->WasShown(); | 185 observer_->WasShown(); |
182 } | 186 } |
183 | 187 |
184 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { | 188 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { |
185 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); | 189 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); |
186 num_errors_ += count; | 190 num_errors_ += count; |
187 } | 191 } |
188 | 192 |
189 void CheckTotalErrorEvents() { | 193 void CheckTotalErrorEvents() { |
190 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); | 194 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); |
191 } | 195 } |
192 | 196 |
193 void CheckNoErrorEvents() { | 197 void CheckNoErrorEvents() { |
194 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, 0); | 198 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, 0); |
195 } | 199 } |
196 | 200 |
197 int CountEmptyCompleteTimingReported() { | 201 int CountEmptyCompleteTimingReported() { |
198 int empty = 0; | 202 int empty = 0; |
199 for (const auto& timing : embedder_interface_->complete_timings()) { | 203 for (const auto& timing : embedder_interface_->complete_timings()) { |
200 if (timing.IsEmpty()) | 204 if (page_load_metrics::IsEmpty(*timing)) |
201 ++empty; | 205 ++empty; |
202 } | 206 } |
203 return empty; | 207 return empty; |
204 } | 208 } |
205 | 209 |
206 int CountCompleteTimingReported() { | 210 int CountCompleteTimingReported() { |
207 return embedder_interface_->complete_timings().size(); | 211 return embedder_interface_->complete_timings().size(); |
208 } | 212 } |
209 int CountUpdatedTimingReported() { | 213 int CountUpdatedTimingReported() { |
210 return embedder_interface_->updated_timings().size(); | 214 return embedder_interface_->updated_timings().size(); |
(...skipping 12 matching lines...) Expand all Loading... |
223 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 227 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
224 MetricsWebContentsObserver* observer_; | 228 MetricsWebContentsObserver* observer_; |
225 | 229 |
226 private: | 230 private: |
227 int num_errors_; | 231 int num_errors_; |
228 | 232 |
229 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 233 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
230 }; | 234 }; |
231 | 235 |
232 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { | 236 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { |
233 PageLoadTiming timing; | 237 mojom::PageLoadTiming timing; |
| 238 page_load_metrics::InitPageLoadTimingForTest(&timing); |
234 timing.navigation_start = base::Time::FromDoubleT(1); | 239 timing.navigation_start = base::Time::FromDoubleT(1); |
235 | 240 |
236 content::WebContentsTester* web_contents_tester = | 241 content::WebContentsTester* web_contents_tester = |
237 content::WebContentsTester::For(web_contents()); | 242 content::WebContentsTester::For(web_contents()); |
238 | 243 |
239 ASSERT_TRUE(observed_committed_urls_from_on_start().empty()); | 244 ASSERT_TRUE(observed_committed_urls_from_on_start().empty()); |
240 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 245 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
241 ASSERT_EQ(1u, observed_committed_urls_from_on_start().size()); | 246 ASSERT_EQ(1u, observed_committed_urls_from_on_start().size()); |
242 ASSERT_TRUE(observed_committed_urls_from_on_start().at(0).is_empty()); | 247 ASSERT_TRUE(observed_committed_urls_from_on_start().at(0).is_empty()); |
243 | 248 |
244 ASSERT_EQ(0, CountUpdatedTimingReported()); | 249 ASSERT_EQ(0, CountUpdatedTimingReported()); |
245 SimulateTimingUpdate(timing); | 250 SimulateTimingUpdate(timing); |
246 ASSERT_EQ(1, CountUpdatedTimingReported()); | 251 ASSERT_EQ(1, CountUpdatedTimingReported()); |
247 ASSERT_EQ(0, CountCompleteTimingReported()); | 252 ASSERT_EQ(0, CountCompleteTimingReported()); |
248 | 253 |
249 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 254 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
250 ASSERT_EQ(1, CountCompleteTimingReported()); | 255 ASSERT_EQ(1, CountCompleteTimingReported()); |
251 ASSERT_EQ(0, CountEmptyCompleteTimingReported()); | 256 ASSERT_EQ(0, CountEmptyCompleteTimingReported()); |
252 ASSERT_EQ(2u, observed_committed_urls_from_on_start().size()); | 257 ASSERT_EQ(2u, observed_committed_urls_from_on_start().size()); |
253 ASSERT_EQ(kDefaultTestUrl, | 258 ASSERT_EQ(kDefaultTestUrl, |
254 observed_committed_urls_from_on_start().at(1).spec()); | 259 observed_committed_urls_from_on_start().at(1).spec()); |
255 ASSERT_EQ(1, CountUpdatedTimingReported()); | 260 ASSERT_EQ(1, CountUpdatedTimingReported()); |
256 | 261 |
257 CheckNoErrorEvents(); | 262 CheckNoErrorEvents(); |
258 } | 263 } |
259 | 264 |
260 TEST_F(MetricsWebContentsObserverTest, NotInMainFrame) { | 265 TEST_F(MetricsWebContentsObserverTest, NotInMainFrame) { |
261 PageLoadTiming timing; | 266 mojom::PageLoadTiming timing; |
| 267 page_load_metrics::InitPageLoadTimingForTest(&timing); |
262 timing.navigation_start = base::Time::FromDoubleT(1); | 268 timing.navigation_start = base::Time::FromDoubleT(1); |
263 | 269 |
264 content::WebContentsTester* web_contents_tester = | 270 content::WebContentsTester* web_contents_tester = |
265 content::WebContentsTester::For(web_contents()); | 271 content::WebContentsTester::For(web_contents()); |
266 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 272 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
267 | 273 |
268 content::RenderFrameHostTester* rfh_tester = | 274 content::RenderFrameHostTester* rfh_tester = |
269 content::RenderFrameHostTester::For(main_rfh()); | 275 content::RenderFrameHostTester::For(main_rfh()); |
270 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); | 276 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); |
271 | 277 |
272 content::RenderFrameHostTester* subframe_tester = | 278 content::RenderFrameHostTester* subframe_tester = |
273 content::RenderFrameHostTester::For(subframe); | 279 content::RenderFrameHostTester::For(subframe); |
274 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); | 280 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); |
275 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | 281 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
276 SimulateTimingUpdate(timing, subframe); | 282 SimulateTimingUpdate(timing, subframe); |
277 subframe_tester->SimulateNavigationStop(); | 283 subframe_tester->SimulateNavigationStop(); |
278 | 284 |
279 // Navigate again to see if the timing updated for a subframe message. | 285 // Navigate again to see if the timing updated for a subframe message. |
280 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 286 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
281 | 287 |
282 ASSERT_EQ(0, CountUpdatedTimingReported()); | 288 ASSERT_EQ(0, CountUpdatedTimingReported()); |
283 ASSERT_EQ(1, CountCompleteTimingReported()); | 289 ASSERT_EQ(1, CountCompleteTimingReported()); |
284 ASSERT_EQ(1, CountEmptyCompleteTimingReported()); | 290 ASSERT_EQ(1, CountEmptyCompleteTimingReported()); |
285 CheckErrorEvent(ERR_TIMING_IPC_FROM_SUBFRAME, 1); | 291 CheckErrorEvent(ERR_TIMING_IPC_FROM_SUBFRAME, 1); |
286 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); | 292 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); |
287 CheckTotalErrorEvents(); | 293 CheckTotalErrorEvents(); |
288 } | 294 } |
289 | 295 |
290 TEST_F(MetricsWebContentsObserverTest, SameDocumentNoTrigger) { | 296 TEST_F(MetricsWebContentsObserverTest, SameDocumentNoTrigger) { |
291 PageLoadTiming timing; | 297 mojom::PageLoadTiming timing; |
| 298 page_load_metrics::InitPageLoadTimingForTest(&timing); |
292 timing.navigation_start = base::Time::FromDoubleT(1); | 299 timing.navigation_start = base::Time::FromDoubleT(1); |
293 | 300 |
294 content::WebContentsTester* web_contents_tester = | 301 content::WebContentsTester* web_contents_tester = |
295 content::WebContentsTester::For(web_contents()); | 302 content::WebContentsTester::For(web_contents()); |
296 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 303 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
297 ASSERT_EQ(0, CountUpdatedTimingReported()); | 304 ASSERT_EQ(0, CountUpdatedTimingReported()); |
298 SimulateTimingUpdate(timing); | 305 SimulateTimingUpdate(timing); |
299 ASSERT_EQ(1, CountUpdatedTimingReported()); | 306 ASSERT_EQ(1, CountUpdatedTimingReported()); |
300 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); | 307 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrlAnchor)); |
301 // Send the same timing update. The original tracker for kDefaultTestUrl | 308 // Send the same timing update. The original tracker for kDefaultTestUrl |
302 // should dedup the update, and the tracker for kDefaultTestUrlAnchor should | 309 // should dedup the update, and the tracker for kDefaultTestUrlAnchor should |
303 // have been destroyed as a result of its being a same page navigation, so | 310 // have been destroyed as a result of its being a same page navigation, so |
304 // CountUpdatedTimingReported() should continue to return 1. | 311 // CountUpdatedTimingReported() should continue to return 1. |
305 SimulateTimingUpdate(timing); | 312 SimulateTimingUpdate(timing); |
306 | 313 |
307 ASSERT_EQ(1, CountUpdatedTimingReported()); | 314 ASSERT_EQ(1, CountUpdatedTimingReported()); |
308 ASSERT_EQ(0, CountCompleteTimingReported()); | 315 ASSERT_EQ(0, CountCompleteTimingReported()); |
309 | 316 |
310 // Navigate again to force histogram logging. | 317 // Navigate again to force histogram logging. |
311 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 318 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
312 | 319 |
313 // A same page navigation shouldn't trigger logging UMA for the original. | 320 // A same page navigation shouldn't trigger logging UMA for the original. |
314 ASSERT_EQ(1, CountUpdatedTimingReported()); | 321 ASSERT_EQ(1, CountUpdatedTimingReported()); |
315 ASSERT_EQ(1, CountCompleteTimingReported()); | 322 ASSERT_EQ(1, CountCompleteTimingReported()); |
316 ASSERT_EQ(0, CountEmptyCompleteTimingReported()); | 323 ASSERT_EQ(0, CountEmptyCompleteTimingReported()); |
317 CheckNoErrorEvents(); | 324 CheckNoErrorEvents(); |
318 } | 325 } |
319 | 326 |
320 TEST_F(MetricsWebContentsObserverTest, DontLogNewTabPage) { | 327 TEST_F(MetricsWebContentsObserverTest, DontLogNewTabPage) { |
321 PageLoadTiming timing; | 328 mojom::PageLoadTiming timing; |
| 329 page_load_metrics::InitPageLoadTimingForTest(&timing); |
322 timing.navigation_start = base::Time::FromDoubleT(1); | 330 timing.navigation_start = base::Time::FromDoubleT(1); |
323 | 331 |
324 content::WebContentsTester* web_contents_tester = | 332 content::WebContentsTester* web_contents_tester = |
325 content::WebContentsTester::For(web_contents()); | 333 content::WebContentsTester::For(web_contents()); |
326 embedder_interface_->set_is_ntp(true); | 334 embedder_interface_->set_is_ntp(true); |
327 | 335 |
328 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 336 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
329 SimulateTimingUpdate(timing); | 337 SimulateTimingUpdate(timing); |
330 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 338 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
331 ASSERT_EQ(0, CountUpdatedTimingReported()); | 339 ASSERT_EQ(0, CountUpdatedTimingReported()); |
332 ASSERT_EQ(0, CountCompleteTimingReported()); | 340 ASSERT_EQ(0, CountCompleteTimingReported()); |
333 CheckErrorEvent(ERR_IPC_WITH_NO_RELEVANT_LOAD, 1); | 341 CheckErrorEvent(ERR_IPC_WITH_NO_RELEVANT_LOAD, 1); |
334 CheckTotalErrorEvents(); | 342 CheckTotalErrorEvents(); |
335 } | 343 } |
336 | 344 |
337 TEST_F(MetricsWebContentsObserverTest, DontLogIrrelevantNavigation) { | 345 TEST_F(MetricsWebContentsObserverTest, DontLogIrrelevantNavigation) { |
338 PageLoadTiming timing; | 346 mojom::PageLoadTiming timing; |
| 347 page_load_metrics::InitPageLoadTimingForTest(&timing); |
339 timing.navigation_start = base::Time::FromDoubleT(10); | 348 timing.navigation_start = base::Time::FromDoubleT(10); |
340 | 349 |
341 content::WebContentsTester* web_contents_tester = | 350 content::WebContentsTester* web_contents_tester = |
342 content::WebContentsTester::For(web_contents()); | 351 content::WebContentsTester::For(web_contents()); |
343 | 352 |
344 GURL about_blank_url = GURL("about:blank"); | 353 GURL about_blank_url = GURL("about:blank"); |
345 web_contents_tester->NavigateAndCommit(about_blank_url); | 354 web_contents_tester->NavigateAndCommit(about_blank_url); |
346 SimulateTimingUpdate(timing); | 355 SimulateTimingUpdate(timing); |
347 ASSERT_EQ(0, CountUpdatedTimingReported()); | 356 ASSERT_EQ(0, CountUpdatedTimingReported()); |
348 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 357 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
349 ASSERT_EQ(0, CountUpdatedTimingReported()); | 358 ASSERT_EQ(0, CountUpdatedTimingReported()); |
350 ASSERT_EQ(0, CountCompleteTimingReported()); | 359 ASSERT_EQ(0, CountCompleteTimingReported()); |
351 | 360 |
352 CheckErrorEvent(ERR_IPC_FROM_BAD_URL_SCHEME, 1); | 361 CheckErrorEvent(ERR_IPC_FROM_BAD_URL_SCHEME, 1); |
353 CheckErrorEvent(ERR_IPC_WITH_NO_RELEVANT_LOAD, 1); | 362 CheckErrorEvent(ERR_IPC_WITH_NO_RELEVANT_LOAD, 1); |
354 CheckTotalErrorEvents(); | 363 CheckTotalErrorEvents(); |
355 } | 364 } |
356 | 365 |
357 TEST_F(MetricsWebContentsObserverTest, EmptyTimingError) { | 366 TEST_F(MetricsWebContentsObserverTest, EmptyTimingError) { |
358 PageLoadTiming timing; | 367 mojom::PageLoadTiming timing; |
| 368 page_load_metrics::InitPageLoadTimingForTest(&timing); |
359 | 369 |
360 content::WebContentsTester* web_contents_tester = | 370 content::WebContentsTester* web_contents_tester = |
361 content::WebContentsTester::For(web_contents()); | 371 content::WebContentsTester::For(web_contents()); |
362 | 372 |
363 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 373 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
364 SimulateTimingUpdate(timing); | 374 SimulateTimingUpdate(timing); |
365 ASSERT_EQ(0, CountUpdatedTimingReported()); | 375 ASSERT_EQ(0, CountUpdatedTimingReported()); |
366 NavigateToUntrackedUrl(); | 376 NavigateToUntrackedUrl(); |
367 ASSERT_EQ(0, CountUpdatedTimingReported()); | 377 ASSERT_EQ(0, CountUpdatedTimingReported()); |
368 ASSERT_EQ(1, CountCompleteTimingReported()); | 378 ASSERT_EQ(1, CountCompleteTimingReported()); |
369 | 379 |
370 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); | 380 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); |
371 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); | 381 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); |
372 CheckTotalErrorEvents(); | 382 CheckTotalErrorEvents(); |
373 | 383 |
374 histogram_tester_.ExpectTotalCount( | 384 histogram_tester_.ExpectTotalCount( |
375 page_load_metrics::internal::kPageLoadTimingStatus, 1); | 385 page_load_metrics::internal::kPageLoadTimingStatus, 1); |
376 histogram_tester_.ExpectBucketCount( | 386 histogram_tester_.ExpectBucketCount( |
377 page_load_metrics::internal::kPageLoadTimingStatus, | 387 page_load_metrics::internal::kPageLoadTimingStatus, |
378 page_load_metrics::internal::INVALID_EMPTY_TIMING, 1); | 388 page_load_metrics::internal::INVALID_EMPTY_TIMING, 1); |
379 } | 389 } |
380 | 390 |
381 TEST_F(MetricsWebContentsObserverTest, NullNavigationStartError) { | 391 TEST_F(MetricsWebContentsObserverTest, NullNavigationStartError) { |
382 PageLoadTiming timing; | 392 mojom::PageLoadTiming timing; |
383 timing.parse_timing.parse_start = base::TimeDelta::FromMilliseconds(1); | 393 page_load_metrics::InitPageLoadTimingForTest(&timing); |
| 394 timing.parse_timing->parse_start = base::TimeDelta::FromMilliseconds(1); |
384 | 395 |
385 content::WebContentsTester* web_contents_tester = | 396 content::WebContentsTester* web_contents_tester = |
386 content::WebContentsTester::For(web_contents()); | 397 content::WebContentsTester::For(web_contents()); |
387 | 398 |
388 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 399 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
389 SimulateTimingUpdate(timing); | 400 SimulateTimingUpdate(timing); |
390 ASSERT_EQ(0, CountUpdatedTimingReported()); | 401 ASSERT_EQ(0, CountUpdatedTimingReported()); |
391 NavigateToUntrackedUrl(); | 402 NavigateToUntrackedUrl(); |
392 ASSERT_EQ(0, CountUpdatedTimingReported()); | 403 ASSERT_EQ(0, CountUpdatedTimingReported()); |
393 ASSERT_EQ(1, CountCompleteTimingReported()); | 404 ASSERT_EQ(1, CountCompleteTimingReported()); |
394 | 405 |
395 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); | 406 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); |
396 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); | 407 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); |
397 CheckTotalErrorEvents(); | 408 CheckTotalErrorEvents(); |
398 | 409 |
399 histogram_tester_.ExpectTotalCount( | 410 histogram_tester_.ExpectTotalCount( |
400 page_load_metrics::internal::kPageLoadTimingStatus, 1); | 411 page_load_metrics::internal::kPageLoadTimingStatus, 1); |
401 histogram_tester_.ExpectBucketCount( | 412 histogram_tester_.ExpectBucketCount( |
402 page_load_metrics::internal::kPageLoadTimingStatus, | 413 page_load_metrics::internal::kPageLoadTimingStatus, |
403 page_load_metrics::internal::INVALID_NULL_NAVIGATION_START, 1); | 414 page_load_metrics::internal::INVALID_NULL_NAVIGATION_START, 1); |
404 } | 415 } |
405 | 416 |
406 TEST_F(MetricsWebContentsObserverTest, TimingOrderError) { | 417 TEST_F(MetricsWebContentsObserverTest, TimingOrderError) { |
407 PageLoadTiming timing; | 418 mojom::PageLoadTiming timing; |
| 419 page_load_metrics::InitPageLoadTimingForTest(&timing); |
408 timing.navigation_start = base::Time::FromDoubleT(1); | 420 timing.navigation_start = base::Time::FromDoubleT(1); |
409 timing.parse_timing.parse_stop = base::TimeDelta::FromMilliseconds(1); | 421 timing.parse_timing->parse_stop = base::TimeDelta::FromMilliseconds(1); |
410 | 422 |
411 content::WebContentsTester* web_contents_tester = | 423 content::WebContentsTester* web_contents_tester = |
412 content::WebContentsTester::For(web_contents()); | 424 content::WebContentsTester::For(web_contents()); |
413 | 425 |
414 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 426 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
415 SimulateTimingUpdate(timing); | 427 SimulateTimingUpdate(timing); |
416 ASSERT_EQ(0, CountUpdatedTimingReported()); | 428 ASSERT_EQ(0, CountUpdatedTimingReported()); |
417 NavigateToUntrackedUrl(); | 429 NavigateToUntrackedUrl(); |
418 ASSERT_EQ(0, CountUpdatedTimingReported()); | 430 ASSERT_EQ(0, CountUpdatedTimingReported()); |
419 ASSERT_EQ(1, CountCompleteTimingReported()); | 431 ASSERT_EQ(1, CountCompleteTimingReported()); |
420 | 432 |
421 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); | 433 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING, 1); |
422 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); | 434 CheckErrorEvent(ERR_NO_IPCS_RECEIVED, 1); |
423 CheckTotalErrorEvents(); | 435 CheckTotalErrorEvents(); |
424 | 436 |
425 histogram_tester_.ExpectTotalCount( | 437 histogram_tester_.ExpectTotalCount( |
426 page_load_metrics::internal::kPageLoadTimingStatus, 1); | 438 page_load_metrics::internal::kPageLoadTimingStatus, 1); |
427 histogram_tester_.ExpectBucketCount( | 439 histogram_tester_.ExpectBucketCount( |
428 page_load_metrics::internal::kPageLoadTimingStatus, | 440 page_load_metrics::internal::kPageLoadTimingStatus, |
429 page_load_metrics::internal::INVALID_ORDER_PARSE_START_PARSE_STOP, 1); | 441 page_load_metrics::internal::INVALID_ORDER_PARSE_START_PARSE_STOP, 1); |
430 } | 442 } |
431 | 443 |
432 TEST_F(MetricsWebContentsObserverTest, NotInMainError) { | 444 TEST_F(MetricsWebContentsObserverTest, NotInMainError) { |
433 PageLoadTiming timing; | 445 mojom::PageLoadTiming timing; |
| 446 page_load_metrics::InitPageLoadTimingForTest(&timing); |
434 timing.navigation_start = base::Time::FromDoubleT(1); | 447 timing.navigation_start = base::Time::FromDoubleT(1); |
435 | 448 |
436 content::WebContentsTester* web_contents_tester = | 449 content::WebContentsTester* web_contents_tester = |
437 content::WebContentsTester::For(web_contents()); | 450 content::WebContentsTester::For(web_contents()); |
438 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 451 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
439 | 452 |
440 content::RenderFrameHostTester* rfh_tester = | 453 content::RenderFrameHostTester* rfh_tester = |
441 content::RenderFrameHostTester::For(main_rfh()); | 454 content::RenderFrameHostTester::For(main_rfh()); |
442 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); | 455 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); |
443 | 456 |
444 content::RenderFrameHostTester* subframe_tester = | 457 content::RenderFrameHostTester* subframe_tester = |
445 content::RenderFrameHostTester::For(subframe); | 458 content::RenderFrameHostTester::For(subframe); |
446 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); | 459 subframe_tester->SimulateNavigationStart(GURL(kDefaultTestUrl2)); |
447 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); | 460 subframe_tester->SimulateNavigationCommit(GURL(kDefaultTestUrl2)); |
448 SimulateTimingUpdate(timing, subframe); | 461 SimulateTimingUpdate(timing, subframe); |
449 CheckErrorEvent(ERR_TIMING_IPC_FROM_SUBFRAME, 1); | 462 CheckErrorEvent(ERR_TIMING_IPC_FROM_SUBFRAME, 1); |
450 CheckTotalErrorEvents(); | 463 CheckTotalErrorEvents(); |
451 ASSERT_EQ(0, CountUpdatedTimingReported()); | 464 ASSERT_EQ(0, CountUpdatedTimingReported()); |
452 ASSERT_EQ(0, CountCompleteTimingReported()); | 465 ASSERT_EQ(0, CountCompleteTimingReported()); |
453 } | 466 } |
454 | 467 |
455 TEST_F(MetricsWebContentsObserverTest, BadIPC) { | 468 TEST_F(MetricsWebContentsObserverTest, BadIPC) { |
456 PageLoadTiming timing; | 469 mojom::PageLoadTiming timing; |
| 470 page_load_metrics::InitPageLoadTimingForTest(&timing); |
457 timing.navigation_start = base::Time::FromDoubleT(10); | 471 timing.navigation_start = base::Time::FromDoubleT(10); |
458 PageLoadTiming timing2; | 472 mojom::PageLoadTiming timing2; |
| 473 page_load_metrics::InitPageLoadTimingForTest(&timing2); |
459 timing2.navigation_start = base::Time::FromDoubleT(100); | 474 timing2.navigation_start = base::Time::FromDoubleT(100); |
460 | 475 |
461 content::WebContentsTester* web_contents_tester = | 476 content::WebContentsTester* web_contents_tester = |
462 content::WebContentsTester::For(web_contents()); | 477 content::WebContentsTester::For(web_contents()); |
463 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 478 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
464 | 479 |
465 SimulateTimingUpdate(timing); | 480 SimulateTimingUpdate(timing); |
466 ASSERT_EQ(1, CountUpdatedTimingReported()); | 481 ASSERT_EQ(1, CountUpdatedTimingReported()); |
467 SimulateTimingUpdate(timing2); | 482 SimulateTimingUpdate(timing2); |
468 ASSERT_EQ(1, CountUpdatedTimingReported()); | 483 ASSERT_EQ(1, CountUpdatedTimingReported()); |
469 | 484 |
470 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING_DESCENDENT, 1); | 485 CheckErrorEvent(ERR_BAD_TIMING_IPC_INVALID_TIMING_DESCENDENT, 1); |
471 CheckTotalErrorEvents(); | 486 CheckTotalErrorEvents(); |
472 } | 487 } |
473 | 488 |
474 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { | 489 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { |
475 // Reset the state of the tests, and attach the MetricsWebContentsObserver in | 490 // Reset the state of the tests, and attach the MetricsWebContentsObserver in |
476 // the middle of a navigation. This tests that the class is robust to only | 491 // the middle of a navigation. This tests that the class is robust to only |
477 // observing some of a navigation. | 492 // observing some of a navigation. |
478 DeleteContents(); | 493 DeleteContents(); |
479 SetContents(CreateTestWebContents()); | 494 SetContents(CreateTestWebContents()); |
480 | 495 |
481 PageLoadTiming timing; | 496 mojom::PageLoadTiming timing; |
| 497 page_load_metrics::InitPageLoadTimingForTest(&timing); |
482 timing.navigation_start = base::Time::FromDoubleT(10); | 498 timing.navigation_start = base::Time::FromDoubleT(10); |
483 | 499 |
484 content::WebContentsTester* web_contents_tester = | 500 content::WebContentsTester* web_contents_tester = |
485 content::WebContentsTester::For(web_contents()); | 501 content::WebContentsTester::For(web_contents()); |
486 content::RenderFrameHostTester* rfh_tester = | 502 content::RenderFrameHostTester* rfh_tester = |
487 content::RenderFrameHostTester::For(main_rfh()); | 503 content::RenderFrameHostTester::For(main_rfh()); |
488 | 504 |
489 // Start the navigation, then start observing the web contents. This used to | 505 // Start the navigation, then start observing the web contents. This used to |
490 // crash us. Make sure we bail out and don't log histograms. | 506 // crash us. Make sure we bail out and don't log histograms. |
491 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); | 507 web_contents_tester->StartNavigation(GURL(kDefaultTestUrl)); |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
663 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); | 679 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl2)); |
664 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), | 680 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl)}), |
665 completed_filtered_urls()); | 681 completed_filtered_urls()); |
666 | 682 |
667 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); | 683 web_contents_tester->NavigateAndCommit(GURL(kDefaultTestUrl)); |
668 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), | 684 ASSERT_EQ(std::vector<GURL>({GURL(kDefaultTestUrl), GURL(kDefaultTestUrl2)}), |
669 completed_filtered_urls()); | 685 completed_filtered_urls()); |
670 } | 686 } |
671 | 687 |
672 } // namespace page_load_metrics | 688 } // namespace page_load_metrics |
OLD | NEW |