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