Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / content_settings / tab_specific_content_settings_unittest.cc
blobd53724694d58f9469cf5988d24d85388c48b599c
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 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 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,
85 std::string(),
86 std::string(),
87 std::string(),
88 std::string());
90 // Check that only the respective content types are affected.
91 EXPECT_TRUE(content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
92 EXPECT_FALSE(
93 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
94 EXPECT_FALSE(
95 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
96 EXPECT_FALSE(
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"),
105 "A=B",
106 options,
107 false);
109 // Block a cookie.
110 content_settings->OnCookieChanged(GURL("http://google.com"),
111 GURL("http://google.com"),
112 "C=D",
113 options,
114 true);
115 EXPECT_TRUE(
116 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
118 // Reset blocked content settings.
119 content_settings->ClearBlockedContentSettingsExceptForCookies();
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_TRUE(
127 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
128 EXPECT_FALSE(
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();
136 EXPECT_FALSE(
137 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES));
138 EXPECT_FALSE(
139 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
140 EXPECT_FALSE(
141 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS));
142 EXPECT_FALSE(
143 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
144 EXPECT_FALSE(
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);
158 EXPECT_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);
163 EXPECT_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.
173 ASSERT_FALSE(
174 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_IMAGES));
175 ASSERT_FALSE(
176 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
177 ASSERT_FALSE(
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));
184 // Record a cookie.
185 content_settings->OnCookieChanged(GURL("http://google.com"),
186 GURL("http://google.com"),
187 "A=B",
188 options,
189 false);
190 ASSERT_TRUE(
191 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
192 ASSERT_FALSE(
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"),
198 "C=D",
199 options,
200 true);
201 ASSERT_TRUE(
202 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
203 ASSERT_TRUE(
204 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
207 TEST_F(TabSpecificContentSettingsTest, EmptyCookieList) {
208 TabSpecificContentSettings* content_settings =
209 TabSpecificContentSettings::FromWebContents(web_contents());
211 ASSERT_FALSE(
212 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
213 ASSERT_FALSE(
214 content_settings->IsContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES));
215 content_settings->OnCookiesRead(GURL("http://google.com"),
216 GURL("http://google.com"),
217 net::CookieList(),
218 true);
219 ASSERT_FALSE(
220 content_settings->IsContentAllowed(CONTENT_SETTINGS_TYPE_COOKIES));
221 ASSERT_FALSE(
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"),
234 "A=B",
235 net::CookieOptions(),
236 blocked_by_policy);
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"),
246 cookie_list,
247 blocked_by_policy);
248 content_settings->OnFileSystemAccessed(GURL("http://google.com"),
249 blocked_by_policy);
250 content_settings->OnIndexedDBAccessed(GURL("http://google.com"),
251 base::UTF8ToUTF16("text"),
252 blocked_by_policy);
253 content_settings->OnLocalStorageAccessed(GURL("http://google.com"),
254 true,
255 blocked_by_policy);
256 content_settings->OnWebDatabaseAccessed(GURL("http://google.com"),
257 base::UTF8ToUTF16("name"),
258 base::UTF8ToUTF16("display_name"),
259 blocked_by_policy);