content_settings_apitest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
5#include "base/prefs/pref_service.h"
6#include "base/utf_string_conversions.h"
7#include "chrome/browser/content_settings/cookie_settings.h"
8#include "chrome/browser/content_settings/host_content_settings_map.h"
9#include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
10#include "chrome/browser/extensions/extension_apitest.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/common/chrome_switches.h"
14#include "chrome/common/pref_names.h"
15#include "webkit/plugins/npapi/mock_plugin_list.h"
16
17namespace extensions {
18
19IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentSettings) {
20  CommandLine::ForCurrentProcess()->AppendSwitch(
21      switches::kEnableExperimentalExtensionApis);
22
23  EXPECT_TRUE(RunExtensionTest("content_settings/standard")) << message_;
24
25  HostContentSettingsMap* map =
26      browser()->profile()->GetHostContentSettingsMap();
27  CookieSettings* cookie_settings =
28      CookieSettings::Factory::GetForProfile(browser()->profile());
29
30  // Check default content settings by using an unknown URL.
31  GURL example_url("http://www.example.com");
32  EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
33      example_url, example_url));
34  EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
35      example_url, example_url));
36  EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(example_url));
37  EXPECT_EQ(CONTENT_SETTING_ALLOW,
38            map->GetContentSetting(example_url,
39                                   example_url,
40                                   CONTENT_SETTINGS_TYPE_IMAGES,
41                                   std::string()));
42  EXPECT_EQ(CONTENT_SETTING_BLOCK,
43            map->GetContentSetting(example_url,
44                                   example_url,
45                                   CONTENT_SETTINGS_TYPE_JAVASCRIPT,
46                                   std::string()));
47  EXPECT_EQ(CONTENT_SETTING_ALLOW,
48            map->GetContentSetting(example_url,
49                                   example_url,
50                                   CONTENT_SETTINGS_TYPE_PLUGINS,
51                                   std::string()));
52  EXPECT_EQ(CONTENT_SETTING_BLOCK,
53            map->GetContentSetting(example_url,
54                                   example_url,
55                                   CONTENT_SETTINGS_TYPE_POPUPS,
56                                   std::string()));
57#if 0
58  // TODO(bauerb): Enable once geolocation settings are integrated into the
59  // HostContentSettingsMap.
60  EXPECT_EQ(CONTENT_SETTING_ALLOW,
61            map->GetContentSetting(example_url,
62                                   example_url,
63                                   CONTENT_SETTINGS_TYPE_GEOLOCATION,
64                                   std::string()));
65#endif
66  EXPECT_EQ(CONTENT_SETTING_ASK,
67            map->GetContentSetting(example_url,
68                                   example_url,
69                                   CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
70                                   std::string()));
71
72  // Check content settings for www.google.com
73  GURL url("http://www.google.com");
74  EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(url, url));
75  EXPECT_EQ(CONTENT_SETTING_ALLOW,
76            map->GetContentSetting(
77                url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
78  EXPECT_EQ(CONTENT_SETTING_BLOCK,
79            map->GetContentSetting(
80                url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
81  EXPECT_EQ(CONTENT_SETTING_BLOCK,
82            map->GetContentSetting(
83                url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
84  EXPECT_EQ(CONTENT_SETTING_ALLOW,
85            map->GetContentSetting(
86                url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
87#if 0
88  EXPECT_EQ(CONTENT_SETTING_BLOCK,
89            map->GetContentSetting(
90                url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, ""));
91#endif
92  EXPECT_EQ(CONTENT_SETTING_BLOCK,
93            map->GetContentSetting(
94                url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
95}
96
97// Flaky on the trybots. See http://crbug.com/96725.
98IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
99                       DISABLED_ContentSettingsGetResourceIdentifiers) {
100  CommandLine::ForCurrentProcess()->AppendSwitch(
101      switches::kEnableExperimentalExtensionApis);
102
103  base::FilePath::CharType kFooPath[] =
104      FILE_PATH_LITERAL("/plugins/foo.plugin");
105  base::FilePath::CharType kBarPath[] =
106      FILE_PATH_LITERAL("/plugins/bar.plugin");
107  const char* kFooName = "Foo Plugin";
108  const char* kBarName = "Bar Plugin";
109
110  webkit::npapi::MockPluginList plugin_list;
111  plugin_list.AddPluginToLoad(
112      webkit::WebPluginInfo(ASCIIToUTF16(kFooName),
113                            base::FilePath(kFooPath),
114                            ASCIIToUTF16("1.2.3"),
115                            ASCIIToUTF16("foo")));
116  plugin_list.AddPluginToLoad(
117      webkit::WebPluginInfo(ASCIIToUTF16(kBarName),
118                            base::FilePath(kBarPath),
119                            ASCIIToUTF16("2.3.4"),
120                            ASCIIToUTF16("bar")));
121
122  std::vector<webkit::WebPluginInfo> plugins;
123  plugin_list.GetPlugins(&plugins);
124
125  ContentSettingsContentSettingGetResourceIdentifiersFunction::
126      SetPluginsForTesting(&plugins);
127
128  EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
129      << message_;
130
131  ContentSettingsContentSettingGetResourceIdentifiersFunction::
132      SetPluginsForTesting(NULL);
133}
134
135}  // namespace extensions
136