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 "components/scheduler/renderer/renderer_scheduler.h"
14 #include "content/common/in_process_child_thread_params.h"
15 #include "content/common/resource_messages.h"
16 #include "content/common/websocket_messages.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "content/public/common/content_client.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/renderer/content_renderer_client.h"
21 #include "content/renderer/render_process_impl.h"
22 #include "content/renderer/render_thread_impl.h"
23 #include "content/test/mock_render_process.h"
24 #include "content/test/render_thread_impl_browser_test_ipc_helper.h"
25 #include "gpu/GLES2/gl2extchromium.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 // IPC messages for testing ----------------------------------------------------
30 #define IPC_MESSAGE_IMPL
31 #include "ipc/ipc_message_macros.h"
33 #undef IPC_MESSAGE_START
34 #define IPC_MESSAGE_START TestMsgStart
35 IPC_MESSAGE_CONTROL0(TestMsg_QuitRunLoop
)
37 // -----------------------------------------------------------------------------
39 // These tests leak memory, this macro disables the test when under the
42 #define WILL_LEAK(NAME) DISABLED_##NAME
44 #define WILL_LEAK(NAME) NAME
50 // FIXME: It would be great if there was a reusable mock SingleThreadTaskRunner
51 class TestTaskCounter
: public base::SingleThreadTaskRunner
{
53 TestTaskCounter() : count_(0) {}
55 // SingleThreadTaskRunner implementation.
56 bool PostDelayedTask(const tracked_objects::Location
&,
58 base::TimeDelta
) override
{
59 base::AutoLock
auto_lock(lock_
);
64 bool PostNonNestableDelayedTask(const tracked_objects::Location
&,
66 base::TimeDelta
) override
{
67 base::AutoLock
auto_lock(lock_
);
72 bool RunsTasksOnCurrentThread() const override
{ return true; }
74 int NumTasksPosted() const {
75 base::AutoLock
auto_lock(lock_
);
80 ~TestTaskCounter() override
{}
82 mutable base::Lock lock_
;
86 #if defined(COMPILER_MSVC)
87 // See explanation for other RenderViewHostImpl which is the same issue.
89 #pragma warning(disable: 4250)
92 class RenderThreadImplForTest
: public RenderThreadImpl
{
94 RenderThreadImplForTest(const InProcessChildThreadParams
& params
,
95 scoped_ptr
<scheduler::RendererScheduler
> scheduler
,
96 scoped_refptr
<TestTaskCounter
> test_task_counter
)
97 : RenderThreadImpl(params
, scheduler
.Pass()),
98 test_task_counter_(test_task_counter
) {}
100 ~RenderThreadImplForTest() override
{}
102 void SetResourceDispatchTaskQueue(
103 const scoped_refptr
<base::SingleThreadTaskRunner
>&) override
{
104 // Use our TestTaskCounter instead.
105 RenderThreadImpl::SetResourceDispatchTaskQueue(test_task_counter_
);
108 using ChildThreadImpl::OnMessageReceived
;
111 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
114 #if defined(COMPILER_MSVC)
118 void QuitTask(base::MessageLoop
* message_loop
) {
119 message_loop
->QuitWhenIdle();
122 class QuitOnTestMsgFilter
: public IPC::MessageFilter
{
124 explicit QuitOnTestMsgFilter(base::MessageLoop
* message_loop
)
125 : message_loop_(message_loop
) {}
127 // IPC::MessageFilter overrides:
128 bool OnMessageReceived(const IPC::Message
& message
) override
{
129 message_loop_
->task_runner()->PostTask(
130 FROM_HERE
, base::Bind(&QuitTask
, message_loop_
));
134 bool GetSupportedMessageClasses(
135 std::vector
<uint32
>* supported_message_classes
) const override
{
136 supported_message_classes
->push_back(TestMsgStart
);
141 ~QuitOnTestMsgFilter() override
{}
143 base::MessageLoop
* message_loop_
;
146 class RenderThreadImplBrowserTest
: public testing::Test
{
148 void SetUp() override
{
149 content_client_
.reset(new ContentClient());
150 content_browser_client_
.reset(new ContentBrowserClient());
151 content_renderer_client_
.reset(new ContentRendererClient());
152 SetContentClient(content_client_
.get());
153 SetBrowserClientForTesting(content_browser_client_
.get());
154 SetRendererClientForTesting(content_renderer_client_
.get());
156 test_helper_
.reset(new RenderThreadImplBrowserIPCTestHelper());
158 mock_process_
.reset(new MockRenderProcess
);
159 test_task_counter_
= make_scoped_refptr(new TestTaskCounter());
161 // RenderThreadImpl expects the browser to pass these flags.
162 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
163 base::CommandLine::StringVector old_argv
= cmd
->argv();
165 cmd
->AppendSwitchASCII(switches::kNumRasterThreads
, "1");
166 cmd
->AppendSwitchASCII(switches::kContentImageTextureTarget
,
167 base::UintToString(GL_TEXTURE_2D
));
169 scoped_ptr
<scheduler::RendererScheduler
> renderer_scheduler
=
170 scheduler::RendererScheduler::Create();
171 thread_
= new RenderThreadImplForTest(
172 InProcessChildThreadParams(test_helper_
->GetChannelId(),
173 test_helper_
->GetIOTaskRunner()),
174 renderer_scheduler
.Pass(), test_task_counter_
);
175 cmd
->InitFromArgv(old_argv
);
177 thread_
->EnsureWebKitInitialized();
179 test_msg_filter_
= make_scoped_refptr(
180 new QuitOnTestMsgFilter(test_helper_
->GetMessageLoop()));
181 thread_
->AddFilter(test_msg_filter_
.get());
184 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
185 scoped_ptr
<ContentClient
> content_client_
;
186 scoped_ptr
<ContentBrowserClient
> content_browser_client_
;
187 scoped_ptr
<ContentRendererClient
> content_renderer_client_
;
188 scoped_ptr
<RenderThreadImplBrowserIPCTestHelper
> test_helper_
;
189 scoped_ptr
<MockRenderProcess
> mock_process_
;
190 scoped_refptr
<QuitOnTestMsgFilter
> test_msg_filter_
;
191 RenderThreadImplForTest
* thread_
; // Owned by mock_process_.
192 std::string channel_id_
;
195 void CheckRenderThreadInputHandlerManager(RenderThreadImpl
* thread
) {
196 ASSERT_TRUE(thread
->input_handler_manager());
199 // Check that InputHandlerManager outlives compositor thread because it uses
200 // raw pointers to post tasks.
201 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
202 TEST_F(RenderThreadImplBrowserTest
,
203 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread
)) {
204 ASSERT_TRUE(thread_
->input_handler_manager());
206 thread_
->compositor_task_runner()->PostTask(
207 FROM_HERE
, base::Bind(&CheckRenderThreadInputHandlerManager
, thread_
));
210 // Disabled under LeakSanitizer due to memory leaks.
211 TEST_F(RenderThreadImplBrowserTest
,
212 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler
)) {
213 test_helper_
->Sender()->Send(new ResourceHostMsg_FollowRedirect(0));
214 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
216 test_helper_
->GetMessageLoop()->Run();
217 EXPECT_EQ(1, test_task_counter_
->NumTasksPosted());
220 // Disabled under LeakSanitizer due to memory leaks.
221 TEST_F(RenderThreadImplBrowserTest
,
222 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler
)) {
223 // NOTE other than not being a resource message, the actual message is
225 test_helper_
->Sender()->Send(new WebSocketMsg_NotifyFailure(1, ""));
226 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
228 test_helper_
->GetMessageLoop()->Run();
230 EXPECT_EQ(0, test_task_counter_
->NumTasksPosted());
234 } // namespace content