diff options
-rw-r--r-- | chrome/browser/render_view_host.cc | 1 | ||||
-rw-r--r-- | chrome/browser/renderer_host/mock_render_process_host.cc | 65 | ||||
-rw-r--r-- | chrome/browser/renderer_host/mock_render_process_host.h | 56 | ||||
-rw-r--r-- | chrome/browser/renderer_host/render_view_host_unittest.cc | 21 | ||||
-rw-r--r-- | chrome/browser/renderer_host/test_render_view_host.cc | 41 | ||||
-rw-r--r-- | chrome/browser/renderer_host/test_render_view_host.h | 93 | ||||
-rw-r--r-- | chrome/test/unit/unittests.vcproj | 20 |
7 files changed, 296 insertions, 1 deletions
diff --git a/chrome/browser/render_view_host.cc b/chrome/browser/render_view_host.cc index 15bb6b4..152c3d6 100644 --- a/chrome/browser/render_view_host.cc +++ b/chrome/browser/render_view_host.cc @@ -826,7 +826,6 @@ void RenderViewHost::OnMsgRendererGone() { // as part of a wider page load, the page_id will be the same as for the top // level frame. If the user explicitly requests a subframe navigation, we will // get a new page_id because we need to create a new navigation entry for that - // action. void RenderViewHost::OnMsgNavigate(const IPC::Message& msg) { // Read the parameters out of the IPC message directly to avoid making another diff --git a/chrome/browser/renderer_host/mock_render_process_host.cc b/chrome/browser/renderer_host/mock_render_process_host.cc new file mode 100644 index 0000000..4b8d1b2 --- /dev/null +++ b/chrome/browser/renderer_host/mock_render_process_host.cc @@ -0,0 +1,65 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/renderer_host/mock_render_process_host.h"
+
+MockRenderProcessHost::MockRenderProcessHost(Profile* profile)
+ : RenderProcessHost(profile) {
+}
+
+MockRenderProcessHost::~MockRenderProcessHost() {
+}
+
+bool MockRenderProcessHost::Init() {
+ return true;
+}
+
+int MockRenderProcessHost::GetNextRoutingID() {
+ return 5;
+}
+
+void MockRenderProcessHost::CancelResourceRequests(int render_widget_id) {
+}
+
+void MockRenderProcessHost::CrossSiteClosePageACK(
+ int new_render_process_host_id,
+ int new_request_id) {
+}
+
+bool MockRenderProcessHost::WaitForPaintMsg(int render_widget_id,
+ const base::TimeDelta& max_delay,
+ IPC::Message* msg) {
+ return true;
+}
+
+void MockRenderProcessHost::ReceivedBadMessage(uint16 msg_type) {
+}
+
+void MockRenderProcessHost::WidgetRestored() {
+}
+
+void MockRenderProcessHost::WidgetHidden() {
+}
+
+void MockRenderProcessHost::AddWord(const std::wstring& word) {
+}
+
+bool MockRenderProcessHost::FastShutdownIfPossible() {
+ return false;
+}
+
+bool MockRenderProcessHost::Send(IPC::Message* msg) {
+ // Save the message in the sink.
+ sink_.OnMessageReceived(*msg);
+ return true;
+}
+
+void MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) {
+}
+
+void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) {
+}
+
+void MockRenderProcessHost::Unregister() {
+}
diff --git a/chrome/browser/renderer_host/mock_render_process_host.h b/chrome/browser/renderer_host/mock_render_process_host.h new file mode 100644 index 0000000..a50d32e --- /dev/null +++ b/chrome/browser/renderer_host/mock_render_process_host.h @@ -0,0 +1,56 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_RENDERER_HOST_MOCK_RENDER_PROCESS_HOST_H_
+#define CHROME_BROWSER_RENDERER_HOST_MOCK_RENDER_PROCESS_HOST_H_
+
+#include "base/basictypes.h"
+#include "chrome/browser/renderer_host/render_process_host.h"
+#include "chrome/common/ipc_test_sink.h"
+
+// A mock render process host that has no corresponding renderer process. The
+// process() refers to the current process, and all IPC messages are sent into
+// the message sink for inspection by tests.
+class MockRenderProcessHost : public RenderProcessHost {
+ public:
+ MockRenderProcessHost(Profile* profile);
+ virtual ~MockRenderProcessHost();
+
+ // Provides access to all IPC messages that would have been sent to the
+ // renderer via this RenderProcessHost.
+ IPC::TestSink& sink() { return sink_; }
+
+ // RenderProcessHost implementation (public portion).
+ virtual bool Init();
+ virtual int GetNextRoutingID();
+ virtual void CancelResourceRequests(int render_widget_id);
+ virtual void CrossSiteClosePageACK(int new_render_process_host_id,
+ int new_request_id);
+ virtual bool WaitForPaintMsg(int render_widget_id,
+ const base::TimeDelta& max_delay,
+ IPC::Message* msg);
+ virtual void ReceivedBadMessage(uint16 msg_type);
+ virtual void WidgetRestored();
+ virtual void WidgetHidden();
+ virtual void AddWord(const std::wstring& word);
+ virtual bool FastShutdownIfPossible();
+
+ // IPC::Channel::Sender via RenderProcessHost.
+ virtual bool Send(IPC::Message* msg);
+
+ // IPC::Channel::Listener via RenderProcessHost.
+ virtual void OnMessageReceived(const IPC::Message& msg);
+ virtual void OnChannelConnected(int32 peer_pid);
+
+ private:
+ // RenderProcessHost implementation (protected portion).
+ virtual void Unregister();
+
+ // Stores IPC messages that would have been sent to the renderer.
+ IPC::TestSink sink_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost);
+};
+
+#endif // CHROME_BROWSER_RENDERER_HOST_MOCK_RENDER_PROCESS_HOST_H_
diff --git a/chrome/browser/renderer_host/render_view_host_unittest.cc b/chrome/browser/renderer_host/render_view_host_unittest.cc new file mode 100644 index 0000000..0ae9e46 --- /dev/null +++ b/chrome/browser/renderer_host/render_view_host_unittest.cc @@ -0,0 +1,21 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/renderer_host/test_render_view_host.h"
+#include "chrome/browser/tab_contents/navigation_entry.h"
+
+namespace {
+
+class RenderViewHostTest : public RenderViewHostTestHarness {
+};
+
+} // namespace
+
+// All about URLs reported by the renderer should get rewritten to about:blank.
+// See RenderViewHost::OnMsgNavigate for a discussion.
+TEST_F(RenderViewHostTest, FilterAbout) {
+ rvh()->SendNavigate(1, GURL("about:cache"));
+ ASSERT_TRUE(controller_->GetActiveEntry());
+ EXPECT_EQ(GURL("about:blank"), controller_->GetActiveEntry()->url());
+}
diff --git a/chrome/browser/renderer_host/test_render_view_host.cc b/chrome/browser/renderer_host/test_render_view_host.cc new file mode 100644 index 0000000..bd01ca9 --- /dev/null +++ b/chrome/browser/renderer_host/test_render_view_host.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/renderer_host/test_render_view_host.h"
+
+void TestRenderViewHost::SendNavigate(int page_id, const GURL& url) {
+ ViewHostMsg_FrameNavigate_Params params; + params.is_post = false; + params.page_id = page_id; + params.is_content_filtered = false; + params.url = url;
+ params.should_update_history = true;
+ params.transition = PageTransition::LINK;
+ params.is_post = false;
+
+ ViewHostMsg_FrameNavigate msg(1, params);
+ OnMsgNavigate(msg);
+}
+
+void RenderViewHostTestHarness::SetUp() {
+ // This will be deleted when the WebContents goes away.
+ SiteInstance* instance = SiteInstance::CreateSiteInstance(&profile_);
+
+ // Make the SiteInstance use our RenderProcessHost as its own.
+ process_ = new MockRenderProcessHost(&profile_);
+ instance->set_process_host_id(process_->host_id());
+
+ contents_ = new WebContents(&profile_, instance, &rvh_factory_, 12, NULL);
+ controller_ = new NavigationController(contents_, &profile_);
+}
+
+void RenderViewHostTestHarness::TearDown() {
+ contents_->CloseContents();
+ contents_ = NULL;
+ controller_ = NULL;
+
+ // Make sure that we flush any messages related to WebContents destruction
+ // before we destroy the profile.
+ MessageLoop::current()->RunAllPending();
+}
diff --git a/chrome/browser/renderer_host/test_render_view_host.h b/chrome/browser/renderer_host/test_render_view_host.h new file mode 100644 index 0000000..5aa8703 --- /dev/null +++ b/chrome/browser/renderer_host/test_render_view_host.h @@ -0,0 +1,93 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ +#define CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "chrome/browser/tab_contents/navigation_controller.h" +#include "chrome/browser/tab_contents/web_contents.h" +#include "chrome/browser/renderer_host/mock_render_process_host.h" +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +// This file provides a testing framework for mocking out the RenderProcessHost +// layer. It allows you to test RenderViewHost, WebContents, +// NavigationController, and other layers above that without running an actual +// renderer process. +// +// To use, derive your test base class from RenderViewHostTestHarness. + +class TestRenderViewHost : public RenderViewHost { + public: + TestRenderViewHost(SiteInstance* instance, + RenderViewHostDelegate* delegate, + int routing_id, + base::WaitableEvent* modal_dialog_event) + : RenderViewHost(instance, delegate, routing_id, modal_dialog_event) { + } + + // Calls OnMsgNavigate on the RenderViewHost with the given information, + // setting the rest of the parameters in the message to the "typical" values. + // This is a helper function for simulating the most common types of loads. + void SendNavigate(int page_id, const GURL& url); + + private: + FRIEND_TEST(RenderViewHostTest, FilterNavigate); + + DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); +}; + +class TestRenderViewHostFactory : public RenderViewHostFactory { + public: + TestRenderViewHostFactory() {} + virtual ~TestRenderViewHostFactory() {} + + virtual RenderViewHost* CreateRenderViewHost( + SiteInstance* instance, + RenderViewHostDelegate* delegate, + int routing_id, + base::WaitableEvent* modal_dialog_event) { + return new TestRenderViewHost(instance, delegate, routing_id, + modal_dialog_event); + } + + private: + + DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); +}; + +class RenderViewHostTestHarness : public testing::Test { + public: + RenderViewHostTestHarness() + : process_(NULL), + contents_(NULL), + controller_(NULL) {} + ~RenderViewHostTestHarness() {} + + TestRenderViewHost* rvh() { + return reinterpret_cast<TestRenderViewHost*>(contents_->render_view_host()); + } + + protected: + // testing::Test + virtual void SetUp(); + virtual void TearDown(); + + MessageLoopForUI message_loop_; + TestingProfile profile_; + + TestRenderViewHostFactory rvh_factory_; + + // We clean up the WebContents by calling CloseContents, which deletes itself. + // This in turn causes the destruction of these other things. + MockRenderProcessHost* process_; + WebContents* contents_; + NavigationController* controller_; + + DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); +}; + +#endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index b94cee1..df300fa 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -519,6 +519,14 @@ > </File> <File + RelativePath="..\..\browser\renderer_host\mock_render_process_host.cc" + > + </File> + <File + RelativePath="..\..\browser\renderer_host\mock_render_process_host.h" + > + </File> + <File RelativePath="..\..\renderer\mock_render_thread.cc" > </File> @@ -607,6 +615,10 @@ > </File> <File + RelativePath="..\..\browser\renderer_host\render_view_host_unittest.cc" + > + </File> + <File RelativePath="..\..\renderer\render_view_unittest.cc" > </File> @@ -711,6 +723,14 @@ > </File> <File + RelativePath="..\..\browser\renderer_host\test_render_view_host.cc" + > + </File> + <File + RelativePath="..\..\browser\renderer_host\test_render_view_host.h" + > + </File> + <File RelativePath="..\..\browser\history\text_database_manager_unittest.cc" > </File> |