OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_memory.h" | 5 #include "base/memory/discardable_memory.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
7 | 7 |
8 namespace base { | 8 namespace base { |
9 | 9 |
10 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 10 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 11 const size_t kSize = 1024; |
| 12 |
11 // Test Lock() and Unlock() functionalities. | 13 // Test Lock() and Unlock() functionalities. |
12 TEST(DiscardableMemoryTest, LockAndUnLock) { | 14 TEST(DiscardableMemoryTest, LockAndUnLock) { |
13 ASSERT_TRUE(DiscardableMemory::Supported()); | 15 ASSERT_TRUE(DiscardableMemory::Supported()); |
14 | 16 |
15 const size_t size = 1024; | 17 const scoped_ptr<DiscardableMemory> memory( |
16 | 18 DiscardableMemory::CreateLockedMemory(kSize)); |
17 DiscardableMemory memory; | 19 ASSERT_TRUE(memory); |
18 ASSERT_TRUE(memory.InitializeAndLock(size)); | 20 void* addr = memory->Memory(); |
19 void* addr = memory.Memory(); | |
20 ASSERT_NE(static_cast<void*>(NULL), addr); | 21 ASSERT_NE(static_cast<void*>(NULL), addr); |
21 | 22 |
22 memory.Unlock(); | 23 memory->Unlock(); |
23 // The system should have no reason to purge discardable blocks in this brief | 24 // The system should have no reason to purge discardable blocks in this brief |
24 // interval, though technically speaking this might flake. | 25 // interval, though technically speaking this might flake. |
25 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory.Lock()); | 26 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); |
26 addr = memory.Memory(); | 27 addr = memory->Memory(); |
27 ASSERT_NE(static_cast<void*>(NULL), addr); | 28 ASSERT_NE(static_cast<void*>(NULL), addr); |
28 | 29 |
29 memory.Unlock(); | 30 memory->Unlock(); |
30 } | 31 } |
31 | 32 |
32 // Test delete a discardable memory while it is locked. | 33 // Test delete a discardable memory while it is locked. |
33 TEST(DiscardableMemoryTest, DeleteWhileLocked) { | 34 TEST(DiscardableMemoryTest, DeleteWhileLocked) { |
34 ASSERT_TRUE(DiscardableMemory::Supported()); | 35 ASSERT_TRUE(DiscardableMemory::Supported()); |
35 | 36 |
36 const size_t size = 1024; | 37 const scoped_ptr<DiscardableMemory> memory( |
37 | 38 DiscardableMemory::CreateLockedMemory(kSize)); |
38 DiscardableMemory memory; | 39 ASSERT_TRUE(memory); |
39 ASSERT_TRUE(memory.InitializeAndLock(size)); | |
40 } | 40 } |
41 | 41 |
42 #if defined(OS_MACOSX) | 42 #if defined(OS_MACOSX) |
43 // Test forced purging. | 43 // Test forced purging. |
44 TEST(DiscardableMemoryTest, Purge) { | 44 TEST(DiscardableMemoryTest, Purge) { |
45 ASSERT_TRUE(DiscardableMemory::Supported()); | 45 ASSERT_TRUE(DiscardableMemory::Supported()); |
46 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | 46 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
47 | 47 |
48 const size_t size = 1024; | 48 const scoped_ptr<DiscardableMemory> memory( |
49 | 49 DiscardableMemory::CreateLockedMemory(kSize)); |
50 DiscardableMemory memory; | 50 ASSERT_TRUE(memory); |
51 ASSERT_TRUE(memory.InitializeAndLock(size)); | 51 memory->Unlock(); |
52 memory.Unlock(); | |
53 | 52 |
54 DiscardableMemory::PurgeForTesting(); | 53 DiscardableMemory::PurgeForTesting(); |
55 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory.Lock()); | 54 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); |
56 } | 55 } |
57 #endif // OS_MACOSX | 56 #endif // OS_MACOSX |
58 | 57 |
| 58 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
| 59 // Death tests are not supported with Android APKs. |
| 60 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
| 61 const scoped_ptr<DiscardableMemory> memory( |
| 62 DiscardableMemory::CreateLockedMemory(kSize)); |
| 63 ASSERT_TRUE(memory); |
| 64 memory->Unlock(); |
| 65 ASSERT_DEATH({ *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
| 66 } |
| 67 #endif |
| 68 |
59 #endif // OS_* | 69 #endif // OS_* |
60 | 70 |
61 } | 71 } |
OLD | NEW |