OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 #import "chrome/browser/ui/cocoa/content_settings_dialog_controller.h" | |
6 | |
7 #include "base/auto_reset.h" | |
8 #include "base/command_line.h" | |
9 #import "base/scoped_nsobject.h" | |
10 #include "base/ref_counted.h" | |
11 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
12 #include "chrome/browser/geolocation/geolocation_content_settings_map.h" | |
13 #include "chrome/browser/notifications/desktop_notification_service.h" | |
14 #include "chrome/browser/prefs/pref_service.h" | |
15 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | |
16 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "testing/platform_test.h" | |
21 | |
22 namespace { | |
23 | |
24 class ContentSettingsDialogControllerTest : public CocoaTest { | |
25 public: | |
26 virtual void SetUp() { | |
27 CocoaTest::SetUp(); | |
28 TestingProfile* profile = browser_helper_.profile(); | |
29 settingsMap_ = new HostContentSettingsMap(profile); | |
30 geoSettingsMap_ = new GeolocationContentSettingsMap(profile); | |
31 notificationsService_.reset(new DesktopNotificationService(profile, NULL)); | |
32 controller_ = [ContentSettingsDialogController | |
33 showContentSettingsForType:CONTENT_SETTINGS_TYPE_DEFAULT | |
34 profile:browser_helper_.profile()]; | |
35 } | |
36 | |
37 virtual void TearDown() { | |
38 [controller_ close]; | |
39 CocoaTest::TearDown(); | |
40 } | |
41 | |
42 protected: | |
43 ContentSettingsDialogController* controller_; | |
44 BrowserTestHelper browser_helper_; | |
45 scoped_refptr<HostContentSettingsMap> settingsMap_; | |
46 scoped_refptr<GeolocationContentSettingsMap> geoSettingsMap_; | |
47 scoped_ptr<DesktopNotificationService> notificationsService_; | |
48 }; | |
49 | |
50 // Test that +showContentSettingsDialogForProfile brings up the existing editor | |
51 // and doesn't leak or crash. | |
52 TEST_F(ContentSettingsDialogControllerTest, CreateDialog) { | |
53 EXPECT_TRUE(controller_); | |
54 } | |
55 | |
56 TEST_F(ContentSettingsDialogControllerTest, CookieSetting) { | |
57 // Change setting, check dialog property. | |
58 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | |
59 CONTENT_SETTING_ALLOW); | |
60 EXPECT_EQ([controller_ cookieSettingIndex], kCookieEnabledIndex); | |
61 | |
62 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, | |
63 CONTENT_SETTING_BLOCK); | |
64 EXPECT_EQ([controller_ cookieSettingIndex], kCookieDisabledIndex); | |
65 | |
66 // Change dialog property, check setting. | |
67 NSInteger setting; | |
68 [controller_ setCookieSettingIndex:kCookieEnabledIndex]; | |
69 setting = | |
70 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES); | |
71 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
72 | |
73 [controller_ setCookieSettingIndex:kCookieDisabledIndex]; | |
74 setting = | |
75 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES); | |
76 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
77 } | |
78 | |
79 TEST_F(ContentSettingsDialogControllerTest, BlockThirdPartyCookiesSetting) { | |
80 // Change setting, check dialog property. | |
81 settingsMap_->SetBlockThirdPartyCookies(YES); | |
82 EXPECT_TRUE([controller_ blockThirdPartyCookies]); | |
83 | |
84 settingsMap_->SetBlockThirdPartyCookies(NO); | |
85 EXPECT_FALSE([controller_ blockThirdPartyCookies]); | |
86 | |
87 // Change dialog property, check setting. | |
88 [controller_ setBlockThirdPartyCookies:YES]; | |
89 EXPECT_TRUE(settingsMap_->BlockThirdPartyCookies()); | |
90 | |
91 [controller_ setBlockThirdPartyCookies:NO]; | |
92 EXPECT_FALSE(settingsMap_->BlockThirdPartyCookies()); | |
93 } | |
94 | |
95 TEST_F(ContentSettingsDialogControllerTest, ClearSiteDataOnExitSetting) { | |
96 TestingProfile* profile = browser_helper_.profile(); | |
97 | |
98 // Change setting, check dialog property. | |
99 profile->GetPrefs()->SetBoolean(prefs::kClearSiteDataOnExit, true); | |
100 EXPECT_TRUE([controller_ clearSiteDataOnExit]); | |
101 | |
102 profile->GetPrefs()->SetBoolean(prefs::kClearSiteDataOnExit, false); | |
103 EXPECT_FALSE([controller_ clearSiteDataOnExit]); | |
104 | |
105 // Change dialog property, check setting. | |
106 [controller_ setClearSiteDataOnExit:YES]; | |
107 EXPECT_TRUE(profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit)); | |
108 | |
109 [controller_ setClearSiteDataOnExit:NO]; | |
110 EXPECT_FALSE(profile->GetPrefs()->GetBoolean(prefs::kClearSiteDataOnExit)); | |
111 } | |
112 | |
113 TEST_F(ContentSettingsDialogControllerTest, ImagesSetting) { | |
114 // Change setting, check dialog property. | |
115 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_IMAGES, | |
116 CONTENT_SETTING_ALLOW); | |
117 EXPECT_EQ([controller_ imagesEnabledIndex], kContentSettingsEnabledIndex); | |
118 | |
119 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_IMAGES, | |
120 CONTENT_SETTING_BLOCK); | |
121 EXPECT_EQ([controller_ imagesEnabledIndex], kContentSettingsDisabledIndex); | |
122 | |
123 // Change dialog property, check setting. | |
124 NSInteger setting; | |
125 [controller_ setImagesEnabledIndex:kContentSettingsEnabledIndex]; | |
126 setting = | |
127 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_IMAGES); | |
128 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
129 | |
130 [controller_ setImagesEnabledIndex:kContentSettingsDisabledIndex]; | |
131 setting = | |
132 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_IMAGES); | |
133 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
134 } | |
135 | |
136 TEST_F(ContentSettingsDialogControllerTest, JavaScriptSetting) { | |
137 // Change setting, check dialog property. | |
138 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
139 CONTENT_SETTING_ALLOW); | |
140 EXPECT_EQ([controller_ javaScriptEnabledIndex], kContentSettingsEnabledIndex); | |
141 | |
142 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT, | |
143 CONTENT_SETTING_BLOCK); | |
144 EXPECT_EQ([controller_ javaScriptEnabledIndex], | |
145 kContentSettingsDisabledIndex); | |
146 | |
147 // Change dialog property, check setting. | |
148 NSInteger setting; | |
149 [controller_ setJavaScriptEnabledIndex:kContentSettingsEnabledIndex]; | |
150 setting = | |
151 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT); | |
152 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
153 | |
154 [controller_ setJavaScriptEnabledIndex:kContentSettingsDisabledIndex]; | |
155 setting = | |
156 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT); | |
157 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
158 } | |
159 | |
160 TEST_F(ContentSettingsDialogControllerTest, PluginsSetting) { | |
161 // Change setting, check dialog property. | |
162 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS, | |
163 CONTENT_SETTING_ALLOW); | |
164 EXPECT_EQ(kPluginsAllowIndex, [controller_ pluginsEnabledIndex]); | |
165 | |
166 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS, | |
167 CONTENT_SETTING_BLOCK); | |
168 EXPECT_EQ(kPluginsBlockIndex, [controller_ pluginsEnabledIndex]); | |
169 | |
170 { | |
171 // Click-to-play needs to be enabled to set the content setting to ASK. | |
172 CommandLine* cmd = CommandLine::ForCurrentProcess(); | |
173 AutoReset<CommandLine> auto_reset(cmd, *cmd); | |
174 cmd->AppendSwitch(switches::kEnableClickToPlay); | |
175 | |
176 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS, | |
177 CONTENT_SETTING_ASK); | |
178 EXPECT_EQ(kPluginsAskIndex, [controller_ pluginsEnabledIndex]); | |
179 } | |
180 | |
181 // Change dialog property, check setting. | |
182 NSInteger setting; | |
183 [controller_ setPluginsEnabledIndex:kPluginsAllowIndex]; | |
184 setting = | |
185 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS); | |
186 EXPECT_EQ(CONTENT_SETTING_ALLOW, setting); | |
187 | |
188 [controller_ setPluginsEnabledIndex:kPluginsBlockIndex]; | |
189 setting = | |
190 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS); | |
191 EXPECT_EQ(CONTENT_SETTING_BLOCK, setting); | |
192 | |
193 { | |
194 CommandLine* cmd = CommandLine::ForCurrentProcess(); | |
195 AutoReset<CommandLine> auto_reset(cmd, *cmd); | |
196 cmd->AppendSwitch(switches::kEnableClickToPlay); | |
197 | |
198 [controller_ setPluginsEnabledIndex:kPluginsAskIndex]; | |
199 setting = | |
200 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS); | |
201 EXPECT_EQ(CONTENT_SETTING_ASK, setting); | |
202 } | |
203 } | |
204 | |
205 TEST_F(ContentSettingsDialogControllerTest, PopupsSetting) { | |
206 // Change setting, check dialog property. | |
207 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | |
208 CONTENT_SETTING_ALLOW); | |
209 EXPECT_EQ([controller_ popupsEnabledIndex], kContentSettingsEnabledIndex); | |
210 | |
211 settingsMap_->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS, | |
212 CONTENT_SETTING_BLOCK); | |
213 EXPECT_EQ([controller_ popupsEnabledIndex], kContentSettingsDisabledIndex); | |
214 | |
215 // Change dialog property, check setting. | |
216 NSInteger setting; | |
217 [controller_ setPopupsEnabledIndex:kContentSettingsEnabledIndex]; | |
218 setting = | |
219 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS); | |
220 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
221 | |
222 [controller_ setPopupsEnabledIndex:kContentSettingsDisabledIndex]; | |
223 setting = | |
224 settingsMap_->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_POPUPS); | |
225 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
226 } | |
227 | |
228 TEST_F(ContentSettingsDialogControllerTest, GeolocationSetting) { | |
229 // Change setting, check dialog property. | |
230 geoSettingsMap_->SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
231 EXPECT_EQ([controller_ geolocationSettingIndex], kGeolocationEnabledIndex); | |
232 | |
233 geoSettingsMap_->SetDefaultContentSetting(CONTENT_SETTING_ASK); | |
234 EXPECT_EQ([controller_ geolocationSettingIndex], kGeolocationAskIndex); | |
235 | |
236 geoSettingsMap_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); | |
237 EXPECT_EQ([controller_ geolocationSettingIndex], kGeolocationDisabledIndex); | |
238 | |
239 // Change dialog property, check setting. | |
240 NSInteger setting; | |
241 [controller_ setGeolocationSettingIndex:kGeolocationEnabledIndex]; | |
242 setting = | |
243 geoSettingsMap_->GetDefaultContentSetting(); | |
244 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
245 | |
246 [controller_ setGeolocationSettingIndex:kGeolocationAskIndex]; | |
247 setting = | |
248 geoSettingsMap_->GetDefaultContentSetting(); | |
249 EXPECT_EQ(setting, CONTENT_SETTING_ASK); | |
250 | |
251 [controller_ setGeolocationSettingIndex:kGeolocationDisabledIndex]; | |
252 setting = | |
253 geoSettingsMap_->GetDefaultContentSetting(); | |
254 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
255 } | |
256 | |
257 TEST_F(ContentSettingsDialogControllerTest, NotificationsSetting) { | |
258 // Change setting, check dialog property. | |
259 notificationsService_->SetDefaultContentSetting(CONTENT_SETTING_ALLOW); | |
260 EXPECT_EQ([controller_ notificationsSettingIndex], | |
261 kNotificationsEnabledIndex); | |
262 | |
263 notificationsService_->SetDefaultContentSetting(CONTENT_SETTING_ASK); | |
264 EXPECT_EQ([controller_ notificationsSettingIndex], kNotificationsAskIndex); | |
265 | |
266 notificationsService_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); | |
267 EXPECT_EQ([controller_ notificationsSettingIndex], | |
268 kNotificationsDisabledIndex); | |
269 | |
270 // Change dialog property, check setting. | |
271 NSInteger setting; | |
272 [controller_ setNotificationsSettingIndex:kNotificationsEnabledIndex]; | |
273 setting = | |
274 notificationsService_->GetDefaultContentSetting(); | |
275 EXPECT_EQ(setting, CONTENT_SETTING_ALLOW); | |
276 | |
277 [controller_ setNotificationsSettingIndex:kNotificationsAskIndex]; | |
278 setting = | |
279 notificationsService_->GetDefaultContentSetting(); | |
280 EXPECT_EQ(setting, CONTENT_SETTING_ASK); | |
281 | |
282 [controller_ setNotificationsSettingIndex:kNotificationsDisabledIndex]; | |
283 setting = | |
284 notificationsService_->GetDefaultContentSetting(); | |
285 EXPECT_EQ(setting, CONTENT_SETTING_BLOCK); | |
286 } | |
287 | |
288 } // namespace | |
289 | |
OLD | NEW |