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 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 TabSpecificContentSettings::MicrophoneCameraState
78 blocked_microphone_camera_state
=
79 TabSpecificContentSettings::MICROPHONE_ACCESSED
|
80 TabSpecificContentSettings::MICROPHONE_BLOCKED
|
81 TabSpecificContentSettings::CAMERA_ACCESSED
|
82 TabSpecificContentSettings::CAMERA_BLOCKED
;
83 content_settings
->OnMediaStreamPermissionSet(GURL("http://google.com"),
84 blocked_microphone_camera_state
,
90 // Check that only the respective content types are affected.
91 EXPECT_TRUE(content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
93 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
95 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
97 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
98 EXPECT_TRUE(content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
99 EXPECT_TRUE(content_settings
->IsContentBlocked(
100 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
101 EXPECT_TRUE(content_settings
->IsContentBlocked(
102 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
103 content_settings
->OnCookieChanged(GURL("http://google.com"),
104 GURL("http://google.com"),
110 content_settings
->OnCookieChanged(GURL("http://google.com"),
111 GURL("http://google.com"),
116 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
118 // Reset blocked content settings.
119 content_settings
->ClearBlockedContentSettingsExceptForCookies();
121 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
123 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
125 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
127 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
129 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
130 EXPECT_FALSE(content_settings
->IsContentBlocked(
131 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
132 EXPECT_FALSE(content_settings
->IsContentBlocked(
133 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
135 content_settings
->ClearCookieSpecificContentSettings();
137 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
139 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
141 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
143 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
145 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
146 EXPECT_FALSE(content_settings
->IsContentBlocked(
147 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
148 EXPECT_FALSE(content_settings
->IsContentBlocked(
149 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
152 TEST_F(TabSpecificContentSettingsTest
, BlockedFileSystems
) {
153 TabSpecificContentSettings
* content_settings
=
154 TabSpecificContentSettings::FromWebContents(web_contents());
156 // Access a file system.
157 content_settings
->OnFileSystemAccessed(GURL("http://google.com"), false);
159 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
161 // Block access to a file system.
162 content_settings
->OnFileSystemAccessed(GURL("http://google.com"), true);
164 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
167 TEST_F(TabSpecificContentSettingsTest
, AllowedContent
) {
168 TabSpecificContentSettings
* content_settings
=
169 TabSpecificContentSettings::FromWebContents(web_contents());
170 net::CookieOptions options
;
172 // Test default settings.
174 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_IMAGES
));
176 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
178 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
179 ASSERT_FALSE(content_settings
->IsContentAllowed(
180 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
181 ASSERT_FALSE(content_settings
->IsContentAllowed(
182 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
185 content_settings
->OnCookieChanged(GURL("http://google.com"),
186 GURL("http://google.com"),
191 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
193 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
195 // Record a blocked cookie.
196 content_settings
->OnCookieChanged(GURL("http://google.com"),
197 GURL("http://google.com"),
202 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
204 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
207 TEST_F(TabSpecificContentSettingsTest
, EmptyCookieList
) {
208 TabSpecificContentSettings
* content_settings
=
209 TabSpecificContentSettings::FromWebContents(web_contents());
212 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
214 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
215 content_settings
->OnCookiesRead(GURL("http://google.com"),
216 GURL("http://google.com"),
220 content_settings
->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES
));
222 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
225 TEST_F(TabSpecificContentSettingsTest
, SiteDataObserver
) {
226 TabSpecificContentSettings
* content_settings
=
227 TabSpecificContentSettings::FromWebContents(web_contents());
228 MockSiteDataObserver
mock_observer(content_settings
);
229 EXPECT_CALL(mock_observer
, OnSiteDataAccessed()).Times(6);
231 bool blocked_by_policy
= false;
232 content_settings
->OnCookieChanged(GURL("http://google.com"),
233 GURL("http://google.com"),
235 net::CookieOptions(),
237 net::CookieList cookie_list
;
238 scoped_ptr
<net::CanonicalCookie
> cookie(
239 net::CanonicalCookie::Create(GURL("http://google.com"),
240 "CookieName=CookieValue",
241 base::Time::Now(), net::CookieOptions()));
243 cookie_list
.push_back(*cookie
);
244 content_settings
->OnCookiesRead(GURL("http://google.com"),
245 GURL("http://google.com"),
248 content_settings
->OnFileSystemAccessed(GURL("http://google.com"),
250 content_settings
->OnIndexedDBAccessed(GURL("http://google.com"),
251 base::UTF8ToUTF16("text"),
253 content_settings
->OnLocalStorageAccessed(GURL("http://google.com"),
256 content_settings
->OnWebDatabaseAccessed(GURL("http://google.com"),
257 base::UTF8ToUTF16("name"),
258 base::UTF8ToUTF16("display_name"),