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 <vector> |
| 6 |
5 #include "chrome/common/content_settings.h" | 7 #include "chrome/common/content_settings.h" |
6 #include "chrome/common/render_messages.h" | 8 #include "chrome/common/render_messages.h" |
7 #include "chrome/renderer/content_settings_observer.h" | 9 #include "chrome/renderer/content_settings_observer.h" |
8 #include "chrome/test/base/chrome_render_view_test.h" | 10 #include "chrome/test/base/chrome_render_view_test.h" |
9 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
10 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
14 | 16 |
15 using testing::_; | 17 using testing::_; |
16 using testing::DeleteArg; | 18 using testing::DeleteArg; |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 class MockContentSettingsObserver : public ContentSettingsObserver { | 22 class MockContentSettingsObserver : public ContentSettingsObserver { |
21 public: | 23 public: |
22 explicit MockContentSettingsObserver(content::RenderView* render_view); | 24 explicit MockContentSettingsObserver( |
| 25 content::RenderView* render_view, |
| 26 const ContentSettingsForOneType* image_setting_rules_); |
23 | 27 |
24 virtual bool Send(IPC::Message* message); | 28 virtual bool Send(IPC::Message* message); |
25 | 29 |
26 MOCK_METHOD2(OnContentBlocked, | 30 MOCK_METHOD2(OnContentBlocked, |
27 void(ContentSettingsType, const std::string&)); | 31 void(ContentSettingsType, const std::string&)); |
28 | 32 |
29 MOCK_METHOD5(OnAllowDOMStorage, | 33 MOCK_METHOD5(OnAllowDOMStorage, |
30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); | 34 void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
| 35 GURL image_url_; |
| 36 std::string image_origin_; |
31 }; | 37 }; |
32 | 38 |
33 MockContentSettingsObserver::MockContentSettingsObserver( | 39 MockContentSettingsObserver::MockContentSettingsObserver( |
34 content::RenderView* render_view) | 40 content::RenderView* render_view, |
35 : ContentSettingsObserver(render_view) { | 41 const ContentSettingsForOneType* image_setting_rules) |
| 42 : ContentSettingsObserver(render_view, image_setting_rules), |
| 43 image_url_("http://www.foo.com/image.jpg"), |
| 44 image_origin_("http://www.foo.com") { |
36 } | 45 } |
37 | 46 |
38 bool MockContentSettingsObserver::Send(IPC::Message* message) { | 47 bool MockContentSettingsObserver::Send(IPC::Message* message) { |
39 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) | 48 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
40 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) | 49 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked) |
41 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, | 50 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage, |
42 OnAllowDOMStorage) | 51 OnAllowDOMStorage) |
43 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) | 52 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
44 IPC_END_MESSAGE_MAP() | 53 IPC_END_MESSAGE_MAP() |
45 | 54 |
46 // Our super class deletes the message. | 55 // Our super class deletes the message. |
47 return RenderViewObserver::Send(message); | 56 return RenderViewObserver::Send(message); |
48 } | 57 } |
49 | 58 |
50 } // namespace | 59 } // namespace |
51 | 60 |
52 TEST_F(ChromeRenderViewTest, DidBlockContentType) { | 61 TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
53 MockContentSettingsObserver observer(view_); | 62 MockContentSettingsObserver observer(view_, NULL); |
54 EXPECT_CALL(observer, | 63 EXPECT_CALL(observer, |
55 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 64 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
56 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 65 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
57 | 66 |
58 // Blocking the same content type a second time shouldn't send a notification. | 67 // Blocking the same content type a second time shouldn't send a notification. |
59 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 68 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
60 ::testing::Mock::VerifyAndClearExpectations(&observer); | 69 ::testing::Mock::VerifyAndClearExpectations(&observer); |
61 | 70 |
62 // Blocking two different plugins should send two notifications. | 71 // Blocking two different plugins should send two notifications. |
63 std::string kFooPlugin = "foo"; | 72 std::string kFooPlugin = "foo"; |
64 std::string kBarPlugin = "bar"; | 73 std::string kBarPlugin = "bar"; |
65 EXPECT_CALL(observer, | 74 EXPECT_CALL(observer, |
66 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); | 75 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); |
67 EXPECT_CALL(observer, | 76 EXPECT_CALL(observer, |
68 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); | 77 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); |
69 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); | 78 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); |
70 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); | 79 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); |
71 } | 80 } |
72 | 81 |
73 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. | 82 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
74 TEST_F(ChromeRenderViewTest, AllowDOMStorage) { | 83 TEST_F(ChromeRenderViewTest, AllowDOMStorage) { |
75 // Load some HTML, so we have a valid security origin. | 84 // Load some HTML, so we have a valid security origin. |
76 LoadHTML("<html></html>"); | 85 LoadHTML("<html></html>"); |
77 MockContentSettingsObserver observer(view_); | 86 MockContentSettingsObserver observer(view_, NULL); |
78 ON_CALL(observer, | 87 ON_CALL(observer, |
79 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); | 88 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
80 EXPECT_CALL(observer, | 89 EXPECT_CALL(observer, |
81 OnAllowDOMStorage(_, _, _, _, _)); | 90 OnAllowDOMStorage(_, _, _, _, _)); |
82 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); | 91 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
83 | 92 |
84 // Accessing localStorage from the same origin again shouldn't result in a | 93 // Accessing localStorage from the same origin again shouldn't result in a |
85 // new IPC. | 94 // new IPC. |
86 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); | 95 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
87 ::testing::Mock::VerifyAndClearExpectations(&observer); | 96 ::testing::Mock::VerifyAndClearExpectations(&observer); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 // Simulate a navigation within the page. | 168 // Simulate a navigation within the page. |
160 DidNavigateWithinPage(GetMainFrame(), true); | 169 DidNavigateWithinPage(GetMainFrame(), true); |
161 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 170 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
162 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 171 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
163 | 172 |
164 // Navigate to a different page. | 173 // Navigate to a different page. |
165 LoadHTML("<html>Bar</html>"); | 174 LoadHTML("<html>Bar</html>"); |
166 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 175 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
167 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 176 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
168 } | 177 } |
| 178 |
| 179 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
| 180 MockContentSettingsObserver mock_observer(view_, &image_setting_rules_); |
| 181 |
| 182 // Load some HTML. |
| 183 LoadHTML("<html>Foo</html>"); |
| 184 |
| 185 // Set the default image blocking setting. |
| 186 image_setting_rules_.push_back( |
| 187 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 188 ContentSettingsPattern::Wildcard(), |
| 189 CONTENT_SETTING_BLOCK, |
| 190 "", |
| 191 false)); |
| 192 |
| 193 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 194 EXPECT_CALL(mock_observer, |
| 195 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 196 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 197 true, mock_observer.image_url_)); |
| 198 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 199 |
| 200 // Create an exception which allows the image. |
| 201 image_setting_rules_.insert( |
| 202 image_setting_rules_.begin(), |
| 203 ContentSettingPatternSource( |
| 204 ContentSettingsPattern::Wildcard(), |
| 205 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 206 CONTENT_SETTING_ALLOW, |
| 207 "", |
| 208 false)); |
| 209 |
| 210 EXPECT_CALL( |
| 211 mock_observer, |
| 212 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 213 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 214 mock_observer.image_url_)); |
| 215 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 216 } |
| 217 |
| 218 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 219 MockContentSettingsObserver mock_observer(view_, &image_setting_rules_); |
| 220 |
| 221 // Load some HTML. |
| 222 LoadHTML("<html>Foo</html>"); |
| 223 |
| 224 // Set the default image blocking setting. |
| 225 image_setting_rules_.push_back( |
| 226 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 227 ContentSettingsPattern::Wildcard(), |
| 228 CONTENT_SETTING_ALLOW, |
| 229 "", |
| 230 false)); |
| 231 |
| 232 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 233 EXPECT_CALL( |
| 234 mock_observer, |
| 235 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 236 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 237 mock_observer.image_url_)); |
| 238 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 239 |
| 240 // Create an exception which blocks the image. |
| 241 image_setting_rules_.insert( |
| 242 image_setting_rules_.begin(), |
| 243 ContentSettingPatternSource( |
| 244 ContentSettingsPattern::Wildcard(), |
| 245 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 246 CONTENT_SETTING_BLOCK, |
| 247 "", |
| 248 false)); |
| 249 EXPECT_CALL(mock_observer, |
| 250 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 251 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 252 true, mock_observer.image_url_)); |
| 253 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 254 } |
OLD | NEW |