OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/memory/memory_pressure_monitor_chromeos.h" | 5 #include "base/memory/memory_pressure_monitor_chromeos.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/memory_pressure_listener.h" | 8 #include "base/memory/memory_pressure_listener.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 namespace chromeos { | 14 namespace chromeos { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // True if the memory notifier got called. | 18 // True if the memory notifier got called. |
18 // Do not read/modify value directly. | 19 // Do not read/modify value directly. |
19 bool on_memory_pressure_called = false; | 20 bool on_memory_pressure_called = false; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 std::unique_ptr<TestMemoryPressureMonitor> monitor( | 78 std::unique_ptr<TestMemoryPressureMonitor> monitor( |
78 new TestMemoryPressureMonitor); | 79 new TestMemoryPressureMonitor); |
79 std::unique_ptr<MemoryPressureListener> listener( | 80 std::unique_ptr<MemoryPressureListener> listener( |
80 new MemoryPressureListener(base::Bind(&OnMemoryPressure))); | 81 new MemoryPressureListener(base::Bind(&OnMemoryPressure))); |
81 // Checking the memory pressure while 0% are used should not produce any | 82 // Checking the memory pressure while 0% are used should not produce any |
82 // events. | 83 // events. |
83 monitor->SetMemoryInPercentOverride(0); | 84 monitor->SetMemoryInPercentOverride(0); |
84 ResetOnMemoryPressureCalled(); | 85 ResetOnMemoryPressureCalled(); |
85 | 86 |
86 monitor->CheckMemoryPressureForTest(); | 87 monitor->CheckMemoryPressureForTest(); |
87 message_loop.RunUntilIdle(); | 88 RunLoop().RunUntilIdle(); |
88 EXPECT_FALSE(WasOnMemoryPressureCalled()); | 89 EXPECT_FALSE(WasOnMemoryPressureCalled()); |
89 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 90 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
90 monitor->GetCurrentPressureLevel()); | 91 monitor->GetCurrentPressureLevel()); |
91 | 92 |
92 // Setting the memory level to 80% should produce a moderate pressure level. | 93 // Setting the memory level to 80% should produce a moderate pressure level. |
93 monitor->SetMemoryInPercentOverride(80); | 94 monitor->SetMemoryInPercentOverride(80); |
94 monitor->CheckMemoryPressureForTest(); | 95 monitor->CheckMemoryPressureForTest(); |
95 message_loop.RunUntilIdle(); | 96 RunLoop().RunUntilIdle(); |
96 EXPECT_TRUE(WasOnMemoryPressureCalled()); | 97 EXPECT_TRUE(WasOnMemoryPressureCalled()); |
97 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 98 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
98 monitor->GetCurrentPressureLevel()); | 99 monitor->GetCurrentPressureLevel()); |
99 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 100 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
100 on_memory_pressure_level); | 101 on_memory_pressure_level); |
101 | 102 |
102 // We need to check that the event gets reposted after a while. | 103 // We need to check that the event gets reposted after a while. |
103 int i = 0; | 104 int i = 0; |
104 for (; i < 100; i++) { | 105 for (; i < 100; i++) { |
105 monitor->CheckMemoryPressureForTest(); | 106 monitor->CheckMemoryPressureForTest(); |
106 message_loop.RunUntilIdle(); | 107 RunLoop().RunUntilIdle(); |
107 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 108 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
108 monitor->GetCurrentPressureLevel()); | 109 monitor->GetCurrentPressureLevel()); |
109 if (WasOnMemoryPressureCalled()) { | 110 if (WasOnMemoryPressureCalled()) { |
110 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 111 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
111 on_memory_pressure_level); | 112 on_memory_pressure_level); |
112 break; | 113 break; |
113 } | 114 } |
114 } | 115 } |
115 // Should be more than 5 and less than 100. | 116 // Should be more than 5 and less than 100. |
116 EXPECT_LE(5, i); | 117 EXPECT_LE(5, i); |
117 EXPECT_GE(99, i); | 118 EXPECT_GE(99, i); |
118 | 119 |
119 // Setting the memory usage to 99% should produce critical levels. | 120 // Setting the memory usage to 99% should produce critical levels. |
120 monitor->SetMemoryInPercentOverride(99); | 121 monitor->SetMemoryInPercentOverride(99); |
121 monitor->CheckMemoryPressureForTest(); | 122 monitor->CheckMemoryPressureForTest(); |
122 message_loop.RunUntilIdle(); | 123 RunLoop().RunUntilIdle(); |
123 EXPECT_TRUE(WasOnMemoryPressureCalled()); | 124 EXPECT_TRUE(WasOnMemoryPressureCalled()); |
124 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 125 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
125 on_memory_pressure_level); | 126 on_memory_pressure_level); |
126 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 127 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
127 monitor->GetCurrentPressureLevel()); | 128 monitor->GetCurrentPressureLevel()); |
128 | 129 |
129 // Calling it again should immediately produce a second call. | 130 // Calling it again should immediately produce a second call. |
130 monitor->CheckMemoryPressureForTest(); | 131 monitor->CheckMemoryPressureForTest(); |
131 message_loop.RunUntilIdle(); | 132 RunLoop().RunUntilIdle(); |
132 EXPECT_TRUE(WasOnMemoryPressureCalled()); | 133 EXPECT_TRUE(WasOnMemoryPressureCalled()); |
133 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 134 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
134 on_memory_pressure_level); | 135 on_memory_pressure_level); |
135 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 136 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
136 monitor->GetCurrentPressureLevel()); | 137 monitor->GetCurrentPressureLevel()); |
137 | 138 |
138 // When lowering the pressure again we should not get an event, but the | 139 // When lowering the pressure again we should not get an event, but the |
139 // pressure should go back to moderate. | 140 // pressure should go back to moderate. |
140 monitor->SetMemoryInPercentOverride(80); | 141 monitor->SetMemoryInPercentOverride(80); |
141 monitor->CheckMemoryPressureForTest(); | 142 monitor->CheckMemoryPressureForTest(); |
142 message_loop.RunUntilIdle(); | 143 RunLoop().RunUntilIdle(); |
143 EXPECT_FALSE(WasOnMemoryPressureCalled()); | 144 EXPECT_FALSE(WasOnMemoryPressureCalled()); |
144 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 145 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
145 monitor->GetCurrentPressureLevel()); | 146 monitor->GetCurrentPressureLevel()); |
146 | 147 |
147 // We should need exactly the same amount of calls as before, before the next | 148 // We should need exactly the same amount of calls as before, before the next |
148 // call comes in. | 149 // call comes in. |
149 int j = 0; | 150 int j = 0; |
150 for (; j < 100; j++) { | 151 for (; j < 100; j++) { |
151 monitor->CheckMemoryPressureForTest(); | 152 monitor->CheckMemoryPressureForTest(); |
152 message_loop.RunUntilIdle(); | 153 RunLoop().RunUntilIdle(); |
153 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 154 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
154 monitor->GetCurrentPressureLevel()); | 155 monitor->GetCurrentPressureLevel()); |
155 if (WasOnMemoryPressureCalled()) { | 156 if (WasOnMemoryPressureCalled()) { |
156 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 157 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
157 on_memory_pressure_level); | 158 on_memory_pressure_level); |
158 break; | 159 break; |
159 } | 160 } |
160 } | 161 } |
161 // We should have needed exactly the same amount of checks as before. | 162 // We should have needed exactly the same amount of checks as before. |
162 EXPECT_EQ(j, i); | 163 EXPECT_EQ(j, i); |
163 } | 164 } |
164 | 165 |
165 } // namespace chromeos | 166 } // namespace chromeos |
166 } // namespace base | 167 } // namespace base |
OLD | NEW |