cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / renderer / render_thread_impl_browsertest.cc
blob1afb439320f7110279fe38a5896164fc2ba5e896
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/command_line.h"
6 #include "base/memory/discardable_memory.h"
7 #include "base/memory/scoped_vector.h"
8 #include "content/public/browser/content_browser_client.h"
9 #include "content/public/common/content_client.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/renderer/content_renderer_client.h"
12 #include "content/renderer/render_process_impl.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "content/test/mock_render_process.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace content {
18 namespace {
20 class RenderThreadImplBrowserTest : public testing::Test {
21 public:
22 virtual ~RenderThreadImplBrowserTest() {}
25 class DummyListener : public IPC::Listener {
26 public:
27 bool OnMessageReceived(const IPC::Message& message) override { return true; }
30 void CheckRenderThreadInputHandlerManager(RenderThreadImpl* thread) {
31 ASSERT_TRUE(thread->input_handler_manager());
34 // Check that InputHandlerManager outlives compositor thread because it uses
35 // raw pointers to post tasks.
36 // Disabled under LeakSanitizer due to memory leaks. http://crbug.com/348994
37 #if defined(LEAK_SANITIZER)
38 #define MAYBE_InputHandlerManagerDestroyedAfterCompositorThread \
39 DISABLED_InputHandlerManagerDestroyedAfterCompositorThread
40 #else
41 #define MAYBE_InputHandlerManagerDestroyedAfterCompositorThread \
42 InputHandlerManagerDestroyedAfterCompositorThread
43 #endif
44 TEST_F(RenderThreadImplBrowserTest,
45 MAYBE_InputHandlerManagerDestroyedAfterCompositorThread) {
46 ContentClient content_client;
47 ContentBrowserClient content_browser_client;
48 ContentRendererClient content_renderer_client;
49 SetContentClient(&content_client);
50 SetBrowserClientForTesting(&content_browser_client);
51 SetRendererClientForTesting(&content_renderer_client);
52 base::MessageLoopForIO message_loop_;
54 std::string channel_id = IPC::Channel::GenerateVerifiedChannelID(
55 std::string());
56 DummyListener dummy_listener;
57 scoped_ptr<IPC::Channel> channel(
58 IPC::Channel::CreateServer(channel_id, &dummy_listener));
59 ASSERT_TRUE(channel->Connect());
61 scoped_ptr<MockRenderProcess> mock_process(new MockRenderProcess);
62 // Owned by mock_process.
63 RenderThreadImpl* thread = new RenderThreadImpl(channel_id);
64 thread->EnsureWebKitInitialized();
66 ASSERT_TRUE(thread->input_handler_manager());
68 thread->compositor_message_loop_proxy()->PostTask(
69 FROM_HERE,
70 base::Bind(&CheckRenderThreadInputHandlerManager, thread));
73 // Checks that emulated discardable memory is discarded when the last widget
74 // is hidden.
75 // Disabled under LeakSanitizer due to memory leaks.
76 #if defined(LEAK_SANITIZER)
77 #define MAYBE_EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden \
78 DISABLED_EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden
79 #else
80 #define MAYBE_EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden \
81 EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden
82 #endif
83 TEST_F(RenderThreadImplBrowserTest,
84 MAYBE_EmulatedDiscardableMemoryDiscardedWhenWidgetsHidden) {
85 ContentClient content_client;
86 ContentBrowserClient content_browser_client;
87 ContentRendererClient content_renderer_client;
88 SetContentClient(&content_client);
89 SetBrowserClientForTesting(&content_browser_client);
90 SetRendererClientForTesting(&content_renderer_client);
91 base::MessageLoopForIO message_loop_;
93 std::string channel_id =
94 IPC::Channel::GenerateVerifiedChannelID(std::string());
95 DummyListener dummy_listener;
96 scoped_ptr<IPC::Channel> channel(
97 IPC::Channel::CreateServer(channel_id, &dummy_listener));
98 ASSERT_TRUE(channel->Connect());
100 scoped_ptr<MockRenderProcess> mock_process(new MockRenderProcess);
101 // Owned by mock_process.
102 RenderThreadImpl* thread = new RenderThreadImpl(channel_id);
103 thread->EnsureWebKitInitialized();
104 thread->WidgetCreated();
106 // Allocate 128MB of discardable memory.
107 ScopedVector<base::DiscardableMemory> discardable_memory;
108 for (int i = 0; i < 32; ++i) {
109 discardable_memory.push_back(
110 base::DiscardableMemory::CreateLockedMemoryWithType(
111 base::DISCARDABLE_MEMORY_TYPE_EMULATED, 4 * 1024 * 1024).release());
112 ASSERT_TRUE(discardable_memory.back());
113 discardable_memory.back()->Unlock();
116 // Hide all widgets.
117 thread->WidgetHidden();
119 // Count how much memory is left, should be at most one block.
120 int blocks_left = 0;
121 for (auto iter = discardable_memory.begin(); iter != discardable_memory.end();
122 ++iter) {
123 if ((*iter)->Lock() == base::DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS)
124 ++blocks_left;
126 EXPECT_LE(blocks_left, 1);
128 thread->WidgetDestroyed();
131 } // namespace
132 } // namespace content