OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 RendererContentSettingRules content_setting_rules; | 109 RendererContentSettingRules content_setting_rules; |
110 ContentSettingsForOneType& script_setting_rules = | 110 ContentSettingsForOneType& script_setting_rules = |
111 content_setting_rules.script_rules; | 111 content_setting_rules.script_rules; |
112 script_setting_rules.push_back( | 112 script_setting_rules.push_back( |
113 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 113 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
114 ContentSettingsPattern::Wildcard(), | 114 ContentSettingsPattern::Wildcard(), |
115 CONTENT_SETTING_BLOCK, | 115 CONTENT_SETTING_BLOCK, |
116 std::string(), | 116 std::string(), |
117 false)); | 117 false)); |
118 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 118 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
119 observer->SetContentSettingRules(&content_setting_rules); | 119 observer->OnSetContentSettingRules(content_setting_rules); |
120 | 120 |
121 // Make sure no pending messages are in the queue. | 121 // Make sure no pending messages are in the queue. |
122 ProcessPendingMessages(); | 122 ProcessPendingMessages(); |
123 render_thread_->sink().ClearMessages(); | 123 render_thread_->sink().ClearMessages(); |
124 | 124 |
125 // 3. Reload page. | 125 // 3. Reload page. |
126 std::string url_str = "data:text/html;charset=utf-8,"; | 126 std::string url_str = "data:text/html;charset=utf-8,"; |
127 url_str.append(html); | 127 url_str.append(html); |
128 GURL url(url_str); | 128 GURL url(url_str); |
129 Reload(url); | 129 Reload(url); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 ContentSettingsForOneType& image_setting_rules = | 189 ContentSettingsForOneType& image_setting_rules = |
190 content_setting_rules.image_rules; | 190 content_setting_rules.image_rules; |
191 image_setting_rules.push_back( | 191 image_setting_rules.push_back( |
192 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 192 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
193 ContentSettingsPattern::Wildcard(), | 193 ContentSettingsPattern::Wildcard(), |
194 CONTENT_SETTING_BLOCK, | 194 CONTENT_SETTING_BLOCK, |
195 std::string(), | 195 std::string(), |
196 false)); | 196 false)); |
197 | 197 |
198 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 198 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
199 observer->SetContentSettingRules(&content_setting_rules); | 199 observer->OnSetContentSettingRules(content_setting_rules); |
200 EXPECT_CALL(mock_observer, | 200 EXPECT_CALL(mock_observer, |
201 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 201 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
202 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 202 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
203 true, mock_observer.image_url_)); | 203 true, mock_observer.image_url_)); |
204 ::testing::Mock::VerifyAndClearExpectations(&observer); | 204 ::testing::Mock::VerifyAndClearExpectations(&observer); |
205 | 205 |
206 // Create an exception which allows the image. | 206 // Create an exception which allows the image. |
207 image_setting_rules.insert( | 207 image_setting_rules.insert( |
208 image_setting_rules.begin(), | 208 image_setting_rules.begin(), |
209 ContentSettingPatternSource( | 209 ContentSettingPatternSource( |
210 ContentSettingsPattern::Wildcard(), | 210 ContentSettingsPattern::Wildcard(), |
211 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 211 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
212 CONTENT_SETTING_ALLOW, | 212 CONTENT_SETTING_ALLOW, |
213 std::string(), | 213 std::string(), |
214 false)); | 214 false)); |
| 215 observer->OnSetContentSettingRules(content_setting_rules); |
215 | 216 |
216 EXPECT_CALL( | 217 EXPECT_CALL( |
217 mock_observer, | 218 mock_observer, |
218 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 219 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
219 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 220 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
220 mock_observer.image_url_)); | 221 mock_observer.image_url_)); |
221 ::testing::Mock::VerifyAndClearExpectations(&observer); | 222 ::testing::Mock::VerifyAndClearExpectations(&observer); |
222 } | 223 } |
223 | 224 |
224 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 225 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
225 MockContentSettingsObserver mock_observer(view_); | 226 MockContentSettingsObserver mock_observer(view_); |
226 | 227 |
227 // Load some HTML. | 228 // Load some HTML. |
228 LoadHTML("<html>Foo</html>"); | 229 LoadHTML("<html>Foo</html>"); |
229 | 230 |
230 // Set the default image blocking setting. | 231 // Set the default image blocking setting. |
231 RendererContentSettingRules content_setting_rules; | 232 RendererContentSettingRules content_setting_rules; |
232 ContentSettingsForOneType& image_setting_rules = | 233 ContentSettingsForOneType& image_setting_rules = |
233 content_setting_rules.image_rules; | 234 content_setting_rules.image_rules; |
234 image_setting_rules.push_back( | 235 image_setting_rules.push_back( |
235 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 236 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
236 ContentSettingsPattern::Wildcard(), | 237 ContentSettingsPattern::Wildcard(), |
237 CONTENT_SETTING_ALLOW, | 238 CONTENT_SETTING_ALLOW, |
238 std::string(), | 239 std::string(), |
239 false)); | 240 false)); |
240 | 241 |
241 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 242 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
242 observer->SetContentSettingRules(&content_setting_rules); | 243 observer->OnSetContentSettingRules(content_setting_rules); |
243 EXPECT_CALL( | 244 EXPECT_CALL( |
244 mock_observer, | 245 mock_observer, |
245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 246 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
246 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 247 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
247 mock_observer.image_url_)); | 248 mock_observer.image_url_)); |
248 ::testing::Mock::VerifyAndClearExpectations(&observer); | 249 ::testing::Mock::VerifyAndClearExpectations(&observer); |
249 | 250 |
250 // Create an exception which blocks the image. | 251 // Create an exception which blocks the image. |
251 image_setting_rules.insert( | 252 image_setting_rules.insert( |
252 image_setting_rules.begin(), | 253 image_setting_rules.begin(), |
253 ContentSettingPatternSource( | 254 ContentSettingPatternSource( |
254 ContentSettingsPattern::Wildcard(), | 255 ContentSettingsPattern::Wildcard(), |
255 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 256 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
256 CONTENT_SETTING_BLOCK, | 257 CONTENT_SETTING_BLOCK, |
257 std::string(), | 258 std::string(), |
258 false)); | 259 false)); |
| 260 observer->OnSetContentSettingRules(content_setting_rules); |
259 EXPECT_CALL(mock_observer, | 261 EXPECT_CALL(mock_observer, |
260 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 262 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
261 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 263 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
262 true, mock_observer.image_url_)); | 264 true, mock_observer.image_url_)); |
263 ::testing::Mock::VerifyAndClearExpectations(&observer); | 265 ::testing::Mock::VerifyAndClearExpectations(&observer); |
264 } | 266 } |
265 | 267 |
266 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { | 268 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
267 // Set the content settings for scripts. | 269 // Set the content settings for scripts. |
268 RendererContentSettingRules content_setting_rules; | 270 RendererContentSettingRules content_setting_rules; |
269 ContentSettingsForOneType& script_setting_rules = | 271 ContentSettingsForOneType& script_setting_rules = |
270 content_setting_rules.script_rules; | 272 content_setting_rules.script_rules; |
271 script_setting_rules.push_back( | 273 script_setting_rules.push_back( |
272 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 274 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
273 ContentSettingsPattern::Wildcard(), | 275 ContentSettingsPattern::Wildcard(), |
274 CONTENT_SETTING_BLOCK, | 276 CONTENT_SETTING_BLOCK, |
275 std::string(), | 277 std::string(), |
276 false)); | 278 false)); |
277 | 279 |
278 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 280 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
279 observer->SetContentSettingRules(&content_setting_rules); | 281 observer->OnSetContentSettingRules(content_setting_rules); |
280 | 282 |
281 // Load a page which contains a script. | 283 // Load a page which contains a script. |
282 std::string html = "<html>" | 284 std::string html = "<html>" |
283 "<head>" | 285 "<head>" |
284 "<script src='data:foo'></script>" | 286 "<script src='data:foo'></script>" |
285 "</head>" | 287 "</head>" |
286 "<body>" | 288 "<body>" |
287 "</body>" | 289 "</body>" |
288 "</html>"; | 290 "</html>"; |
289 LoadHTML(html.c_str()); | 291 LoadHTML(html.c_str()); |
(...skipping 14 matching lines...) Expand all Loading... |
304 ContentSettingsForOneType& script_setting_rules = | 306 ContentSettingsForOneType& script_setting_rules = |
305 content_setting_rules.script_rules; | 307 content_setting_rules.script_rules; |
306 script_setting_rules.push_back( | 308 script_setting_rules.push_back( |
307 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 309 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
308 ContentSettingsPattern::Wildcard(), | 310 ContentSettingsPattern::Wildcard(), |
309 CONTENT_SETTING_ALLOW, | 311 CONTENT_SETTING_ALLOW, |
310 std::string(), | 312 std::string(), |
311 false)); | 313 false)); |
312 | 314 |
313 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 315 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
314 observer->SetContentSettingRules(&content_setting_rules); | 316 observer->OnSetContentSettingRules(content_setting_rules); |
315 | 317 |
316 // Load a page which contains a script. | 318 // Load a page which contains a script. |
317 std::string html = "<html>" | 319 std::string html = "<html>" |
318 "<head>" | 320 "<head>" |
319 "<script src='data:foo'></script>" | 321 "<script src='data:foo'></script>" |
320 "</head>" | 322 "</head>" |
321 "<body>" | 323 "<body>" |
322 "</body>" | 324 "</body>" |
323 "</html>"; | 325 "</html>"; |
324 LoadHTML(html.c_str()); | 326 LoadHTML(html.c_str()); |
(...skipping 24 matching lines...) Expand all Loading... |
349 ContentSettingsForOneType& image_setting_rules = | 351 ContentSettingsForOneType& image_setting_rules = |
350 content_setting_rules.image_rules; | 352 content_setting_rules.image_rules; |
351 image_setting_rules.push_back( | 353 image_setting_rules.push_back( |
352 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 354 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
353 ContentSettingsPattern::Wildcard(), | 355 ContentSettingsPattern::Wildcard(), |
354 CONTENT_SETTING_BLOCK, | 356 CONTENT_SETTING_BLOCK, |
355 std::string(), | 357 std::string(), |
356 false)); | 358 false)); |
357 | 359 |
358 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 360 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
359 observer->SetContentSettingRules(&content_setting_rules); | 361 observer->OnSetContentSettingRules(content_setting_rules); |
360 observer->OnSetAsInterstitial(); | 362 observer->OnSetAsInterstitial(); |
361 | 363 |
362 // Load a page which contains a script. | 364 // Load a page which contains a script. |
363 std::string html = "<html>" | 365 std::string html = "<html>" |
364 "<head>" | 366 "<head>" |
365 "<script src='data:foo'></script>" | 367 "<script src='data:foo'></script>" |
366 "</head>" | 368 "</head>" |
367 "<body>" | 369 "<body>" |
368 "</body>" | 370 "</body>" |
369 "</html>"; | 371 "</html>"; |
370 LoadHTML(html.c_str()); | 372 LoadHTML(html.c_str()); |
371 | 373 |
372 // Verify that the script was allowed. | 374 // Verify that the script was allowed. |
373 bool was_blocked = false; | 375 bool was_blocked = false; |
374 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 376 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
375 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 377 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
376 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 378 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
377 was_blocked = true; | 379 was_blocked = true; |
378 } | 380 } |
379 EXPECT_FALSE(was_blocked); | 381 EXPECT_FALSE(was_blocked); |
380 | 382 |
381 // Verify that images are allowed. | 383 // Verify that images are allowed. |
382 EXPECT_CALL( | 384 EXPECT_CALL( |
383 mock_observer, | 385 mock_observer, |
384 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 386 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
385 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 387 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
386 mock_observer.image_url_)); | 388 mock_observer.image_url_)); |
387 ::testing::Mock::VerifyAndClearExpectations(&observer); | 389 ::testing::Mock::VerifyAndClearExpectations(&observer); |
388 } | 390 } |
OLD | NEW |