| Index: base/memory/discardable_memory_unittest.cc
|
| diff --git a/base/memory/discardable_memory_unittest.cc b/base/memory/discardable_memory_unittest.cc
|
| index c9f67b2556f17a24a1e7f65d9533403106e6c94e..ddea01fb085dde982c0dea55744561c54b2bb341 100644
|
| --- a/base/memory/discardable_memory_unittest.cc
|
| +++ b/base/memory/discardable_memory_unittest.cc
|
| @@ -2,18 +2,39 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "base/memory/discardable_memory.h"
|
| -
|
| +#include <algorithm>
|
| +#if defined(OS_ANDROID)
|
| #include <limits>
|
| +#endif
|
|
|
| +#include "base/memory/discardable_memory.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| namespace base {
|
| +namespace {
|
| +
|
| +class DiscardableMemoryTest
|
| + : public testing::TestWithParam<DiscardableMemoryType> {
|
| + public:
|
| + // Overridden from testing::Test:
|
| + virtual void SetUp() OVERRIDE {
|
| + old_type_ = DiscardableMemory::GetType();
|
| + DiscardableMemory::SetType(GetParam());
|
| + }
|
| + virtual void TearDown() OVERRIDE {
|
| + DiscardableMemory::SetType(old_type_);
|
| + }
|
| +
|
| + private:
|
| + DiscardableMemoryType old_type_;
|
| +};
|
|
|
| const size_t kSize = 1024;
|
|
|
| #if defined(OS_ANDROID)
|
| -TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| +TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| + if (GetParam() != DISCARDABLE_MEMORY_TYPE_ANDROID)
|
| + return;
|
| const size_t kPageSize = 4096;
|
| const size_t max_allowed_allocation_size =
|
| std::numeric_limits<size_t>::max() - kPageSize + 1;
|
| @@ -26,20 +47,32 @@ TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| }
|
| #endif
|
|
|
| -TEST(DiscardableMemoryTest, SupportedNatively) {
|
| +bool IsNativeType(DiscardableMemoryType type) {
|
| + return
|
| + type == DISCARDABLE_MEMORY_TYPE_ANDROID ||
|
| + type == DISCARDABLE_MEMORY_TYPE_MAC;
|
| +}
|
| +
|
| +TEST_P(DiscardableMemoryTest, SupportedNatively) {
|
| + std::vector<DiscardableMemoryType> supported_types;
|
| + DiscardableMemory::GetSupportedTypes(&supported_types);
|
| #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
|
| - ASSERT_TRUE(DiscardableMemory::SupportedNatively());
|
| + EXPECT_NE(0u, std::count_if(supported_types.begin(),
|
| + supported_types.end(),
|
| + IsNativeType));
|
| #else
|
| // If we ever have a platform that decides at runtime if it can support
|
| // discardable memory natively, then we'll have to add a 'never supported
|
| // natively' define for this case. At present, if it's not always supported
|
| // natively, it's never supported.
|
| - ASSERT_FALSE(DiscardableMemory::SupportedNatively());
|
| + EXPECT_EQ(0u, std::count_if(supported_types.begin(),
|
| + supported_types.end(),
|
| + IsNativeType));
|
| #endif
|
| }
|
|
|
| // Test Lock() and Unlock() functionalities.
|
| -TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| +TEST_P(DiscardableMemoryTest, LockAndUnLock) {
|
| const scoped_ptr<DiscardableMemory> memory(
|
| DiscardableMemory::CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| @@ -49,7 +82,7 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| memory->Unlock();
|
| // The system should have no reason to purge discardable blocks in this brief
|
| // interval, though technically speaking this might flake.
|
| - EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
|
| + EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock());
|
| addr = memory->Memory();
|
| ASSERT_NE(static_cast<void*>(NULL), addr);
|
|
|
| @@ -57,7 +90,7 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| }
|
|
|
| // Test delete a discardable memory while it is locked.
|
| -TEST(DiscardableMemoryTest, DeleteWhileLocked) {
|
| +TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
|
| const scoped_ptr<DiscardableMemory> memory(
|
| DiscardableMemory::CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| @@ -65,7 +98,7 @@ TEST(DiscardableMemoryTest, DeleteWhileLocked) {
|
|
|
| #if !defined(OS_ANDROID)
|
| // Test forced purging.
|
| -TEST(DiscardableMemoryTest, Purge) {
|
| +TEST_P(DiscardableMemoryTest, Purge) {
|
| ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
|
|
|
| const scoped_ptr<DiscardableMemory> memory(
|
| @@ -74,13 +107,13 @@ TEST(DiscardableMemoryTest, Purge) {
|
| memory->Unlock();
|
|
|
| DiscardableMemory::PurgeForTesting();
|
| - EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
|
| + EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
|
| }
|
| #endif // !OS_ANDROID
|
|
|
| #if !defined(NDEBUG) && !defined(OS_ANDROID)
|
| // Death tests are not supported with Android APKs.
|
| -TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| +TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| const scoped_ptr<DiscardableMemory> memory(
|
| DiscardableMemory::CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| @@ -90,4 +123,16 @@ TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| }
|
| #endif
|
|
|
| +std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() {
|
| + std::vector<DiscardableMemoryType> supported_types;
|
| + DiscardableMemory::GetSupportedTypes(&supported_types);
|
| + return supported_types;
|
| }
|
| +
|
| +INSTANTIATE_TEST_CASE_P(
|
| + DiscardableMemoryTests,
|
| + DiscardableMemoryTest,
|
| + ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes()));
|
| +
|
| +} // namespace
|
| +} // namespace base
|
|
|