| 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/render_view_test.h" |
| 9 #include "content/common/view_messages.h" | 9 #include "content/common/view_messages.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/Source/WebKit/chromium/public/WebView.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/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(RenderView* render_view); | 22 explicit MockContentSettingsObserver(RenderView* render_view); |
| 23 | 23 |
| 24 virtual bool Send(IPC::Message* message); | 24 virtual bool Send(IPC::Message* message); |
| 25 | 25 |
| 26 MOCK_METHOD2(OnContentBlocked, | |
| 27 void(ContentSettingsType, const std::string&)); | |
| 28 | |
| 29 MOCK_METHOD5(OnAllowDOMStorage, | 26 MOCK_METHOD5(OnAllowDOMStorage, |
| 30 void(int, const GURL&, const GURL&, bool, IPC::Message*)); | 27 void(int, const GURL&, const GURL&, bool, IPC::Message*)); |
| 31 }; | 28 }; |
| 32 | 29 |
| 33 MockContentSettingsObserver::MockContentSettingsObserver( | 30 MockContentSettingsObserver::MockContentSettingsObserver( |
| 34 RenderView* render_view) | 31 RenderView* render_view) |
| 35 : ContentSettingsObserver(render_view) { | 32 : ContentSettingsObserver(render_view) { |
| 36 } | 33 } |
| 37 | 34 |
| 38 bool MockContentSettingsObserver::Send(IPC::Message* message) { | 35 bool MockContentSettingsObserver::Send(IPC::Message* message) { |
| 39 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) | 36 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message) |
| 40 IPC_MESSAGE_HANDLER(ViewHostMsg_ContentBlocked, OnContentBlocked) | |
| 41 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_AllowDOMStorage, | 37 IPC_MESSAGE_HANDLER_DELAY_REPLY(ViewHostMsg_AllowDOMStorage, |
| 42 OnAllowDOMStorage) | 38 OnAllowDOMStorage) |
| 43 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) | 39 IPC_MESSAGE_UNHANDLED(ADD_FAILURE()) |
| 44 IPC_END_MESSAGE_MAP() | 40 IPC_END_MESSAGE_MAP() |
| 45 | 41 |
| 46 // Our super class deletes the message. | 42 // Our super class deletes the message. |
| 47 return RenderViewObserver::Send(message); | 43 return RenderViewObserver::Send(message); |
| 48 } | 44 } |
| 49 | 45 |
| 50 } // namespace | 46 } // namespace |
| 51 | 47 |
| 52 TEST_F(RenderViewTest, DidBlockContentType) { | |
| 53 MockContentSettingsObserver observer(view_); | |
| 54 EXPECT_CALL(observer, | |
| 55 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string())); | |
| 56 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | |
| 57 | |
| 58 // Blocking the same content type a second time shouldn't send a notification. | |
| 59 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string()); | |
| 60 ::testing::Mock::VerifyAndClearExpectations(&observer); | |
| 61 | |
| 62 // Blocking two different plugins should send two notifications. | |
| 63 std::string kFooPlugin = "foo"; | |
| 64 std::string kBarPlugin = "bar"; | |
| 65 EXPECT_CALL(observer, | |
| 66 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin)); | |
| 67 EXPECT_CALL(observer, | |
| 68 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin)); | |
| 69 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin); | |
| 70 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin); | |
| 71 } | |
| 72 | |
| 73 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. | 48 // Tests that multiple invokations of AllowDOMStorage result in a single IPC. |
| 74 TEST_F(RenderViewTest, AllowDOMStorage) { | 49 TEST_F(RenderViewTest, AllowDOMStorage) { |
| 75 // Load some HTML, so we have a valid security origin. | 50 // Load some HTML, so we have a valid security origin. |
| 76 LoadHTML("<html></html>"); | 51 LoadHTML("<html></html>"); |
| 77 MockContentSettingsObserver observer(view_); | 52 MockContentSettingsObserver observer(view_); |
| 78 ON_CALL(observer, | 53 ON_CALL(observer, |
| 79 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); | 54 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>()); |
| 80 EXPECT_CALL(observer, | 55 EXPECT_CALL(observer, |
| 81 OnAllowDOMStorage(_, _, _, _, _)); | 56 OnAllowDOMStorage(_, _, _, _, _)); |
| 82 observer.AllowStorage(view_->webview()->focusedFrame(), true); | 57 observer.AllowStorage(view_->webview()->focusedFrame(), true); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 // Simulate a navigation within the page. | 137 // Simulate a navigation within the page. |
| 163 view_->didNavigateWithinPage(GetMainFrame(), true); | 138 view_->didNavigateWithinPage(GetMainFrame(), true); |
| 164 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 139 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 165 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 140 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 166 | 141 |
| 167 // Navigate to a different page. | 142 // Navigate to a different page. |
| 168 LoadHTML("<html>Bar</html>"); | 143 LoadHTML("<html>Bar</html>"); |
| 169 EXPECT_EQ(CONTENT_SETTING_BLOCK, | 144 EXPECT_EQ(CONTENT_SETTING_BLOCK, |
| 170 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); | 145 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS)); |
| 171 } | 146 } |
| OLD | NEW |