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/memory/discardable_memory.h"
8 #include "base/memory/scoped_vector.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/common/in_process_child_thread_params.h"
12 #include "content/common/resource_messages.h"
13 #include "content/common/websocket_messages.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/common/content_client.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/renderer/content_renderer_client.h"
18 #include "content/renderer/render_process_impl.h"
19 #include "content/renderer/render_thread_impl.h"
20 #include "content/test/mock_render_process.h"
21 #include "content/test/render_thread_impl_browser_test_ipc_helper.h"
22 #include "gpu/GLES2/gl2extchromium.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 // IPC messages for testing ----------------------------------------------------
27 #define IPC_MESSAGE_IMPL
28 #include "ipc/ipc_message_macros.h"
30 #undef IPC_MESSAGE_START
31 #define IPC_MESSAGE_START TestMsgStart
32 IPC_MESSAGE_CONTROL0(TestMsg_QuitRunLoop
)
34 // -----------------------------------------------------------------------------
36 // These tests leak memory, this macro disables the test when under the
39 #define WILL_LEAK(NAME) DISABLED_##NAME
41 #define WILL_LEAK(NAME) NAME
47 // FIXME: It would be great if there was a reusable mock SingleThreadTaskRunner
48 class TestTaskCounter
: public base::SingleThreadTaskRunner
{
50 TestTaskCounter() : count_(0) {}
52 // SingleThreadTaskRunner implementation.
53 bool PostDelayedTask(const tracked_objects::Location
&,
55 base::TimeDelta
) override
{
56 base::AutoLock
auto_lock(lock_
);
61 bool PostNonNestableDelayedTask(const tracked_objects::Location
&,
63 base::TimeDelta
) override
{
64 base::AutoLock
auto_lock(lock_
);
69 bool RunsTasksOnCurrentThread() const override
{ return true; }
71 int NumTasksPosted() const {
72 base::AutoLock
auto_lock(lock_
);
77 ~TestTaskCounter() override
{}
79 mutable base::Lock lock_
;
83 #if defined(COMPILER_MSVC)
84 // See explanation for other RenderViewHostImpl which is the same issue.
86 #pragma warning(disable: 4250)
89 class RenderThreadImplForTest
: public RenderThreadImpl
{
91 RenderThreadImplForTest(const InProcessChildThreadParams
& params
,
92 scoped_refptr
<TestTaskCounter
> test_task_counter
)
93 : RenderThreadImpl(params
), test_task_counter_(test_task_counter
) {}
95 ~RenderThreadImplForTest() override
{}
97 void SetResourceDispatchTaskQueue(
98 const scoped_refptr
<base::SingleThreadTaskRunner
>&) override
{
99 // Use our TestTaskCounter instead.
100 RenderThreadImpl::SetResourceDispatchTaskQueue(test_task_counter_
);
103 using ChildThreadImpl::OnMessageReceived
;
106 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
109 #if defined(COMPILER_MSVC)
113 void QuitTask(base::MessageLoop
* message_loop
) {
114 message_loop
->QuitWhenIdle();
117 class QuitOnTestMsgFilter
: public IPC::MessageFilter
{
119 explicit QuitOnTestMsgFilter(base::MessageLoop
* message_loop
)
120 : message_loop_(message_loop
) {}
122 // IPC::MessageFilter overrides:
123 bool OnMessageReceived(const IPC::Message
& message
) override
{
124 message_loop_
->PostTask(FROM_HERE
, base::Bind(&QuitTask
, message_loop_
));
128 bool GetSupportedMessageClasses(
129 std::vector
<uint32
>* supported_message_classes
) const override
{
130 supported_message_classes
->push_back(TestMsgStart
);
135 ~QuitOnTestMsgFilter() override
{}
137 base::MessageLoop
* message_loop_
;
140 class RenderThreadImplBrowserTest
: public testing::Test
{
142 void SetUp() override
{
143 content_client_
.reset(new ContentClient());
144 content_browser_client_
.reset(new ContentBrowserClient());
145 content_renderer_client_
.reset(new ContentRendererClient());
146 SetContentClient(content_client_
.get());
147 SetBrowserClientForTesting(content_browser_client_
.get());
148 SetRendererClientForTesting(content_renderer_client_
.get());
150 test_helper_
.reset(new RenderThreadImplBrowserIPCTestHelper());
152 mock_process_
.reset(new MockRenderProcess
);
153 test_task_counter_
= make_scoped_refptr(new TestTaskCounter());
155 // RenderThreadImpl expects the browser to pass these flags.
156 base::CommandLine
* cmd
= base::CommandLine::ForCurrentProcess();
157 base::CommandLine::StringVector old_argv
= cmd
->argv();
159 cmd
->AppendSwitchASCII(switches::kNumRasterThreads
, "1");
160 cmd
->AppendSwitchASCII(switches::kUseImageTextureTarget
,
161 base::UintToString(GL_TEXTURE_2D
));
163 thread_
= new RenderThreadImplForTest(
164 InProcessChildThreadParams(test_helper_
->GetChannelId(),
165 test_helper_
->GetIOTaskRunner()),
167 cmd
->InitFromArgv(old_argv
);
169 thread_
->EnsureWebKitInitialized();
171 test_msg_filter_
= make_scoped_refptr(
172 new QuitOnTestMsgFilter(test_helper_
->GetMessageLoop()));
173 thread_
->AddFilter(test_msg_filter_
.get());
176 scoped_refptr
<TestTaskCounter
> test_task_counter_
;
177 scoped_ptr
<ContentClient
> content_client_
;
178 scoped_ptr
<ContentBrowserClient
> content_browser_client_
;
179 scoped_ptr
<ContentRendererClient
> content_renderer_client_
;
180 scoped_ptr
<RenderThreadImplBrowserIPCTestHelper
> test_helper_
;
181 scoped_ptr
<MockRenderProcess
> mock_process_
;
182 scoped_refptr
<QuitOnTestMsgFilter
> test_msg_filter_
;
183 RenderThreadImplForTest
* thread_
; // Owned by mock_process_.
184 std::string channel_id_
;
187 void CheckRenderThreadInputHandlerManager(RenderThreadImpl
* thread
) {
188 ASSERT_TRUE(thread
->input_handler_manager());
191 // Check that InputHandlerManager outlives compositor thread because it uses
192 // raw pointers to post tasks.
193 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
194 TEST_F(RenderThreadImplBrowserTest
,
195 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread
)) {
196 ASSERT_TRUE(thread_
->input_handler_manager());
198 thread_
->compositor_message_loop_proxy()->PostTask(
199 FROM_HERE
, base::Bind(&CheckRenderThreadInputHandlerManager
, thread_
));
202 // Disabled under LeakSanitizer due to memory leaks.
203 TEST_F(RenderThreadImplBrowserTest
,
204 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler
)) {
205 test_helper_
->Sender()->Send(new ResourceHostMsg_FollowRedirect(0));
206 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
208 test_helper_
->GetMessageLoop()->Run();
209 EXPECT_EQ(1, test_task_counter_
->NumTasksPosted());
212 // Disabled under LeakSanitizer due to memory leaks.
213 TEST_F(RenderThreadImplBrowserTest
,
214 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler
)) {
215 // NOTE other than not being a resource message, the actual message is
217 test_helper_
->Sender()->Send(new WebSocketMsg_NotifyFailure(1, ""));
218 test_helper_
->Sender()->Send(new TestMsg_QuitRunLoop());
220 test_helper_
->GetMessageLoop()->Run();
222 EXPECT_EQ(0, test_task_counter_
->NumTasksPosted());
226 } // namespace content