Add UMA to identify where a hotword trigger comes from.
[chromium-blink-merge.git] / base / memory / discardable_memory_unittest.cc
blob600475ee06bc92e0fd51254761b7b9f6600b897e
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"
7 #include <algorithm>
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if defined(OS_ANDROID)
12 #include <limits>
13 #endif
15 namespace base {
16 namespace {
18 class DiscardableMemoryTest
19 : public testing::TestWithParam<DiscardableMemoryType> {
20 public:
21 DiscardableMemoryTest() {}
22 virtual ~DiscardableMemoryTest() {
25 protected:
26 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) {
27 return DiscardableMemory::CreateLockedMemoryWithType(
28 GetParam(), size).Pass();
32 const size_t kSize = 1024;
34 TEST_P(DiscardableMemoryTest, IsNamed) {
35 std::string type_name(DiscardableMemory::GetTypeName(GetParam()));
36 EXPECT_NE("unknown", type_name);
37 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name));
40 bool IsNativeType(DiscardableMemoryType type) {
41 return type == DISCARDABLE_MEMORY_TYPE_ASHMEM ||
42 type == DISCARDABLE_MEMORY_TYPE_MACH;
45 TEST_P(DiscardableMemoryTest, SupportedNatively) {
46 std::vector<DiscardableMemoryType> supported_types;
47 DiscardableMemory::GetSupportedTypes(&supported_types);
48 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
49 EXPECT_NE(0, std::count_if(supported_types.begin(),
50 supported_types.end(),
51 IsNativeType));
52 #else
53 // If we ever have a platform that decides at runtime if it can support
54 // discardable memory natively, then we'll have to add a 'never supported
55 // natively' define for this case. At present, if it's not always supported
56 // natively, it's never supported.
57 EXPECT_EQ(0, std::count_if(supported_types.begin(),
58 supported_types.end(),
59 IsNativeType));
60 #endif
63 // Test Lock() and Unlock() functionalities.
64 TEST_P(DiscardableMemoryTest, LockAndUnLock) {
65 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
66 ASSERT_TRUE(memory);
67 void* addr = memory->Memory();
68 ASSERT_NE(nullptr, addr);
70 memory->Unlock();
72 EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, memory->Lock());
73 addr = memory->Memory();
74 ASSERT_NE(nullptr, addr);
76 memory->Unlock();
79 // Test delete a discardable memory while it is locked.
80 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
81 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
82 ASSERT_TRUE(memory);
85 // Test forced purging.
86 TEST_P(DiscardableMemoryTest, Purge) {
87 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
88 ASSERT_TRUE(memory);
89 memory->Unlock();
91 DiscardableMemory::PurgeForTesting();
92 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
95 #if !defined(NDEBUG) && !defined(OS_ANDROID)
96 // Death tests are not supported with Android APKs.
97 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
98 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
99 ASSERT_TRUE(memory);
100 memory->Unlock();
101 ASSERT_DEATH_IF_SUPPORTED(
102 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
104 #endif
106 // Test behavior when creating enough instances that could use up a 32-bit
107 // address space.
108 TEST_P(DiscardableMemoryTest, AddressSpace) {
109 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB.
110 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total.
112 scoped_ptr<DiscardableMemory> instances[kNumberOfInstances];
113 for (auto& memory : instances) {
114 memory = CreateLockedMemory(kLargeSize);
115 ASSERT_TRUE(memory);
116 void* addr = memory->Memory();
117 ASSERT_NE(nullptr, addr);
118 memory->Unlock();
122 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() {
123 std::vector<DiscardableMemoryType> supported_types;
124 DiscardableMemory::GetSupportedTypes(&supported_types);
125 return supported_types;
128 INSTANTIATE_TEST_CASE_P(
129 DiscardableMemoryTests,
130 DiscardableMemoryTest,
131 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes()));
133 } // namespace
134 } // namespace base