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 <algorithm> |
| 6 #if defined(OS_ANDROID) |
| 7 #include <limits> |
| 8 #endif |
| 9 |
5 #include "base/memory/discardable_memory.h" | 10 #include "base/memory/discardable_memory.h" |
6 | |
7 #include <limits> | |
8 | |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
| 14 namespace { |
| 15 |
| 16 class DiscardableMemoryTest |
| 17 : public testing::TestWithParam<DiscardableMemoryType> { |
| 18 public: |
| 19 // Overridden from testing::Test: |
| 20 virtual void SetUp() OVERRIDE { |
| 21 old_type_ = DiscardableMemory::GetType(); |
| 22 DiscardableMemory::SetType(GetParam()); |
| 23 } |
| 24 virtual void TearDown() OVERRIDE { |
| 25 DiscardableMemory::SetType(old_type_); |
| 26 } |
| 27 |
| 28 private: |
| 29 DiscardableMemoryType old_type_; |
| 30 }; |
12 | 31 |
13 const size_t kSize = 1024; | 32 const size_t kSize = 1024; |
14 | 33 |
15 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
16 TEST(DiscardableMemoryTest, TooLargeAllocationFails) { | 35 TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) { |
| 36 if (GetParam() != DISCARDABLE_MEMORY_TYPE_ANDROID) |
| 37 return; |
17 const size_t kPageSize = 4096; | 38 const size_t kPageSize = 4096; |
18 const size_t max_allowed_allocation_size = | 39 const size_t max_allowed_allocation_size = |
19 std::numeric_limits<size_t>::max() - kPageSize + 1; | 40 std::numeric_limits<size_t>::max() - kPageSize + 1; |
20 scoped_ptr<DiscardableMemory> memory( | 41 scoped_ptr<DiscardableMemory> memory( |
21 DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size + 1)); | 42 DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size + 1)); |
22 // On certain platforms (e.g. Android), page-alignment would have caused an | 43 // On certain platforms (e.g. Android), page-alignment would have caused an |
23 // overflow resulting in a small allocation if the input size wasn't checked | 44 // overflow resulting in a small allocation if the input size wasn't checked |
24 // correctly. | 45 // correctly. |
25 ASSERT_FALSE(memory); | 46 ASSERT_FALSE(memory); |
26 } | 47 } |
27 #endif | 48 #endif |
28 | 49 |
29 TEST(DiscardableMemoryTest, SupportedNatively) { | 50 bool IsNativeType(DiscardableMemoryType type) { |
| 51 return |
| 52 type == DISCARDABLE_MEMORY_TYPE_ANDROID || |
| 53 type == DISCARDABLE_MEMORY_TYPE_MAC; |
| 54 } |
| 55 |
| 56 TEST_P(DiscardableMemoryTest, SupportedNatively) { |
| 57 std::vector<DiscardableMemoryType> supported_types; |
| 58 DiscardableMemory::GetSupportedTypes(&supported_types); |
30 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) | 59 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) |
31 ASSERT_TRUE(DiscardableMemory::SupportedNatively()); | 60 EXPECT_NE(0u, std::count_if(supported_types.begin(), |
| 61 supported_types.end(), |
| 62 IsNativeType)); |
32 #else | 63 #else |
33 // If we ever have a platform that decides at runtime if it can support | 64 // If we ever have a platform that decides at runtime if it can support |
34 // discardable memory natively, then we'll have to add a 'never supported | 65 // discardable memory natively, then we'll have to add a 'never supported |
35 // natively' define for this case. At present, if it's not always supported | 66 // natively' define for this case. At present, if it's not always supported |
36 // natively, it's never supported. | 67 // natively, it's never supported. |
37 ASSERT_FALSE(DiscardableMemory::SupportedNatively()); | 68 EXPECT_EQ(0u, std::count_if(supported_types.begin(), |
| 69 supported_types.end(), |
| 70 IsNativeType)); |
38 #endif | 71 #endif |
39 } | 72 } |
40 | 73 |
41 // Test Lock() and Unlock() functionalities. | 74 // Test Lock() and Unlock() functionalities. |
42 TEST(DiscardableMemoryTest, LockAndUnLock) { | 75 TEST_P(DiscardableMemoryTest, LockAndUnLock) { |
43 const scoped_ptr<DiscardableMemory> memory( | 76 const scoped_ptr<DiscardableMemory> memory( |
44 DiscardableMemory::CreateLockedMemory(kSize)); | 77 DiscardableMemory::CreateLockedMemory(kSize)); |
45 ASSERT_TRUE(memory); | 78 ASSERT_TRUE(memory); |
46 void* addr = memory->Memory(); | 79 void* addr = memory->Memory(); |
47 ASSERT_NE(static_cast<void*>(NULL), addr); | 80 ASSERT_NE(static_cast<void*>(NULL), addr); |
48 | 81 |
49 memory->Unlock(); | 82 memory->Unlock(); |
50 // The system should have no reason to purge discardable blocks in this brief | 83 // The system should have no reason to purge discardable blocks in this brief |
51 // interval, though technically speaking this might flake. | 84 // interval, though technically speaking this might flake. |
52 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); | 85 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock()); |
53 addr = memory->Memory(); | 86 addr = memory->Memory(); |
54 ASSERT_NE(static_cast<void*>(NULL), addr); | 87 ASSERT_NE(static_cast<void*>(NULL), addr); |
55 | 88 |
56 memory->Unlock(); | 89 memory->Unlock(); |
57 } | 90 } |
58 | 91 |
59 // Test delete a discardable memory while it is locked. | 92 // Test delete a discardable memory while it is locked. |
60 TEST(DiscardableMemoryTest, DeleteWhileLocked) { | 93 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { |
61 const scoped_ptr<DiscardableMemory> memory( | 94 const scoped_ptr<DiscardableMemory> memory( |
62 DiscardableMemory::CreateLockedMemory(kSize)); | 95 DiscardableMemory::CreateLockedMemory(kSize)); |
63 ASSERT_TRUE(memory); | 96 ASSERT_TRUE(memory); |
64 } | 97 } |
65 | 98 |
66 #if !defined(OS_ANDROID) | 99 #if !defined(OS_ANDROID) |
67 // Test forced purging. | 100 // Test forced purging. |
68 TEST(DiscardableMemoryTest, Purge) { | 101 TEST_P(DiscardableMemoryTest, Purge) { |
69 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | 102 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
70 | 103 |
71 const scoped_ptr<DiscardableMemory> memory( | 104 const scoped_ptr<DiscardableMemory> memory( |
72 DiscardableMemory::CreateLockedMemory(kSize)); | 105 DiscardableMemory::CreateLockedMemory(kSize)); |
73 ASSERT_TRUE(memory); | 106 ASSERT_TRUE(memory); |
74 memory->Unlock(); | 107 memory->Unlock(); |
75 | 108 |
76 DiscardableMemory::PurgeForTesting(); | 109 DiscardableMemory::PurgeForTesting(); |
77 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); | 110 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); |
78 } | 111 } |
79 #endif // !OS_ANDROID | 112 #endif // !OS_ANDROID |
80 | 113 |
81 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 114 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
82 // Death tests are not supported with Android APKs. | 115 // Death tests are not supported with Android APKs. |
83 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 116 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
84 const scoped_ptr<DiscardableMemory> memory( | 117 const scoped_ptr<DiscardableMemory> memory( |
85 DiscardableMemory::CreateLockedMemory(kSize)); | 118 DiscardableMemory::CreateLockedMemory(kSize)); |
86 ASSERT_TRUE(memory); | 119 ASSERT_TRUE(memory); |
87 memory->Unlock(); | 120 memory->Unlock(); |
88 ASSERT_DEATH_IF_SUPPORTED( | 121 ASSERT_DEATH_IF_SUPPORTED( |
89 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 122 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
90 } | 123 } |
91 #endif | 124 #endif |
92 | 125 |
| 126 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { |
| 127 std::vector<DiscardableMemoryType> supported_types; |
| 128 DiscardableMemory::GetSupportedTypes(&supported_types); |
| 129 return supported_types; |
93 } | 130 } |
| 131 |
| 132 INSTANTIATE_TEST_CASE_P( |
| 133 DiscardableMemoryTests, |
| 134 DiscardableMemoryTest, |
| 135 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); |
| 136 |
| 137 } // namespace |
| 138 } // namespace base |
OLD | NEW |