OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "content/renderer/render_thread_impl.h" | 5 #include "content/renderer/render_thread_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/memory/discardable_memory.h" | 14 #include "base/memory/discardable_memory.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
19 #include "cc/output/buffer_to_texture_target_map.h" | 19 #include "cc/output/buffer_to_texture_target_map.h" |
20 #include "content/app/mojo/mojo_init.h" | 20 #include "content/app/mojo/mojo_init.h" |
21 #include "content/common/in_process_child_thread_params.h" | 21 #include "content/common/in_process_child_thread_params.h" |
| 22 #include "content/common/mojo/constants.h" |
| 23 #include "content/common/mojo/mojo_child_connection.h" |
22 #include "content/common/resource_messages.h" | 24 #include "content/common/resource_messages.h" |
| 25 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
24 #include "content/public/common/content_client.h" | 27 #include "content/public/common/content_client.h" |
25 #include "content/public/common/content_switches.h" | 28 #include "content/public/common/content_switches.h" |
| 29 #include "content/public/common/mojo_shell_connection.h" |
26 #include "content/public/renderer/content_renderer_client.h" | 30 #include "content/public/renderer/content_renderer_client.h" |
| 31 #include "content/public/test/test_browser_thread_bundle.h" |
| 32 #include "content/public/test/test_content_client_initializer.h" |
| 33 #include "content/public/test/test_mojo_shell_context.h" |
27 #include "content/renderer/render_process_impl.h" | 34 #include "content/renderer/render_process_impl.h" |
28 #include "content/test/mock_render_process.h" | 35 #include "content/test/mock_render_process.h" |
29 #include "content/test/render_thread_impl_browser_test_ipc_helper.h" | |
30 #include "gpu/GLES2/gl2extchromium.h" | 36 #include "gpu/GLES2/gl2extchromium.h" |
| 37 #include "ipc/ipc.mojom.h" |
| 38 #include "ipc/ipc_channel_mojo.h" |
| 39 #include "mojo/edk/embedder/embedder.h" |
| 40 #include "mojo/edk/test/scoped_ipc_support.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "third_party/WebKit/public/platform/scheduler/renderer/renderer_schedul
er.h" | 42 #include "third_party/WebKit/public/platform/scheduler/renderer/renderer_schedul
er.h" |
33 | 43 |
34 // IPC messages for testing ---------------------------------------------------- | 44 // IPC messages for testing ---------------------------------------------------- |
35 | 45 |
36 // TODO(mdempsky): Fix properly by moving into a separate | 46 // TODO(mdempsky): Fix properly by moving into a separate |
37 // browsertest_message_generator.cc file. | 47 // browsertest_message_generator.cc file. |
38 #undef IPC_IPC_MESSAGE_MACROS_H_ | 48 #undef IPC_IPC_MESSAGE_MACROS_H_ |
39 #undef IPC_MESSAGE_EXTRA | 49 #undef IPC_MESSAGE_EXTRA |
40 #define IPC_MESSAGE_IMPL | 50 #define IPC_MESSAGE_IMPL |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 112 |
103 class RenderThreadImplForTest : public RenderThreadImpl { | 113 class RenderThreadImplForTest : public RenderThreadImpl { |
104 public: | 114 public: |
105 RenderThreadImplForTest( | 115 RenderThreadImplForTest( |
106 const InProcessChildThreadParams& params, | 116 const InProcessChildThreadParams& params, |
107 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler, | 117 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler, |
108 scoped_refptr<base::SingleThreadTaskRunner>& test_task_counter) | 118 scoped_refptr<base::SingleThreadTaskRunner>& test_task_counter) |
109 : RenderThreadImpl(params, std::move(scheduler), test_task_counter) {} | 119 : RenderThreadImpl(params, std::move(scheduler), test_task_counter) {} |
110 | 120 |
111 ~RenderThreadImplForTest() override {} | 121 ~RenderThreadImplForTest() override {} |
| 122 }; |
112 | 123 |
113 using ChildThreadImpl::OnMessageReceived; | 124 class DummyListener : public IPC::Listener { |
| 125 public: |
| 126 ~DummyListener() override {} |
114 | 127 |
115 private: | 128 bool OnMessageReceived(const IPC::Message& message) override { return true; } |
116 }; | 129 }; |
117 | 130 |
118 #if defined(COMPILER_MSVC) | 131 #if defined(COMPILER_MSVC) |
119 #pragma warning(pop) | 132 #pragma warning(pop) |
120 #endif | 133 #endif |
121 | 134 |
122 void QuitTask(base::MessageLoop* message_loop) { | 135 void QuitTask(base::MessageLoop* message_loop) { |
123 message_loop->QuitWhenIdle(); | 136 message_loop->QuitWhenIdle(); |
124 } | 137 } |
125 | 138 |
(...skipping 17 matching lines...) Expand all Loading... |
143 | 156 |
144 private: | 157 private: |
145 ~QuitOnTestMsgFilter() override {} | 158 ~QuitOnTestMsgFilter() override {} |
146 | 159 |
147 base::MessageLoop* message_loop_; | 160 base::MessageLoop* message_loop_; |
148 }; | 161 }; |
149 | 162 |
150 class RenderThreadImplBrowserTest : public testing::Test { | 163 class RenderThreadImplBrowserTest : public testing::Test { |
151 public: | 164 public: |
152 void SetUp() override { | 165 void SetUp() override { |
153 content_client_.reset(new ContentClient()); | |
154 content_browser_client_.reset(new ContentBrowserClient()); | |
155 content_renderer_client_.reset(new ContentRendererClient()); | 166 content_renderer_client_.reset(new ContentRendererClient()); |
156 SetContentClient(content_client_.get()); | |
157 SetBrowserClientForTesting(content_browser_client_.get()); | |
158 SetRendererClientForTesting(content_renderer_client_.get()); | 167 SetRendererClientForTesting(content_renderer_client_.get()); |
159 | 168 |
160 test_helper_.reset(new RenderThreadImplBrowserIPCTestHelper()); | 169 browser_threads_.reset( |
| 170 new TestBrowserThreadBundle(TestBrowserThreadBundle::IO_MAINLOOP)); |
| 171 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner = |
| 172 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO); |
| 173 |
| 174 InitializeMojo(); |
| 175 ipc_support_.reset(new mojo::edk::test::ScopedIPCSupport(io_task_runner)); |
| 176 shell_context_.reset(new TestMojoShellContext); |
| 177 child_connection_.reset(new MojoChildConnection( |
| 178 kRendererMojoApplicationName, "test", mojo::edk::GenerateRandomToken(), |
| 179 MojoShellConnection::GetForProcess()->GetConnector(), io_task_runner)); |
| 180 |
| 181 mojo::MessagePipe pipe; |
| 182 IPC::mojom::ChannelBootstrapPtr channel_bootstrap; |
| 183 child_connection_->GetRemoteInterfaces()->GetInterface(&channel_bootstrap); |
| 184 |
| 185 dummy_listener_.reset(new DummyListener); |
| 186 channel_ = IPC::ChannelProxy::Create( |
| 187 IPC::ChannelMojo::CreateServerFactory( |
| 188 channel_bootstrap.PassInterface().PassHandle(), io_task_runner), |
| 189 dummy_listener_.get(), io_task_runner); |
161 | 190 |
162 mock_process_.reset(new MockRenderProcess); | 191 mock_process_.reset(new MockRenderProcess); |
163 test_task_counter_ = make_scoped_refptr(new TestTaskCounter()); | 192 test_task_counter_ = make_scoped_refptr(new TestTaskCounter()); |
164 | 193 |
165 // RenderThreadImpl expects the browser to pass these flags. | 194 // RenderThreadImpl expects the browser to pass these flags. |
166 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); | 195 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); |
167 base::CommandLine::StringVector old_argv = cmd->argv(); | 196 base::CommandLine::StringVector old_argv = cmd->argv(); |
168 | 197 |
169 cmd->AppendSwitchASCII(switches::kNumRasterThreads, "1"); | 198 cmd->AppendSwitchASCII(switches::kNumRasterThreads, "1"); |
170 cmd->AppendSwitchASCII( | 199 cmd->AppendSwitchASCII( |
171 switches::kContentImageTextureTarget, | 200 switches::kContentImageTextureTarget, |
172 cc::BufferToTextureTargetMapToString( | 201 cc::BufferToTextureTargetMapToString( |
173 cc::DefaultBufferToTextureTargetMapForTesting())); | 202 cc::DefaultBufferToTextureTargetMapForTesting())); |
174 | 203 |
175 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler = | 204 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler = |
176 blink::scheduler::RendererScheduler::Create(); | 205 blink::scheduler::RendererScheduler::Create(); |
177 InitializeMojo(); | |
178 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter( | 206 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter( |
179 test_task_counter_.get()); | 207 test_task_counter_.get()); |
180 thread_ = new RenderThreadImplForTest( | 208 thread_ = new RenderThreadImplForTest( |
181 InProcessChildThreadParams(test_helper_->GetChannelId(), | 209 InProcessChildThreadParams("", io_task_runner, |
182 test_helper_->GetIOTaskRunner(), | 210 child_connection_->service_token()), |
183 test_helper_->GetMojoIpcToken(), | |
184 test_helper_->GetMojoApplicationToken()), | |
185 std::move(renderer_scheduler), test_task_counter); | 211 std::move(renderer_scheduler), test_task_counter); |
186 cmd->InitFromArgv(old_argv); | 212 cmd->InitFromArgv(old_argv); |
187 | 213 |
188 test_msg_filter_ = make_scoped_refptr( | 214 test_msg_filter_ = make_scoped_refptr( |
189 new QuitOnTestMsgFilter(test_helper_->GetMessageLoop())); | 215 new QuitOnTestMsgFilter(base::MessageLoop::current())); |
190 thread_->AddFilter(test_msg_filter_.get()); | 216 thread_->AddFilter(test_msg_filter_.get()); |
191 } | 217 } |
192 | 218 |
| 219 IPC::Sender* sender() { return channel_.get(); } |
| 220 |
193 scoped_refptr<TestTaskCounter> test_task_counter_; | 221 scoped_refptr<TestTaskCounter> test_task_counter_; |
194 std::unique_ptr<ContentClient> content_client_; | 222 TestContentClientInitializer content_client_initializer_; |
195 std::unique_ptr<ContentBrowserClient> content_browser_client_; | |
196 std::unique_ptr<ContentRendererClient> content_renderer_client_; | 223 std::unique_ptr<ContentRendererClient> content_renderer_client_; |
197 std::unique_ptr<RenderThreadImplBrowserIPCTestHelper> test_helper_; | 224 |
| 225 std::unique_ptr<TestBrowserThreadBundle> browser_threads_; |
| 226 std::unique_ptr<mojo::edk::test::ScopedIPCSupport> ipc_support_; |
| 227 std::unique_ptr<TestMojoShellContext> shell_context_; |
| 228 std::unique_ptr<MojoChildConnection> child_connection_; |
| 229 std::unique_ptr<DummyListener> dummy_listener_; |
| 230 std::unique_ptr<IPC::ChannelProxy> channel_; |
| 231 |
198 std::unique_ptr<MockRenderProcess> mock_process_; | 232 std::unique_ptr<MockRenderProcess> mock_process_; |
199 scoped_refptr<QuitOnTestMsgFilter> test_msg_filter_; | 233 scoped_refptr<QuitOnTestMsgFilter> test_msg_filter_; |
200 RenderThreadImplForTest* thread_; // Owned by mock_process_. | 234 RenderThreadImplForTest* thread_; // Owned by mock_process_. |
201 std::string channel_id_; | |
202 }; | 235 }; |
203 | 236 |
204 void CheckRenderThreadInputHandlerManager(RenderThreadImpl* thread) { | 237 void CheckRenderThreadInputHandlerManager(RenderThreadImpl* thread) { |
205 ASSERT_TRUE(thread->input_handler_manager()); | 238 ASSERT_TRUE(thread->input_handler_manager()); |
206 } | 239 } |
207 | 240 |
208 // Check that InputHandlerManager outlives compositor thread because it uses | 241 // Check that InputHandlerManager outlives compositor thread because it uses |
209 // raw pointers to post tasks. | 242 // raw pointers to post tasks. |
210 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994 | 243 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994 |
211 TEST_F(RenderThreadImplBrowserTest, | 244 TEST_F(RenderThreadImplBrowserTest, |
212 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread)) { | 245 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread)) { |
213 ASSERT_TRUE(thread_->input_handler_manager()); | 246 ASSERT_TRUE(thread_->input_handler_manager()); |
214 | 247 |
215 thread_->compositor_task_runner()->PostTask( | 248 thread_->compositor_task_runner()->PostTask( |
216 FROM_HERE, base::Bind(&CheckRenderThreadInputHandlerManager, thread_)); | 249 FROM_HERE, base::Bind(&CheckRenderThreadInputHandlerManager, thread_)); |
217 } | 250 } |
218 | 251 |
219 // Disabled under LeakSanitizer due to memory leaks. | 252 // Disabled under LeakSanitizer due to memory leaks. |
220 TEST_F(RenderThreadImplBrowserTest, | 253 TEST_F(RenderThreadImplBrowserTest, |
221 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler)) { | 254 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler)) { |
222 test_helper_->Sender()->Send(new ResourceHostMsg_FollowRedirect(0)); | 255 sender()->Send(new ResourceHostMsg_FollowRedirect(0)); |
223 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop()); | 256 sender()->Send(new TestMsg_QuitRunLoop()); |
224 | 257 |
225 base::RunLoop().Run(); | 258 base::RunLoop().Run(); |
226 EXPECT_EQ(1, test_task_counter_->NumTasksPosted()); | 259 EXPECT_EQ(1, test_task_counter_->NumTasksPosted()); |
227 } | 260 } |
228 | 261 |
229 // Disabled under LeakSanitizer due to memory leaks. | 262 // Disabled under LeakSanitizer due to memory leaks. |
230 TEST_F(RenderThreadImplBrowserTest, | 263 TEST_F(RenderThreadImplBrowserTest, |
231 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler)) { | 264 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler)) { |
232 // NOTE other than not being a resource message, the actual message is | 265 // NOTE other than not being a resource message, the actual message is |
233 // unimportant. | 266 // unimportant. |
234 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop()); | 267 |
| 268 sender()->Send(new TestMsg_QuitRunLoop()); |
235 | 269 |
236 base::RunLoop().Run(); | 270 base::RunLoop().Run(); |
237 | 271 |
238 EXPECT_EQ(0, test_task_counter_->NumTasksPosted()); | 272 EXPECT_EQ(0, test_task_counter_->NumTasksPosted()); |
239 } | 273 } |
240 | 274 |
241 } // namespace | 275 } // namespace |
242 } // namespace content | 276 } // namespace content |
OLD | NEW |