Fix crash on app list start page contents not existing.
[chromium-blink-merge.git] / content / child / child_discardable_shared_memory_manager_browsertest.cc
blob1ffa01ef7a0093fd5e546600dd1c9104346de2c2
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 "content/child/child_discardable_shared_memory_manager.h"
8 #include "content/child/child_thread_impl.h"
9 #include "content/common/host_discardable_shared_memory_manager.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/shell/browser/shell.h"
14 #include "url/gurl.h"
16 namespace content {
18 class ChildDiscardableSharedMemoryManagerBrowserTest
19 : public ContentBrowserTest {
20 public:
21 void SetUpCommandLine(base::CommandLine* command_line) override {
22 command_line->AppendSwitch(switches::kSingleProcess);
23 command_line->AppendSwitch(switches::kDisableGpu);
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::DiscardableMemoryShmemChunk>* memory) {
35 *memory = ChildThreadImpl::current()
36 ->discardable_shared_memory_manager()
37 ->AllocateLockedDiscardableMemory(size);
40 static void LockMemory(base::DiscardableMemoryShmemChunk* memory,
41 bool* result) {
42 *result = memory->Lock();
45 static void UnlockMemory(base::DiscardableMemoryShmemChunk* memory) {
46 memory->Unlock();
49 static void FreeMemory(scoped_ptr<base::DiscardableMemoryShmemChunk> memory) {
53 #if defined(OS_WIN)
54 #define MAYBE_LockMemory DISABLED_LockMemory
55 #else
56 #define MAYBE_LockMemory LockMemory
57 #endif
59 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
60 MAYBE_LockMemory) {
61 const size_t kSize = 1024 * 1024; // 1MiB.
63 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
65 scoped_ptr<base::DiscardableMemoryShmemChunk> memory;
66 PostTaskToInProcessRendererAndWait(base::Bind(
67 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
68 kSize, &memory));
70 ASSERT_TRUE(memory);
71 void* addr = memory->Memory();
72 ASSERT_NE(nullptr, addr);
74 PostTaskToInProcessRendererAndWait(
75 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
76 memory.get()));
78 // Purge all unlocked memory.
79 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
81 bool result = true;
82 PostTaskToInProcessRendererAndWait(
83 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::LockMemory,
84 memory.get(), &result));
86 // Should fail as memory should have been purged.
87 EXPECT_FALSE(result);
89 PostTaskToInProcessRendererAndWait(
90 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory,
91 base::Passed(&memory)));
94 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest,
95 AddressSpace) {
96 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB.
97 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total.
99 NavigateToURL(shell(), GURL(url::kAboutBlankURL));
101 scoped_ptr<base::DiscardableMemoryShmemChunk> instances[kNumberOfInstances];
102 for (auto& memory : instances) {
103 PostTaskToInProcessRendererAndWait(base::Bind(
104 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory,
105 kLargeSize, &memory));
106 ASSERT_TRUE(memory);
107 void* addr = memory->Memory();
108 ASSERT_NE(nullptr, addr);
109 PostTaskToInProcessRendererAndWait(base::Bind(
110 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory,
111 memory.get()));
114 for (auto& memory : instances) {
115 PostTaskToInProcessRendererAndWait(
116 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory,
117 base::Passed(&memory)));
121 } // content