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 | |
28 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) { | 20 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, TestMetrics) { |
29 content::WebContents* web_contents = | 21 content::WebContents* web_contents = |
30 browser()->tab_strip_model()->GetActiveWebContents(); | 22 browser()->tab_strip_model()->GetActiveWebContents(); |
31 | 23 |
32 NavigationMetricsRecorder* recorder = | 24 NavigationMetricsRecorder* recorder = |
33 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents( | 25 content::WebContentsUserData<NavigationMetricsRecorder>::FromWebContents( |
34 web_contents); | 26 web_contents); |
35 ASSERT_TRUE(recorder); | 27 ASSERT_TRUE(recorder); |
36 rappor::TestRapporServiceImpl rappor_service; | 28 rappor::TestRapporServiceImpl rappor_service; |
37 recorder->set_rappor_service_for_testing(&rappor_service); | 29 recorder->set_rappor_service_for_testing(&rappor_service); |
38 | 30 |
39 base::HistogramTester histograms; | 31 base::HistogramTester histograms; |
40 ui_test_utils::NavigateToURL(browser(), | 32 ui_test_utils::NavigateToURL(browser(), |
41 GURL("data:text/html, <html></html>")); | 33 GURL("data:text/html, <html></html>")); |
42 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); | 34 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); |
43 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); | 35 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); |
44 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); | 36 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); |
45 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 37 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
46 5 /* data: */, 1); | 38 5 /* data: */, 1); |
47 // Since there was no previous URL, Rappor shouldn't record anything. | 39 // Since there was no previous URL, Rappor shouldn't record anything. |
48 EXPECT_EQ(0, rappor_service.GetReportsCount()); | 40 EXPECT_EQ(0, rappor_service.GetReportsCount()); |
49 | |
50 // Navigate to an empty page and redirect it to a data: URL. Rappor should | |
51 // record a report. | |
52 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | |
53 content::TestNavigationObserver observer(web_contents, 1); | |
54 EXPECT_TRUE(content::ExecuteScript( | |
55 web_contents, "window.location.href='data:text/html, <html></html>'")); | |
56 observer.Wait(); | |
57 | |
58 EXPECT_EQ(1, rappor_service.GetReportsCount()); | |
59 std::string sample; | |
60 rappor::RapporType type; | |
61 EXPECT_TRUE(rappor_service.GetRecordedSampleForMetric( | |
62 "Navigation.Scheme.Data", &sample, &type)); | |
63 EXPECT_EQ("about://", sample); | |
64 EXPECT_EQ(rappor::ETLD_PLUS_ONE_RAPPOR_TYPE, type); | |
65 } | 41 } |
66 | 42 |
67 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, DataURLMimeTypes) { | 43 IN_PROC_BROWSER_TEST_F(NavigationMetricsRecorderBrowserTest, DataURLMimeTypes) { |
68 base::HistogramTester histograms; | 44 base::HistogramTester histograms; |
69 content::WebContents* web_contents = | |
70 browser()->tab_strip_model()->GetActiveWebContents(); | |
71 | 45 |
72 // HTML: | 46 // HTML: |
73 RedirectToUrl(web_contents, GURL("data:text/html, <html></html>")); | 47 ui_test_utils::NavigateToURL(browser(), |
| 48 GURL("data:text/html, <html></html>")); |
74 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); | 49 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 1); |
75 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); | 50 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 1); |
76 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); | 51 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 1); |
77 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 52 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
78 5 /* data: */, 1); | 53 5 /* data: */, 1); |
79 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 1); | 54 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0); |
80 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", | |
81 NavigationMetricsRecorder::MIME_TYPE_HTML, 1); | |
82 | 55 |
83 // SVG: | 56 // SVG: |
84 RedirectToUrl(web_contents, | 57 ui_test_utils::NavigateToURL( |
85 GURL("data:image/svg+xml,<!DOCTYPE svg><svg version=\"1.1\" " | 58 browser(), GURL("data:image/svg+xml,<!DOCTYPE svg><svg version=\"1.1\" " |
86 "xmlns=\"http://www.w3.org/2000/svg\"></svg>")); | 59 "xmlns=\"http://www.w3.org/2000/svg\"></svg>")); |
87 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 2); | 60 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 2); |
88 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 2); | 61 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 2); |
89 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 2); | 62 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 2); |
90 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 63 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
91 5 /* data: */, 2); | 64 5 /* data: */, 2); |
92 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 2); | 65 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0); |
93 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", | |
94 NavigationMetricsRecorder::MIME_TYPE_SVG, 1); | |
95 | 66 |
96 // Base64 encoded HTML: | 67 // Base64 encoded HTML: |
97 RedirectToUrl(web_contents, | 68 ui_test_utils::NavigateToURL( |
98 GURL("data:text/html;base64, PGh0bWw+YmFzZTY0PC9odG1sPg==")); | 69 browser(), GURL("data:text/html;base64, PGh0bWw+YmFzZTY0PC9odG1sPg==")); |
99 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 3); | 70 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 3); |
100 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 3); | 71 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 3); |
101 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 3); | 72 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 3); |
102 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 73 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
103 5 /* data: */, 3); | 74 5 /* data: */, 3); |
104 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 3); | 75 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0); |
105 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", | |
106 NavigationMetricsRecorder::MIME_TYPE_HTML, 2); | |
107 | 76 |
108 // Plain text: | 77 // Plain text: |
109 RedirectToUrl(web_contents, GURL("data:text/plain, test")); | 78 ui_test_utils::NavigateToURL(browser(), GURL("data:text/plain, test")); |
110 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 4); | 79 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 4); |
111 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 4); | 80 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 4); |
112 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 4); | 81 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 4); |
113 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 82 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
114 5 /* data: */, 4); | 83 5 /* data: */, 4); |
115 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 4); | 84 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0); |
116 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", | |
117 NavigationMetricsRecorder::MIME_TYPE_OTHER, 1); | |
118 | 85 |
119 // Base64 encoded PNG: | 86 // Base64 encoded PNG: |
120 RedirectToUrl( | 87 ui_test_utils::NavigateToURL( |
121 web_contents, | 88 browser(), |
122 GURL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" | 89 GURL("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" |
123 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" | 90 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" |
124 "9TXL0Y4OHwAAAABJRU5ErkJggg==")); | 91 "9TXL0Y4OHwAAAABJRU5ErkJggg==")); |
125 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 5); | 92 histograms.ExpectTotalCount("Navigation.MainFrameScheme", 5); |
126 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 5); | 93 histograms.ExpectBucketCount("Navigation.MainFrameScheme", 5 /* data: */, 5); |
127 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 5); | 94 histograms.ExpectTotalCount("Navigation.MainFrameSchemeDifferentPage", 5); |
128 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", | 95 histograms.ExpectBucketCount("Navigation.MainFrameSchemeDifferentPage", |
129 5 /* data: */, 5); | 96 5 /* data: */, 5); |
130 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 5); | 97 histograms.ExpectTotalCount("Navigation.MainFrameScheme.DataUrl.MimeType", 0); |
131 histograms.ExpectBucketCount("Navigation.MainFrameScheme.DataUrl.MimeType", | |
132 NavigationMetricsRecorder::MIME_TYPE_OTHER, 2); | |
133 } | 98 } |
134 | 99 |
135 } // namespace | 100 } // namespace |
OLD | NEW |