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

Side by Side Diff: chrome/browser/page_load_metrics/metrics_web_contents_observer_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698