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_ANDROID || | 50 type == DISCARDABLE_MEMORY_TYPE_ASHMEM || |
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) | |
95 // Test forced purging. | 94 // Test forced purging. |
96 TEST_P(DiscardableMemoryTest, Purge) { | 95 TEST_P(DiscardableMemoryTest, Purge) { |
97 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | |
98 | |
99 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | 96 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
100 ASSERT_TRUE(memory); | 97 ASSERT_TRUE(memory); |
101 memory->Unlock(); | 98 memory->Unlock(); |
102 | 99 |
103 DiscardableMemory::PurgeForTesting(); | 100 DiscardableMemory::PurgeForTesting(); |
104 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); | 101 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); |
105 } | 102 } |
106 #endif // !OS_ANDROID | |
107 | 103 |
108 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 104 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
109 // Death tests are not supported with Android APKs. | 105 // Death tests are not supported with Android APKs. |
110 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 106 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
111 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | 107 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
112 ASSERT_TRUE(memory); | 108 ASSERT_TRUE(memory); |
113 memory->Unlock(); | 109 memory->Unlock(); |
114 ASSERT_DEATH_IF_SUPPORTED( | 110 ASSERT_DEATH_IF_SUPPORTED( |
115 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 111 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
116 } | 112 } |
117 #endif | 113 #endif |
118 | 114 |
119 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { | 115 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { |
120 std::vector<DiscardableMemoryType> supported_types; | 116 std::vector<DiscardableMemoryType> supported_types; |
121 DiscardableMemory::GetSupportedTypes(&supported_types); | 117 DiscardableMemory::GetSupportedTypes(&supported_types); |
122 return supported_types; | 118 return supported_types; |
123 } | 119 } |
124 | 120 |
125 INSTANTIATE_TEST_CASE_P( | 121 INSTANTIATE_TEST_CASE_P( |
126 DiscardableMemoryTests, | 122 DiscardableMemoryTests, |
127 DiscardableMemoryTest, | 123 DiscardableMemoryTest, |
128 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); | 124 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); |
129 | 125 |
130 } // namespace | 126 } // namespace |
131 } // namespace base | 127 } // namespace base |
OLD | NEW |