OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/process/kill.h" | 12 #include "base/process/kill.h" |
13 #include "base/test/histogram_tester.h" | 13 #include "base/test/histogram_tester.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/page_load_metrics/metrics_navigation_throttle.h" |
15 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" | 16 #include "chrome/browser/page_load_metrics/page_load_metrics_observer.h" |
16 #include "chrome/common/page_load_metrics/page_load_metrics_messages.h" | 17 #include "chrome/common/page_load_metrics/page_load_metrics_messages.h" |
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 18 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
18 #include "content/public/browser/navigation_handle.h" | 19 #include "content/public/browser/navigation_handle.h" |
19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
20 #include "content/public/test/test_renderer_host.h" | 21 #include "content/public/test/test_renderer_host.h" |
21 #include "content/public/test/web_contents_tester.h" | 22 #include "content/public/test/web_contents_tester.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
24 | 25 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 void SimulateTimingUpdate(const PageLoadTiming& timing, | 121 void SimulateTimingUpdate(const PageLoadTiming& timing, |
121 content::RenderFrameHost* render_frame_host) { | 122 content::RenderFrameHost* render_frame_host) { |
122 ASSERT_TRUE(observer_->OnMessageReceived( | 123 ASSERT_TRUE(observer_->OnMessageReceived( |
123 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, | 124 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, |
124 PageLoadMetadata()), | 125 PageLoadMetadata()), |
125 render_frame_host)); | 126 render_frame_host)); |
126 } | 127 } |
127 | 128 |
128 void AttachObserver() { | 129 void AttachObserver() { |
129 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); | 130 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); |
130 observer_.reset(new MetricsWebContentsObserver( | 131 // Owned by the web_contents. Tests must be careful not to call |
131 web_contents(), base::WrapUnique(embedder_interface_))); | 132 // SimulateTimingUpdate after they call DeleteContents() without also |
| 133 // calling AttachObserver() again. Otherwise they will use-after-free the |
| 134 // observer_. |
| 135 observer_ = MetricsWebContentsObserver::CreateForWebContents( |
| 136 web_contents(), base::WrapUnique(embedder_interface_)); |
132 observer_->WasShown(); | 137 observer_->WasShown(); |
133 } | 138 } |
134 | 139 |
135 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { | 140 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { |
136 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); | 141 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); |
137 num_errors_ += count; | 142 num_errors_ += count; |
138 } | 143 } |
139 | 144 |
140 void CheckTotalErrorEvents() { | 145 void CheckTotalErrorEvents() { |
141 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); | 146 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); |
(...skipping 19 matching lines...) Expand all Loading... |
161 return embedder_interface_->updated_timings().size(); | 166 return embedder_interface_->updated_timings().size(); |
162 } | 167 } |
163 | 168 |
164 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 169 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
165 return embedder_interface_->observed_committed_urls_from_on_start(); | 170 return embedder_interface_->observed_committed_urls_from_on_start(); |
166 } | 171 } |
167 | 172 |
168 protected: | 173 protected: |
169 base::HistogramTester histogram_tester_; | 174 base::HistogramTester histogram_tester_; |
170 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 175 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
171 std::unique_ptr<MetricsWebContentsObserver> observer_; | 176 MetricsWebContentsObserver* observer_; |
172 | 177 |
173 private: | 178 private: |
174 int num_errors_; | 179 int num_errors_; |
175 | 180 |
176 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 181 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
177 }; | 182 }; |
178 | 183 |
179 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { | 184 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { |
180 PageLoadTiming timing; | 185 PageLoadTiming timing; |
181 timing.navigation_start = base::Time::FromDoubleT(1); | 186 timing.navigation_start = base::Time::FromDoubleT(1); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 SimulateTimingUpdate(timing); | 360 SimulateTimingUpdate(timing); |
356 ASSERT_EQ(1, CountUpdatedTimingReported()); | 361 ASSERT_EQ(1, CountUpdatedTimingReported()); |
357 SimulateTimingUpdate(timing2); | 362 SimulateTimingUpdate(timing2); |
358 ASSERT_EQ(1, CountUpdatedTimingReported()); | 363 ASSERT_EQ(1, CountUpdatedTimingReported()); |
359 | 364 |
360 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); | 365 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); |
361 CheckTotalErrorEvents(); | 366 CheckTotalErrorEvents(); |
362 } | 367 } |
363 | 368 |
364 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { | 369 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { |
365 // Delete the observer for this test, add it once the navigation has started. | 370 // Reset the state of the tests, and attach the MetricsWebContentsObserver in |
366 observer_.reset(); | 371 // the middle of a navigation. This tests that the class is robust to only |
| 372 // observing some of a navigation. |
| 373 DeleteContents(); |
| 374 SetContents(CreateTestWebContents()); |
| 375 |
367 PageLoadTiming timing; | 376 PageLoadTiming timing; |
368 timing.navigation_start = base::Time::FromDoubleT(10); | 377 timing.navigation_start = base::Time::FromDoubleT(10); |
369 | 378 |
370 content::WebContentsTester* web_contents_tester = | 379 content::WebContentsTester* web_contents_tester = |
371 content::WebContentsTester::For(web_contents()); | 380 content::WebContentsTester::For(web_contents()); |
372 content::RenderFrameHostTester* rfh_tester = | 381 content::RenderFrameHostTester* rfh_tester = |
373 content::RenderFrameHostTester::For(main_rfh()); | 382 content::RenderFrameHostTester::For(main_rfh()); |
374 | 383 |
375 // Start the navigation, then start observing the web contents. This used to | 384 // Start the navigation, then start observing the web contents. This used to |
376 // crash us. Make sure we bail out and don't log histograms. | 385 // crash us. Make sure we bail out and don't log histograms. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 | 491 |
483 observer_->FlushMetricsOnAppEnterBackground(); | 492 observer_->FlushMetricsOnAppEnterBackground(); |
484 | 493 |
485 histogram_tester_.ExpectTotalCount( | 494 histogram_tester_.ExpectTotalCount( |
486 internal::kPageLoadCompletedAfterAppBackground, 1); | 495 internal::kPageLoadCompletedAfterAppBackground, 1); |
487 histogram_tester_.ExpectBucketCount( | 496 histogram_tester_.ExpectBucketCount( |
488 internal::kPageLoadCompletedAfterAppBackground, false, 1); | 497 internal::kPageLoadCompletedAfterAppBackground, false, 1); |
489 histogram_tester_.ExpectBucketCount( | 498 histogram_tester_.ExpectBucketCount( |
490 internal::kPageLoadCompletedAfterAppBackground, true, 0); | 499 internal::kPageLoadCompletedAfterAppBackground, true, 0); |
491 | 500 |
492 observer_.reset(); | 501 DeleteContents(); |
493 | 502 |
494 histogram_tester_.ExpectTotalCount( | 503 histogram_tester_.ExpectTotalCount( |
495 internal::kPageLoadCompletedAfterAppBackground, 2); | 504 internal::kPageLoadCompletedAfterAppBackground, 2); |
496 histogram_tester_.ExpectBucketCount( | 505 histogram_tester_.ExpectBucketCount( |
497 internal::kPageLoadCompletedAfterAppBackground, false, 1); | 506 internal::kPageLoadCompletedAfterAppBackground, false, 1); |
498 histogram_tester_.ExpectBucketCount( | 507 histogram_tester_.ExpectBucketCount( |
499 internal::kPageLoadCompletedAfterAppBackground, true, 1); | 508 internal::kPageLoadCompletedAfterAppBackground, true, 1); |
500 } | 509 } |
501 | 510 |
502 } // namespace page_load_metrics | 511 } // namespace page_load_metrics |
OLD | NEW |