Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / content_settings / content_settings_apitest.cc
blobbf1389c8bbc344bf0da1806d1aaad2b9ffd81985
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/location.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/content_settings/cookie_settings_factory.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
14 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
15 #include "chrome/browser/extensions/extension_apitest.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "components/content_settings/core/browser/cookie_settings.h"
20 #include "components/content_settings/core/browser/host_content_settings_map.h"
21 #include "components/content_settings/core/browser/plugins_field_trial.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/plugin_service.h"
24 #include "content/public/common/webplugininfo.h"
25 #include "content/public/test/test_utils.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/test_extension_registry_observer.h"
29 namespace {
31 void ReleaseBrowserProcessModule() {
32 g_browser_process->ReleaseModule();
35 using content_settings::PluginsFieldTrial;
37 } // namespace
39 namespace extensions {
41 class ExtensionContentSettingsApiTest : public ExtensionApiTest {
42 public:
43 ExtensionContentSettingsApiTest() : profile_(NULL) {}
45 void SetUpCommandLine(base::CommandLine* command_line) override {
46 ExtensionApiTest::SetUpCommandLine(command_line);
47 command_line->AppendSwitch(switches::kDisablePluginsDiscovery);
50 void SetUpOnMainThread() override {
51 ExtensionApiTest::SetUpOnMainThread();
53 // The browser might get closed later (and therefore be destroyed), so we
54 // save the profile.
55 profile_ = browser()->profile();
57 // Closing the last browser window also releases a module reference. Make
58 // sure it's not the last one, so the message loop doesn't quit
59 // unexpectedly.
60 g_browser_process->AddRefModule();
63 void TearDownOnMainThread() override {
64 // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
65 // post a task to do it, then run the message loop.
66 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE, base::Bind(&ReleaseBrowserProcessModule));
68 content::RunAllPendingInMessageLoop();
70 ExtensionApiTest::TearDownOnMainThread();
73 protected:
74 void CheckContentSettingsSet() {
75 HostContentSettingsMap* map =
76 HostContentSettingsMapFactory::GetForProfile(profile_);
77 content_settings::CookieSettings* cookie_settings =
78 CookieSettingsFactory::GetForProfile(profile_).get();
80 // Check default content settings by using an unknown URL.
81 GURL example_url("http://www.example.com");
82 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
83 example_url, example_url));
84 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
85 example_url, example_url));
86 EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(example_url));
87 EXPECT_EQ(CONTENT_SETTING_ALLOW,
88 map->GetContentSetting(example_url,
89 example_url,
90 CONTENT_SETTINGS_TYPE_IMAGES,
91 std::string()));
92 EXPECT_EQ(CONTENT_SETTING_BLOCK,
93 map->GetContentSetting(example_url,
94 example_url,
95 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
96 std::string()));
97 EXPECT_EQ(CONTENT_SETTING_ALLOW,
98 map->GetContentSetting(example_url,
99 example_url,
100 CONTENT_SETTINGS_TYPE_PLUGINS,
101 std::string()));
102 EXPECT_EQ(CONTENT_SETTING_BLOCK,
103 map->GetContentSetting(example_url,
104 example_url,
105 CONTENT_SETTINGS_TYPE_POPUPS,
106 std::string()));
107 EXPECT_EQ(CONTENT_SETTING_ASK,
108 map->GetContentSetting(example_url,
109 example_url,
110 CONTENT_SETTINGS_TYPE_GEOLOCATION,
111 std::string()));
112 EXPECT_EQ(CONTENT_SETTING_ASK,
113 map->GetContentSetting(example_url,
114 example_url,
115 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
116 std::string()));
117 EXPECT_EQ(CONTENT_SETTING_ASK,
118 map->GetContentSetting(example_url,
119 example_url,
120 CONTENT_SETTINGS_TYPE_FULLSCREEN,
121 std::string()));
122 EXPECT_EQ(CONTENT_SETTING_ASK,
123 map->GetContentSetting(example_url,
124 example_url,
125 CONTENT_SETTINGS_TYPE_MOUSELOCK,
126 std::string()));
127 EXPECT_EQ(CONTENT_SETTING_ASK,
128 map->GetContentSetting(example_url,
129 example_url,
130 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
131 std::string()));
132 EXPECT_EQ(CONTENT_SETTING_ASK,
133 map->GetContentSetting(example_url,
134 example_url,
135 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
136 std::string()));
137 EXPECT_EQ(CONTENT_SETTING_ASK,
138 map->GetContentSetting(example_url,
139 example_url,
140 CONTENT_SETTINGS_TYPE_PPAPI_BROKER,
141 std::string()));
142 EXPECT_EQ(CONTENT_SETTING_ASK,
143 map->GetContentSetting(example_url,
144 example_url,
145 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
146 std::string()));
148 // Check content settings for www.google.com
149 GURL url("http://www.google.com");
150 EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(url, url));
151 EXPECT_EQ(CONTENT_SETTING_ALLOW,
152 map->GetContentSetting(
153 url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
154 EXPECT_EQ(CONTENT_SETTING_BLOCK,
155 map->GetContentSetting(
156 url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
157 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT,
158 map->GetContentSetting(
159 url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
160 EXPECT_EQ(CONTENT_SETTING_ALLOW,
161 map->GetContentSetting(
162 url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
163 EXPECT_EQ(CONTENT_SETTING_BLOCK,
164 map->GetContentSetting(
165 url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
166 EXPECT_EQ(
167 CONTENT_SETTING_BLOCK,
168 map->GetContentSetting(
169 url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
170 EXPECT_EQ(CONTENT_SETTING_ALLOW,
171 map->GetContentSetting(
172 url, url, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
173 EXPECT_EQ(CONTENT_SETTING_BLOCK,
174 map->GetContentSetting(
175 url, url, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
176 EXPECT_EQ(CONTENT_SETTING_BLOCK,
177 map->GetContentSetting(
178 url, url, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, std::string()));
179 EXPECT_EQ(CONTENT_SETTING_BLOCK,
180 map->GetContentSetting(
181 url, url, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, std::string()));
182 EXPECT_EQ(CONTENT_SETTING_BLOCK,
183 map->GetContentSetting(
184 url, url, CONTENT_SETTINGS_TYPE_PPAPI_BROKER, std::string()));
185 EXPECT_EQ(CONTENT_SETTING_BLOCK,
186 map->GetContentSetting(
187 url, url, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
188 std::string()));
191 void CheckContentSettingsDefault() {
192 HostContentSettingsMap* map =
193 HostContentSettingsMapFactory::GetForProfile(profile_);
194 content_settings::CookieSettings* cookie_settings =
195 CookieSettingsFactory::GetForProfile(profile_).get();
197 // Check content settings for www.google.com
198 GURL url("http://www.google.com");
199 EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(url, url));
200 EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(url, url));
201 EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(url));
202 EXPECT_EQ(CONTENT_SETTING_ALLOW,
203 map->GetContentSetting(
204 url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
205 EXPECT_EQ(CONTENT_SETTING_ALLOW,
206 map->GetContentSetting(
207 url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
208 EXPECT_EQ(PluginsFieldTrial::GetDefaultPluginsContentSetting(),
209 map->GetContentSetting(
210 url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
211 EXPECT_EQ(CONTENT_SETTING_BLOCK,
212 map->GetContentSetting(
213 url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
214 EXPECT_EQ(CONTENT_SETTING_ASK,
215 map->GetContentSetting(
216 url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
217 EXPECT_EQ(
218 CONTENT_SETTING_ASK,
219 map->GetContentSetting(
220 url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
221 EXPECT_EQ(CONTENT_SETTING_ASK,
222 map->GetContentSetting(
223 url, url, CONTENT_SETTINGS_TYPE_FULLSCREEN, std::string()));
224 EXPECT_EQ(CONTENT_SETTING_ASK,
225 map->GetContentSetting(
226 url, url, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string()));
227 EXPECT_EQ(CONTENT_SETTING_ASK,
228 map->GetContentSetting(
229 url, url, CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, std::string()));
230 EXPECT_EQ(CONTENT_SETTING_ASK,
231 map->GetContentSetting(
232 url, url, CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, std::string()));
233 EXPECT_EQ(CONTENT_SETTING_ASK,
234 map->GetContentSetting(
235 url, url, CONTENT_SETTINGS_TYPE_PPAPI_BROKER, std::string()));
236 EXPECT_EQ(CONTENT_SETTING_ASK,
237 map->GetContentSetting(
238 url, url, CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS,
239 std::string()));
242 private:
243 Profile* profile_;
246 // http://crbug.com/177163
247 #if defined(OS_WIN) && !defined(NDEBUG)
248 #define MAYBE_Standard DISABLED_Standard
249 #else
250 #define MAYBE_Standard Standard
251 #endif
252 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest, MAYBE_Standard) {
253 CheckContentSettingsDefault();
255 const char kExtensionPath[] = "content_settings/standard";
257 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
258 CheckContentSettingsSet();
260 // The settings should not be reset when the extension is reloaded.
261 ReloadExtension(last_loaded_extension_id());
262 CheckContentSettingsSet();
264 // Uninstalling and installing the extension (without running the test that
265 // calls the extension API) should clear the settings.
266 TestExtensionRegistryObserver observer(ExtensionRegistry::Get(profile()),
267 last_loaded_extension_id());
268 UninstallExtension(last_loaded_extension_id());
269 observer.WaitForExtensionUninstalled();
270 CheckContentSettingsDefault();
272 LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
273 CheckContentSettingsDefault();
276 // Flaky on the trybots. See http://crbug.com/96725.
277 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest,
278 DISABLED_GetResourceIdentifiers) {
279 base::FilePath::CharType kFooPath[] =
280 FILE_PATH_LITERAL("/plugins/foo.plugin");
281 base::FilePath::CharType kBarPath[] =
282 FILE_PATH_LITERAL("/plugins/bar.plugin");
283 const char kFooName[] = "Foo Plugin";
284 const char kBarName[] = "Bar Plugin";
286 content::PluginService::GetInstance()->RegisterInternalPlugin(
287 content::WebPluginInfo(base::ASCIIToUTF16(kFooName),
288 base::FilePath(kFooPath),
289 base::ASCIIToUTF16("1.2.3"),
290 base::ASCIIToUTF16("foo")),
291 false);
292 content::PluginService::GetInstance()->RegisterInternalPlugin(
293 content::WebPluginInfo(base::ASCIIToUTF16(kBarName),
294 base::FilePath(kBarPath),
295 base::ASCIIToUTF16("2.3.4"),
296 base::ASCIIToUTF16("bar")),
297 false);
299 EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
300 << message_;
303 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest,
304 UnsupportedDefaultSettings) {
305 const char kExtensionPath[] = "content_settings/unsupporteddefaultsettings";
306 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
309 } // namespace extensions