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
3 // found in the LICENSE file.
5 #include "base/callback.h"
6 #include "base/command_line.h"
7 #include "base/location.h"
8 #include "base/memory/discardable_memory.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/common/in_process_child_thread_params.h"
14 #include "content/common/resource_messages.h"
15 #include "content/common/websocket_messages.h"
16 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/renderer/content_renderer_client.h"
20 #include "content/renderer/render_process_impl.h"
21 #include "content/renderer/render_thread_impl.h"
22 #include "content/test/mock_render_process.h"
23 #include "content/test/render_thread_impl_browser_test_ipc_helper.h"
24 #include "gpu/GLES2/gl2extchromium.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 // IPC messages for testing ----------------------------------------------------
29 #define IPC_MESSAGE_IMPL
30 #include "ipc/ipc_message_macros.h"
32 #undef IPC_MESSAGE_START
33 #define IPC_MESSAGE_START TestMsgStart
34 IPC_MESSAGE_CONTROL0(TestMsg_QuitRunLoop
)
36 // -----------------------------------------------------------------------------
38 // These tests leak memory, this macro disables the test when under the
41 #define WILL_LEAK(NAME) DISABLED_##NAME
43 #define WILL_LEAK(NAME) NAME
49 // FIXME: It would be great if there was a reusable mock SingleThreadTaskRunner
50 class TestTaskCounter
: public base::SingleThreadTaskRunner
{
52 TestTaskCounter() : count_(0) {}
54 // SingleThreadTaskRunner implementation.
55 bool PostDelayedTask(const tracked_objects::Location
&,
57 base::TimeDelta
) override
{
58 base::AutoLock
auto_lock(lock_
);
63 bool PostNonNestableDelayedTask(const tracked_objects::Location
&,
65 base::TimeDelta
) override
{
66 base::AutoLock
auto_lock(lock_
);
71 bool RunsTasksOnCurrentThread() const override
{ return true; }
73 int NumTasksPosted() const {
74 base::AutoLock
auto_lock(lock_
);
79 ~TestTaskCounter() override
{}
81 mutable base::Lock lock_
;
85 #if defined(COMPILER_MSVC)
86 // See explanation for other RenderViewHostImpl which is the same issue.
88 #pragma warning(disable: 4250)
91 class RenderThreadImplForTest
: public RenderThreadImpl
{
93 RenderThreadImplForTest(const InProcessChildThreadParams
& params
,
94 scoped_refptr
<TestTaskCounter
> test_task_counter
)
95 : RenderThreadImpl(params
), test_task_counter_(test_task_counter
) {}
97 ~RenderThreadImplForTest() override
{}
99 void SetResourceDispatchTaskQueue(
100 const scoped_refptr
<base::SingleThreadTaskRunner
>&) override
{
101 // Use our TestTaskCounter instead.
102 RenderThreadImpl::SetResourceDispatchTaskQueue(test_task_counter_
);
105 using ChildThreadImpl::OnMessageReceived
;
108 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
111 #if defined(COMPILER_MSVC)
115 void QuitTask(base::MessageLoop
* message_loop
) {
116 message_loop
->QuitWhenIdle();
119 class QuitOnTestMsgFilter
: public IPC::MessageFilter
{
121 explicit QuitOnTestMsgFilter(base::MessageLoop
* message_loop
)
122 : message_loop_(message_loop
) {}
124 // IPC::MessageFilter overrides:
125 bool OnMessageReceived(const IPC::Message
& message
) override
{
126 message_loop_
->task_runner()->PostTask(
127 FROM_HERE
, base::Bind(&QuitTask
, message_loop_
));
131 bool GetSupportedMessageClasses(
132 std::vector
<uint32
>* supported_message_classes
) const override
{
133 supported_message_classes
->push_back(TestMsgStart
);
138 ~QuitOnTestMsgFilter() override
{}
140 base::MessageLoop
* message_loop_
;
143 class RenderThreadImplBrowserTest
: public testing::Test
{
145 void SetUp() override
{
146 content_client_
.reset(new ContentClient());
147 content_browser_client_
.reset(new ContentBrowserClient());
148 content_renderer_client_
.reset(new ContentRendererClient());
149 SetContentClient(content_client_
.get());
150 SetBrowserClientForTesting(content_browser_client_
.get());
151 SetRendererClientForTesting(content_renderer_client_
.get());
153 test_helper_
.reset(new RenderThreadImplBrowserIPCTestHelper());
155 mock_process_
.reset(new MockRenderProcess
);
156 test_task_counter_
= make_scoped_refptr(new TestTaskCounter());
158 // RenderThreadImpl expects the browser to pass these flags.
159 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
160 base::CommandLine::StringVector old_argv
= cmd
->argv();
162 cmd
->AppendSwitchASCII(switches::kNumRasterThreads
, "1");
163 cmd
->AppendSwitchASCII(switches::kContentImageTextureTarget
,
164 base::UintToString(GL_TEXTURE_2D
));
166 thread_
= new RenderThreadImplForTest(
167 InProcessChildThreadParams(test_helper_
->GetChannelId(),
168 test_helper_
->GetIOTaskRunner()),
170 cmd
->InitFromArgv(old_argv
);
172 thread_
->EnsureWebKitInitialized();
174 test_msg_filter_
= make_scoped_refptr(
175 new QuitOnTestMsgFilter(test_helper_
->GetMessageLoop()));
176 thread_
->AddFilter(test_msg_filter_
.get());
179 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
180 scoped_ptr
<ContentClient
> content_client_
;
181 scoped_ptr
<ContentBrowserClient
> content_browser_client_
;
182 scoped_ptr
<ContentRendererClient
> content_renderer_client_
;
183 scoped_ptr
<RenderThreadImplBrowserIPCTestHelper
> test_helper_
;
184 scoped_ptr
<MockRenderProcess
> mock_process_
;
185 scoped_refptr
<QuitOnTestMsgFilter
> test_msg_filter_
;
186 RenderThreadImplForTest
* thread_
; // Owned by mock_process_.
187 std::string channel_id_
;
190 void CheckRenderThreadInputHandlerManager(RenderThreadImpl
* thread
) {
191 ASSERT_TRUE(thread
->input_handler_manager());
194 // Check that InputHandlerManager outlives compositor thread because it uses
195 // raw pointers to post tasks.
196 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
197 TEST_F(RenderThreadImplBrowserTest
,
198 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread
)) {
199 ASSERT_TRUE(thread_
->input_handler_manager());
201 thread_
->compositor_task_runner()->PostTask(
202 FROM_HERE
, base::Bind(&CheckRenderThreadInputHandlerManager
, thread_
));
205 // Disabled under LeakSanitizer due to memory leaks.
206 TEST_F(RenderThreadImplBrowserTest
,
207 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler
)) {
208 test_helper_
->Sender()->Send(new ResourceHostMsg_FollowRedirect(0));
209 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
211 test_helper_
->GetMessageLoop()->Run();
212 EXPECT_EQ(1, test_task_counter_
->NumTasksPosted());
215 // Disabled under LeakSanitizer due to memory leaks.
216 TEST_F(RenderThreadImplBrowserTest
,
217 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler
)) {
218 // NOTE other than not being a resource message, the actual message is
220 test_helper_
->Sender()->Send(new WebSocketMsg_NotifyFailure(1, ""));
221 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
223 test_helper_
->GetMessageLoop()->Run();
225 EXPECT_EQ(0, test_task_counter_
->NumTasksPosted());
229 } // namespace content