| 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/test_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/browser/render_widget_host_view.h" |
| 14 #include "chrome/test/testing_profile.h" | 14 #include "chrome/test/testing_profile.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 class TestWebContents; |
| 18 |
| 17 // This file provides a testing framework for mocking out the RenderProcessHost | 19 // This file provides a testing framework for mocking out the RenderProcessHost |
| 18 // layer. It allows you to test RenderViewHost, WebContents, | 20 // layer. It allows you to test RenderViewHost, WebContents, |
| 19 // NavigationController, and other layers above that without running an actual | 21 // NavigationController, and other layers above that without running an actual |
| 20 // renderer process. | 22 // renderer process. |
| 21 // | 23 // |
| 22 // To use, derive your test base class from RenderViewHostTestHarness. | 24 // To use, derive your test base class from RenderViewHostTestHarness. |
| 23 | 25 |
| 24 // TestRenderViewHostView ------------------------------------------------------ | 26 // TestRenderViewHostView ------------------------------------------------------ |
| 25 | 27 |
| 26 // Subclass the RenderViewHost's view so that we can call Show(), etc., | 28 // Subclass the RenderViewHost's view so that we can call Show(), etc., |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 // See set_delete_counter() above. May be NULL. | 117 // See set_delete_counter() above. May be NULL. |
| 116 int* delete_counter_; | 118 int* delete_counter_; |
| 117 | 119 |
| 118 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); | 120 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHost); |
| 119 }; | 121 }; |
| 120 | 122 |
| 121 // TestRenderViewHostFactory --------------------------------------------------- | 123 // TestRenderViewHostFactory --------------------------------------------------- |
| 122 | 124 |
| 123 class TestRenderViewHostFactory : public RenderViewHostFactory { | 125 class TestRenderViewHostFactory : public RenderViewHostFactory { |
| 124 public: | 126 public: |
| 125 TestRenderViewHostFactory() {} | 127 TestRenderViewHostFactory(RenderProcessHostFactory* rph_factory) |
| 126 virtual ~TestRenderViewHostFactory() {} | 128 : render_process_host_factory_(rph_factory) { |
| 127 | 129 } |
| 128 static TestRenderViewHostFactory* GetInstance() { | 130 virtual ~TestRenderViewHostFactory() { |
| 129 static TestRenderViewHostFactory instance; | |
| 130 return &instance; | |
| 131 } | 131 } |
| 132 | 132 |
| 133 virtual RenderViewHost* CreateRenderViewHost( | 133 virtual RenderViewHost* CreateRenderViewHost( |
| 134 SiteInstance* instance, | 134 SiteInstance* instance, |
| 135 RenderViewHostDelegate* delegate, | 135 RenderViewHostDelegate* delegate, |
| 136 int routing_id, | 136 int routing_id, |
| 137 base::WaitableEvent* modal_dialog_event) { | 137 base::WaitableEvent* modal_dialog_event) { |
| 138 // See declaration of render_process_host_factory_ below. |
| 139 instance->set_render_process_host_factory(render_process_host_factory_); |
| 138 return new TestRenderViewHost(instance, delegate, routing_id, | 140 return new TestRenderViewHost(instance, delegate, routing_id, |
| 139 modal_dialog_event); | 141 modal_dialog_event); |
| 140 } | 142 } |
| 141 | 143 |
| 142 private: | 144 private: |
| 145 // This is a bit of a hack. With the current design of the site instances / |
| 146 // browsing instances, it's difficult to pass a RenderProcessHostFactory |
| 147 // around properly. |
| 148 // |
| 149 // Instead, we set it right before we create a new RenderViewHost, which |
| 150 // happens before the RenderProcessHost is created. This way, the instance |
| 151 // has the correct factory and creates our special RenderProcessHosts. |
| 152 RenderProcessHostFactory* render_process_host_factory_; |
| 153 |
| 143 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); | 154 DISALLOW_COPY_AND_ASSIGN(TestRenderViewHostFactory); |
| 144 }; | 155 }; |
| 145 | 156 |
| 146 // RenderViewHostTestHarness --------------------------------------------------- | 157 // RenderViewHostTestHarness --------------------------------------------------- |
| 147 | 158 |
| 148 class RenderViewHostTestHarness : public testing::Test { | 159 class RenderViewHostTestHarness : public testing::Test { |
| 149 public: | 160 public: |
| 150 RenderViewHostTestHarness() | 161 RenderViewHostTestHarness() |
| 151 : process_(NULL), | 162 : rph_factory_(), |
| 163 rvh_factory_(&rph_factory_), |
| 164 process_(NULL), |
| 152 contents_(NULL), | 165 contents_(NULL), |
| 153 controller_(NULL) {} | 166 controller_(NULL) {} |
| 154 virtual ~RenderViewHostTestHarness() {} | 167 virtual ~RenderViewHostTestHarness() {} |
| 155 | 168 |
| 169 NavigationController* controller() { |
| 170 return contents_->controller(); |
| 171 } |
| 172 |
| 173 TestWebContents* contents() { |
| 174 return contents_; |
| 175 } |
| 176 |
| 156 TestRenderViewHost* rvh() { | 177 TestRenderViewHost* rvh() { |
| 157 return reinterpret_cast<TestRenderViewHost*>(contents_->render_view_host()); | 178 return reinterpret_cast<TestRenderViewHost*>(contents_->render_view_host()); |
| 158 } | 179 } |
| 159 | 180 |
| 181 Profile* profile() { |
| 182 return profile_.get(); |
| 183 } |
| 184 |
| 160 protected: | 185 protected: |
| 161 // testing::Test | 186 // testing::Test |
| 162 virtual void SetUp(); | 187 virtual void SetUp(); |
| 163 virtual void TearDown(); | 188 virtual void TearDown(); |
| 164 | 189 |
| 165 MessageLoopForUI message_loop_; | 190 MessageLoopForUI message_loop_; |
| 166 TestingProfile profile_; | |
| 167 | 191 |
| 192 // This profile will be created in SetUp if it has not already been created. |
| 193 // This allows tests to override the profile if they so choose in their own |
| 194 // SetUp function before calling the base class's (us) SetUp(). |
| 195 scoped_ptr<TestingProfile> profile_; |
| 196 |
| 197 MockRenderProcessHostFactory rph_factory_; |
| 168 TestRenderViewHostFactory rvh_factory_; | 198 TestRenderViewHostFactory rvh_factory_; |
| 169 | 199 |
| 170 // We clean up the WebContents by calling CloseContents, which deletes itself. | 200 // We clean up the WebContents by calling CloseContents, which deletes itself. |
| 171 // This in turn causes the destruction of these other things. | 201 // This in turn causes the destruction of these other things. |
| 172 MockRenderProcessHost* process_; | 202 MockRenderProcessHost* process_; |
| 173 WebContents* contents_; | 203 TestWebContents* contents_; |
| 174 NavigationController* controller_; | 204 NavigationController* controller_; |
| 175 | 205 |
| 176 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); | 206 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestHarness); |
| 177 }; | 207 }; |
| 178 | 208 |
| 179 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ | 209 #endif // CHROME_BROWSER_RENDERER_HOST_TEST_RENDER_VIEW_HOST_H_ |
| OLD | NEW |