Reland of ll WebRTC 9687:9699, Libjingle 9690:9699 (patchset #1 id:1 of https://coder...
[chromium-blink-merge.git] / content / renderer / render_thread_impl_browsertest.cc
blob145a7fa4680a5a8f40409b58ca40ec1febd5591a
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
40 // LeakSanitizer.
41 #ifdef LEAK_SANITIZER
42 #define WILL_LEAK(NAME) DISABLED_##NAME
43 #else
44 #define WILL_LEAK(NAME) NAME
45 #endif
47 namespace content {
48 namespace {
50 // FIXME: It would be great if there was a reusable mock SingleThreadTaskRunner
51 class TestTaskCounter : public base::SingleThreadTaskRunner {
52 public:
53 TestTaskCounter() : count_(0) {}
55 // SingleThreadTaskRunner implementation.
56 bool PostDelayedTask(const tracked_objects::Location&,
57 const base::Closure&,
58 base::TimeDelta) override {
59 base::AutoLock auto_lock(lock_);
60 count_++;
61 return true;
64 bool PostNonNestableDelayedTask(const tracked_objects::Location&,
65 const base::Closure&,
66 base::TimeDelta) override {
67 base::AutoLock auto_lock(lock_);
68 count_++;
69 return true;
72 bool RunsTasksOnCurrentThread() const override { return true; }
74 int NumTasksPosted() const {
75 base::AutoLock auto_lock(lock_);
76 return count_;
79 private:
80 ~TestTaskCounter() override {}
82 mutable base::Lock lock_;
83 int count_;
86 #if defined(COMPILER_MSVC)
87 // See explanation for other RenderViewHostImpl which is the same issue.
88 #pragma warning(push)
89 #pragma warning(disable: 4250)
90 #endif
92 class RenderThreadImplForTest : public RenderThreadImpl {
93 public:
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;
110 private:
111 scoped_refptr<TestTaskCounter> test_task_counter_;
114 #if defined(COMPILER_MSVC)
115 #pragma warning(pop)
116 #endif
118 void QuitTask(base::MessageLoop* message_loop) {
119 message_loop->QuitWhenIdle();
122 class QuitOnTestMsgFilter : public IPC::MessageFilter {
123 public:
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_));
131 return true;
134 bool GetSupportedMessageClasses(
135 std::vector<uint32>* supported_message_classes) const override {
136 supported_message_classes->push_back(TestMsgStart);
137 return true;
140 private:
141 ~QuitOnTestMsgFilter() override {}
143 base::MessageLoop* message_loop_;
146 class RenderThreadImplBrowserTest : public testing::Test {
147 public:
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 std::string image_targets;
167 for (size_t format = 0;
168 format < static_cast<size_t>(gfx::BufferFormat::LAST) + 1; format++) {
169 if (!image_targets.empty())
170 image_targets += ",";
171 image_targets += base::UintToString(GL_TEXTURE_2D);
173 cmd->AppendSwitchASCII(switches::kContentImageTextureTarget, image_targets);
175 scoped_ptr<scheduler::RendererScheduler> renderer_scheduler =
176 scheduler::RendererScheduler::Create();
177 thread_ = new RenderThreadImplForTest(
178 InProcessChildThreadParams(test_helper_->GetChannelId(),
179 test_helper_->GetIOTaskRunner()),
180 renderer_scheduler.Pass(), test_task_counter_);
181 cmd->InitFromArgv(old_argv);
183 thread_->EnsureWebKitInitialized();
185 test_msg_filter_ = make_scoped_refptr(
186 new QuitOnTestMsgFilter(test_helper_->GetMessageLoop()));
187 thread_->AddFilter(test_msg_filter_.get());
190 scoped_refptr<TestTaskCounter> test_task_counter_;
191 scoped_ptr<ContentClient> content_client_;
192 scoped_ptr<ContentBrowserClient> content_browser_client_;
193 scoped_ptr<ContentRendererClient> content_renderer_client_;
194 scoped_ptr<RenderThreadImplBrowserIPCTestHelper> test_helper_;
195 scoped_ptr<MockRenderProcess> mock_process_;
196 scoped_refptr<QuitOnTestMsgFilter> test_msg_filter_;
197 RenderThreadImplForTest* thread_; // Owned by mock_process_.
198 std::string channel_id_;
201 void CheckRenderThreadInputHandlerManager(RenderThreadImpl* thread) {
202 ASSERT_TRUE(thread->input_handler_manager());
205 // Check that InputHandlerManager outlives compositor thread because it uses
206 // raw pointers to post tasks.
207 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
208 TEST_F(RenderThreadImplBrowserTest,
209 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread)) {
210 ASSERT_TRUE(thread_->input_handler_manager());
212 thread_->compositor_task_runner()->PostTask(
213 FROM_HERE, base::Bind(&CheckRenderThreadInputHandlerManager, thread_));
216 // Disabled under LeakSanitizer due to memory leaks.
217 TEST_F(RenderThreadImplBrowserTest,
218 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler)) {
219 test_helper_->Sender()->Send(new ResourceHostMsg_FollowRedirect(0));
220 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop());
222 test_helper_->GetMessageLoop()->Run();
223 EXPECT_EQ(1, test_task_counter_->NumTasksPosted());
226 // Disabled under LeakSanitizer due to memory leaks.
227 TEST_F(RenderThreadImplBrowserTest,
228 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler)) {
229 // NOTE other than not being a resource message, the actual message is
230 // unimportant.
231 test_helper_->Sender()->Send(new WebSocketMsg_NotifyFailure(1, ""));
232 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop());
234 test_helper_->GetMessageLoop()->Run();
236 EXPECT_EQ(0, test_task_counter_->NumTasksPosted());
239 } // namespace
240 } // namespace content