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
3 // found in the LICENSE file.
5 #include "base/memory/discardable_memory.h"
9 #include "base/run_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #if defined(OS_ANDROID)
19 class DiscardableMemoryTest
20 : public testing::TestWithParam
<DiscardableMemoryType
> {
22 DiscardableMemoryTest() : message_loop_(MessageLoop::TYPE_IO
) {
23 // Register memory pressure listeners now that we have a message loop.
24 DiscardableMemory::RegisterMemoryPressureListeners();
26 virtual ~DiscardableMemoryTest() {
27 DiscardableMemory::UnregisterMemoryPressureListeners();
31 scoped_ptr
<DiscardableMemory
> CreateLockedMemory(size_t size
) {
32 return DiscardableMemory::CreateLockedMemoryWithType(
33 GetParam(), size
).Pass();
37 MessageLoop message_loop_
;
40 const size_t kSize
= 1024;
42 TEST_P(DiscardableMemoryTest
, IsNamed
) {
43 std::string
type_name(DiscardableMemory::GetTypeName(GetParam()));
44 EXPECT_NE("unknown", type_name
);
45 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name
));
48 bool IsNativeType(DiscardableMemoryType type
) {
50 type
== DISCARDABLE_MEMORY_TYPE_ASHMEM
||
51 type
== DISCARDABLE_MEMORY_TYPE_MAC
;
54 TEST_P(DiscardableMemoryTest
, SupportedNatively
) {
55 std::vector
<DiscardableMemoryType
> supported_types
;
56 DiscardableMemory::GetSupportedTypes(&supported_types
);
57 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
58 EXPECT_NE(0, std::count_if(supported_types
.begin(),
59 supported_types
.end(),
62 // If we ever have a platform that decides at runtime if it can support
63 // discardable memory natively, then we'll have to add a 'never supported
64 // natively' define for this case. At present, if it's not always supported
65 // natively, it's never supported.
66 EXPECT_EQ(0, std::count_if(supported_types
.begin(),
67 supported_types
.end(),
72 // Test Lock() and Unlock() functionalities.
73 TEST_P(DiscardableMemoryTest
, LockAndUnLock
) {
74 const scoped_ptr
<DiscardableMemory
> memory(CreateLockedMemory(kSize
));
76 void* addr
= memory
->Memory();
77 ASSERT_NE(static_cast<void*>(NULL
), addr
);
81 EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED
, memory
->Lock());
82 addr
= memory
->Memory();
83 ASSERT_NE(static_cast<void*>(NULL
), addr
);
88 // Test delete a discardable memory while it is locked.
89 TEST_P(DiscardableMemoryTest
, DeleteWhileLocked
) {
90 const scoped_ptr
<DiscardableMemory
> memory(CreateLockedMemory(kSize
));
94 // Test forced purging.
95 TEST_P(DiscardableMemoryTest
, Purge
) {
96 const scoped_ptr
<DiscardableMemory
> memory(CreateLockedMemory(kSize
));
100 DiscardableMemory::PurgeForTesting();
101 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
, memory
->Lock());
104 #if !defined(NDEBUG) && !defined(OS_ANDROID)
105 // Death tests are not supported with Android APKs.
106 TEST_P(DiscardableMemoryTest
, UnlockedMemoryAccessCrashesInDebugMode
) {
107 const scoped_ptr
<DiscardableMemory
> memory(CreateLockedMemory(kSize
));
110 ASSERT_DEATH_IF_SUPPORTED(
111 { *static_cast<int*>(memory
->Memory()) = 0xdeadbeef; }, ".*");
115 std::vector
<DiscardableMemoryType
> GetSupportedDiscardableMemoryTypes() {
116 std::vector
<DiscardableMemoryType
> supported_types
;
117 DiscardableMemory::GetSupportedTypes(&supported_types
);
118 return supported_types
;
121 INSTANTIATE_TEST_CASE_P(
122 DiscardableMemoryTests
,
123 DiscardableMemoryTest
,
124 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes()));