blob: 735501dec3d077306a40539a44da75c82a17a06f [file] [log] [blame]
[email protected]5e56df82011-04-18 17:00:151// 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
3// found in the LICENSE file.
4
5#include "chrome/common/content_settings.h"
6#include "chrome/common/render_messages.h"
7#include "chrome/renderer/content_settings_observer.h"
[email protected]c6d068ff2011-10-14 17:28:238#include "chrome/test/base/chrome_render_view_test.h"
[email protected]5e56df82011-04-18 17:00:159#include "content/common/view_messages.h"
[email protected]068970d2011-10-11 00:05:1610#include "content/public/renderer/render_view.h"
[email protected]4dad4a82011-04-27 15:44:5211#include "ipc/ipc_message_macros.h"
12#include "testing/gmock/include/gmock/gmock.h"
[email protected]5e56df82011-04-18 17:00:1513#include "testing/gtest/include/gtest/gtest.h"
[email protected]566fa0f2011-06-01 23:26:0614#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
15
16using testing::_;
17using testing::DeleteArg;
[email protected]5e56df82011-04-18 17:00:1518
[email protected]4dad4a82011-04-27 15:44:5219namespace {
20
21class MockContentSettingsObserver : public ContentSettingsObserver {
22 public:
[email protected]310ebd6302011-10-10 19:06:2823 explicit MockContentSettingsObserver(content::RenderView* render_view);
[email protected]4dad4a82011-04-27 15:44:5224
25 virtual bool Send(IPC::Message* message);
26
27 MOCK_METHOD2(OnContentBlocked,
28 void(ContentSettingsType, const std::string&));
[email protected]566fa0f2011-06-01 23:26:0629
30 MOCK_METHOD5(OnAllowDOMStorage,
31 void(int, const GURL&, const GURL&, bool, IPC::Message*));
[email protected]4dad4a82011-04-27 15:44:5232};
33
34MockContentSettingsObserver::MockContentSettingsObserver(
[email protected]310ebd6302011-10-10 19:06:2835 content::RenderView* render_view)
[email protected]4dad4a82011-04-27 15:44:5236 : ContentSettingsObserver(render_view) {
37}
38
39bool MockContentSettingsObserver::Send(IPC::Message* message) {
40 IPC_BEGIN_MESSAGE_MAP(MockContentSettingsObserver, *message)
[email protected]2ccf45c2011-08-19 23:35:5041 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_ContentBlocked, OnContentBlocked)
42 IPC_MESSAGE_HANDLER_DELAY_REPLY(ChromeViewHostMsg_AllowDOMStorage,
[email protected]566fa0f2011-06-01 23:26:0643 OnAllowDOMStorage)
[email protected]4dad4a82011-04-27 15:44:5244 IPC_MESSAGE_UNHANDLED(ADD_FAILURE())
45 IPC_END_MESSAGE_MAP()
46
47 // Our super class deletes the message.
48 return RenderViewObserver::Send(message);
49}
50
51} // namespace
52
[email protected]c6d068ff2011-10-14 17:28:2353TEST_F(ChromeRenderViewTest, DidBlockContentType) {
[email protected]4dad4a82011-04-27 15:44:5254 MockContentSettingsObserver observer(view_);
55 EXPECT_CALL(observer,
56 OnContentBlocked(CONTENT_SETTINGS_TYPE_COOKIES, std::string()));
57 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
58
59 // Blocking the same content type a second time shouldn't send a notification.
60 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_COOKIES, std::string());
61 ::testing::Mock::VerifyAndClearExpectations(&observer);
62
63 // Blocking two different plugins should send two notifications.
64 std::string kFooPlugin = "foo";
65 std::string kBarPlugin = "bar";
66 EXPECT_CALL(observer,
67 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin));
68 EXPECT_CALL(observer,
69 OnContentBlocked(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin));
70 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kFooPlugin);
71 observer.DidBlockContentType(CONTENT_SETTINGS_TYPE_PLUGINS, kBarPlugin);
72}
73
[email protected]566fa0f2011-06-01 23:26:0674// Tests that multiple invokations of AllowDOMStorage result in a single IPC.
[email protected]c6d068ff2011-10-14 17:28:2375TEST_F(ChromeRenderViewTest, AllowDOMStorage) {
[email protected]566fa0f2011-06-01 23:26:0676 // Load some HTML, so we have a valid security origin.
77 LoadHTML("<html></html>");
78 MockContentSettingsObserver observer(view_);
79 ON_CALL(observer,
80 OnAllowDOMStorage(_, _, _, _, _)).WillByDefault(DeleteArg<4>());
81 EXPECT_CALL(observer,
82 OnAllowDOMStorage(_, _, _, _, _));
[email protected]068970d2011-10-11 00:05:1683 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true);
[email protected]566fa0f2011-06-01 23:26:0684
85 // Accessing localStorage from the same origin again shouldn't result in a
86 // new IPC.
[email protected]068970d2011-10-11 00:05:1687 observer.AllowStorage(view_->GetWebView()->focusedFrame(), true);
[email protected]566fa0f2011-06-01 23:26:0688 ::testing::Mock::VerifyAndClearExpectations(&observer);
89}
90
[email protected]5e56df82011-04-18 17:00:1591// Regression test for http://crbug.com/35011
[email protected]c6d068ff2011-10-14 17:28:2392TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) {
[email protected]5e56df82011-04-18 17:00:1593 // 1. Load page with JS.
94 std::string html = "<html>"
[email protected]4dad4a82011-04-27 15:44:5295 "<head>"
96 "<script>document.createElement('div');</script>"
97 "</head>"
98 "<body>"
99 "</body>"
100 "</html>";
[email protected]c6d068ff2011-10-14 17:28:23101 render_thread_->sink().ClearMessages();
[email protected]5e56df82011-04-18 17:00:15102 LoadHTML(html.c_str());
103
104 // 2. Block JavaScript.
105 ContentSettings settings;
106 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
107 settings.settings[i] = CONTENT_SETTING_ALLOW;
108 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK;
109 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
110 observer->SetContentSettings(settings);
[email protected]6326d8b2011-06-17 10:39:09111 ContentSettingsObserver::SetDefaultContentSettings(settings);
[email protected]5e56df82011-04-18 17:00:15112
113 // Make sure no pending messages are in the queue.
114 ProcessPendingMessages();
[email protected]c6d068ff2011-10-14 17:28:23115 render_thread_->sink().ClearMessages();
[email protected]5e56df82011-04-18 17:00:15116
117 // 3. Reload page.
118 ViewMsg_Navigate_Params params;
119 std::string url_str = "data:text/html;charset=utf-8,";
120 url_str.append(html);
121 GURL url(url_str);
122 params.url = url;
123 params.navigation_type = ViewMsg_Navigate_Type::RELOAD;
[email protected]068970d2011-10-11 00:05:16124 OnNavigate(params);
[email protected]5e56df82011-04-18 17:00:15125 ProcessPendingMessages();
126
127 // 4. Verify that the notification that javascript was blocked is sent after
128 // the navigation notifiction is sent.
129 int navigation_index = -1;
130 int block_index = -1;
[email protected]c6d068ff2011-10-14 17:28:23131 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
132 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
[email protected]5e56df82011-04-18 17:00:15133 if (msg->type() == ViewHostMsg_FrameNavigate::ID)
134 navigation_index = i;
[email protected]2ccf45c2011-08-19 23:35:50135 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
[email protected]5e56df82011-04-18 17:00:15136 block_index = i;
137 }
138 EXPECT_NE(-1, navigation_index);
139 EXPECT_NE(-1, block_index);
140 EXPECT_LT(navigation_index, block_index);
141}
[email protected]134efc32011-08-02 18:14:21142
[email protected]c6d068ff2011-10-14 17:28:23143TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) {
[email protected]134efc32011-08-02 18:14:21144 // Load some HTML.
145 LoadHTML("<html>Foo</html>");
146
147 // Block plugins.
148 ContentSettings settings;
149 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i)
150 settings.settings[i] = CONTENT_SETTING_ALLOW;
151 settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK;
152 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_);
153 observer->SetContentSettings(settings);
154 ContentSettingsObserver::SetDefaultContentSettings(settings);
155 EXPECT_EQ(CONTENT_SETTING_BLOCK,
156 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS));
157
158 // Temporarily allow plugins.
[email protected]068970d2011-10-11 00:05:16159 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE));
[email protected]134efc32011-08-02 18:14:21160 EXPECT_EQ(CONTENT_SETTING_ALLOW,
161 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS));
162
163 // Simulate a navigation within the page.
[email protected]068970d2011-10-11 00:05:16164 DidNavigateWithinPage(GetMainFrame(), true);
[email protected]134efc32011-08-02 18:14:21165 EXPECT_EQ(CONTENT_SETTING_ALLOW,
166 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS));
167
168 // Navigate to a different page.
169 LoadHTML("<html>Bar</html>");
170 EXPECT_EQ(CONTENT_SETTING_BLOCK,
171 observer->GetContentSetting(CONTENT_SETTINGS_TYPE_PLUGINS));
172}