Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings_unittest.cc
blobe0bbb45c8282211db3221f53ca4c0182163cd5ba
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;
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 TabSpecificContentSettingsTest()
42 : browser_thread_(BrowserThread::UI, &message_loop_) {}
44 virtual void SetUp() OVERRIDE {
45 ChromeRenderViewHostTestHarness::SetUp();
46 TabSpecificContentSettings::CreateForWebContents(web_contents());
49 private:
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.
61 EXPECT_FALSE(
62 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
63 EXPECT_FALSE(
64 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
65 EXPECT_FALSE(
66 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
67 EXPECT_FALSE(
68 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
69 EXPECT_FALSE(
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"),
75 "A=B",
76 options,
77 false);
78 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES,
79 std::string());
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));
84 EXPECT_FALSE(
85 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
86 EXPECT_FALSE(
87 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
88 EXPECT_FALSE(
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"),
93 "A=B",
94 options,
95 false);
97 // Block a cookie.
98 content_settings->OnCookieChanged(GURL("http://google.com"),
99 GURL("http://google.com"),
100 "C=D",
101 options,
102 true);
103 EXPECT_TRUE(
104 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
106 // Reset blocked content settings.
107 content_settings->ClearBlockedContentSettingsExceptForCookies();
108 EXPECT_FALSE(
109 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
110 EXPECT_FALSE(
111 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
112 EXPECT_FALSE(
113 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
114 EXPECT_TRUE(
115 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
116 EXPECT_FALSE(
117 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS));
119 content_settings->ClearCookieSpecificContentSettings();
120 EXPECT_FALSE(
121 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
122 EXPECT_FALSE(
123 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
124 EXPECT_FALSE(
125 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
126 EXPECT_FALSE(
127 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
128 EXPECT_FALSE(
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);
138 EXPECT_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);
143 EXPECT_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;
152 ASSERT_FALSE(
153 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_IMAGES));
154 ASSERT_FALSE(
155 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
156 ASSERT_FALSE(
157 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
158 content_settings->OnCookieChanged(GURL("http://google.com"),
159 GURL("http://google.com"),
160 "A=B",
161 options,
162 false);
163 ASSERT_TRUE(
164 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
165 ASSERT_FALSE(
166 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
167 content_settings->OnCookieChanged(GURL("http://google.com"),
168 GURL("http://google.com"),
169 "C=D",
170 options,
171 true);
172 ASSERT_TRUE(
173 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
174 ASSERT_TRUE(
175 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
178 TEST_F(TabSpecificContentSettingsTest, EmptyCookieList) {
179 TabSpecificContentSettings* content_settings =
180 TabSpecificContentSettings::FromWebContents(web_contents());
182 ASSERT_FALSE(
183 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
184 ASSERT_FALSE(
185 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
186 content_settings->OnCookiesRead(GURL("http://google.com"),
187 GURL("http://google.com"),
188 net::CookieList(),
189 true);
190 ASSERT_FALSE(
191 content_settings->IsContentAccessed(CONTENT_SETTINGS_TYPE_COOKIES));
192 ASSERT_FALSE(
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"),
205 "A=B",
206 net::CookieOptions(),
207 blocked_by_policy);
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"),
217 cookie_list,
218 blocked_by_policy);
219 content_settings->OnFileSystemAccessed(GURL("http://google.com"),
220 blocked_by_policy);
221 content_settings->OnIndexedDBAccessed(GURL("http://google.com"),
222 UTF8ToUTF16("text"),
223 blocked_by_policy);
224 content_settings->OnLocalStorageAccessed(GURL("http://google.com"),
225 true,
226 blocked_by_policy);
227 content_settings->OnWebDatabaseAccessed(GURL("http://google.com"),
228 UTF8ToUTF16("name"),
229 UTF8ToUTF16("display_name"),
230 blocked_by_policy);