Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / child_discardable_shared_memory_manager_browsertest.cc
blob1764cb045c6060ccbcd689317d43111ce93255bc
1 // Copyright 2015 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/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/discardable_memory.h"
8 #include "content/child/child_discardable_shared_memory_manager.h"
9 #include "content/child/child_thread_impl.h"
10 #include "content/common/host_discardable_shared_memory_manager.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/shell/browser/shell.h"
15 #include "url/gurl.h"
17 namespace content {
19 class ChildDiscardableSharedMemoryManagerBrowserTest
20 : public ContentBrowserTest {
21 public:
22 void SetUpCommandLine(base::CommandLine* command_line) override {
23 command_line->AppendSwitch(switches::kSingleProcess);
26 static void ReleaseFreeMemory() {
27 ChildThreadImpl::current()
28 ->discardable_shared_memory_manager()
29 ->ReleaseFreeMemory();
32 static void AllocateLockedMemory(
33 size_t size,
34 scoped_ptr<base::DiscardableMemory>* memory) {
35 *memory = ChildThreadImpl::current()
36 ->discardable_shared_memory_manager()
37 ->AllocateLockedDiscardableMemory(size);
40 static void LockMemory(base::DiscardableMemory* memory, bool* result) {
41 *result = memory->Lock();
44 static void UnlockMemory(base::DiscardableMemory* memory) {
45 memory->Unlock();
48 static void FreeMemory(scoped_ptr<base::DiscardableMemory> memory) {}
51 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
52 DISABLED_LockMemory) {
53 const size_t kSize = 1024 * 1024; // 1MiB.
55 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
57 scoped_ptr<base::DiscardableMemory> memory;
58 PostTaskToInProcessRendererAndWait(base::Bind(
59 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
60 kSize, &memory));
62 ASSERT_TRUE(memory);
63 void* addr = memory->Memory();
64 ASSERT_NE(nullptr, addr);
66 PostTaskToInProcessRendererAndWait(
67 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
68 memory.get()));
70 // Purge all unlocked memory.
71 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
73 bool result = true;
74 PostTaskToInProcessRendererAndWait(
75 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::LockMemory,
76 memory.get(), &result));
78 // Should fail as memory should have been purged.
79 EXPECT_FALSE(result);
81 PostTaskToInProcessRendererAndWait(
82 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory,
83 base::Passed(&memory)));
86 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
87 DISABLED_AddressSpace) {
88 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB.
89 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total.
91 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
93 scoped_ptr<base::DiscardableMemory> instances[kNumberOfInstances];
94 for (auto& memory : instances) {
95 PostTaskToInProcessRendererAndWait(base::Bind(
96 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
97 kLargeSize, &memory));
98 ASSERT_TRUE(memory);
99 void* addr = memory->Memory();
100 ASSERT_NE(nullptr, addr);
101 PostTaskToInProcessRendererAndWait(base::Bind(
102 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
103 memory.get()));
106 for (auto& memory : instances) {
107 PostTaskToInProcessRendererAndWait(
108 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory,
109 base::Passed(&memory)));
113 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
114 DISABLED_ReleaseFreeMemory) {
115 const size_t kSize = 1024 * 1024; // 1MiB.
117 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
119 scoped_ptr<base::DiscardableMemory> memory;
120 PostTaskToInProcessRendererAndWait(base::Bind(
121 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
122 kSize, &memory));
123 PostTaskToInProcessRendererAndWait(
124 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
125 memory.get()));
126 PostTaskToInProcessRendererAndWait(
127 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory,
128 base::Passed(&memory)));
130 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),
131 kSize);
133 PostTaskToInProcessRendererAndWait(base::Bind(
134 &ChildDiscardableSharedMemoryManagerBrowserTest::ReleaseFreeMemory));
136 EXPECT_EQ(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),
137 0u);
140 } // content