| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/tab_contents/navigation_controller.h" | 10 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 11 #include "chrome/browser/tab_contents/web_contents.h" | 11 #include "chrome/browser/tab_contents/web_contents.h" |
| 12 #include "chrome/browser/renderer_host/mock_render_process_host.h" | 12 #include "chrome/browser/renderer_host/mock_render_process_host.h" |
| 13 #include "chrome/browser/render_widget_host_view.h" |
| 13 #include "chrome/test/testing_profile.h" | 14 #include "chrome/test/testing_profile.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 // This file provides a testing framework for mocking out the RenderProcessHost | 17 // This file provides a testing framework for mocking out the RenderProcessHost |
| 17 // layer. It allows you to test RenderViewHost, WebContents, | 18 // layer. It allows you to test RenderViewHost, WebContents, |
| 18 // NavigationController, and other layers above that without running an actual | 19 // NavigationController, and other layers above that without running an actual |
| 19 // renderer process. | 20 // renderer process. |
| 20 // | 21 // |
| 21 // To use, derive your test base class from RenderViewHostTestHarness. | 22 // To use, derive your test base class from RenderViewHostTestHarness. |
| 22 | 23 |
| 24 // TestRenderViewHostView ------------------------------------------------------ |
| 25 |
| 26 // Subclass the RenderViewHost's view so that we can call Show(), etc., |
| 27 // without having side-effects. |
| 28 class TestRenderWidgetHostView : public RenderWidgetHostView { |
| 29 public: |
| 30 TestRenderWidgetHostView() : is_showing_(false) {} |
| 31 |
| 32 virtual RenderWidgetHost* GetRenderWidgetHost() const { return NULL; } |
| 33 virtual void DidBecomeSelected() {} |
| 34 virtual void WasHidden() {} |
| 35 virtual void SetSize(const gfx::Size& size) {} |
| 36 virtual HWND GetPluginHWND() { return NULL; } |
| 37 virtual HANDLE ModalDialogEvent() { return NULL; } |
| 38 virtual void ForwardMouseEventToRenderer(UINT message, |
| 39 WPARAM wparam, |
| 40 LPARAM lparam) {} |
| 41 virtual void Focus() {} |
| 42 virtual void Blur() {} |
| 43 virtual bool HasFocus() { return true; } |
| 44 virtual void AdvanceFocus(bool reverse) {} |
| 45 virtual void Show() { is_showing_ = true; } |
| 46 virtual void Hide() { is_showing_ = false; } |
| 47 virtual gfx::Rect GetViewBounds() const { return gfx::Rect(); } |
| 48 virtual void UpdateCursor(const WebCursor& cursor) {} |
| 49 virtual void UpdateCursorIfOverSelf() {} |
| 50 // Indicates if the page has finished loading. |
| 51 virtual void SetIsLoading(bool is_loading) {} |
| 52 virtual void IMEUpdateStatus(ViewHostMsg_ImeControl control, |
| 53 const gfx::Rect& caret_rect) {} |
| 54 virtual void DidPaintRect(const gfx::Rect& rect) {} |
| 55 virtual void DidScrollRect(const gfx::Rect& rect, int dx, int dy) {} |
| 56 virtual void RendererGone() {} |
| 57 virtual void Destroy() {} |
| 58 virtual void PrepareToDestroy() {} |
| 59 virtual void SetTooltipText(const std::wstring& tooltip_text) {} |
| 60 |
| 61 bool is_showing() const { return is_showing_; } |
| 62 |
| 63 private: |
| 64 bool is_showing_; |
| 65 }; |
| 66 |
| 67 // TestRenderViewHost ---------------------------------------------------------- |
| 68 |
| 69 // TODO(brettw) this should use a TestWebContents which should be generalized |
| 70 // from the WebContents test. We will probably also need that class' version of |
| 71 // CreateRenderViewForRenderManager when more complicate tests start using this. |
| 23 class TestRenderViewHost : public RenderViewHost { | 72 class TestRenderViewHost : public RenderViewHost { |
| 24 public: | 73 public: |
| 25 TestRenderViewHost(SiteInstance* instance, | 74 TestRenderViewHost(SiteInstance* instance, |
| 26 RenderViewHostDelegate* delegate, | 75 RenderViewHostDelegate* delegate, |
| 27 int routing_id, | 76 int routing_id, |
| 28 base::WaitableEvent* modal_dialog_event) | 77 base::WaitableEvent* modal_dialog_event); |
| 29 : RenderViewHost(instance, delegate, routing_id, modal_dialog_event) { | 78 virtual ~TestRenderViewHost(); |
| 30 } | 79 |
| 80 // Testing functions --------------------------------------------------------- |
| 81 |
| 82 // Calls the RenderViewHosts' private OnMessageReceived function with the |
| 83 // given message. |
| 84 void TestOnMessageReceived(const IPC::Message& msg); |
| 31 | 85 |
| 32 // Calls OnMsgNavigate on the RenderViewHost with the given information, | 86 // Calls OnMsgNavigate on the RenderViewHost with the given information, |
| 33 // setting the rest of the parameters in the message to the "typical" values. | 87 // setting the rest of the parameters in the message to the "typical" values. |
| 34 // This is a helper function for simulating the most common types of loads. | 88 // This is a helper function for simulating the most common types of loads. |
| 35 void SendNavigate(int page_id, const GURL& url); | 89 void SendNavigate(int page_id, const GURL& url); |
| 36 | 90 |
| 91 // If set, *delete_counter is incremented when this object destructs. |
| 92 void set_delete_counter(int* delete_counter) { |
| 93 delete_counter_ = delete_counter; |
| 94 } |
| 95 |
| 96 // Sets whether the RenderView currently exists or not. This controls the |
| 97 // return value from IsRenderViewLive, which the rest of the system uses to |
| 98 // check whether the RenderView has crashed or not. |
| 99 void set_render_view_created(bool created) { |
| 100 render_view_created_ = created; |
| 101 } |
| 102 |
| 103 // RenderViewHost overrides -------------------------------------------------- |
| 104 |
| 105 virtual bool CreateRenderView(); |
| 106 virtual bool IsRenderViewLive() const; |
| 107 |
| 37 private: | 108 private: |
| 38 FRIEND_TEST(RenderViewHostTest, FilterNavigate); | 109 FRIEND_TEST(RenderViewHostTest, FilterNavigate); |
| 39 | 110 |
| 111 // Tracks if the caller thinks if it created the RenderView. This is so we can |
| 112 // respond to IsRenderViewLive appropriately. |
| 113 bool render_view_created_; |
| 114 |
| 115 // See set_delete_counter() above. May be NULL. |
| 116 int* delete_counter_; |
| 117 |
| 40 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); | 118 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); |
| 41 }; | 119 }; |
| 42 | 120 |
| 121 // TestRenderViewHostFactory --------------------------------------------------- |
| 122 |
| 43 class TestRenderViewHostFactory : public RenderViewHostFactory { | 123 class TestRenderViewHostFactory : public RenderViewHostFactory { |
| 44 public: | 124 public: |
| 45 TestRenderViewHostFactory() {} | 125 TestRenderViewHostFactory() {} |
| 46 virtual ~TestRenderViewHostFactory() {} | 126 virtual ~TestRenderViewHostFactory() {} |
| 47 | 127 |
| 128 static TestRenderViewHostFactory* GetInstance() { |
| 129 static TestRenderViewHostFactory instance; |
| 130 return &instance; |
| 131 } |
| 132 |
| 48 virtual RenderViewHost* CreateRenderViewHost( | 133 virtual RenderViewHost* CreateRenderViewHost( |
| 49 SiteInstance* instance, | 134 SiteInstance* instance, |
| 50 RenderViewHostDelegate* delegate, | 135 RenderViewHostDelegate* delegate, |
| 51 int routing_id, | 136 int routing_id, |
| 52 base::WaitableEvent* modal_dialog_event) { | 137 base::WaitableEvent* modal_dialog_event) { |
| 53 return new TestRenderViewHost(instance, delegate, routing_id, | 138 return new TestRenderViewHost(instance, delegate, routing_id, |
| 54 modal_dialog_event); | 139 modal_dialog_event); |
| 55 } | 140 } |
| 56 | 141 |
| 57 private: | 142 private: |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); | 143 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); |
| 60 }; | 144 }; |
| 61 | 145 |
| 146 // RenderViewHostTestHarness --------------------------------------------------- |
| 147 |
| 62 class RenderViewHostTestHarness : public testing::Test { | 148 class RenderViewHostTestHarness : public testing::Test { |
| 63 public: | 149 public: |
| 64 RenderViewHostTestHarness() | 150 RenderViewHostTestHarness() |
| 65 : process_(NULL), | 151 : process_(NULL), |
| 66 contents_(NULL), | 152 contents_(NULL), |
| 67 controller_(NULL) {} | 153 controller_(NULL) {} |
| 68 ~RenderViewHostTestHarness() {} | 154 virtual ~RenderViewHostTestHarness() {} |
| 69 | 155 |
| 70 TestRenderViewHost* rvh() { | 156 TestRenderViewHost* rvh() { |
| 71 return reinterpret_cast<TestRenderViewHost*>(contents_->render_view_host()); | 157 return reinterpret_cast<TestRenderViewHost*>(contents_->render_view_host()); |
| 72 } | 158 } |
| 73 | 159 |
| 74 protected: | 160 protected: |
| 75 // testing::Test | 161 // testing::Test |
| 76 virtual void SetUp(); | 162 virtual void SetUp(); |
| 77 virtual void TearDown(); | 163 virtual void TearDown(); |
| 78 | 164 |
| 79 MessageLoopForUI message_loop_; | 165 MessageLoopForUI message_loop_; |
| 80 TestingProfile profile_; | 166 TestingProfile profile_; |
| 81 | 167 |
| 82 TestRenderViewHostFactory rvh_factory_; | 168 TestRenderViewHostFactory rvh_factory_; |
| 83 | 169 |
| 84 // We clean up the WebContents by calling CloseContents, which deletes itself. | 170 // We clean up the WebContents by calling CloseContents, which deletes itself. |
| 85 // This in turn causes the destruction of these other things. | 171 // This in turn causes the destruction of these other things. |
| 86 MockRenderProcessHost* process_; | 172 MockRenderProcessHost* process_; |
| 87 WebContents* contents_; | 173 WebContents* contents_; |
| 88 NavigationController* controller_; | 174 NavigationController* controller_; |
| 89 | 175 |
| 90 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | 176 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); |
| 91 }; | 177 }; |
| 92 | 178 |
| 93 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 179 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
| OLD | NEW |