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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 const size_t kSize = 1024; | 40 const size_t kSize = 1024; |
41 | 41 |
42 TEST_P(DiscardableMemoryTest, IsNamed) { | 42 TEST_P(DiscardableMemoryTest, IsNamed) { |
43 std::string type_name(DiscardableMemory::GetTypeName(GetParam())); | 43 std::string type_name(DiscardableMemory::GetTypeName(GetParam())); |
44 EXPECT_NE("unknown", type_name); | 44 EXPECT_NE("unknown", type_name); |
45 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name)); | 45 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name)); |
46 } | 46 } |
47 | 47 |
48 bool IsNativeType(DiscardableMemoryType type) { | 48 bool IsNativeType(DiscardableMemoryType type) { |
49 return | 49 return |
50 type == DISCARDABLE_MEMORY_TYPE_ASHMEM || | 50 type == DISCARDABLE_MEMORY_TYPE_ANDROID || |
51 type == DISCARDABLE_MEMORY_TYPE_MAC; | 51 type == DISCARDABLE_MEMORY_TYPE_MAC; |
52 } | 52 } |
53 | 53 |
54 TEST_P(DiscardableMemoryTest, SupportedNatively) { | 54 TEST_P(DiscardableMemoryTest, SupportedNatively) { |
55 std::vector<DiscardableMemoryType> supported_types; | 55 std::vector<DiscardableMemoryType> supported_types; |
56 DiscardableMemory::GetSupportedTypes(&supported_types); | 56 DiscardableMemory::GetSupportedTypes(&supported_types); |
57 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) | 57 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) |
58 EXPECT_NE(0, std::count_if(supported_types.begin(), | 58 EXPECT_NE(0, std::count_if(supported_types.begin(), |
59 supported_types.end(), | 59 supported_types.end(), |
60 IsNativeType)); | 60 IsNativeType)); |
(...skipping 23 matching lines...) Expand all Loading... |
84 | 84 |
85 memory->Unlock(); | 85 memory->Unlock(); |
86 } | 86 } |
87 | 87 |
88 // Test delete a discardable memory while it is locked. | 88 // Test delete a discardable memory while it is locked. |
89 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { | 89 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { |
90 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | 90 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
91 ASSERT_TRUE(memory); | 91 ASSERT_TRUE(memory); |
92 } | 92 } |
93 | 93 |
| 94 #if !defined(OS_ANDROID) |
94 // Test forced purging. | 95 // Test forced purging. |
95 TEST_P(DiscardableMemoryTest, Purge) { | 96 TEST_P(DiscardableMemoryTest, Purge) { |
| 97 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
| 98 |
96 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | 99 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
97 ASSERT_TRUE(memory); | 100 ASSERT_TRUE(memory); |
98 memory->Unlock(); | 101 memory->Unlock(); |
99 | 102 |
100 DiscardableMemory::PurgeForTesting(); | 103 DiscardableMemory::PurgeForTesting(); |
101 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); | 104 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); |
102 } | 105 } |
| 106 #endif // !OS_ANDROID |
103 | 107 |
104 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 108 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
105 // Death tests are not supported with Android APKs. | 109 // Death tests are not supported with Android APKs. |
106 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 110 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
107 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | 111 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
108 ASSERT_TRUE(memory); | 112 ASSERT_TRUE(memory); |
109 memory->Unlock(); | 113 memory->Unlock(); |
110 ASSERT_DEATH_IF_SUPPORTED( | 114 ASSERT_DEATH_IF_SUPPORTED( |
111 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 115 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
112 } | 116 } |
113 #endif | 117 #endif |
114 | 118 |
115 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { | 119 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { |
116 std::vector<DiscardableMemoryType> supported_types; | 120 std::vector<DiscardableMemoryType> supported_types; |
117 DiscardableMemory::GetSupportedTypes(&supported_types); | 121 DiscardableMemory::GetSupportedTypes(&supported_types); |
118 return supported_types; | 122 return supported_types; |
119 } | 123 } |
120 | 124 |
121 INSTANTIATE_TEST_CASE_P( | 125 INSTANTIATE_TEST_CASE_P( |
122 DiscardableMemoryTests, | 126 DiscardableMemoryTests, |
123 DiscardableMemoryTest, | 127 DiscardableMemoryTest, |
124 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); | 128 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); |
125 | 129 |
126 } // namespace | 130 } // namespace |
127 } // namespace base | 131 } // namespace base |
OLD | NEW |