| 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 "components/page_load_metrics/browser/metrics_web_contents_observer.h" | 5 #include "components/page_load_metrics/browser/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 "components/page_load_metrics/browser/metrics_navigation_throttle.h" |
| 15 #include "components/page_load_metrics/browser/page_load_metrics_observer.h" | 16 #include "components/page_load_metrics/browser/page_load_metrics_observer.h" |
| 16 #include "components/page_load_metrics/common/page_load_metrics_messages.h" | 17 #include "components/page_load_metrics/common/page_load_metrics_messages.h" |
| 17 #include "content/public/browser/navigation_handle.h" | 18 #include "content/public/browser/navigation_handle.h" |
| 18 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 19 #include "content/public/test/test_renderer_host.h" | 20 #include "content/public/test/test_renderer_host.h" |
| 20 #include "content/public/test/web_contents_tester.h" | 21 #include "content/public/test/web_contents_tester.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| 24 namespace page_load_metrics { | 25 namespace page_load_metrics { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 private: | 97 private: |
| 97 std::vector<PageLoadTiming> updated_timings_; | 98 std::vector<PageLoadTiming> updated_timings_; |
| 98 std::vector<PageLoadTiming> complete_timings_; | 99 std::vector<PageLoadTiming> complete_timings_; |
| 99 std::vector<GURL> observed_committed_urls_; | 100 std::vector<GURL> observed_committed_urls_; |
| 100 bool is_prerendering_; | 101 bool is_prerendering_; |
| 101 bool is_ntp_; | 102 bool is_ntp_; |
| 102 }; | 103 }; |
| 103 | 104 |
| 104 } // namespace | 105 } // namespace |
| 105 | 106 |
| 107 class AddThrottleWebContentsObserver : public content::WebContentsObserver { |
| 108 public: |
| 109 AddThrottleWebContentsObserver(content::WebContents* contents) |
| 110 : content::WebContentsObserver(contents) {} |
| 111 |
| 112 void DidStartNavigation(content::NavigationHandle* handle) override { |
| 113 handle->RegisterThrottleForTesting( |
| 114 MetricsNavigationThrottle::Create(handle)); |
| 115 } |
| 116 |
| 117 private: |
| 118 DISALLOW_COPY_AND_ASSIGN(AddThrottleWebContentsObserver); |
| 119 }; |
| 120 |
| 106 class MetricsWebContentsObserverTest | 121 class MetricsWebContentsObserverTest |
| 107 : public content::RenderViewHostTestHarness { | 122 : public content::RenderViewHostTestHarness { |
| 108 public: | 123 public: |
| 109 MetricsWebContentsObserverTest() : num_errors_(0) {} | 124 MetricsWebContentsObserverTest() : num_errors_(0) {} |
| 110 | 125 |
| 111 void SetUp() override { | 126 void SetUp() override { |
| 112 RenderViewHostTestHarness::SetUp(); | 127 RenderViewHostTestHarness::SetUp(); |
| 113 AttachObserver(); | 128 AttachObserver(); |
| 114 } | 129 } |
| 115 | 130 |
| 116 void SimulateTimingUpdate(const PageLoadTiming& timing) { | 131 void SimulateTimingUpdate(const PageLoadTiming& timing) { |
| 117 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); | 132 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); |
| 118 } | 133 } |
| 119 | 134 |
| 120 void SimulateTimingUpdate(const PageLoadTiming& timing, | 135 void SimulateTimingUpdate(const PageLoadTiming& timing, |
| 121 content::RenderFrameHost* render_frame_host) { | 136 content::RenderFrameHost* render_frame_host) { |
| 122 ASSERT_TRUE(observer_->OnMessageReceived( | 137 ASSERT_TRUE(observer_->OnMessageReceived( |
| 123 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, | 138 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, |
| 124 PageLoadMetadata()), | 139 PageLoadMetadata()), |
| 125 render_frame_host)); | 140 render_frame_host)); |
| 126 } | 141 } |
| 127 | 142 |
| 128 void AttachObserver() { | 143 void AttachObserver() { |
| 129 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); | 144 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); |
| 130 observer_.reset(new MetricsWebContentsObserver( | 145 // Owned by the web_contents. Tests must be careful not to call |
| 131 web_contents(), base::WrapUnique(embedder_interface_))); | 146 // SimulateTimingUpdate after they call DeleteContents() without also |
| 147 // calling AttachObserver() again. Otherwise they will use-after-free the |
| 148 // observer_. |
| 149 observer_ = MetricsWebContentsObserver::CreateForWebContents( |
| 150 web_contents(), base::WrapUnique(embedder_interface_)); |
| 132 observer_->WasShown(); | 151 observer_->WasShown(); |
| 152 |
| 153 add_throttling_observer_.reset( |
| 154 new AddThrottleWebContentsObserver(web_contents())); |
| 133 } | 155 } |
| 134 | 156 |
| 135 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { | 157 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { |
| 136 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); | 158 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); |
| 137 num_errors_ += count; | 159 num_errors_ += count; |
| 138 } | 160 } |
| 139 | 161 |
| 140 void CheckTotalErrorEvents() { | 162 void CheckTotalErrorEvents() { |
| 141 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); | 163 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); |
| 142 } | 164 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 161 return embedder_interface_->updated_timings().size(); | 183 return embedder_interface_->updated_timings().size(); |
| 162 } | 184 } |
| 163 | 185 |
| 164 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 186 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
| 165 return embedder_interface_->observed_committed_urls_from_on_start(); | 187 return embedder_interface_->observed_committed_urls_from_on_start(); |
| 166 } | 188 } |
| 167 | 189 |
| 168 protected: | 190 protected: |
| 169 base::HistogramTester histogram_tester_; | 191 base::HistogramTester histogram_tester_; |
| 170 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 192 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
| 171 std::unique_ptr<MetricsWebContentsObserver> observer_; | 193 MetricsWebContentsObserver* observer_; |
| 194 std::unique_ptr<AddThrottleWebContentsObserver> add_throttling_observer_; |
| 172 | 195 |
| 173 private: | 196 private: |
| 174 int num_errors_; | 197 int num_errors_; |
| 175 | 198 |
| 176 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 199 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
| 177 }; | 200 }; |
| 178 | 201 |
| 179 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { | 202 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { |
| 180 PageLoadTiming timing; | 203 PageLoadTiming timing; |
| 181 timing.navigation_start = base::Time::FromDoubleT(1); | 204 timing.navigation_start = base::Time::FromDoubleT(1); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 SimulateTimingUpdate(timing); | 378 SimulateTimingUpdate(timing); |
| 356 ASSERT_EQ(1, CountUpdatedTimingReported()); | 379 ASSERT_EQ(1, CountUpdatedTimingReported()); |
| 357 SimulateTimingUpdate(timing2); | 380 SimulateTimingUpdate(timing2); |
| 358 ASSERT_EQ(1, CountUpdatedTimingReported()); | 381 ASSERT_EQ(1, CountUpdatedTimingReported()); |
| 359 | 382 |
| 360 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); | 383 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); |
| 361 CheckTotalErrorEvents(); | 384 CheckTotalErrorEvents(); |
| 362 } | 385 } |
| 363 | 386 |
| 364 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { | 387 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { |
| 365 // Delete the observer for this test, add it once the navigation has started. | 388 // Reset the state of the tests, and attach the MetricsWebContentsObserver in |
| 366 observer_.reset(); | 389 // the middle of a navigation. This tests that the class is robust to only |
| 390 // observing some of a navigation. |
| 391 DeleteContents(); |
| 392 SetContents(CreateTestWebContents()); |
| 393 |
| 367 PageLoadTiming timing; | 394 PageLoadTiming timing; |
| 368 timing.navigation_start = base::Time::FromDoubleT(10); | 395 timing.navigation_start = base::Time::FromDoubleT(10); |
| 369 | 396 |
| 370 content::WebContentsTester* web_contents_tester = | 397 content::WebContentsTester* web_contents_tester = |
| 371 content::WebContentsTester::For(web_contents()); | 398 content::WebContentsTester::For(web_contents()); |
| 372 content::RenderFrameHostTester* rfh_tester = | 399 content::RenderFrameHostTester* rfh_tester = |
| 373 content::RenderFrameHostTester::For(main_rfh()); | 400 content::RenderFrameHostTester::For(main_rfh()); |
| 374 | 401 |
| 375 // Start the navigation, then start observing the web contents. This used to | 402 // 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. | 403 // crash us. Make sure we bail out and don't log histograms. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 rfh_tester->SimulateNavigationStop(); | 494 rfh_tester->SimulateNavigationStop(); |
| 468 | 495 |
| 469 web_contents()->Stop(); | 496 web_contents()->Stop(); |
| 470 | 497 |
| 471 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); | 498 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); |
| 472 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, | 499 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, |
| 473 1); | 500 1); |
| 474 } | 501 } |
| 475 | 502 |
| 476 } // namespace page_load_metrics | 503 } // namespace page_load_metrics |
| OLD | NEW |