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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
97 private: | 98 private: |
98 std::vector<PageLoadTiming> updated_timings_; | 99 std::vector<PageLoadTiming> updated_timings_; |
99 std::vector<PageLoadTiming> complete_timings_; | 100 std::vector<PageLoadTiming> complete_timings_; |
100 std::vector<GURL> observed_committed_urls_; | 101 std::vector<GURL> observed_committed_urls_; |
101 bool is_prerendering_; | 102 bool is_prerendering_; |
102 bool is_ntp_; | 103 bool is_ntp_; |
103 }; | 104 }; |
104 | 105 |
105 } // namespace | 106 } // namespace |
106 | 107 |
107 class MetricsWebContentsObserverTest : public ChromeRenderViewHostTestHarness { | 108 class MetricsWebContentsObserverTest |
Charlie Harrison
2016/07/28 01:29:59
Fixed this formatting in next CL.
| |
109 : public ChromeRenderViewHostTestHarness { | |
108 public: | 110 public: |
109 MetricsWebContentsObserverTest() : num_errors_(0) {} | 111 MetricsWebContentsObserverTest() : num_errors_(0) {} |
110 | 112 |
111 void SetUp() override { | 113 void SetUp() override { |
112 ChromeRenderViewHostTestHarness::SetUp(); | 114 ChromeRenderViewHostTestHarness::SetUp(); |
113 AttachObserver(); | 115 AttachObserver(); |
114 } | 116 } |
115 | 117 |
116 void SimulateTimingUpdate(const PageLoadTiming& timing) { | 118 void SimulateTimingUpdate(const PageLoadTiming& timing) { |
117 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); | 119 SimulateTimingUpdate(timing, web_contents()->GetMainFrame()); |
118 } | 120 } |
119 | 121 |
120 void SimulateTimingUpdate(const PageLoadTiming& timing, | 122 void SimulateTimingUpdate(const PageLoadTiming& timing, |
121 content::RenderFrameHost* render_frame_host) { | 123 content::RenderFrameHost* render_frame_host) { |
122 ASSERT_TRUE(observer_->OnMessageReceived( | 124 ASSERT_TRUE(observer_->OnMessageReceived( |
123 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, | 125 PageLoadMetricsMsg_TimingUpdated(observer_->routing_id(), timing, |
124 PageLoadMetadata()), | 126 PageLoadMetadata()), |
125 render_frame_host)); | 127 render_frame_host)); |
126 } | 128 } |
127 | 129 |
128 void AttachObserver() { | 130 void AttachObserver() { |
129 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); | 131 embedder_interface_ = new TestPageLoadMetricsEmbedderInterface(); |
130 observer_.reset(new MetricsWebContentsObserver( | 132 // Owned by the web_contents. Tests must be careful not to call |
131 web_contents(), base::WrapUnique(embedder_interface_))); | 133 // SimulateTimingUpdate after they call DeleteContents() without also |
134 // calling AttachObserver() again. Otherwise they will use-after-free the | |
135 // observer_. | |
136 observer_ = MetricsWebContentsObserver::CreateForWebContents( | |
137 web_contents(), base::WrapUnique(embedder_interface_)); | |
132 observer_->WasShown(); | 138 observer_->WasShown(); |
133 } | 139 } |
134 | 140 |
135 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { | 141 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { |
136 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); | 142 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); |
137 num_errors_ += count; | 143 num_errors_ += count; |
138 } | 144 } |
139 | 145 |
140 void CheckTotalErrorEvents() { | 146 void CheckTotalErrorEvents() { |
141 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); | 147 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); |
(...skipping 19 matching lines...) Expand all Loading... | |
161 return embedder_interface_->updated_timings().size(); | 167 return embedder_interface_->updated_timings().size(); |
162 } | 168 } |
163 | 169 |
164 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 170 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
165 return embedder_interface_->observed_committed_urls_from_on_start(); | 171 return embedder_interface_->observed_committed_urls_from_on_start(); |
166 } | 172 } |
167 | 173 |
168 protected: | 174 protected: |
169 base::HistogramTester histogram_tester_; | 175 base::HistogramTester histogram_tester_; |
170 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 176 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
171 std::unique_ptr<MetricsWebContentsObserver> observer_; | 177 MetricsWebContentsObserver* observer_; |
172 | 178 |
173 private: | 179 private: |
174 int num_errors_; | 180 int num_errors_; |
175 | 181 |
176 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 182 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
177 }; | 183 }; |
178 | 184 |
179 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { | 185 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { |
180 PageLoadTiming timing; | 186 PageLoadTiming timing; |
181 timing.navigation_start = base::Time::FromDoubleT(1); | 187 timing.navigation_start = base::Time::FromDoubleT(1); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 SimulateTimingUpdate(timing); | 361 SimulateTimingUpdate(timing); |
356 ASSERT_EQ(1, CountUpdatedTimingReported()); | 362 ASSERT_EQ(1, CountUpdatedTimingReported()); |
357 SimulateTimingUpdate(timing2); | 363 SimulateTimingUpdate(timing2); |
358 ASSERT_EQ(1, CountUpdatedTimingReported()); | 364 ASSERT_EQ(1, CountUpdatedTimingReported()); |
359 | 365 |
360 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); | 366 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); |
361 CheckTotalErrorEvents(); | 367 CheckTotalErrorEvents(); |
362 } | 368 } |
363 | 369 |
364 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { | 370 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { |
365 // Delete the observer for this test, add it once the navigation has started. | 371 // Reset the state of the tests, and attach the MetricsWebContentsObserver in |
366 observer_.reset(); | 372 // the middle of a navigation. This tests that the class is robust to only |
373 // observing some of a navigation. | |
374 DeleteContents(); | |
375 SetContents(CreateTestWebContents()); | |
376 | |
367 PageLoadTiming timing; | 377 PageLoadTiming timing; |
368 timing.navigation_start = base::Time::FromDoubleT(10); | 378 timing.navigation_start = base::Time::FromDoubleT(10); |
369 | 379 |
370 content::WebContentsTester* web_contents_tester = | 380 content::WebContentsTester* web_contents_tester = |
371 content::WebContentsTester::For(web_contents()); | 381 content::WebContentsTester::For(web_contents()); |
372 content::RenderFrameHostTester* rfh_tester = | 382 content::RenderFrameHostTester* rfh_tester = |
373 content::RenderFrameHostTester::For(main_rfh()); | 383 content::RenderFrameHostTester::For(main_rfh()); |
374 | 384 |
375 // Start the navigation, then start observing the web contents. This used to | 385 // 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. | 386 // crash us. Make sure we bail out and don't log histograms. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 rfh_tester->SimulateNavigationError(GURL(kDefaultTestUrl), net::ERR_ABORTED); | 476 rfh_tester->SimulateNavigationError(GURL(kDefaultTestUrl), net::ERR_ABORTED); |
467 rfh_tester->SimulateNavigationStop(); | 477 rfh_tester->SimulateNavigationStop(); |
468 | 478 |
469 web_contents()->Stop(); | 479 web_contents()->Stop(); |
470 | 480 |
471 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); | 481 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); |
472 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, 1); | 482 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, 1); |
473 } | 483 } |
474 | 484 |
475 } // namespace page_load_metrics | 485 } // namespace page_load_metrics |
OLD | NEW |