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) | |
11 const size_t kSize = 1024; | 10 const size_t kSize = 1024; |
12 | 11 |
| 12 TEST(DiscardableMemoryTest, SupportedNatively) { |
| 13 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) |
| 14 ASSERT_TRUE(DiscardableMemory::SupportedNatively()); |
| 15 #else |
| 16 // If we ever have a platform that decides at runtime if it can support |
| 17 // discardable memory natively, then we'll have to add a 'never supported |
| 18 // natively' define for this case. At present, if it's not always supported |
| 19 // natively, it's never supported. |
| 20 ASSERT_FALSE(DiscardableMemory::SupportedNatively()); |
| 21 #endif |
| 22 } |
| 23 |
13 // Test Lock() and Unlock() functionalities. | 24 // Test Lock() and Unlock() functionalities. |
14 TEST(DiscardableMemoryTest, LockAndUnLock) { | 25 TEST(DiscardableMemoryTest, LockAndUnLock) { |
15 ASSERT_TRUE(DiscardableMemory::Supported()); | |
16 | |
17 const scoped_ptr<DiscardableMemory> memory( | 26 const scoped_ptr<DiscardableMemory> memory( |
18 DiscardableMemory::CreateLockedMemory(kSize)); | 27 DiscardableMemory::CreateLockedMemory(kSize)); |
19 ASSERT_TRUE(memory); | 28 ASSERT_TRUE(memory); |
20 void* addr = memory->Memory(); | 29 void* addr = memory->Memory(); |
21 ASSERT_NE(static_cast<void*>(NULL), addr); | 30 ASSERT_NE(static_cast<void*>(NULL), addr); |
22 | 31 |
23 memory->Unlock(); | 32 memory->Unlock(); |
24 // The system should have no reason to purge discardable blocks in this brief | 33 // The system should have no reason to purge discardable blocks in this brief |
25 // interval, though technically speaking this might flake. | 34 // interval, though technically speaking this might flake. |
26 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); | 35 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); |
27 addr = memory->Memory(); | 36 addr = memory->Memory(); |
28 ASSERT_NE(static_cast<void*>(NULL), addr); | 37 ASSERT_NE(static_cast<void*>(NULL), addr); |
29 | 38 |
30 memory->Unlock(); | 39 memory->Unlock(); |
31 } | 40 } |
32 | 41 |
33 // Test delete a discardable memory while it is locked. | 42 // Test delete a discardable memory while it is locked. |
34 TEST(DiscardableMemoryTest, DeleteWhileLocked) { | 43 TEST(DiscardableMemoryTest, DeleteWhileLocked) { |
35 ASSERT_TRUE(DiscardableMemory::Supported()); | |
36 | |
37 const scoped_ptr<DiscardableMemory> memory( | 44 const scoped_ptr<DiscardableMemory> memory( |
38 DiscardableMemory::CreateLockedMemory(kSize)); | 45 DiscardableMemory::CreateLockedMemory(kSize)); |
39 ASSERT_TRUE(memory); | 46 ASSERT_TRUE(memory); |
40 } | 47 } |
41 | 48 |
42 #if defined(OS_MACOSX) | 49 #if !defined(OS_ANDROID) |
43 // Test forced purging. | 50 // Test forced purging. |
44 TEST(DiscardableMemoryTest, Purge) { | 51 TEST(DiscardableMemoryTest, Purge) { |
45 ASSERT_TRUE(DiscardableMemory::Supported()); | |
46 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | 52 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
47 | 53 |
48 const scoped_ptr<DiscardableMemory> memory( | 54 const scoped_ptr<DiscardableMemory> memory( |
49 DiscardableMemory::CreateLockedMemory(kSize)); | 55 DiscardableMemory::CreateLockedMemory(kSize)); |
50 ASSERT_TRUE(memory); | 56 ASSERT_TRUE(memory); |
51 memory->Unlock(); | 57 memory->Unlock(); |
52 | 58 |
53 DiscardableMemory::PurgeForTesting(); | 59 DiscardableMemory::PurgeForTesting(); |
54 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); | 60 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); |
55 } | 61 } |
56 #endif // OS_MACOSX | 62 #endif // !OS_ANDROID |
57 | 63 |
58 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 64 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
59 // Death tests are not supported with Android APKs. | 65 // Death tests are not supported with Android APKs. |
60 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 66 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
61 const scoped_ptr<DiscardableMemory> memory( | 67 const scoped_ptr<DiscardableMemory> memory( |
62 DiscardableMemory::CreateLockedMemory(kSize)); | 68 DiscardableMemory::CreateLockedMemory(kSize)); |
63 ASSERT_TRUE(memory); | 69 ASSERT_TRUE(memory); |
64 memory->Unlock(); | 70 memory->Unlock(); |
65 ASSERT_DEATH_IF_SUPPORTED( | 71 ASSERT_DEATH_IF_SUPPORTED( |
66 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 72 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
67 } | 73 } |
68 #endif | 74 #endif |
69 | 75 |
70 #endif // OS_* | |
71 | |
72 } | 76 } |
OLD | NEW |