| 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 "components/content_settings/core/common/content_settings.h" | 5 #include "components/content_settings/core/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" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/WebKit/public/web/WebView.h" | 13 #include "third_party/WebKit/public/web/WebView.h" |
| 14 | 14 |
| 15 using testing::_; | 15 using testing::_; |
| 16 using testing::DeleteArg; | 16 using testing::DeleteArg; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockContentSettingsObserver : public ContentSettingsObserver { | 20 class MockContentSettingsObserver : public ContentSettingsObserver { |
| 21 public: | 21 public: |
| 22 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); | 22 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); |
| 23 | 23 |
| 24 virtual bool Send(IPC::Message* message); | 24 virtual bool Send(IPC::Message* message); |
| 25 | 25 |
| 26 MOCK_METHOD1(OnContentBlocked, | 26 MOCK_METHOD2(OnContentBlocked, |
| 27 void(ContentSettingsType)); | 27 void(ContentSettingsType, const base::string16&)); |
| 28 | 28 |
| 29 MOCK_METHOD5(OnAllowDOMStorage, | 29 MOCK_METHOD5(OnAllowDOMStorage, |
| 30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); | 30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
| 31 GURL image_url_; | 31 GURL image_url_; |
| 32 std::string image_origin_; | 32 std::string image_origin_; |
| 33 }; | 33 }; |
| 34 | 34 |
| 35 MockContentSettingsObserver::MockContentSettingsObserver( | 35 MockContentSettingsObserver::MockContentSettingsObserver( |
| 36 content::RenderFrame* render_frame) | 36 content::RenderFrame* render_frame) |
| 37 : ContentSettingsObserver(render_frame, NULL), | 37 : ContentSettingsObserver(render_frame, NULL), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 48 IPC_END_MESSAGE_MAP() | 48 IPC_END_MESSAGE_MAP() |
| 49 | 49 |
| 50 // Our super class deletes the message. | 50 // Our super class deletes the message. |
| 51 return RenderFrameObserver::Send(message); | 51 return RenderFrameObserver::Send(message); |
| 52 } | 52 } |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 TEST_F(ChromeRenderViewTest, DidBlockContentType) { | 56 TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
| 57 MockContentSettingsObserver observer(view_->GetMainRenderFrame()); | 57 MockContentSettingsObserver observer(view_->GetMainRenderFrame()); |
| 58 EXPECT_CALL(observer, | 58 EXPECT_CALL(observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, |
| 59 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES)); | 59 base::string16())); |
| 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); | 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 61 | 61 |
| 62 // Blocking the same content type a second time shouldn't send a notification. | 62 // Blocking the same content type a second time shouldn't send a notification. |
| 63 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); | 63 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES); |
| 64 ::testing::Mock::VerifyAndClearExpectations(&observer); | 64 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. | 67 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
| 68 // Fails due to http://crbug.com/104300 | 68 // Fails due to http://crbug.com/104300 |
| 69 TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) { | 69 TEST_F(ChromeRenderViewTest, DISABLED_AllowDOMStorage) { |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 182 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 183 ContentSettingsPattern::Wildcard(), | 183 ContentSettingsPattern::Wildcard(), |
| 184 CONTENT_SETTING_BLOCK, | 184 CONTENT_SETTING_BLOCK, |
| 185 std::string(), | 185 std::string(), |
| 186 false)); | 186 false)); |
| 187 | 187 |
| 188 ContentSettingsObserver* observer = ContentSettingsObserver::Get( | 188 ContentSettingsObserver* observer = ContentSettingsObserver::Get( |
| 189 view_->GetMainRenderFrame()); | 189 view_->GetMainRenderFrame()); |
| 190 observer->SetContentSettingRules(&content_setting_rules); | 190 observer->SetContentSettingRules(&content_setting_rules); |
| 191 EXPECT_CALL(mock_observer, | 191 EXPECT_CALL(mock_observer, |
| 192 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); | 192 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, base::string16())); |
| 193 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); | 193 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); |
| 194 ::testing::Mock::VerifyAndClearExpectations(&observer); | 194 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 195 | 195 |
| 196 // Create an exception which allows the image. | 196 // Create an exception which allows the image. |
| 197 image_setting_rules.insert( | 197 image_setting_rules.insert( |
| 198 image_setting_rules.begin(), | 198 image_setting_rules.begin(), |
| 199 ContentSettingPatternSource( | 199 ContentSettingPatternSource( |
| 200 ContentSettingsPattern::Wildcard(), | 200 ContentSettingsPattern::Wildcard(), |
| 201 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 201 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 202 CONTENT_SETTING_ALLOW, | 202 CONTENT_SETTING_ALLOW, |
| 203 std::string(), | 203 std::string(), |
| 204 false)); | 204 false)); |
| 205 | 205 |
| 206 EXPECT_CALL( | 206 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 207 mock_observer, | 207 base::string16())).Times(0); |
| 208 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); | |
| 209 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 208 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 210 ::testing::Mock::VerifyAndClearExpectations(&observer); | 209 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 211 } | 210 } |
| 212 | 211 |
| 213 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 212 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 214 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); | 213 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); |
| 215 | 214 |
| 216 // Load some HTML. | 215 // Load some HTML. |
| 217 LoadHTML("<html>Foo</html>"); | 216 LoadHTML("<html>Foo</html>"); |
| 218 | 217 |
| 219 // Set the default image blocking setting. | 218 // Set the default image blocking setting. |
| 220 RendererContentSettingRules content_setting_rules; | 219 RendererContentSettingRules content_setting_rules; |
| 221 ContentSettingsForOneType& image_setting_rules = | 220 ContentSettingsForOneType& image_setting_rules = |
| 222 content_setting_rules.image_rules; | 221 content_setting_rules.image_rules; |
| 223 image_setting_rules.push_back( | 222 image_setting_rules.push_back( |
| 224 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 223 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 225 ContentSettingsPattern::Wildcard(), | 224 ContentSettingsPattern::Wildcard(), |
| 226 CONTENT_SETTING_ALLOW, | 225 CONTENT_SETTING_ALLOW, |
| 227 std::string(), | 226 std::string(), |
| 228 false)); | 227 false)); |
| 229 | 228 |
| 230 ContentSettingsObserver* observer = | 229 ContentSettingsObserver* observer = |
| 231 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); | 230 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); |
| 232 observer->SetContentSettingRules(&content_setting_rules); | 231 observer->SetContentSettingRules(&content_setting_rules); |
| 233 EXPECT_CALL( | 232 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 234 mock_observer, | 233 base::string16())).Times(0); |
| 235 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); | |
| 236 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 234 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 237 ::testing::Mock::VerifyAndClearExpectations(&observer); | 235 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 238 | 236 |
| 239 // Create an exception which blocks the image. | 237 // Create an exception which blocks the image. |
| 240 image_setting_rules.insert( | 238 image_setting_rules.insert( |
| 241 image_setting_rules.begin(), | 239 image_setting_rules.begin(), |
| 242 ContentSettingPatternSource( | 240 ContentSettingPatternSource( |
| 243 ContentSettingsPattern::Wildcard(), | 241 ContentSettingsPattern::Wildcard(), |
| 244 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 242 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 245 CONTENT_SETTING_BLOCK, | 243 CONTENT_SETTING_BLOCK, |
| 246 std::string(), | 244 std::string(), |
| 247 false)); | 245 false)); |
| 248 EXPECT_CALL(mock_observer, | 246 EXPECT_CALL(mock_observer, |
| 249 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)); | 247 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, base::string16())); |
| 250 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); | 248 EXPECT_FALSE(observer->allowImage(true, mock_observer.image_url_)); |
| 251 ::testing::Mock::VerifyAndClearExpectations(&observer); | 249 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 252 } | 250 } |
| 253 | 251 |
| 254 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { | 252 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 255 // Set the content settings for scripts. | 253 // Set the content settings for scripts. |
| 256 RendererContentSettingRules content_setting_rules; | 254 RendererContentSettingRules content_setting_rules; |
| 257 ContentSettingsForOneType& script_setting_rules = | 255 ContentSettingsForOneType& script_setting_rules = |
| 258 content_setting_rules.script_rules; | 256 content_setting_rules.script_rules; |
| 259 script_setting_rules.push_back( | 257 script_setting_rules.push_back( |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 // Verify that the script was allowed. | 361 // Verify that the script was allowed. |
| 364 bool was_blocked = false; | 362 bool was_blocked = false; |
| 365 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { | 363 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 366 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); | 364 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 367 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 365 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 368 was_blocked = true; | 366 was_blocked = true; |
| 369 } | 367 } |
| 370 EXPECT_FALSE(was_blocked); | 368 EXPECT_FALSE(was_blocked); |
| 371 | 369 |
| 372 // Verify that images are allowed. | 370 // Verify that images are allowed. |
| 373 EXPECT_CALL( | 371 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, |
| 374 mock_observer, | 372 base::string16())).Times(0); |
| 375 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES)).Times(0); | |
| 376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); | 373 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); |
| 377 ::testing::Mock::VerifyAndClearExpectations(&observer); | 374 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 378 } | 375 } |
| OLD | NEW |