- add sources.
[platform/framework/web/crosswalk.git] / src / base / memory / discardable_memory_unittest.cc
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.
4
5 #include "base/memory/discardable_memory.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7
8 namespace base {
9
10 const size_t kSize = 1024;
11
12 TEST(DiscardableMemoryTest, SupportedNatively) {
13 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
14   ASSERT_TRUE(DiscardableMemory::SupportedNatively());
15 #else
16   // If we ever have a platform that decides at runtime if it can support
17   // discardable memory natively, then we'll have to add a 'never supported
18   // natively' define for this case. At present, if it's not always supported
19   // natively, it's never supported.
20   ASSERT_FALSE(DiscardableMemory::SupportedNatively());
21 #endif
22 }
23
24 // Test Lock() and Unlock() functionalities.
25 TEST(DiscardableMemoryTest, LockAndUnLock) {
26   const scoped_ptr<DiscardableMemory> memory(
27       DiscardableMemory::CreateLockedMemory(kSize));
28   ASSERT_TRUE(memory);
29   void* addr = memory->Memory();
30   ASSERT_NE(static_cast<void*>(NULL), addr);
31
32   memory->Unlock();
33   // The system should have no reason to purge discardable blocks in this brief
34   // interval, though technically speaking this might flake.
35   EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
36   addr = memory->Memory();
37   ASSERT_NE(static_cast<void*>(NULL), addr);
38
39   memory->Unlock();
40 }
41
42 // Test delete a discardable memory while it is locked.
43 TEST(DiscardableMemoryTest, DeleteWhileLocked) {
44   const scoped_ptr<DiscardableMemory> memory(
45       DiscardableMemory::CreateLockedMemory(kSize));
46   ASSERT_TRUE(memory);
47 }
48
49 #if !defined(OS_ANDROID)
50 // Test forced purging.
51 TEST(DiscardableMemoryTest, Purge) {
52   ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
53
54   const scoped_ptr<DiscardableMemory> memory(
55       DiscardableMemory::CreateLockedMemory(kSize));
56   ASSERT_TRUE(memory);
57   memory->Unlock();
58
59   DiscardableMemory::PurgeForTesting();
60   EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
61 }
62 #endif  // !OS_ANDROID
63
64 #if !defined(NDEBUG) && !defined(OS_ANDROID)
65 // Death tests are not supported with Android APKs.
66 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
67   const scoped_ptr<DiscardableMemory> memory(
68       DiscardableMemory::CreateLockedMemory(kSize));
69   ASSERT_TRUE(memory);
70   memory->Unlock();
71   ASSERT_DEATH_IF_SUPPORTED(
72       { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
73 }
74 #endif
75
76 }