1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/metrics/histogram.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/extensions/activity_log/uma_policy.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/test_extension_dir.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/common/extensions/extension_file_util.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "net/dns/mock_host_resolver.h"
18 using extensions::UmaPolicy
;
20 const char* kGooglePrefix
= "ExtensionActivity.Google";
21 const char* kNonGooglePrefix
= "ExtensionActivity";
23 // These tests need to ensure that all of the extension JavaScript completes
24 // before the histograms are checked. To accomplish this, the test relies on
25 // some JavaScript in chrome/test/data/extensions/api_test/uma_policy/:
26 // * When the test navigates to opener.com, opener.js will use window.open() to
27 // pop open a new window with the appropriate URL for the test case. This
28 // ensures that the testing framework never reuses a window that's still
29 // running a previous test case.
30 // * The test extension code in content_script.js tells background.js when it's
31 // done. When it's finished, background.js closes the blocker.com window. So
32 // blocker.com will remain open (and block) until the tests complete.
33 class ActivityLogUmaPolicyTest
: public ExtensionApiTest
{
36 std::string
ConcatNames(const char* prefix
, int status_num
) {
37 return base::StringPrintf(
40 extensions::UmaPolicy::GetHistogramName(
41 static_cast<extensions::UmaPolicy::PageStatus
>(status_num
)));
44 // TODO(felt): These are disabled due to crbug.com/294500, since they fail
45 // due to a blink bug. The fix went in to Blink on Thursday and should roll
47 // These are all sequential navigations, so they should each be logged
49 IN_PROC_BROWSER_TEST_F(ActivityLogUmaPolicyTest
, DISABLED_SequentialNavs
) {
50 host_resolver()->AddRule("*", "127.0.0.1");
51 StartEmbeddedTestServer();
53 const extensions::Extension
* ext
=
54 LoadExtension(test_data_dir_
.AppendASCII("uma_policy"));
57 ui_test_utils::NavigateToURLWithDisposition(
58 browser(), GURL("http://www.opener.com/#google"), NEW_WINDOW
,
59 ui_test_utils::BROWSER_TEST_NONE
);
60 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
61 browser(), GURL("http://www.blocker.com"), 2);
62 ui_test_utils::NavigateToURLWithDisposition(
63 browser(), GURL("http://www.opener.com/#google?q=a"), NEW_WINDOW
,
64 ui_test_utils::BROWSER_TEST_NONE
);
65 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
66 browser(), GURL("http://www.blocker.com"), 2);
67 ui_test_utils::NavigateToURLWithDisposition(
68 browser(), GURL("http://www.opener.com/#google?q=b"), NEW_WINDOW
,
69 ui_test_utils::BROWSER_TEST_NONE
);
70 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
71 browser(), GURL("http://www.blocker.com"), 2);
72 ui_test_utils::NavigateToURLWithDisposition(
73 browser(), GURL("http://www.opener.com/#cnn?q=a"), NEW_WINDOW
,
74 ui_test_utils::BROWSER_TEST_NONE
);
75 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
76 browser(), GURL("http://www.blocker.com"), 2);
77 ui_test_utils::NavigateToURLWithDisposition(
78 browser(), GURL("http://www.opener.com/#cnn?q=b"), NEW_WINDOW
,
79 ui_test_utils::BROWSER_TEST_NONE
);
80 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
81 browser(), GURL("http://www.blocker.com"), 2);
83 for (int i
= UmaPolicy::NONE
+ 1; i
< UmaPolicy::MAX_STATUS
- 2; ++i
) {
84 base::HistogramBase
* google_histogram
= base::Histogram::FactoryGet(
85 ConcatNames(kGooglePrefix
, i
),
86 1, 100, 50, base::HistogramBase::kNoFlags
);
87 scoped_ptr
<base::HistogramSamples
> google_samples
=
88 google_histogram
->SnapshotSamples();
89 EXPECT_EQ(3, google_samples
->TotalCount());
90 EXPECT_EQ(3, google_samples
->GetCount(1));
92 base::HistogramBase
* cnn_histogram
= base::Histogram::FactoryGet(
93 ConcatNames(kNonGooglePrefix
, i
),
94 1, 100, 50, base::HistogramBase::kNoFlags
);
95 scoped_ptr
<base::HistogramSamples
> cnn_samples
=
96 cnn_histogram
->SnapshotSamples();
97 if (ConcatNames(kNonGooglePrefix
, i
) != "ExtensionActivity.ContentScript" &&
98 ConcatNames(kNonGooglePrefix
, i
) != "ExtensionActivity.ReadDom") {
99 // There's a content script on opener.com that checks the location.
100 // The test is not set up to accurately record opener.com histograms due
101 // to the possibility of race conditions in the testing framework, so we
102 // can't check those values.
103 EXPECT_EQ(2, cnn_samples
->GetCount(1));
108 // Two windows are open at once with the same google.com TLD.
109 // However, they should be treated separately because they have different URLs.
110 IN_PROC_BROWSER_TEST_F(
111 ActivityLogUmaPolicyTest
, DISABLED_ParallelDistinctNavs
) {
112 host_resolver()->AddRule("*", "127.0.0.1");
113 StartEmbeddedTestServer();
115 const extensions::Extension
* ext
=
116 LoadExtension(test_data_dir_
.AppendASCII("uma_policy"));
119 ui_test_utils::NavigateToURLWithDisposition(
120 browser(), GURL("http://www.opener.com/#google?p=a"), NEW_WINDOW
,
121 ui_test_utils::BROWSER_TEST_NONE
);
122 ui_test_utils::NavigateToURLWithDisposition(
123 browser(), GURL("http://www.opener.com/#google?p=b"), NEW_WINDOW
,
124 ui_test_utils::BROWSER_TEST_NONE
);
125 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
126 browser(), GURL("http://www.blocker.com"), 2);
128 for (int i
= UmaPolicy::NONE
+ 1; i
< UmaPolicy::MAX_STATUS
- 2; ++i
) {
129 base::HistogramBase
* google_histogram
= base::Histogram::FactoryGet(
130 ConcatNames(kGooglePrefix
, i
),
131 1, 100, 50, base::HistogramBase::kNoFlags
);
132 scoped_ptr
<base::HistogramSamples
> google_samples
=
133 google_histogram
->SnapshotSamples();
134 EXPECT_EQ(2, google_samples
->GetCount(1));
138 // Two windows are open at once with the same Google URLs.
139 // They should be treated the same.
140 IN_PROC_BROWSER_TEST_F(ActivityLogUmaPolicyTest
, DISABLED_Google_ParallelSame
) {
141 host_resolver()->AddRule("*", "127.0.0.1");
142 StartEmbeddedTestServer();
144 const extensions::Extension
* ext
=
145 LoadExtension(test_data_dir_
.AppendASCII("uma_policy"));
148 ui_test_utils::NavigateToURLWithDisposition(
149 browser(), GURL("http://www.opener.com/#googlea"), NEW_WINDOW
,
150 ui_test_utils::BROWSER_TEST_NONE
);
151 ui_test_utils::NavigateToURLWithDisposition(
152 browser(), GURL("http://www.opener.com/#googleb"), NEW_WINDOW
,
153 ui_test_utils::BROWSER_TEST_NONE
);
154 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
155 browser(), GURL("http://www.blocker.com"), 2);
157 for (int i
= UmaPolicy::NONE
+ 1; i
< UmaPolicy::MAX_STATUS
- 2; ++i
) {
158 base::HistogramBase
* google_histogram
= base::Histogram::FactoryGet(
159 ConcatNames(kGooglePrefix
, i
),
160 1, 100, 50, base::HistogramBase::kNoFlags
);
161 scoped_ptr
<base::HistogramSamples
> google_samples
=
162 google_histogram
->SnapshotSamples();
163 EXPECT_EQ(1, google_samples
->GetCount(1));
167 // Two windows are open at once with the same non-Google URLs.
168 // They should be treated the same.
169 IN_PROC_BROWSER_TEST_F(ActivityLogUmaPolicyTest
,
170 DISABLED_NonGoogle_ParallelSame
) {
171 host_resolver()->AddRule("*", "127.0.0.1");
172 StartEmbeddedTestServer();
174 const extensions::Extension
* ext
=
175 LoadExtension(test_data_dir_
.AppendASCII("uma_policy"));
178 ui_test_utils::NavigateToURLWithDisposition(
179 browser(), GURL("http://www.opener.com/#cnna"), NEW_WINDOW
,
180 ui_test_utils::BROWSER_TEST_NONE
);
181 ui_test_utils::NavigateToURLWithDisposition(
182 browser(), GURL("http://www.opener.com/#cnnb"), NEW_WINDOW
,
183 ui_test_utils::BROWSER_TEST_NONE
);
184 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
185 browser(), GURL("http://www.blocker.com"), 2);
187 for (int i
= UmaPolicy::NONE
+ 1; i
< UmaPolicy::MAX_STATUS
- 2; ++i
) {
188 base::HistogramBase
* cnn_histogram
= base::Histogram::FactoryGet(
189 ConcatNames(kNonGooglePrefix
, i
),
190 1, 100, 50, base::HistogramBase::kNoFlags
);
191 scoped_ptr
<base::HistogramSamples
> cnn_samples
=
192 cnn_histogram
->SnapshotSamples();
193 if (ConcatNames(kNonGooglePrefix
, i
) != "ExtensionActivity.ContentScript" &&
194 ConcatNames(kNonGooglePrefix
, i
) != "ExtensionActivity.ReadDom") {
195 // There's a content script on opener.com that checks the location.
196 // The test is not set up to accurately record opener.com histograms due
197 // to the possibility of race conditions in the testing framework, so we
198 // can't check those values.
199 EXPECT_EQ(1, cnn_samples
->GetCount(1));
204 // This runs with multiple extensions installed.
205 IN_PROC_BROWSER_TEST_F(ActivityLogUmaPolicyTest
, DISABLED_MultipleExtensions
) {
206 host_resolver()->AddRule("*", "127.0.0.1");
207 StartEmbeddedTestServer();
209 const extensions::Extension
* ext
=
210 LoadExtension(test_data_dir_
.AppendASCII("uma_policy"));
213 const char* script2
=
214 "document.createElement('script');"
215 "document.createElement('iframe');"
216 "document.createElement('div');"
217 "document.createElement('embed');"
218 "document.createElement('object');";
220 const char* manifest
=
222 " \"name\": \"Activity Log UMA Policy Test Extension\","
223 " \"version\": \"0.%s\","
224 " \"description\": \"Testing the histogramming\","
225 " \"content_scripts\": ["
228 " [\"http://www.google.com/*\","
229 " \"http://www.cnn.com/*\"],"
230 " \"js\": [\"content_script.js\"]"
233 " \"manifest_version\": 2"
236 extensions::TestExtensionDir dir2
;
237 dir2
.WriteManifest(base::StringPrintf(manifest
, "2"));
238 dir2
.WriteFile(FILE_PATH_LITERAL("content_script.js"), script2
);
239 LoadExtension(dir2
.unpacked_path());
241 ui_test_utils::NavigateToURLWithDisposition(
242 browser(), GURL("http://www.opener.com/#google"), NEW_WINDOW
,
243 ui_test_utils::BROWSER_TEST_NONE
);
244 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
245 browser(), GURL("http://www.blocker.com"), 2);
246 ui_test_utils::NavigateToURLWithDisposition(
247 browser(), GURL("http://www.opener.com/#cnn?q=b"), NEW_WINDOW
,
248 ui_test_utils::BROWSER_TEST_NONE
);
249 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
250 browser(), GURL("http://www.blocker.com"), 2);
252 const char* subset_one
[] = {
258 const char* subset_two
[] = {
268 // These were only touched by one of the scripts.
269 for (size_t i
= 0; i
< arraysize(subset_one
); ++i
) {
270 base::HistogramBase
* google_histogram
= base::Histogram::FactoryGet(
271 std::string(kGooglePrefix
) + "." + std::string(subset_one
[i
]),
272 1, 100, 50, base::HistogramBase::kNoFlags
);
273 scoped_ptr
<base::HistogramSamples
> google_samples
=
274 google_histogram
->SnapshotSamples();
275 EXPECT_EQ(1, google_samples
->GetCount(1));
277 base::HistogramBase
* cnn_histogram
= base::Histogram::FactoryGet(
278 std::string(kNonGooglePrefix
) + "." + std::string(subset_one
[i
]),
279 1, 100, 50, base::HistogramBase::kNoFlags
);
280 scoped_ptr
<base::HistogramSamples
> cnn_samples
=
281 cnn_histogram
->SnapshotSamples();
282 EXPECT_EQ(1, cnn_samples
->GetCount(1));
285 // These were touched by both scripts.
286 for (size_t i
= 0; i
< arraysize(subset_two
); ++i
) {
287 base::HistogramBase
* google_histogram
= base::Histogram::FactoryGet(
288 std::string(kGooglePrefix
) + "." + std::string(subset_two
[i
]),
289 1, 100, 50, base::HistogramBase::kNoFlags
);
290 scoped_ptr
<base::HistogramSamples
> google_samples
=
291 google_histogram
->SnapshotSamples();
292 EXPECT_EQ(1, google_samples
->GetCount(2));
294 base::HistogramBase
* cnn_histogram
= base::Histogram::FactoryGet(
295 std::string(kNonGooglePrefix
) + "." + std::string(subset_two
[i
]),
296 1, 100, 50, base::HistogramBase::kNoFlags
);
297 scoped_ptr
<base::HistogramSamples
> cnn_samples
=
298 cnn_histogram
->SnapshotSamples();
299 EXPECT_EQ(1, cnn_samples
->GetCount(2));