OLD | NEW |
(Empty) | |
| 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 |
| 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" |
| 8 #include "chrome/test/render_view_test.h" |
| 9 #include "content/common/view_messages.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 // Regression test for http://crbug.com/35011 |
| 13 TEST_F(RenderViewTest, JSBlockSentAfterPageLoad) { |
| 14 // 1. Load page with JS. |
| 15 std::string html = "<html>" |
| 16 "<head>" |
| 17 "<script>document.createElement('div');</script>" |
| 18 "</head>" |
| 19 "<body>" |
| 20 "</body>" |
| 21 "</html>"; |
| 22 render_thread_.sink().ClearMessages(); |
| 23 LoadHTML(html.c_str()); |
| 24 |
| 25 // 2. Block JavaScript. |
| 26 ContentSettings settings; |
| 27 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) |
| 28 settings.settings[i] = CONTENT_SETTING_ALLOW; |
| 29 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; |
| 30 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 31 observer->SetContentSettings(settings); |
| 32 |
| 33 // Make sure no pending messages are in the queue. |
| 34 ProcessPendingMessages(); |
| 35 render_thread_.sink().ClearMessages(); |
| 36 |
| 37 // 3. Reload page. |
| 38 ViewMsg_Navigate_Params params; |
| 39 std::string url_str = "data:text/html;charset=utf-8,"; |
| 40 url_str.append(html); |
| 41 GURL url(url_str); |
| 42 params.url = url; |
| 43 params.navigation_type = ViewMsg_Navigate_Type::RELOAD; |
| 44 view_->OnNavigate(params); |
| 45 ProcessPendingMessages(); |
| 46 |
| 47 // 4. Verify that the notification that javascript was blocked is sent after |
| 48 // the navigation notifiction is sent. |
| 49 int navigation_index = -1; |
| 50 int block_index = -1; |
| 51 for (size_t i = 0; i < render_thread_.sink().message_count(); ++i) { |
| 52 const IPC::Message* msg = render_thread_.sink().GetMessageAt(i); |
| 53 if (msg->type() == ViewHostMsg_FrameNavigate::ID) |
| 54 navigation_index = i; |
| 55 if (msg->type() == ViewHostMsg_ContentBlocked::ID) |
| 56 block_index = i; |
| 57 } |
| 58 EXPECT_NE(-1, navigation_index); |
| 59 EXPECT_NE(-1, block_index); |
| 60 EXPECT_LT(navigation_index, block_index); |
| 61 } |
OLD | NEW |