| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/test/histogram_tester.h" | 5 #include "base/test/histogram_tester.h" |
| 6 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h" | 6 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h" |
| 7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 9 #include "chrome/test/base/in_process_browser_test.h" | 9 #include "chrome/test/base/in_process_browser_test.h" |
| 10 #include "chrome/test/base/ui_test_utils.h" | 10 #include "chrome/test/base/ui_test_utils.h" |
| 11 #include "components/rappor/test_rappor_service.h" | 11 #include "components/rappor/test_rappor_service.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/test/browser_test_utils.h" | 13 #include "content/public/test/browser_test_utils.h" |
| 14 #include "content/public/test/test_navigation_observer.h" | 14 #include "content/public/test/test_navigation_observer.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 typedef InProcessBrowserTest NavigationMetricsRecorderBrowserTest; | 18 typedef InProcessBrowserTest NavigationMetricsRecorderBrowserTest; |
| 19 | 19 |
| 20 // Performs a content initiated navigation to |url|. |
| 21 void RedirectToUrl(content::WebContents* web_contents, const GURL& url) { |
| 22 content::TestNavigationObserver observer(web_contents, 1); |
| 23 EXPECT_TRUE(content::ExecuteScript( |
| 24 web_contents, std::string("window.location.href='") + url.spec() + "'")); |
| 25 observer.Wait(); |
| 26 } |
| 27 |
| 20 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) { | 28 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) { |
| 21 content::WebContents* web_contents = | 29 content::WebContents* web_contents = |
| 22 browser()->tab_strip_model()->GetActiveWebContents(); | 30 browser()->tab_strip_model()->GetActiveWebContents(); |
| 23 | 31 |
| 24 NavigationMetricsRecorder* recorder = | 32 NavigationMetricsRecorder* recorder = |
| 25 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents( | 33 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents( |
| 26 web_contents); | 34 web_contents); |
| 27 ASSERT_TRUE(recorder); | 35 ASSERT_TRUE(recorder); |
| 28 rappor::TestRapporServiceImpl rappor_service; | 36 rappor::TestRapporServiceImpl rappor_service; |
| 29 recorder->set_rappor_service_for_testing(&rappor_service); | 37 recorder->set_rappor_service_for_testing(&rappor_service); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 46 | 54 |
| 47 EXPECT_EQ(1, rappor_service.GetReportsCount()); | 55 EXPECT_EQ(1, rappor_service.GetReportsCount()); |
| 48 std::string sample; | 56 std::string sample; |
| 49 rappor::RapporType type; | 57 rappor::RapporType type; |
| 50 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( | 58 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( |
| 51 "Navigation.Scheme.Data", &sample, &type)); | 59 "Navigation.Scheme.Data", &sample, &type)); |
| 52 EXPECT_EQ("about://", sample); | 60 EXPECT_EQ("about://", sample); |
| 53 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); | 61 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); |
| 54 } | 62 } |
| 55 | 63 |
| 64 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, DataURLMimeTypes) { |
| 65 base::HistogramTester histograms; |
| 66 content::WebContents* web_contents = |
| 67 browser()->tab_strip_model()->GetActiveWebContents(); |
| 68 |
| 69 // HTML: |
| 70 RedirectToUrl(web_contents, GURL("data:text/html, <html></html>")); |
| 71 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); |
| 72 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); |
| 73 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 1); |
| 74 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", |
| 75 NavigationMetricsRecorder::MIME_TYPE_HTML, 1); |
| 76 |
| 77 // SVG: |
| 78 RedirectToUrl(web_contents, |
| 79 GURL("data:image/svg+xml,<!DOCTYPE svg><svg version=\"1.1\" " |
| 80 "xmlns=\"http://www.w3.org/2000/svg\"></svg>")); |
| 81 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 2); |
| 82 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 2); |
| 83 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 2); |
| 84 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", |
| 85 NavigationMetricsRecorder::MIME_TYPE_SVG, 1); |
| 86 |
| 87 // Base64 encoded HTML: |
| 88 RedirectToUrl(web_contents, |
| 89 GURL("data:text/html;base64, PGh0bWw+YmFzZTY0PC9odG1sPg==")); |
| 90 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 3); |
| 91 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 3); |
| 92 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 3); |
| 93 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", |
| 94 NavigationMetricsRecorder::MIME_TYPE_HTML, 2); |
| 95 |
| 96 // Plain text: |
| 97 RedirectToUrl(web_contents, GURL("data:text/plain, test")); |
| 98 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 4); |
| 99 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 4); |
| 100 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 4); |
| 101 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", |
| 102 NavigationMetricsRecorder::MIME_TYPE_OTHER, 1); |
| 103 |
| 104 // Base64 encoded PNG: |
| 105 RedirectToUrl( |
| 106 web_contents, |
| 107 GURL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" |
| 108 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" |
| 109 "9TXL0Y4OHwAAAABJRU5ErkJggg==")); |
| 110 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 5); |
| 111 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 5); |
| 112 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 5); |
| 113 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", |
| 114 NavigationMetricsRecorder::MIME_TYPE_OTHER, 2); |
| 115 } |
| 116 |
| 56 } // namespace | 117 } // namespace |
| OLD | NEW |