| Index: mojo/services/html_viewer/discardable_memory_allocator_unittest.cc
|
| diff --git a/mojo/services/html_viewer/discardable_memory_allocator_unittest.cc b/mojo/services/html_viewer/discardable_memory_allocator_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..747810b2096c6774028a842172fbff5e19ef08f6
|
| --- /dev/null
|
| +++ b/mojo/services/html_viewer/discardable_memory_allocator_unittest.cc
|
| @@ -0,0 +1,67 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "mojo/services/html_viewer/discardable_memory_allocator.h"
|
| +
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace {
|
| +
|
| +const size_t kOneKilobyte = 1024;
|
| +const size_t kAlmostOneMegabyte = 1023 * kOneKilobyte;
|
| +const size_t kOneMegabyte = 1024 * kOneKilobyte;
|
| +
|
| +} // namespace
|
| +
|
| +TEST(DiscardableMemoryAllocator, Basic) {
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk;
|
| +
|
| + {
|
| + html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte);
|
| +
|
| + // Make sure the chunk is locked when allocated. In debug mode, we will
|
| + // dcheck.
|
| + chunk = allocator.AllocateLockedDiscardableMemory(kOneKilobyte);
|
| + chunk->Unlock();
|
| +
|
| + // Make sure we can lock a chunk.
|
| + EXPECT_TRUE(chunk->Lock());
|
| + chunk->Unlock();
|
| + }
|
| +
|
| + // The chunk's backing should have disappeared with the allocator.
|
| + EXPECT_FALSE(chunk->Lock());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocator, DiscardChunks) {
|
| + html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte);
|
| +
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk_to_remove =
|
| + allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
|
| + chunk_to_remove->Unlock();
|
| +
|
| + // Allocating a second chunk should deallocate the first one due to memory
|
| + // pressure, since we only have one megabyte available.
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk_to_keep =
|
| + allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
|
| +
|
| + // Fail to get a lock because allocating the second chunk removed the first.
|
| + EXPECT_FALSE(chunk_to_remove->Lock());
|
| +}
|
| +
|
| +TEST(DiscardableMemoryAllocator, DontDiscardLiveChunks) {
|
| + html_viewer::DiscardableMemoryAllocator allocator(kOneMegabyte);
|
| +
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk_one =
|
| + allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk_two =
|
| + allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
|
| + scoped_ptr<base::DiscardableMemoryShmemChunk> chunk_three =
|
| + allocator.AllocateLockedDiscardableMemory(kAlmostOneMegabyte);
|
| +
|
| + // These accesses will fail if the underlying weak ptr has been deallocated.
|
| + EXPECT_NE(nullptr, chunk_one->Memory());
|
| + EXPECT_NE(nullptr, chunk_two->Memory());
|
| + EXPECT_NE(nullptr, chunk_three->Memory());
|
| +}
|
|
|