Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings_unittest.cc
blob5d08a4b018b52ab7f8856d65f07e01a204646c24
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 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
78 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
80 // Check that only the respective content types are affected.
81 EXPECT_TRUE(content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
82 EXPECT_FALSE(
83 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
84 EXPECT_FALSE(
85 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
86 EXPECT_FALSE(
87 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
88 EXPECT_TRUE(content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
89 EXPECT_TRUE(content_settings->IsContentBlocked(
90 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
91 EXPECT_TRUE(content_settings->IsContentBlocked(
92 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
93 content_settings->OnCookieChanged(GURL("http://google.com"),
94 GURL("http://google.com"),
95 "A=B",
96 options,
97 false);
99 // Block a cookie.
100 content_settings->OnCookieChanged(GURL("http://google.com"),
101 GURL("http://google.com"),
102 "C=D",
103 options,
104 true);
105 EXPECT_TRUE(
106 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
108 // Reset blocked content settings.
109 content_settings->ClearBlockedContentSettingsExceptForCookies();
110 EXPECT_FALSE(
111 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
112 EXPECT_FALSE(
113 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
114 EXPECT_FALSE(
115 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
116 EXPECT_TRUE(
117 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
118 EXPECT_FALSE(
119 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
120 EXPECT_FALSE(content_settings->IsContentBlocked(
121 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
122 EXPECT_FALSE(content_settings->IsContentBlocked(
123 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
125 content_settings->ClearCookieSpecificContentSettings();
126 EXPECT_FALSE(
127 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
128 EXPECT_FALSE(
129 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
130 EXPECT_FALSE(
131 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
132 EXPECT_FALSE(
133 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
134 EXPECT_FALSE(
135 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
136 EXPECT_FALSE(content_settings->IsContentBlocked(
137 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
138 EXPECT_FALSE(content_settings->IsContentBlocked(
139 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
142 TEST_F(TabSpecificContentSettingsTest, BlockedFileSystems) {
143 TabSpecificContentSettings* content_settings =
144 TabSpecificContentSettings::FromWebContents(web_contents());
146 // Access a file system.
147 content_settings->OnFileSystemAccessed(GURL("http://google.com"), false);
148 EXPECT_FALSE(
149 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
151 // Block access to a file system.
152 content_settings->OnFileSystemAccessed(GURL("http://google.com"), true);
153 EXPECT_TRUE(
154 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
157 TEST_F(TabSpecificContentSettingsTest, AllowedContent) {
158 TabSpecificContentSettings* content_settings =
159 TabSpecificContentSettings::FromWebContents(web_contents());
160 net::CookieOptions options;
162 // Test default settings.
163 ASSERT_FALSE(
164 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_IMAGES));
165 ASSERT_FALSE(
166 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
167 ASSERT_FALSE(
168 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
169 ASSERT_FALSE(content_settings->IsContentAllowed(
170 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
171 ASSERT_FALSE(content_settings->IsContentAllowed(
172 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
174 // Record a cookie.
175 content_settings->OnCookieChanged(GURL("http://google.com"),
176 GURL("http://google.com"),
177 "A=B",
178 options,
179 false);
180 ASSERT_TRUE(
181 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
182 ASSERT_FALSE(
183 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
185 // Record a blocked cookie.
186 content_settings->OnCookieChanged(GURL("http://google.com"),
187 GURL("http://google.com"),
188 "C=D",
189 options,
190 true);
191 ASSERT_TRUE(
192 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
193 ASSERT_TRUE(
194 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
197 TEST_F(TabSpecificContentSettingsTest, AllowedBlockedMediaContent) {
198 TabSpecificContentSettings* content_settings =
199 TabSpecificContentSettings::FromWebContents(web_contents());
201 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_NOT_ACCESSED,
202 content_settings->GetMicrophoneCameraState());
204 // Request and allow microphone access.
205 GURL security_origin("http://google.com");
206 MediaStreamDevicesController::MediaStreamTypeSettingsMap
207 request_permissions;
208 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
209 MediaStreamDevicesController::MEDIA_ALLOWED;
210 content_settings->OnMediaStreamPermissionSet(security_origin,
211 request_permissions);
212 ASSERT_TRUE(content_settings->IsContentAllowed(
213 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
214 ASSERT_FALSE(content_settings->IsContentBlocked(
215 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
216 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED,
217 content_settings->GetMicrophoneCameraState());
219 // Request and allow camera access.
220 request_permissions.clear();
221 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
222 MediaStreamDevicesController::MEDIA_ALLOWED;
223 content_settings->OnMediaStreamPermissionSet(security_origin,
224 request_permissions);
225 ASSERT_TRUE(content_settings->IsContentAllowed(
226 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
227 ASSERT_FALSE(content_settings->IsContentBlocked(
228 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
229 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_ACCESSED,
230 content_settings->GetMicrophoneCameraState());
232 // Request and block microphone access.
233 request_permissions.clear();
234 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
235 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
236 content_settings->OnMediaStreamPermissionSet(security_origin,
237 request_permissions);
238 ASSERT_FALSE(content_settings->IsContentAllowed(
239 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
240 ASSERT_TRUE(content_settings->IsContentBlocked(
241 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
242 ASSERT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
243 content_settings->GetMicrophoneCameraState());
245 // Request and block camera access.
246 request_permissions.clear();
247 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
248 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
249 content_settings->OnMediaStreamPermissionSet(security_origin,
250 request_permissions);
251 ASSERT_FALSE(content_settings->IsContentAllowed(
252 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
253 ASSERT_TRUE(content_settings->IsContentBlocked(
254 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
255 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_BLOCKED,
256 content_settings->GetMicrophoneCameraState());
258 // Request and allow microphone and camera access.
259 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
260 MediaStreamDevicesController::MEDIA_ALLOWED;
261 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
262 MediaStreamDevicesController::MEDIA_ALLOWED;
263 content_settings->OnMediaStreamPermissionSet(security_origin,
264 request_permissions);
265 ASSERT_TRUE(content_settings->IsContentAllowed(
266 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
267 ASSERT_FALSE(content_settings->IsContentBlocked(
268 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
269 ASSERT_TRUE(content_settings->IsContentAllowed(
270 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
271 ASSERT_FALSE(content_settings->IsContentBlocked(
272 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
273 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_ACCESSED,
274 content_settings->GetMicrophoneCameraState());
276 // Request and block microphone and camera access.
277 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
278 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
279 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
280 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
281 content_settings->OnMediaStreamPermissionSet(security_origin,
282 request_permissions);
283 ASSERT_FALSE(content_settings->IsContentAllowed(
284 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
285 ASSERT_TRUE(content_settings->IsContentBlocked(
286 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
287 ASSERT_FALSE(content_settings->IsContentAllowed(
288 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
289 ASSERT_TRUE(content_settings->IsContentBlocked(
290 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
291 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_CAMERA_BLOCKED,
292 content_settings->GetMicrophoneCameraState());
294 // Request microphone and camera access. Allow microphone, block camera.
295 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
296 MediaStreamDevicesController::MEDIA_ALLOWED;
297 content_settings->OnMediaStreamPermissionSet(security_origin,
298 request_permissions);
299 ASSERT_TRUE(content_settings->IsContentAllowed(
300 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
301 ASSERT_FALSE(content_settings->IsContentBlocked(
302 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
303 ASSERT_FALSE(content_settings->IsContentAllowed(
304 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
305 ASSERT_TRUE(content_settings->IsContentBlocked(
306 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
307 ASSERT_EQ(TabSpecificContentSettings::MICROPHONE_ACCESSED,
308 content_settings->GetMicrophoneCameraState());
310 // Request microphone and camera access. Block microphone, allow camera.
311 request_permissions[content::MEDIA_DEVICE_AUDIO_CAPTURE].permission =
312 MediaStreamDevicesController::MEDIA_BLOCKED_BY_USER;
313 request_permissions[content::MEDIA_DEVICE_VIDEO_CAPTURE].permission =
314 MediaStreamDevicesController::MEDIA_ALLOWED;
315 content_settings->OnMediaStreamPermissionSet(security_origin,
316 request_permissions);
317 ASSERT_FALSE(content_settings->IsContentAllowed(
318 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
319 ASSERT_TRUE(content_settings->IsContentBlocked(
320 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC));
321 ASSERT_TRUE(content_settings->IsContentAllowed(
322 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
323 ASSERT_FALSE(content_settings->IsContentBlocked(
324 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA));
325 ASSERT_EQ(TabSpecificContentSettings::CAMERA_ACCESSED,
326 content_settings->GetMicrophoneCameraState());
329 TEST_F(TabSpecificContentSettingsTest, EmptyCookieList) {
330 TabSpecificContentSettings* content_settings =
331 TabSpecificContentSettings::FromWebContents(web_contents());
333 ASSERT_FALSE(
334 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
335 ASSERT_FALSE(
336 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
337 content_settings->OnCookiesRead(GURL("http://google.com"),
338 GURL("http://google.com"),
339 net::CookieList(),
340 true);
341 ASSERT_FALSE(
342 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
343 ASSERT_FALSE(
344 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
347 TEST_F(TabSpecificContentSettingsTest, SiteDataObserver) {
348 TabSpecificContentSettings* content_settings =
349 TabSpecificContentSettings::FromWebContents(web_contents());
350 MockSiteDataObserver mock_observer(content_settings);
351 EXPECT_CALL(mock_observer, OnSiteDataAccessed()).Times(6);
353 bool blocked_by_policy = false;
354 content_settings->OnCookieChanged(GURL("http://google.com"),
355 GURL("http://google.com"),
356 "A=B",
357 net::CookieOptions(),
358 blocked_by_policy);
359 net::CookieList cookie_list;
360 scoped_ptr<net::CanonicalCookie> cookie(
361 net::CanonicalCookie::Create(GURL("http://google.com"),
362 "CookieName=CookieValue",
363 base::Time::Now(), net::CookieOptions()));
365 cookie_list.push_back(*cookie);
366 content_settings->OnCookiesRead(GURL("http://google.com"),
367 GURL("http://google.com"),
368 cookie_list,
369 blocked_by_policy);
370 content_settings->OnFileSystemAccessed(GURL("http://google.com"),
371 blocked_by_policy);
372 content_settings->OnIndexedDBAccessed(GURL("http://google.com"),
373 base::UTF8ToUTF16("text"),
374 blocked_by_policy);
375 content_settings->OnLocalStorageAccessed(GURL("http://google.com"),
376 true,
377 blocked_by_policy);
378 content_settings->OnWebDatabaseAccessed(GURL("http://google.com"),
379 base::UTF8ToUTF16("name"),
380 base::UTF8ToUTF16("display_name"),
381 blocked_by_policy);