OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_win.h" | 5 #include "base/memory/memory_pressure_monitor_win.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/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 namespace win { | 15 namespace win { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 struct PressureSettings { | 19 struct PressureSettings { |
19 int phys_left_mb; | 20 int phys_left_mb; |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // Large-memory. | 203 // Large-memory. |
203 testing::StrictMock<TestMemoryPressureMonitor> monitor(true); | 204 testing::StrictMock<TestMemoryPressureMonitor> monitor(true); |
204 MemoryPressureListener listener( | 205 MemoryPressureListener listener( |
205 base::Bind(&TestMemoryPressureMonitor::OnMemoryPressure, | 206 base::Bind(&TestMemoryPressureMonitor::OnMemoryPressure, |
206 base::Unretained(&monitor))); | 207 base::Unretained(&monitor))); |
207 | 208 |
208 // Checking the memory pressure at 0% load should not produce any | 209 // Checking the memory pressure at 0% load should not produce any |
209 // events. | 210 // events. |
210 monitor.SetNone(); | 211 monitor.SetNone(); |
211 monitor.CheckMemoryPressure(); | 212 monitor.CheckMemoryPressure(); |
212 message_loop_.RunUntilIdle(); | 213 RunLoop().RunUntilIdle(); |
213 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 214 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
214 monitor.GetCurrentPressureLevel()); | 215 monitor.GetCurrentPressureLevel()); |
215 | 216 |
216 // Setting the memory level to 80% should produce a moderate pressure level. | 217 // Setting the memory level to 80% should produce a moderate pressure level. |
217 EXPECT_CALL(monitor, | 218 EXPECT_CALL(monitor, |
218 OnMemoryPressure(MemoryPressureListener:: | 219 OnMemoryPressure(MemoryPressureListener:: |
219 MEMORY_PRESSURE_LEVEL_MODERATE)); | 220 MEMORY_PRESSURE_LEVEL_MODERATE)); |
220 monitor.SetModerate(); | 221 monitor.SetModerate(); |
221 monitor.CheckMemoryPressure(); | 222 monitor.CheckMemoryPressure(); |
222 message_loop_.RunUntilIdle(); | 223 RunLoop().RunUntilIdle(); |
223 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 224 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
224 monitor.GetCurrentPressureLevel()); | 225 monitor.GetCurrentPressureLevel()); |
225 testing::Mock::VerifyAndClearExpectations(&monitor); | 226 testing::Mock::VerifyAndClearExpectations(&monitor); |
226 | 227 |
227 // Check that the event gets reposted after a while. | 228 // Check that the event gets reposted after a while. |
228 for (int i = 0; i < monitor.kModeratePressureCooldownCycles; ++i) { | 229 for (int i = 0; i < monitor.kModeratePressureCooldownCycles; ++i) { |
229 if (i + 1 == monitor.kModeratePressureCooldownCycles) { | 230 if (i + 1 == monitor.kModeratePressureCooldownCycles) { |
230 EXPECT_CALL(monitor, | 231 EXPECT_CALL(monitor, |
231 OnMemoryPressure(MemoryPressureListener:: | 232 OnMemoryPressure(MemoryPressureListener:: |
232 MEMORY_PRESSURE_LEVEL_MODERATE)); | 233 MEMORY_PRESSURE_LEVEL_MODERATE)); |
233 } | 234 } |
234 monitor.CheckMemoryPressure(); | 235 monitor.CheckMemoryPressure(); |
235 message_loop_.RunUntilIdle(); | 236 RunLoop().RunUntilIdle(); |
236 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 237 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
237 monitor.GetCurrentPressureLevel()); | 238 monitor.GetCurrentPressureLevel()); |
238 testing::Mock::VerifyAndClearExpectations(&monitor); | 239 testing::Mock::VerifyAndClearExpectations(&monitor); |
239 } | 240 } |
240 | 241 |
241 // Setting the memory usage to 99% should produce critical levels. | 242 // Setting the memory usage to 99% should produce critical levels. |
242 EXPECT_CALL(monitor, | 243 EXPECT_CALL(monitor, |
243 OnMemoryPressure(MemoryPressureListener:: | 244 OnMemoryPressure(MemoryPressureListener:: |
244 MEMORY_PRESSURE_LEVEL_CRITICAL)); | 245 MEMORY_PRESSURE_LEVEL_CRITICAL)); |
245 monitor.SetCritical(); | 246 monitor.SetCritical(); |
246 monitor.CheckMemoryPressure(); | 247 monitor.CheckMemoryPressure(); |
247 message_loop_.RunUntilIdle(); | 248 RunLoop().RunUntilIdle(); |
248 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 249 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
249 monitor.GetCurrentPressureLevel()); | 250 monitor.GetCurrentPressureLevel()); |
250 testing::Mock::VerifyAndClearExpectations(&monitor); | 251 testing::Mock::VerifyAndClearExpectations(&monitor); |
251 | 252 |
252 // Calling it again should immediately produce a second call. | 253 // Calling it again should immediately produce a second call. |
253 EXPECT_CALL(monitor, | 254 EXPECT_CALL(monitor, |
254 OnMemoryPressure(MemoryPressureListener:: | 255 OnMemoryPressure(MemoryPressureListener:: |
255 MEMORY_PRESSURE_LEVEL_CRITICAL)); | 256 MEMORY_PRESSURE_LEVEL_CRITICAL)); |
256 monitor.CheckMemoryPressure(); | 257 monitor.CheckMemoryPressure(); |
257 message_loop_.RunUntilIdle(); | 258 RunLoop().RunUntilIdle(); |
258 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 259 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
259 monitor.GetCurrentPressureLevel()); | 260 monitor.GetCurrentPressureLevel()); |
260 testing::Mock::VerifyAndClearExpectations(&monitor); | 261 testing::Mock::VerifyAndClearExpectations(&monitor); |
261 | 262 |
262 // When lowering the pressure again there should be a notification and the | 263 // When lowering the pressure again there should be a notification and the |
263 // pressure should go back to moderate. | 264 // pressure should go back to moderate. |
264 EXPECT_CALL(monitor, | 265 EXPECT_CALL(monitor, |
265 OnMemoryPressure(MemoryPressureListener:: | 266 OnMemoryPressure(MemoryPressureListener:: |
266 MEMORY_PRESSURE_LEVEL_MODERATE)); | 267 MEMORY_PRESSURE_LEVEL_MODERATE)); |
267 monitor.SetModerate(); | 268 monitor.SetModerate(); |
268 monitor.CheckMemoryPressure(); | 269 monitor.CheckMemoryPressure(); |
269 message_loop_.RunUntilIdle(); | 270 RunLoop().RunUntilIdle(); |
270 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 271 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
271 monitor.GetCurrentPressureLevel()); | 272 monitor.GetCurrentPressureLevel()); |
272 testing::Mock::VerifyAndClearExpectations(&monitor); | 273 testing::Mock::VerifyAndClearExpectations(&monitor); |
273 | 274 |
274 // Check that the event gets reposted after a while. | 275 // Check that the event gets reposted after a while. |
275 for (int i = 0; i < monitor.kModeratePressureCooldownCycles; ++i) { | 276 for (int i = 0; i < monitor.kModeratePressureCooldownCycles; ++i) { |
276 if (i + 1 == monitor.kModeratePressureCooldownCycles) { | 277 if (i + 1 == monitor.kModeratePressureCooldownCycles) { |
277 EXPECT_CALL(monitor, | 278 EXPECT_CALL(monitor, |
278 OnMemoryPressure(MemoryPressureListener:: | 279 OnMemoryPressure(MemoryPressureListener:: |
279 MEMORY_PRESSURE_LEVEL_MODERATE)); | 280 MEMORY_PRESSURE_LEVEL_MODERATE)); |
280 } | 281 } |
281 monitor.CheckMemoryPressure(); | 282 monitor.CheckMemoryPressure(); |
282 message_loop_.RunUntilIdle(); | 283 RunLoop().RunUntilIdle(); |
283 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 284 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
284 monitor.GetCurrentPressureLevel()); | 285 monitor.GetCurrentPressureLevel()); |
285 testing::Mock::VerifyAndClearExpectations(&monitor); | 286 testing::Mock::VerifyAndClearExpectations(&monitor); |
286 } | 287 } |
287 | 288 |
288 // Going down to no pressure should not produce an notification. | 289 // Going down to no pressure should not produce an notification. |
289 monitor.SetNone(); | 290 monitor.SetNone(); |
290 monitor.CheckMemoryPressure(); | 291 monitor.CheckMemoryPressure(); |
291 message_loop_.RunUntilIdle(); | 292 RunLoop().RunUntilIdle(); |
292 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | 293 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, |
293 monitor.GetCurrentPressureLevel()); | 294 monitor.GetCurrentPressureLevel()); |
294 testing::Mock::VerifyAndClearExpectations(&monitor); | 295 testing::Mock::VerifyAndClearExpectations(&monitor); |
295 } | 296 } |
296 | 297 |
297 } // namespace win | 298 } // namespace win |
298 } // namespace base | 299 } // namespace base |
OLD | NEW |