OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/content_settings.h" | 5 #include "chrome/common/content_settings.h" |
6 #include "chrome/common/render_messages.h" | 6 #include "chrome/common/render_messages.h" |
7 #include "chrome/renderer/content_settings_observer.h" | 7 #include "chrome/renderer/content_settings_observer.h" |
8 #include "chrome/test/base/chrome_render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
10 #include "ipc/ipc_message_macros.h" | 10 #include "ipc/ipc_message_macros.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 EXPECT_FALSE(observer->plugins_temporarily_allowed()); | 167 EXPECT_FALSE(observer->plugins_temporarily_allowed()); |
168 } | 168 } |
169 | 169 |
170 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { | 170 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
171 MockContentSettingsObserver mock_observer(view_); | 171 MockContentSettingsObserver mock_observer(view_); |
172 | 172 |
173 // Load some HTML. | 173 // Load some HTML. |
174 LoadHTML("<html>Foo</html>"); | 174 LoadHTML("<html>Foo</html>"); |
175 | 175 |
176 // Set the default image blocking setting. | 176 // Set the default image blocking setting. |
177 ContentSettingsForOneType image_setting_rules; | 177 ContentSettingsForOneType content_setting_rules[CONTENT_SETTINGS_NUM_TYPES]; |
| 178 ContentSettingsForOneType& image_setting_rules = |
| 179 content_setting_rules[CONTENT_SETTINGS_TYPE_IMAGES]; |
178 image_setting_rules.push_back( | 180 image_setting_rules.push_back( |
179 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 181 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
180 ContentSettingsPattern::Wildcard(), | 182 ContentSettingsPattern::Wildcard(), |
181 CONTENT_SETTING_BLOCK, | 183 CONTENT_SETTING_BLOCK, |
182 "", | 184 "", |
183 false)); | 185 false)); |
184 | 186 |
185 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 187 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
186 observer->SetImageSettingRules(&image_setting_rules); | 188 observer->SetContentSettingRules(content_setting_rules); |
187 EXPECT_CALL(mock_observer, | 189 EXPECT_CALL(mock_observer, |
188 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 190 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
189 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 191 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
190 true, mock_observer.image_url_)); | 192 true, mock_observer.image_url_)); |
191 ::testing::Mock::VerifyAndClearExpectations(&observer); | 193 ::testing::Mock::VerifyAndClearExpectations(&observer); |
192 | 194 |
193 // Create an exception which allows the image. | 195 // Create an exception which allows the image. |
194 image_setting_rules.insert( | 196 image_setting_rules.insert( |
195 image_setting_rules.begin(), | 197 image_setting_rules.begin(), |
196 ContentSettingPatternSource( | 198 ContentSettingPatternSource( |
(...skipping 11 matching lines...) Expand all Loading... |
208 ::testing::Mock::VerifyAndClearExpectations(&observer); | 210 ::testing::Mock::VerifyAndClearExpectations(&observer); |
209 } | 211 } |
210 | 212 |
211 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 213 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
212 MockContentSettingsObserver mock_observer(view_); | 214 MockContentSettingsObserver mock_observer(view_); |
213 | 215 |
214 // Load some HTML. | 216 // Load some HTML. |
215 LoadHTML("<html>Foo</html>"); | 217 LoadHTML("<html>Foo</html>"); |
216 | 218 |
217 // Set the default image blocking setting. | 219 // Set the default image blocking setting. |
218 ContentSettingsForOneType image_setting_rules; | 220 ContentSettingsForOneType content_setting_rules[CONTENT_SETTINGS_NUM_TYPES]; |
| 221 ContentSettingsForOneType& image_setting_rules = |
| 222 content_setting_rules[CONTENT_SETTINGS_TYPE_IMAGES]; |
219 image_setting_rules.push_back( | 223 image_setting_rules.push_back( |
220 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 224 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
221 ContentSettingsPattern::Wildcard(), | 225 ContentSettingsPattern::Wildcard(), |
222 CONTENT_SETTING_ALLOW, | 226 CONTENT_SETTING_ALLOW, |
223 "", | 227 "", |
224 false)); | 228 false)); |
225 | 229 |
226 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 230 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
227 observer->SetImageSettingRules(&image_setting_rules); | 231 observer->SetContentSettingRules(content_setting_rules); |
228 EXPECT_CALL( | 232 EXPECT_CALL( |
229 mock_observer, | 233 mock_observer, |
230 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 234 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
231 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 235 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
232 mock_observer.image_url_)); | 236 mock_observer.image_url_)); |
233 ::testing::Mock::VerifyAndClearExpectations(&observer); | 237 ::testing::Mock::VerifyAndClearExpectations(&observer); |
234 | 238 |
235 // Create an exception which blocks the image. | 239 // Create an exception which blocks the image. |
236 image_setting_rules.insert( | 240 image_setting_rules.insert( |
237 image_setting_rules.begin(), | 241 image_setting_rules.begin(), |
238 ContentSettingPatternSource( | 242 ContentSettingPatternSource( |
239 ContentSettingsPattern::Wildcard(), | 243 ContentSettingsPattern::Wildcard(), |
240 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 244 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
241 CONTENT_SETTING_BLOCK, | 245 CONTENT_SETTING_BLOCK, |
242 "", | 246 "", |
243 false)); | 247 false)); |
244 EXPECT_CALL(mock_observer, | 248 EXPECT_CALL(mock_observer, |
245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 249 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
246 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 250 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
247 true, mock_observer.image_url_)); | 251 true, mock_observer.image_url_)); |
248 ::testing::Mock::VerifyAndClearExpectations(&observer); | 252 ::testing::Mock::VerifyAndClearExpectations(&observer); |
249 } | 253 } |
| 254 |
| 255 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 256 // Set the content settings for scripts. |
| 257 ContentSettingsForOneType content_setting_rules[CONTENT_SETTINGS_NUM_TYPES]; |
| 258 ContentSettingsForOneType& script_setting_rules = |
| 259 content_setting_rules[CONTENT_SETTINGS_TYPE_JAVASCRIPT]; |
| 260 script_setting_rules.push_back( |
| 261 ContentSettingPatternSource( |
| 262 ContentSettingsPattern::Wildcard(), |
| 263 ContentSettingsPattern::Wildcard(), |
| 264 CONTENT_SETTING_BLOCK, |
| 265 "", |
| 266 false)); |
| 267 |
| 268 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 269 observer->SetContentSettingRules(content_setting_rules); |
| 270 |
| 271 // Load a page which contains a script. |
| 272 std::string html = "<html>" |
| 273 "<head>" |
| 274 "<script src='data:foo'></script>" |
| 275 "</head>" |
| 276 "<body>" |
| 277 "</body>" |
| 278 "</html>"; |
| 279 LoadHTML(html.c_str()); |
| 280 |
| 281 // Verify that the script was blocked. |
| 282 bool was_blocked = false; |
| 283 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 284 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 285 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 286 was_blocked = true; |
| 287 } |
| 288 EXPECT_TRUE(was_blocked); |
| 289 } |
| 290 |
| 291 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| 292 // Set the content settings for scripts. |
| 293 ContentSettingsForOneType content_setting_rules[CONTENT_SETTINGS_NUM_TYPES]; |
| 294 ContentSettingsForOneType& script_setting_rules = |
| 295 content_setting_rules[CONTENT_SETTINGS_TYPE_JAVASCRIPT]; |
| 296 script_setting_rules.push_back( |
| 297 ContentSettingPatternSource( |
| 298 ContentSettingsPattern::Wildcard(), |
| 299 ContentSettingsPattern::Wildcard(), |
| 300 CONTENT_SETTING_ALLOW, |
| 301 "", |
| 302 false)); |
| 303 |
| 304 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 305 observer->SetContentSettingRules(content_setting_rules); |
| 306 |
| 307 // Load a page which contains a script. |
| 308 std::string html = "<html>" |
| 309 "<head>" |
| 310 "<script src='data:foo'></script>" |
| 311 "</head>" |
| 312 "<body>" |
| 313 "</body>" |
| 314 "</html>"; |
| 315 LoadHTML(html.c_str()); |
| 316 |
| 317 // Verify that the script was not blocked. |
| 318 bool was_blocked = false; |
| 319 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 320 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 321 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 322 was_blocked = true; |
| 323 } |
| 324 EXPECT_FALSE(was_blocked); |
| 325 } |
OLD | NEW |