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
;
21 class MockSiteDataObserver
22 : public TabSpecificContentSettings::SiteDataObserver
{
24 explicit MockSiteDataObserver(
25 TabSpecificContentSettings
* tab_specific_content_settings
)
26 : SiteDataObserver(tab_specific_content_settings
) {
29 virtual ~MockSiteDataObserver() {}
31 MOCK_METHOD0(OnSiteDataAccessed
, void());
34 DISALLOW_COPY_AND_ASSIGN(MockSiteDataObserver
);
39 class TabSpecificContentSettingsTest
: public ChromeRenderViewHostTestHarness
{
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.
54 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
56 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
58 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
60 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
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
70 content_settings
->OnCookieChanged(GURL("http://google.com"),
71 GURL("http://google.com"),
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
));
83 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
85 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
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"),
100 content_settings
->OnCookieChanged(GURL("http://google.com"),
101 GURL("http://google.com"),
106 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
108 // Reset blocked content settings.
109 content_settings
->ClearBlockedContentSettingsExceptForCookies();
111 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
113 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
115 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
117 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
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();
127 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
129 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
131 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
133 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
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);
149 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
151 // Block access to a file system.
152 content_settings
->OnFileSystemAccessed(GURL("http://google.com"), 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.
164 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_IMAGES
));
166 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
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
));
175 content_settings
->OnCookieChanged(GURL("http://google.com"),
176 GURL("http://google.com"),
181 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
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"),
192 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
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
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());
334 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
336 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
337 content_settings
->OnCookiesRead(GURL("http://google.com"),
338 GURL("http://google.com"),
342 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
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"),
357 net::CookieOptions(),
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"),
370 content_settings
->OnFileSystemAccessed(GURL("http://google.com"),
372 content_settings
->OnIndexedDBAccessed(GURL("http://google.com"),
373 base::UTF8ToUTF16("text"),
375 content_settings
->OnLocalStorageAccessed(GURL("http://google.com"),
378 content_settings
->OnWebDatabaseAccessed(GURL("http://google.com"),
379 base::UTF8ToUTF16("name"),
380 base::UTF8ToUTF16("display_name"),