OLD | NEW |
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 "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/test/histogram_tester.h" | 7 #include "base/test/histogram_tester.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" | 9 #include "chrome/browser/page_load_metrics/metrics_web_contents_observer.h" |
10 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob
server.h" | 10 #include "chrome/browser/page_load_metrics/observers/aborts_page_load_metrics_ob
server.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
24 #include "chrome/test/base/in_process_browser_test.h" | 24 #include "chrome/test/base/in_process_browser_test.h" |
25 #include "chrome/test/base/ui_test_utils.h" | 25 #include "chrome/test/base/ui_test_utils.h" |
26 #include "components/prefs/pref_service.h" | 26 #include "components/prefs/pref_service.h" |
27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/render_process_host.h" | 28 #include "content/public/browser/render_process_host.h" |
29 #include "content/public/browser/render_view_host.h" | 29 #include "content/public/browser/render_view_host.h" |
30 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
31 #include "content/public/test/download_test_observer.h" | 31 #include "content/public/test/download_test_observer.h" |
| 32 #include "content/public/test/web_contents_binding_set_test_binder.h" |
| 33 #include "mojo/public/cpp/bindings/associated_binding.h" |
32 #include "net/http/failing_http_transaction_factory.h" | 34 #include "net/http/failing_http_transaction_factory.h" |
33 #include "net/http/http_cache.h" | 35 #include "net/http/http_cache.h" |
34 #include "net/test/embedded_test_server/embedded_test_server.h" | 36 #include "net/test/embedded_test_server/embedded_test_server.h" |
35 #include "net/test/url_request/url_request_failed_job.h" | 37 #include "net/test/url_request/url_request_failed_job.h" |
36 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
37 #include "net/url_request/url_request_context_getter.h" | 39 #include "net/url_request/url_request_context_getter.h" |
| 40 #include "testing/gmock/include/gmock/gmock.h" |
38 | 41 |
39 namespace { | 42 namespace { |
40 | 43 |
41 // Waits until a PageLoadMetricsMsg_TimingUpdated message IPC is received | 44 // Waits until a PageLoadMetricsMsg_TimingUpdated message IPC is received |
42 // matching a PageLoadTiming. See WaitForMatchingIPC for details. | 45 // matching a PageLoadTiming. See WaitForMatchingIPC for details. |
43 class TimingUpdatedObserver : public content::BrowserMessageFilter { | 46 class TimingUpdatedObserver : public content::BrowserMessageFilter { |
44 public: | 47 public: |
45 // A bitvector to express which timing fields to match on. | 48 // A bitvector to express which timing fields to match on. |
46 enum ExpectedTimingFields { | 49 enum ExpectedTimingFields { |
47 FIRST_PAINT = 1 << 0, | 50 FIRST_PAINT = 1 << 0, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 ~TimingUpdatedObserver() override {} | 153 ~TimingUpdatedObserver() override {} |
151 | 154 |
152 std::unique_ptr<base::RunLoop> run_loop_; | 155 std::unique_ptr<base::RunLoop> run_loop_; |
153 int matching_fields_ = 0; // A bitvector composed from ExpectedTimingFields. | 156 int matching_fields_ = 0; // A bitvector composed from ExpectedTimingFields. |
154 bool matched_timing_update_ = false; | 157 bool matched_timing_update_ = false; |
155 int match_document_write_block_reload_ = 0; | 158 int match_document_write_block_reload_ = 0; |
156 }; | 159 }; |
157 | 160 |
158 } // namespace | 161 } // namespace |
159 | 162 |
| 163 namespace page_load_metrics { |
| 164 |
| 165 class MockPageLoadMetrics : public mojom::PageLoadMetrics { |
| 166 public: |
| 167 MockPageLoadMetrics() {} |
| 168 ~MockPageLoadMetrics() override {} |
| 169 MOCK_METHOD2(UpdateTiming, |
| 170 void(const PageLoadTiming&, const PageLoadMetadata&)); |
| 171 }; |
| 172 |
| 173 } // namespace page_load_metrics |
| 174 |
| 175 namespace { |
| 176 |
| 177 class MockPageLoadMetricsBinder |
| 178 : public content::WebContentsBindingSetTestBinder< |
| 179 page_load_metrics::mojom::PageLoadMetrics> { |
| 180 public: |
| 181 MockPageLoadMetricsBinder( |
| 182 page_load_metrics::mojom::PageLoadMetrics* page_load_metrics) |
| 183 : page_load_metrics_(page_load_metrics) {} |
| 184 ~MockPageLoadMetricsBinder() override {} |
| 185 |
| 186 void BindRequest(content::RenderFrameHost* frame_host, |
| 187 page_load_metrics::mojom::PageLoadMetricsAssociatedRequest |
| 188 request) override { |
| 189 bindings_.AddBinding(page_load_metrics_, std::move(request), frame_host); |
| 190 } |
| 191 |
| 192 private: |
| 193 page_load_metrics::mojom::PageLoadMetrics* page_load_metrics_; |
| 194 mojo::AssociatedBindingSet<page_load_metrics::mojom::PageLoadMetrics, |
| 195 content::RenderFrameHost*> |
| 196 bindings_; |
| 197 |
| 198 DISALLOW_COPY_AND_ASSIGN(MockPageLoadMetricsBinder); |
| 199 }; |
| 200 |
| 201 } // namespace |
| 202 |
160 class PageLoadMetricsBrowserTest : public InProcessBrowserTest { | 203 class PageLoadMetricsBrowserTest : public InProcessBrowserTest { |
161 public: | 204 public: |
162 PageLoadMetricsBrowserTest() {} | 205 PageLoadMetricsBrowserTest() {} |
163 ~PageLoadMetricsBrowserTest() override {} | 206 ~PageLoadMetricsBrowserTest() override {} |
164 | 207 |
165 protected: | 208 protected: |
166 void NavigateToUntrackedUrl() { | 209 void NavigateToUntrackedUrl() { |
167 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)); | 210 ui_test_utils::NavigateToURL(browser(), GURL(url::kAboutBlankURL)); |
168 } | 211 } |
169 | 212 |
170 bool NoPageLoadMetricsRecorded() { | 213 bool NoPageLoadMetricsRecorded() { |
171 // Determine whether any 'public' page load metrics are recorded. We exclude | 214 // Determine whether any 'public' page load metrics are recorded. We exclude |
172 // 'internal' metrics as these may be recorded for debugging purposes. | 215 // 'internal' metrics as these may be recorded for debugging purposes. |
173 size_t total_pageload_histograms = | 216 size_t total_pageload_histograms = |
174 histogram_tester_.GetTotalCountsForPrefix("PageLoad.").size(); | 217 histogram_tester_.GetTotalCountsForPrefix("PageLoad.").size(); |
175 size_t total_internal_histograms = | 218 size_t total_internal_histograms = |
176 histogram_tester_.GetTotalCountsForPrefix("PageLoad.Internal.").size(); | 219 histogram_tester_.GetTotalCountsForPrefix("PageLoad.Internal.").size(); |
177 DCHECK_GE(total_pageload_histograms, total_internal_histograms); | 220 DCHECK_GE(total_pageload_histograms, total_internal_histograms); |
178 return total_pageload_histograms - total_internal_histograms == 0; | 221 return total_pageload_histograms - total_internal_histograms == 0; |
179 } | 222 } |
180 | 223 |
181 scoped_refptr<TimingUpdatedObserver> CreateTimingUpdatedObserver() { | 224 scoped_refptr<TimingUpdatedObserver> CreateTimingUpdatedObserver() { |
182 content::WebContents* web_contents = | 225 content::WebContents* web_contents = |
183 browser()->tab_strip_model()->GetActiveWebContents(); | 226 browser()->tab_strip_model()->GetActiveWebContents(); |
184 scoped_refptr<TimingUpdatedObserver> observer(new TimingUpdatedObserver( | 227 scoped_refptr<TimingUpdatedObserver> observer(new TimingUpdatedObserver( |
185 web_contents->GetRenderViewHost()->GetWidget())); | 228 web_contents->GetRenderViewHost()->GetWidget())); |
| 229 if (!mock_page_load_metrics_) { |
| 230 mock_page_load_metrics_.reset( |
| 231 new page_load_metrics::MockPageLoadMetrics()); |
| 232 content::WebContentsBindingSet::GetForWebContents< |
| 233 page_load_metrics::mojom::PageLoadMetrics>(web_contents) |
| 234 ->SetBinderForTesting(base::MakeUnique<MockPageLoadMetricsBinder>( |
| 235 mock_page_load_metrics_.get())); |
| 236 } |
186 return observer; | 237 return observer; |
187 } | 238 } |
188 | 239 |
| 240 std::unique_ptr<page_load_metrics::MockPageLoadMetrics> |
| 241 mock_page_load_metrics_; |
189 base::HistogramTester histogram_tester_; | 242 base::HistogramTester histogram_tester_; |
190 | 243 |
191 private: | 244 private: |
192 DISALLOW_COPY_AND_ASSIGN(PageLoadMetricsBrowserTest); | 245 DISALLOW_COPY_AND_ASSIGN(PageLoadMetricsBrowserTest); |
193 }; | 246 }; |
194 | 247 |
195 void FailAllNetworkTransactions(net::URLRequestContextGetter* getter) { | 248 void FailAllNetworkTransactions(net::URLRequestContextGetter* getter) { |
196 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 249 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
197 net::HttpCache* cache( | 250 net::HttpCache* cache( |
198 getter->GetURLRequestContext()->http_transaction_factory()->GetCache()); | 251 getter->GetURLRequestContext()->http_transaction_factory()->GetCache()); |
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
777 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( | 830 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL( |
778 "/page_load_metrics/large.html")); | 831 "/page_load_metrics/large.html")); |
779 NavigateToUntrackedUrl(); | 832 NavigateToUntrackedUrl(); |
780 | 833 |
781 histogram_tester_.ExpectTotalCount(internal::kHistogramTotalBytes, 1); | 834 histogram_tester_.ExpectTotalCount(internal::kHistogramTotalBytes, 1); |
782 | 835 |
783 // Verify that there is a single sample recorded in the 10kB bucket (the size | 836 // Verify that there is a single sample recorded in the 10kB bucket (the size |
784 // of the main HTML response). | 837 // of the main HTML response). |
785 histogram_tester_.ExpectBucketCount(internal::kHistogramTotalBytes, 10, 1); | 838 histogram_tester_.ExpectBucketCount(internal::kHistogramTotalBytes, 10, 1); |
786 } | 839 } |
OLD | NEW |