| 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 | 6 |
| 7 #include <limits> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 #if defined(OS_ANDROID) |
| 12 #include <limits> |
| 13 #endif |
| 14 |
| 11 namespace base { | 15 namespace base { |
| 16 namespace { |
| 17 |
| 18 class DiscardableMemoryTest |
| 19 : public testing::TestWithParam<DiscardableMemoryType> { |
| 20 protected: |
| 21 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) { |
| 22 return DiscardableMemory::CreateLockedMemoryWithType( |
| 23 GetParam(), size).Pass(); |
| 24 } |
| 25 }; |
| 12 | 26 |
| 13 const size_t kSize = 1024; | 27 const size_t kSize = 1024; |
| 14 | 28 |
| 15 TEST(DiscardableMemoryTest, SupportedNatively) { | 29 TEST_P(DiscardableMemoryTest, IsNamed) { |
| 30 std::string type_name(DiscardableMemory::GetTypeName(GetParam())); |
| 31 EXPECT_NE("unknown", type_name); |
| 32 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name)); |
| 33 } |
| 34 |
| 35 bool IsNativeType(DiscardableMemoryType type) { |
| 36 return |
| 37 type == DISCARDABLE_MEMORY_TYPE_ANDROID || |
| 38 type == DISCARDABLE_MEMORY_TYPE_MAC; |
| 39 } |
| 40 |
| 41 TEST_P(DiscardableMemoryTest, SupportedNatively) { |
| 42 std::vector<DiscardableMemoryType> supported_types; |
| 43 DiscardableMemory::GetSupportedTypes(&supported_types); |
| 16 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) | 44 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) |
| 17 ASSERT_TRUE(DiscardableMemory::SupportedNatively()); | 45 EXPECT_NE(0, std::count_if(supported_types.begin(), |
| 46 supported_types.end(), |
| 47 IsNativeType)); |
| 18 #else | 48 #else |
| 19 // If we ever have a platform that decides at runtime if it can support | 49 // If we ever have a platform that decides at runtime if it can support |
| 20 // discardable memory natively, then we'll have to add a 'never supported | 50 // discardable memory natively, then we'll have to add a 'never supported |
| 21 // natively' define for this case. At present, if it's not always supported | 51 // natively' define for this case. At present, if it's not always supported |
| 22 // natively, it's never supported. | 52 // natively, it's never supported. |
| 23 ASSERT_FALSE(DiscardableMemory::SupportedNatively()); | 53 EXPECT_EQ(0, std::count_if(supported_types.begin(), |
| 54 supported_types.end(), |
| 55 IsNativeType)); |
| 24 #endif | 56 #endif |
| 25 } | 57 } |
| 26 | 58 |
| 27 // Test Lock() and Unlock() functionalities. | 59 // Test Lock() and Unlock() functionalities. |
| 28 TEST(DiscardableMemoryTest, LockAndUnLock) { | 60 TEST_P(DiscardableMemoryTest, LockAndUnLock) { |
| 29 const scoped_ptr<DiscardableMemory> memory( | 61 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
| 30 DiscardableMemory::CreateLockedMemory(kSize)); | |
| 31 ASSERT_TRUE(memory); | 62 ASSERT_TRUE(memory); |
| 32 void* addr = memory->Memory(); | 63 void* addr = memory->Memory(); |
| 33 ASSERT_NE(static_cast<void*>(NULL), addr); | 64 ASSERT_NE(static_cast<void*>(NULL), addr); |
| 34 | 65 |
| 35 memory->Unlock(); | 66 memory->Unlock(); |
| 36 // The system should have no reason to purge discardable blocks in this brief | 67 // The system should have no reason to purge discardable blocks in this brief |
| 37 // interval, though technically speaking this might flake. | 68 // interval, though technically speaking this might flake. |
| 38 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); | 69 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock()); |
| 39 addr = memory->Memory(); | 70 addr = memory->Memory(); |
| 40 ASSERT_NE(static_cast<void*>(NULL), addr); | 71 ASSERT_NE(static_cast<void*>(NULL), addr); |
| 41 | 72 |
| 42 memory->Unlock(); | 73 memory->Unlock(); |
| 43 } | 74 } |
| 44 | 75 |
| 45 // Test delete a discardable memory while it is locked. | 76 // Test delete a discardable memory while it is locked. |
| 46 TEST(DiscardableMemoryTest, DeleteWhileLocked) { | 77 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { |
| 47 const scoped_ptr<DiscardableMemory> memory( | 78 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
| 48 DiscardableMemory::CreateLockedMemory(kSize)); | |
| 49 ASSERT_TRUE(memory); | 79 ASSERT_TRUE(memory); |
| 50 } | 80 } |
| 51 | 81 |
| 52 #if !defined(OS_ANDROID) | 82 #if !defined(OS_ANDROID) |
| 53 // Test forced purging. | 83 // Test forced purging. |
| 54 TEST(DiscardableMemoryTest, Purge) { | 84 TEST_P(DiscardableMemoryTest, Purge) { |
| 55 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | 85 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
| 56 | 86 |
| 57 const scoped_ptr<DiscardableMemory> memory( | 87 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
| 58 DiscardableMemory::CreateLockedMemory(kSize)); | |
| 59 ASSERT_TRUE(memory); | 88 ASSERT_TRUE(memory); |
| 60 memory->Unlock(); | 89 memory->Unlock(); |
| 61 | 90 |
| 62 DiscardableMemory::PurgeForTesting(); | 91 DiscardableMemory::PurgeForTesting(); |
| 63 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); | 92 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); |
| 64 } | 93 } |
| 65 #endif // !OS_ANDROID | 94 #endif // !OS_ANDROID |
| 66 | 95 |
| 67 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 96 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
| 68 // Death tests are not supported with Android APKs. | 97 // Death tests are not supported with Android APKs. |
| 69 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 98 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
| 70 const scoped_ptr<DiscardableMemory> memory( | 99 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
| 71 DiscardableMemory::CreateLockedMemory(kSize)); | |
| 72 ASSERT_TRUE(memory); | 100 ASSERT_TRUE(memory); |
| 73 memory->Unlock(); | 101 memory->Unlock(); |
| 74 ASSERT_DEATH_IF_SUPPORTED( | 102 ASSERT_DEATH_IF_SUPPORTED( |
| 75 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 103 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
| 76 } | 104 } |
| 77 #endif | 105 #endif |
| 78 | 106 |
| 107 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { |
| 108 std::vector<DiscardableMemoryType> supported_types; |
| 109 DiscardableMemory::GetSupportedTypes(&supported_types); |
| 110 return supported_types; |
| 79 } | 111 } |
| 112 |
| 113 INSTANTIATE_TEST_CASE_P( |
| 114 DiscardableMemoryTests, |
| 115 DiscardableMemoryTest, |
| 116 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); |
| 117 |
| 118 } // namespace |
| 119 } // namespace base |
| OLD | NEW |