OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2008 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 "base/scoped_ptr.h" |
| 6 #include "chrome/renderer/mock_render_process.h" |
| 7 #include "chrome/renderer/mock_render_thread.h" |
| 8 #include "chrome/renderer/render_view.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "webkit/glue/webframe.h" |
| 11 #include "webkit/glue/weburlrequest.h" |
| 12 #include "webkit/glue/webview.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const int32 kRouteId = 5; |
| 17 const int32 kOpenerId = 7; |
| 18 |
| 19 class RenderViewTest : public testing::Test { |
| 20 public: |
| 21 RenderViewTest() {} |
| 22 ~RenderViewTest() {} |
| 23 |
| 24 protected: |
| 25 // Spins the message loop to process all messages that are currently pending. |
| 26 void ProcessPendingMessages() { |
| 27 msg_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 28 msg_loop_.Run(); |
| 29 } |
| 30 |
| 31 // Returns a pointer to the main frame. |
| 32 WebFrame* GetMainFrame() { |
| 33 return view_->webview()->GetMainFrame(); |
| 34 } |
| 35 |
| 36 // Executes the given JavaScript in the context of the main frame. The input |
| 37 // is a NULL-terminated UTF-8 string. |
| 38 void ExecuteJavaScript(const char* js) { |
| 39 GetMainFrame()->ExecuteJavaScript(js, GURL()); |
| 40 } |
| 41 |
| 42 // Loads the given HTML into the main frame as a data: URL. |
| 43 void LoadHTML(const char* html) { |
| 44 std::string url_str = "data:text/html;charset=utf-8,"; |
| 45 url_str.append(html); |
| 46 GURL url(url_str); |
| 47 |
| 48 scoped_ptr<WebRequest> request(WebRequest::Create(url)); |
| 49 GetMainFrame()->LoadRequest(request.get()); |
| 50 |
| 51 // The load actually happens asynchronously, so we pump messages to process |
| 52 // the pending continuation. |
| 53 ProcessPendingMessages(); |
| 54 } |
| 55 |
| 56 // testing::Test |
| 57 virtual void SetUp() { |
| 58 MockProcess::GlobalInit(); |
| 59 |
| 60 render_thread_.set_routing_id(kRouteId); |
| 61 |
| 62 // This needs to pass the mock render thread to the view. |
| 63 view_ = RenderView::Create(&render_thread_, NULL, NULL, kOpenerId, |
| 64 WebPreferences(), |
| 65 new SharedRenderViewCounter(0), kRouteId); |
| 66 } |
| 67 virtual void TearDown() { |
| 68 render_thread_.SendCloseMessage(); |
| 69 |
| 70 // Run the loop so the release task from the renderwidget executes. |
| 71 ProcessPendingMessages(); |
| 72 |
| 73 view_ = NULL; |
| 74 |
| 75 // There is a delayed task that the child process posts to terminate the |
| 76 // message loop so we need to spin the message loop to delete the task. |
| 77 MockProcess::GlobalCleanup(); |
| 78 msg_loop_.Run(); |
| 79 } |
| 80 |
| 81 MessageLoop msg_loop_; |
| 82 MockRenderThread render_thread_; |
| 83 |
| 84 scoped_refptr<RenderView> view_; |
| 85 }; |
| 86 |
| 87 } // namespace |
| 88 |
| 89 TEST_F(RenderViewTest, OnLoadAlternateHTMLText) { |
| 90 // Test a new navigation. |
| 91 GURL test_url("http://www.google.com/some_test_url"); |
| 92 view_->OnLoadAlternateHTMLText("<html></html>", true, test_url, |
| 93 std::string()); |
| 94 |
| 95 // We should have gotten two different types of start messages in the |
| 96 // following order. |
| 97 ASSERT_EQ(2, render_thread_.message_count()); |
| 98 const IPC::Message* msg = render_thread_.GetMessageAt(0); |
| 99 EXPECT_EQ(ViewHostMsg_DidStartLoading::ID, msg->type()); |
| 100 |
| 101 msg = render_thread_.GetMessageAt(1); |
| 102 EXPECT_EQ(ViewHostMsg_DidStartProvisionalLoadForFrame::ID, msg->type()); |
| 103 ViewHostMsg_DidStartProvisionalLoadForFrame::Param start_params; |
| 104 ViewHostMsg_DidStartProvisionalLoadForFrame::Read(msg, &start_params); |
| 105 EXPECT_EQ(GURL("chrome://chromewebdata/"), start_params.b); |
| 106 } |
| 107 |
| 108 // Test that we get form state change notifications when input fields change. |
| 109 TEST_F(RenderViewTest, OnNavStateChanged) { |
| 110 // Don't want any delay for form state sync changes. This will still post a |
| 111 // message so updates will get coalesced, but as soon as we spin the message |
| 112 // loop, it will generate an update. |
| 113 view_->set_delay_seconds_for_form_state_sync(0); |
| 114 |
| 115 LoadHTML("<input type=\"text\" id=\"elt_text\"></input>"); |
| 116 |
| 117 // We should NOT have gotten a form state change notification yet. |
| 118 EXPECT_FALSE(render_thread_.GetFirstMessageMatching( |
| 119 ViewHostMsg_UpdateState::ID)); |
| 120 render_thread_.ClearMessages(); |
| 121 |
| 122 // Change the value of the input. We should have gotten an update state |
| 123 // notification. We need to spin the message loop to catch this update. |
| 124 ExecuteJavaScript("document.getElementById('elt_text').value = 'foo';"); |
| 125 ProcessPendingMessages(); |
| 126 EXPECT_TRUE(render_thread_.GetUniqueMessageMatching( |
| 127 ViewHostMsg_UpdateState::ID)); |
| 128 } |
OLD | NEW |