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 observer_.reset(MetricsWebContentsObserver::CreateForWebContents( |
131 web_contents(), base::WrapUnique(embedder_interface_))); | 146 web_contents(), base::WrapUnique(embedder_interface_))); |
132 observer_->WasShown(); | 147 observer_->WasShown(); |
| 148 add_throttling_observer_.reset( |
| 149 new AddThrottleWebContentsObserver(web_contents())); |
133 } | 150 } |
134 | 151 |
135 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { | 152 void CheckErrorEvent(InternalErrorLoadEvent error, int count) { |
136 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); | 153 histogram_tester_.ExpectBucketCount(internal::kErrorEvents, error, count); |
137 num_errors_ += count; | 154 num_errors_ += count; |
138 } | 155 } |
139 | 156 |
140 void CheckTotalErrorEvents() { | 157 void CheckTotalErrorEvents() { |
141 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); | 158 histogram_tester_.ExpectTotalCount(internal::kErrorEvents, num_errors_); |
142 } | 159 } |
(...skipping 19 matching lines...) Expand all Loading... |
162 } | 179 } |
163 | 180 |
164 const std::vector<GURL>& observed_committed_urls_from_on_start() const { | 181 const std::vector<GURL>& observed_committed_urls_from_on_start() const { |
165 return embedder_interface_->observed_committed_urls_from_on_start(); | 182 return embedder_interface_->observed_committed_urls_from_on_start(); |
166 } | 183 } |
167 | 184 |
168 protected: | 185 protected: |
169 base::HistogramTester histogram_tester_; | 186 base::HistogramTester histogram_tester_; |
170 TestPageLoadMetricsEmbedderInterface* embedder_interface_; | 187 TestPageLoadMetricsEmbedderInterface* embedder_interface_; |
171 std::unique_ptr<MetricsWebContentsObserver> observer_; | 188 std::unique_ptr<MetricsWebContentsObserver> observer_; |
| 189 std::unique_ptr<AddThrottleWebContentsObserver> add_throttling_observer_; |
172 | 190 |
173 private: | 191 private: |
174 int num_errors_; | 192 int num_errors_; |
175 | 193 |
176 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); | 194 DISALLOW_COPY_AND_ASSIGN(MetricsWebContentsObserverTest); |
177 }; | 195 }; |
178 | 196 |
179 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { | 197 TEST_F(MetricsWebContentsObserverTest, SuccessfulMainFrameNavigation) { |
180 PageLoadTiming timing; | 198 PageLoadTiming timing; |
181 timing.navigation_start = base::Time::FromDoubleT(1); | 199 timing.navigation_start = base::Time::FromDoubleT(1); |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 SimulateTimingUpdate(timing); | 373 SimulateTimingUpdate(timing); |
356 ASSERT_EQ(1, CountUpdatedTimingReported()); | 374 ASSERT_EQ(1, CountUpdatedTimingReported()); |
357 SimulateTimingUpdate(timing2); | 375 SimulateTimingUpdate(timing2); |
358 ASSERT_EQ(1, CountUpdatedTimingReported()); | 376 ASSERT_EQ(1, CountUpdatedTimingReported()); |
359 | 377 |
360 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); | 378 CheckErrorEvent(ERR_BAD_TIMING_IPC, 1); |
361 CheckTotalErrorEvents(); | 379 CheckTotalErrorEvents(); |
362 } | 380 } |
363 | 381 |
364 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { | 382 TEST_F(MetricsWebContentsObserverTest, ObservePartialNavigation) { |
365 // Delete the observer for this test, add it once the navigation has started. | 383 // Reset the state of the tests, and attach the MetricsWebContentsObserver in |
366 observer_.reset(); | 384 // the middle of a navigation. This tests that the class is robust to only |
| 385 // observing some of a navigation. |
| 386 DeleteContents(); |
| 387 SetContents(CreateTestWebContents()); |
| 388 |
367 PageLoadTiming timing; | 389 PageLoadTiming timing; |
368 timing.navigation_start = base::Time::FromDoubleT(10); | 390 timing.navigation_start = base::Time::FromDoubleT(10); |
369 | 391 |
370 content::WebContentsTester* web_contents_tester = | 392 content::WebContentsTester* web_contents_tester = |
371 content::WebContentsTester::For(web_contents()); | 393 content::WebContentsTester::For(web_contents()); |
372 content::RenderFrameHostTester* rfh_tester = | 394 content::RenderFrameHostTester* rfh_tester = |
373 content::RenderFrameHostTester::For(main_rfh()); | 395 content::RenderFrameHostTester::For(main_rfh()); |
374 | 396 |
375 // Start the navigation, then start observing the web contents. This used to | 397 // 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. | 398 // 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(); | 489 rfh_tester->SimulateNavigationStop(); |
468 | 490 |
469 web_contents()->Stop(); | 491 web_contents()->Stop(); |
470 | 492 |
471 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); | 493 histogram_tester_.ExpectTotalCount(internal::kAbortChainSizeNoCommit, 1); |
472 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, | 494 histogram_tester_.ExpectBucketCount(internal::kAbortChainSizeNoCommit, 3, |
473 1); | 495 1); |
474 } | 496 } |
475 | 497 |
476 } // namespace page_load_metrics | 498 } // namespace page_load_metrics |
OLD | NEW |