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/string16.h"
6 #include "base/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/gtest/include/gtest/gtest.h"
15 #include "testing/gmock/include/gmock/gmock.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 TabSpecificContentSettingsTest()
42 : browser_thread_(BrowserThread::UI
, &message_loop_
) {}
44 virtual void SetUp() OVERRIDE
{
45 ChromeRenderViewHostTestHarness::SetUp();
46 TabSpecificContentSettings::CreateForWebContents(web_contents());
50 content::TestBrowserThread browser_thread_
;
52 DISALLOW_COPY_AND_ASSIGN(TabSpecificContentSettingsTest
);
55 TEST_F(TabSpecificContentSettingsTest
, BlockedContent
) {
56 TabSpecificContentSettings
* content_settings
=
57 TabSpecificContentSettings::FromWebContents(web_contents());
58 net::CookieOptions options
;
60 // Check that after initializing, nothing is blocked.
62 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
64 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
66 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
68 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
70 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
72 // Set a cookie, block access to images, block a popup.
73 content_settings
->OnCookieChanged(GURL("http://google.com"),
74 GURL("http://google.com"),
78 content_settings
->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
,
80 content_settings
->SetPopupsBlocked(true);
82 // Check that only the respective content types are affected.
83 EXPECT_TRUE(content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
85 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
87 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
89 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
90 EXPECT_TRUE(content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
91 content_settings
->OnCookieChanged(GURL("http://google.com"),
92 GURL("http://google.com"),
98 content_settings
->OnCookieChanged(GURL("http://google.com"),
99 GURL("http://google.com"),
104 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
106 // Reset blocked content settings.
107 content_settings
->ClearBlockedContentSettingsExceptForCookies();
109 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES
));
111 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT
));
113 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS
));
115 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
117 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS
));
119 content_settings
->ClearCookieSpecificContentSettings();
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
));
132 TEST_F(TabSpecificContentSettingsTest
, BlockedFileSystems
) {
133 TabSpecificContentSettings
* content_settings
=
134 TabSpecificContentSettings::FromWebContents(web_contents());
136 // Access a file system.
137 content_settings
->OnFileSystemAccessed(GURL("http://google.com"), false);
139 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
141 // Block access to a file system.
142 content_settings
->OnFileSystemAccessed(GURL("http://google.com"), true);
144 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
147 TEST_F(TabSpecificContentSettingsTest
, AllowedContent
) {
148 TabSpecificContentSettings
* content_settings
=
149 TabSpecificContentSettings::FromWebContents(web_contents());
150 net::CookieOptions options
;
153 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_IMAGES
));
155 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES
));
157 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
158 content_settings
->OnCookieChanged(GURL("http://google.com"),
159 GURL("http://google.com"),
164 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES
));
166 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
167 content_settings
->OnCookieChanged(GURL("http://google.com"),
168 GURL("http://google.com"),
173 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES
));
175 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
178 TEST_F(TabSpecificContentSettingsTest
, EmptyCookieList
) {
179 TabSpecificContentSettings
* content_settings
=
180 TabSpecificContentSettings::FromWebContents(web_contents());
183 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES
));
185 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
186 content_settings
->OnCookiesRead(GURL("http://google.com"),
187 GURL("http://google.com"),
191 content_settings
->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES
));
193 content_settings
->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES
));
196 TEST_F(TabSpecificContentSettingsTest
, SiteDataObserver
) {
197 TabSpecificContentSettings
* content_settings
=
198 TabSpecificContentSettings::FromWebContents(web_contents());
199 MockSiteDataObserver
mock_observer(content_settings
);
200 EXPECT_CALL(mock_observer
, OnSiteDataAccessed()).Times(6);
202 bool blocked_by_policy
= false;
203 content_settings
->OnCookieChanged(GURL("http://google.com"),
204 GURL("http://google.com"),
206 net::CookieOptions(),
208 net::CookieList cookie_list
;
209 scoped_ptr
<net::CanonicalCookie
> cookie(
210 net::CanonicalCookie::Create(GURL("http://google.com"),
211 "CookieName=CookieValue",
212 base::Time::Now(), net::CookieOptions()));
214 cookie_list
.push_back(*cookie
);
215 content_settings
->OnCookiesRead(GURL("http://google.com"),
216 GURL("http://google.com"),
219 content_settings
->OnFileSystemAccessed(GURL("http://google.com"),
221 content_settings
->OnIndexedDBAccessed(GURL("http://google.com"),
224 content_settings
->OnLocalStorageAccessed(GURL("http://google.com"),
227 content_settings
->OnWebDatabaseAccessed(GURL("http://google.com"),
229 UTF8ToUTF16("display_name"),