Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings_unittest.cc
blobd3a18b61a92841e030da836318213e73e3dabe27
1 // Copyright (c) 2012 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/strings/string16.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
8 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
9 #include "chrome/test/base/testing_profile.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "net/cookies/canonical_cookie.h"
12 #include "net/cookies/cookie_options.h"
13 #include "net/cookies/parsed_cookie.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread;
19 namespace {
21 class MockSiteDataObserver
22 : public TabSpecificContentSettings::SiteDataObserver {
23 public:
24 explicit MockSiteDataObserver(
25 TabSpecificContentSettings* tab_specific_content_settings)
26 : SiteDataObserver(tab_specific_content_settings) {
29 virtual ~MockSiteDataObserver() {}
31 MOCK_METHOD0(OnSiteDataAccessed, void());
33 private:
34 DISALLOW_COPY_AND_ASSIGN(MockSiteDataObserver);
37 } // namespace
39 class TabSpecificContentSettingsTest : public ChromeRenderViewHostTestHarness {
40 public:
41 virtual void SetUp() override {
42 ChromeRenderViewHostTestHarness::SetUp();
43 TabSpecificContentSettings::CreateForWebContents(web_contents());
47 TEST_F(TabSpecificContentSettingsTest, BlockedContent) {
48 TabSpecificContentSettings* content_settings =
49 TabSpecificContentSettings::FromWebContents(web_contents());
50 net::CookieOptions options;
52 // Check that after initializing, nothing is blocked.
53 EXPECT_FALSE(
54 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
55 EXPECT_FALSE(
56 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
57 EXPECT_FALSE(
58 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
59 EXPECT_FALSE(
60 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
61 EXPECT_FALSE(
62 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
63 EXPECT_FALSE(content_settings->IsContentBlocked(
64 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
65 EXPECT_FALSE(content_settings->IsContentBlocked(
66 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
68 // Set a cookie, block access to images, block mediastream access and block a
69 // popup.
70 content_settings->OnCookieChanged(GURL("http://google.com"),
71 GURL("http://google.com"),
72 "A=B",
73 options,
74 false);
75 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES);
76 content_settings->SetPopupsBlocked(true);
77 MediaStreamDevicesController::MediaStreamTypeSettingsMap request_permissions;
78 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
79 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
80 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
81 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
82 content_settings->OnMediaStreamPermissionSet(GURL("http://google.com"),
83 request_permissions);
85 // Check that only the respective content types are affected.
86 EXPECT_TRUE(content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
87 EXPECT_FALSE(
88 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
89 EXPECT_FALSE(
90 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
91 EXPECT_FALSE(
92 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
93 EXPECT_TRUE(content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
94 EXPECT_TRUE(content_settings->IsContentBlocked(
95 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
96 EXPECT_TRUE(content_settings->IsContentBlocked(
97 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
98 content_settings->OnCookieChanged(GURL("http://google.com"),
99 GURL("http://google.com"),
100 "A=B",
101 options,
102 false);
104 // Block a cookie.
105 content_settings->OnCookieChanged(GURL("http://google.com"),
106 GURL("http://google.com"),
107 "C=D",
108 options,
109 true);
110 EXPECT_TRUE(
111 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
113 // Reset blocked content settings.
114 content_settings->ClearBlockedContentSettingsExceptForCookies();
115 EXPECT_FALSE(
116 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
117 EXPECT_FALSE(
118 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
119 EXPECT_FALSE(
120 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
121 EXPECT_TRUE(
122 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
123 EXPECT_FALSE(
124 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
125 EXPECT_FALSE(content_settings->IsContentBlocked(
126 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
127 EXPECT_FALSE(content_settings->IsContentBlocked(
128 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
130 content_settings->ClearCookieSpecificContentSettings();
131 EXPECT_FALSE(
132 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
133 EXPECT_FALSE(
134 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
135 EXPECT_FALSE(
136 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
137 EXPECT_FALSE(
138 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
139 EXPECT_FALSE(
140 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
141 EXPECT_FALSE(content_settings->IsContentBlocked(
142 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
143 EXPECT_FALSE(content_settings->IsContentBlocked(
144 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
147 TEST_F(TabSpecificContentSettingsTest, BlockedFileSystems) {
148 TabSpecificContentSettings* content_settings =
149 TabSpecificContentSettings::FromWebContents(web_contents());
151 // Access a file system.
152 content_settings->OnFileSystemAccessed(GURL("http://google.com"), false);
153 EXPECT_FALSE(
154 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
156 // Block access to a file system.
157 content_settings->OnFileSystemAccessed(GURL("http://google.com"), true);
158 EXPECT_TRUE(
159 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
162 TEST_F(TabSpecificContentSettingsTest, AllowedContent) {
163 TabSpecificContentSettings* content_settings =
164 TabSpecificContentSettings::FromWebContents(web_contents());
165 net::CookieOptions options;
167 // Test default settings.
168 ASSERT_FALSE(
169 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_IMAGES));
170 ASSERT_FALSE(
171 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
172 ASSERT_FALSE(
173 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
174 ASSERT_FALSE(content_settings->IsContentAllowed(
175 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
176 ASSERT_FALSE(content_settings->IsContentAllowed(
177 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
179 // Record a cookie.
180 content_settings->OnCookieChanged(GURL("http://google.com"),
181 GURL("http://google.com"),
182 "A=B",
183 options,
184 false);
185 ASSERT_TRUE(
186 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
187 ASSERT_FALSE(
188 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
190 // Record a blocked cookie.
191 content_settings->OnCookieChanged(GURL("http://google.com"),
192 GURL("http://google.com"),
193 "C=D",
194 options,
195 true);
196 ASSERT_TRUE(
197 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
198 ASSERT_TRUE(
199 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
202 TEST_F(TabSpecificContentSettingsTest, AllowedBlockedMediaContent) {
203 TabSpecificContentSettings* content_settings =
204 TabSpecificContentSettings::FromWebContents(web_contents());
206 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED,
207 content_settings->GetMicrophoneCameraState());
209 // Request and allow microphone access.
210 GURL security_origin("http://google.com");
211 MediaStreamDevicesController::MediaStreamTypeSettingsMap
212 request_permissions;
213 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
214 MediaStreamDevicesController::MEDIA_ALLOWED;
215 content_settings->OnMediaStreamPermissionSet(security_origin,
216 request_permissions);
217 ASSERT_TRUE(content_settings->IsContentAllowed(
218 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
219 ASSERT_FALSE(content_settings->IsContentBlocked(
220 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
221 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED,
222 content_settings->GetMicrophoneCameraState());
224 // Request and allow camera access.
225 request_permissions.clear();
226 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
227 MediaStreamDevicesController::MEDIA_ALLOWED;
228 content_settings->OnMediaStreamPermissionSet(security_origin,
229 request_permissions);
230 ASSERT_TRUE(content_settings->IsContentAllowed(
231 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
232 ASSERT_FALSE(content_settings->IsContentBlocked(
233 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
234 // cam request should not affect previous mic settings.
235 ASSERT_TRUE(content_settings->IsContentAllowed(
236 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
237 ASSERT_FALSE(content_settings->IsContentBlocked(
238 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
239 ASSERT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
240 content_settings->GetMicrophoneCameraState());
242 // Request and block microphone access.
243 request_permissions.clear();
244 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
245 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
246 content_settings->OnMediaStreamPermissionSet(security_origin,
247 request_permissions);
248 ASSERT_FALSE(content_settings->IsContentAllowed(
249 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
250 ASSERT_TRUE(content_settings->IsContentBlocked(
251 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
252 // mic request should not affect previous cam settings.
253 ASSERT_TRUE(content_settings->IsContentAllowed(
254 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
255 ASSERT_FALSE(content_settings->IsContentBlocked(
256 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
257 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
258 TabSpecificContentSettings::MICROPHONE_BLOCKED,
259 content_settings->GetMicrophoneCameraState());
261 // Request and block camera access.
262 request_permissions.clear();
263 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
264 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
265 content_settings->OnMediaStreamPermissionSet(security_origin,
266 request_permissions);
267 ASSERT_FALSE(content_settings->IsContentAllowed(
268 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
269 ASSERT_TRUE(content_settings->IsContentBlocked(
270 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
271 // cam request should not affect previous mic settings.
272 ASSERT_FALSE(content_settings->IsContentAllowed(
273 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
274 ASSERT_TRUE(content_settings->IsContentBlocked(
275 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
276 ASSERT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED |
277 TabSpecificContentSettings::CAMERA_BLOCKED,
278 content_settings->GetMicrophoneCameraState());
280 // Request and allow microphone and camera access.
281 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
282 MediaStreamDevicesController::MEDIA_ALLOWED;
283 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
284 MediaStreamDevicesController::MEDIA_ALLOWED;
285 content_settings->OnMediaStreamPermissionSet(security_origin,
286 request_permissions);
287 ASSERT_TRUE(content_settings->IsContentAllowed(
288 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
289 ASSERT_FALSE(content_settings->IsContentBlocked(
290 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
291 ASSERT_TRUE(content_settings->IsContentAllowed(
292 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
293 ASSERT_FALSE(content_settings->IsContentBlocked(
294 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
295 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
296 TabSpecificContentSettings::CAMERA_ACCESSED,
297 content_settings->GetMicrophoneCameraState());
299 // Request and block microphone and camera access.
300 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
301 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
302 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
303 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
304 content_settings->OnMediaStreamPermissionSet(security_origin,
305 request_permissions);
306 ASSERT_FALSE(content_settings->IsContentAllowed(
307 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
308 ASSERT_TRUE(content_settings->IsContentBlocked(
309 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
310 ASSERT_FALSE(content_settings->IsContentAllowed(
311 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
312 ASSERT_TRUE(content_settings->IsContentBlocked(
313 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
314 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
315 TabSpecificContentSettings::MICROPHONE_BLOCKED |
316 TabSpecificContentSettings::CAMERA_ACCESSED |
317 TabSpecificContentSettings::CAMERA_BLOCKED,
318 content_settings->GetMicrophoneCameraState());
320 // Request microphone and camera access. Allow microphone, block camera.
321 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
322 MediaStreamDevicesController::MEDIA_ALLOWED;
323 content_settings->OnMediaStreamPermissionSet(security_origin,
324 request_permissions);
325 ASSERT_TRUE(content_settings->IsContentAllowed(
326 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
327 ASSERT_FALSE(content_settings->IsContentBlocked(
328 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
329 ASSERT_FALSE(content_settings->IsContentAllowed(
330 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
331 ASSERT_TRUE(content_settings->IsContentBlocked(
332 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
333 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
334 TabSpecificContentSettings::CAMERA_ACCESSED |
335 TabSpecificContentSettings::CAMERA_BLOCKED,
336 content_settings->GetMicrophoneCameraState());
338 // Request microphone and camera access. Block microphone, allow camera.
339 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
340 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
341 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
342 MediaStreamDevicesController::MEDIA_ALLOWED;
343 content_settings->OnMediaStreamPermissionSet(security_origin,
344 request_permissions);
345 ASSERT_FALSE(content_settings->IsContentAllowed(
346 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
347 ASSERT_TRUE(content_settings->IsContentBlocked(
348 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
349 ASSERT_TRUE(content_settings->IsContentAllowed(
350 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
351 ASSERT_FALSE(content_settings->IsContentBlocked(
352 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
353 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED |
354 TabSpecificContentSettings::MICROPHONE_BLOCKED |
355 TabSpecificContentSettings::CAMERA_ACCESSED,
356 content_settings->GetMicrophoneCameraState());
359 TEST_F(TabSpecificContentSettingsTest, EmptyCookieList) {
360 TabSpecificContentSettings* content_settings =
361 TabSpecificContentSettings::FromWebContents(web_contents());
363 ASSERT_FALSE(
364 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
365 ASSERT_FALSE(
366 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
367 content_settings->OnCookiesRead(GURL("http://google.com"),
368 GURL("http://google.com"),
369 net::CookieList(),
370 true);
371 ASSERT_FALSE(
372 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
373 ASSERT_FALSE(
374 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
377 TEST_F(TabSpecificContentSettingsTest, SiteDataObserver) {
378 TabSpecificContentSettings* content_settings =
379 TabSpecificContentSettings::FromWebContents(web_contents());
380 MockSiteDataObserver mock_observer(content_settings);
381 EXPECT_CALL(mock_observer, OnSiteDataAccessed()).Times(6);
383 bool blocked_by_policy = false;
384 content_settings->OnCookieChanged(GURL("http://google.com"),
385 GURL("http://google.com"),
386 "A=B",
387 net::CookieOptions(),
388 blocked_by_policy);
389 net::CookieList cookie_list;
390 scoped_ptr<net::CanonicalCookie> cookie(
391 net::CanonicalCookie::Create(GURL("http://google.com"),
392 "CookieName=CookieValue",
393 base::Time::Now(), net::CookieOptions()));
395 cookie_list.push_back(*cookie);
396 content_settings->OnCookiesRead(GURL("http://google.com"),
397 GURL("http://google.com"),
398 cookie_list,
399 blocked_by_policy);
400 content_settings->OnFileSystemAccessed(GURL("http://google.com"),
401 blocked_by_policy);
402 content_settings->OnIndexedDBAccessed(GURL("http://google.com"),
403 base::UTF8ToUTF16("text"),
404 blocked_by_policy);
405 content_settings->OnLocalStorageAccessed(GURL("http://google.com"),
406 true,
407 blocked_by_policy);
408 content_settings->OnWebDatabaseAccessed(GURL("http://google.com"),
409 base::UTF8ToUTF16("name"),
410 base::UTF8ToUTF16("display_name"),
411 blocked_by_policy);