| 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/render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
| 9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.h" |
| 10 #include "content/public/renderer/render_view.h" | 10 #include "content/public/renderer/render_view.h" |
| 11 #include "ipc/ipc_message_macros.h" | 11 #include "ipc/ipc_message_macros.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 15 | 15 |
| 16 using testing::_; | 16 using testing::_; |
| 17 using testing::DeleteArg; | 17 using testing::DeleteArg; |
| 18 | 18 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 OnAllowDOMStorage) | 43 OnAllowDOMStorage) |
| 44 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) | 44 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
| 45 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
| 46 | 46 |
| 47 // Our super class deletes the message. | 47 // Our super class deletes the message. |
| 48 return RenderViewObserver::Send(message); | 48 return RenderViewObserver::Send(message); |
| 49 } | 49 } |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 TEST_F(RenderViewTest, DidBlockContentType) { | 53 TEST_F(ChromeRenderViewTest, DidBlockContentType) { |
| 54 MockContentSettingsObserver observer(view_); | 54 MockContentSettingsObserver observer(view_); |
| 55 EXPECT_CALL(observer, | 55 EXPECT_CALL(observer, |
| 56 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | 56 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); |
| 57 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 57 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
| 58 | 58 |
| 59 // Blocking the same content type a second time shouldn't send a notification. | 59 // Blocking the same content type a second time shouldn't send a notification. |
| 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | 60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); |
| 61 ::testing::Mock::VerifyAndClearExpectations(&observer); | 61 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 62 | 62 |
| 63 // Blocking two different plugins should send two notifications. | 63 // Blocking two different plugins should send two notifications. |
| 64 std::string kFooPlugin = "foo"; | 64 std::string kFooPlugin = "foo"; |
| 65 std::string kBarPlugin = "bar"; | 65 std::string kBarPlugin = "bar"; |
| 66 EXPECT_CALL(observer, | 66 EXPECT_CALL(observer, |
| 67 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); | 67 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); |
| 68 EXPECT_CALL(observer, | 68 EXPECT_CALL(observer, |
| 69 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); | 69 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); |
| 70 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); | 70 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); |
| 71 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); | 71 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. | 74 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
| 75 TEST_F(RenderViewTest, AllowDOMStorage) { | 75 TEST_F(ChromeRenderViewTest, AllowDOMStorage) { |
| 76 // Load some HTML, so we have a valid security origin. | 76 // Load some HTML, so we have a valid security origin. |
| 77 LoadHTML("<html></html>"); | 77 LoadHTML("<html></html>"); |
| 78 MockContentSettingsObserver observer(view_); | 78 MockContentSettingsObserver observer(view_); |
| 79 ON_CALL(observer, | 79 ON_CALL(observer, |
| 80 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); | 80 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
| 81 EXPECT_CALL(observer, | 81 EXPECT_CALL(observer, |
| 82 OnAllowDOMStorage(_, _, _, _, _)); | 82 OnAllowDOMStorage(_, _, _, _, _)); |
| 83 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); | 83 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
| 84 | 84 |
| 85 // Accessing localStorage from the same origin again shouldn't result in a | 85 // Accessing localStorage from the same origin again shouldn't result in a |
| 86 // new IPC. | 86 // new IPC. |
| 87 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); | 87 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true); |
| 88 ::testing::Mock::VerifyAndClearExpectations(&observer); | 88 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Regression test for http://crbug.com/35011 | 91 // Regression test for http://crbug.com/35011 |
| 92 TEST_F(RenderViewTest, JSBlockSentAfterPageLoad) { | 92 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { |
| 93 // 1. Load page with JS. | 93 // 1. Load page with JS. |
| 94 std::string html = "<html>" | 94 std::string html = "<html>" |
| 95 "<head>" | 95 "<head>" |
| 96 "<script>document.createElement('div');</script>" | 96 "<script>document.createElement('div');</script>" |
| 97 "</head>" | 97 "</head>" |
| 98 "<body>" | 98 "<body>" |
| 99 "</body>" | 99 "</body>" |
| 100 "</html>"; | 100 "</html>"; |
| 101 render_thread_.sink().ClearMessages(); | 101 render_thread_->sink().ClearMessages(); |
| 102 LoadHTML(html.c_str()); | 102 LoadHTML(html.c_str()); |
| 103 | 103 |
| 104 // 2. Block JavaScript. | 104 // 2. Block JavaScript. |
| 105 ContentSettings settings; | 105 ContentSettings settings; |
| 106 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) | 106 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 107 settings.settings[i] = CONTENT_SETTING_ALLOW; | 107 settings.settings[i] = CONTENT_SETTING_ALLOW; |
| 108 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; | 108 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; |
| 109 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 109 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 110 observer->SetContentSettings(settings); | 110 observer->SetContentSettings(settings); |
| 111 ContentSettingsObserver::SetDefaultContentSettings(settings); | 111 ContentSettingsObserver::SetDefaultContentSettings(settings); |
| 112 | 112 |
| 113 // Make sure no pending messages are in the queue. | 113 // Make sure no pending messages are in the queue. |
| 114 ProcessPendingMessages(); | 114 ProcessPendingMessages(); |
| 115 render_thread_.sink().ClearMessages(); | 115 render_thread_->sink().ClearMessages(); |
| 116 | 116 |
| 117 // 3. Reload page. | 117 // 3. Reload page. |
| 118 ViewMsg_Navigate_Params params; | 118 ViewMsg_Navigate_Params params; |
| 119 std::string url_str = "data:text/html;charset=utf-8,"; | 119 std::string url_str = "data:text/html;charset=utf-8,"; |
| 120 url_str.append(html); | 120 url_str.append(html); |
| 121 GURL url(url_str); | 121 GURL url(url_str); |
| 122 params.url = url; | 122 params.url = url; |
| 123 params.navigation_type = ViewMsg_Navigate_Type::RELOAD; | 123 params.navigation_type = ViewMsg_Navigate_Type::RELOAD; |
| 124 OnNavigate(params); | 124 OnNavigate(params); |
| 125 ProcessPendingMessages(); | 125 ProcessPendingMessages(); |
| 126 | 126 |
| 127 // 4. Verify that the notification that javascript was blocked is sent after | 127 // 4. Verify that the notification that javascript was blocked is sent after |
| 128 // the navigation notifiction is sent. | 128 // the navigation notifiction is sent. |
| 129 int navigation_index = -1; | 129 int navigation_index = -1; |
| 130 int block_index = -1; | 130 int block_index = -1; |
| 131 for (size_t i = 0; i < render_thread_.sink().message_count(); ++i) { | 131 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 132 const IPC::Message* msg = render_thread_.sink().GetMessageAt(i); | 132 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 133 if (msg->type() == ViewHostMsg_FrameNavigate::ID) | 133 if (msg->type() == ViewHostMsg_FrameNavigate::ID) |
| 134 navigation_index = i; | 134 navigation_index = i; |
| 135 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) | 135 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 136 block_index = i; | 136 block_index = i; |
| 137 } | 137 } |
| 138 EXPECT_NE(-1, navigation_index); | 138 EXPECT_NE(-1, navigation_index); |
| 139 EXPECT_NE(-1, block_index); | 139 EXPECT_NE(-1, block_index); |
| 140 EXPECT_LT(navigation_index, block_index); | 140 EXPECT_LT(navigation_index, block_index); |
| 141 } | 141 } |
| 142 | 142 |
| 143 TEST_F(RenderViewTest, PluginsTemporarilyAllowed) { | 143 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { |
| 144 // Load some HTML. | 144 // Load some HTML. |
| 145 LoadHTML("<html>Foo</html>"); | 145 LoadHTML("<html>Foo</html>"); |
| 146 | 146 |
| 147 // Block plugins. | 147 // Block plugins. |
| 148 ContentSettings settings; | 148 ContentSettings settings; |
| 149 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) | 149 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 150 settings.settings[i] = CONTENT_SETTING_ALLOW; | 150 settings.settings[i] = CONTENT_SETTING_ALLOW; |
| 151 settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK; | 151 settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK; |
| 152 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 152 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 153 observer->SetContentSettings(settings); | 153 observer->SetContentSettings(settings); |
| 154 ContentSettingsObserver::SetDefaultContentSettings(settings); | 154 ContentSettingsObserver::SetDefaultContentSettings(settings); |
| 155 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 155 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 156 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 156 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 157 | 157 |
| 158 // Temporarily allow plugins. | 158 // Temporarily allow plugins. |
| 159 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); | 159 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); |
| 160 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 160 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 161 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 161 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 162 | 162 |
| 163 // Simulate a navigation within the page. | 163 // Simulate a navigation within the page. |
| 164 DidNavigateWithinPage(GetMainFrame(), true); | 164 DidNavigateWithinPage(GetMainFrame(), true); |
| 165 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 165 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 166 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 166 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 167 | 167 |
| 168 // Navigate to a different page. | 168 // Navigate to a different page. |
| 169 LoadHTML("<html>Bar</html>"); | 169 LoadHTML("<html>Bar</html>"); |
| 170 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 170 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 171 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 171 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 172 } | 172 } |
| OLD | NEW |