[ServiceWorker] Implement WebServiceWorkerContextClient::openWindow().
[chromium-blink-merge.git] / content / renderer / render_thread_impl_browsertest.cc
blob39bf764cbe59c24f3771718fd8d73cc9c9215b7b
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/thread_task_runner_handle.h"
10 #include "content/common/resource_messages.h"
11 #include "content/common/websocket_messages.h"
12 #include "content/public/browser/content_browser_client.h"
13 #include "content/public/common/content_client.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/public/renderer/content_renderer_client.h"
16 #include "content/renderer/render_process_impl.h"
17 #include "content/renderer/render_thread_impl.h"
18 #include "content/test/mock_render_process.h"
19 #include "content/test/render_thread_impl_browser_test_ipc_helper.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 // IPC messages for testing ----------------------------------------------------
24 #define IPC_MESSAGE_IMPL
25 #include "ipc/ipc_message_macros.h"
27 #undef IPC_MESSAGE_START
28 #define IPC_MESSAGE_START TestMsgStart
29 IPC_MESSAGE_CONTROL0(TestMsg_QuitRunLoop)
31 // -----------------------------------------------------------------------------
33 // These tests leak memory, this macro disables the test when under the
34 // LeakSanitizer.
35 #ifdef LEAK_SANITIZER
36 #define WILL_LEAK(NAME) DISABLED_##NAME
37 #else
38 #define WILL_LEAK(NAME) NAME
39 #endif
41 namespace content {
42 namespace {
44 // FIXME: It would be great if there was a reusable mock SingleThreadTaskRunner
45 class TestTaskCounter : public base::SingleThreadTaskRunner {
46 public:
47 TestTaskCounter() : count_(0) {}
49 // SingleThreadTaskRunner implementation.
50 bool PostDelayedTask(const tracked_objects::Location&,
51 const base::Closure&,
52 base::TimeDelta) override {
53 base::AutoLock auto_lock(lock_);
54 count_++;
55 return true;
58 bool PostNonNestableDelayedTask(const tracked_objects::Location&,
59 const base::Closure&,
60 base::TimeDelta) override {
61 base::AutoLock auto_lock(lock_);
62 count_++;
63 return true;
66 bool RunsTasksOnCurrentThread() const override { return true; }
68 int NumTasksPosted() const {
69 base::AutoLock auto_lock(lock_);
70 return count_;
73 private:
74 ~TestTaskCounter() override {}
76 mutable base::Lock lock_;
77 int count_;
80 #if defined(COMPILER_MSVC)
81 // See explanation for other RenderViewHostImpl which is the same issue.
82 #pragma warning(push)
83 #pragma warning(disable: 4250)
84 #endif
86 class RenderThreadImplForTest : public RenderThreadImpl {
87 public:
88 RenderThreadImplForTest(const std::string& channel_id,
89 scoped_refptr<TestTaskCounter> test_task_counter)
90 : RenderThreadImpl(channel_id), test_task_counter_(test_task_counter) {}
92 ~RenderThreadImplForTest() override {}
94 void SetResourceDispatchTaskQueue(
95 const scoped_refptr<base::SingleThreadTaskRunner>&) override {
96 // Use our TestTaskCounter instead.
97 RenderThreadImpl::SetResourceDispatchTaskQueue(test_task_counter_);
100 using ChildThreadImpl::OnMessageReceived;
102 private:
103 scoped_refptr<TestTaskCounter> test_task_counter_;
106 #if defined(COMPILER_MSVC)
107 #pragma warning(pop)
108 #endif
110 void QuitTask(base::MessageLoop* message_loop) {
111 message_loop->QuitWhenIdle();
114 class QuitOnTestMsgFilter : public IPC::MessageFilter {
115 public:
116 explicit QuitOnTestMsgFilter(base::MessageLoop* message_loop)
117 : message_loop_(message_loop) {}
119 // IPC::MessageFilter overrides:
120 bool OnMessageReceived(const IPC::Message& message) override {
121 message_loop_->PostTask(FROM_HERE, base::Bind(&QuitTask, message_loop_));
122 return true;
125 bool GetSupportedMessageClasses(
126 std::vector<uint32>* supported_message_classes) const override {
127 supported_message_classes->push_back(TestMsgStart);
128 return true;
131 private:
132 ~QuitOnTestMsgFilter() override {}
134 base::MessageLoop* message_loop_;
137 class RenderThreadImplBrowserTest : public testing::Test {
138 public:
139 void SetUp() override {
140 content_client_.reset(new ContentClient());
141 content_browser_client_.reset(new ContentBrowserClient());
142 content_renderer_client_.reset(new ContentRendererClient());
143 SetContentClient(content_client_.get());
144 SetBrowserClientForTesting(content_browser_client_.get());
145 SetRendererClientForTesting(content_renderer_client_.get());
147 test_helper_.reset(new RenderThreadImplBrowserIPCTestHelper());
149 mock_process_.reset(new MockRenderProcess);
150 test_task_counter_ = make_scoped_refptr(new TestTaskCounter());
152 // RenderThreadImpl expects the browser to pass these flags.
153 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
154 base::CommandLine::StringVector old_argv = cmd->argv();
156 cmd->AppendSwitchASCII(switches::kNumRasterThreads, "1");
157 thread_ = new RenderThreadImplForTest(test_helper_->GetChannelId(),
158 test_task_counter_);
159 cmd->InitFromArgv(old_argv);
161 thread_->EnsureWebKitInitialized();
163 test_msg_filter_ = make_scoped_refptr(
164 new QuitOnTestMsgFilter(test_helper_->GetMessageLoop()));
165 thread_->AddFilter(test_msg_filter_.get());
168 scoped_refptr<TestTaskCounter> test_task_counter_;
169 scoped_ptr<ContentClient> content_client_;
170 scoped_ptr<ContentBrowserClient> content_browser_client_;
171 scoped_ptr<ContentRendererClient> content_renderer_client_;
172 scoped_ptr<RenderThreadImplBrowserIPCTestHelper> test_helper_;
173 scoped_ptr<MockRenderProcess> mock_process_;
174 scoped_refptr<QuitOnTestMsgFilter> test_msg_filter_;
175 RenderThreadImplForTest* thread_; // Owned by mock_process_.
176 std::string channel_id_;
179 void CheckRenderThreadInputHandlerManager(RenderThreadImpl* thread) {
180 ASSERT_TRUE(thread->input_handler_manager());
183 // Check that InputHandlerManager outlives compositor thread because it uses
184 // raw pointers to post tasks.
185 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
186 TEST_F(RenderThreadImplBrowserTest,
187 WILL_LEAK(InputHandlerManagerDestroyedAfterCompositorThread)) {
188 ASSERT_TRUE(thread_->input_handler_manager());
190 thread_->compositor_message_loop_proxy()->PostTask(
191 FROM_HERE, base::Bind(&CheckRenderThreadInputHandlerManager, thread_));
194 // Checks that emulated discardable memory is discarded when the last widget
195 // is hidden.
196 // Disabled under LeakSanitizer due to memory leaks.
197 TEST_F(RenderThreadImplBrowserTest,
198 WILL_LEAK(EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden)) {
199 thread_->WidgetCreated();
201 // Allocate 128MB of discardable memory.
202 ScopedVector<base::DiscardableMemory> discardable_memory;
203 for (int i = 0; i < 32; ++i) {
204 discardable_memory.push_back(
205 base::DiscardableMemory::CreateLockedMemoryWithType(
206 base::DISCARDABLE_MEMORY_TYPE_EMULATED, 4 * 1024 * 1024).release());
207 ASSERT_TRUE(discardable_memory.back());
208 discardable_memory.back()->Unlock();
211 // Hide all widgets.
212 thread_->WidgetHidden();
214 // Count how much memory is left, should be at most one block.
215 int blocks_left = 0;
216 for (auto iter = discardable_memory.begin(); iter != discardable_memory.end();
217 ++iter) {
218 if ((*iter)->Lock() == base::DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS)
219 ++blocks_left;
221 EXPECT_LE(blocks_left, 1);
223 thread_->WidgetDestroyed();
226 // Disabled under LeakSanitizer due to memory leaks.
227 TEST_F(RenderThreadImplBrowserTest,
228 WILL_LEAK(ResourceDispatchIPCTasksGoThroughScheduler)) {
229 test_helper_->Sender()->Send(new ResourceHostMsg_FollowRedirect(0));
230 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop());
232 test_helper_->GetMessageLoop()->Run();
233 EXPECT_EQ(1, test_task_counter_->NumTasksPosted());
236 // Disabled under LeakSanitizer due to memory leaks.
237 TEST_F(RenderThreadImplBrowserTest,
238 WILL_LEAK(NonResourceDispatchIPCTasksDontGoThroughScheduler)) {
239 // NOTE other than not being a resource message, the actual message is
240 // unimportant.
241 test_helper_->Sender()->Send(new WebSocketMsg_NotifyFailure(1, ""));
242 test_helper_->Sender()->Send(new TestMsg_QuitRunLoop());
244 test_helper_->GetMessageLoop()->Run();
246 EXPECT_EQ(0, test_task_counter_->NumTasksPosted());
249 } // namespace
250 } // namespace content