OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/memory_pressure_listener.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 using MockCallback = |
| 14 testing::MockFunction<void(MemoryPressureListener::MemoryPressureLevel)>; |
| 15 |
| 16 TEST(MemoryPressureListenerTest, NotifyMemoryPressure) { |
| 17 MessageLoopForUI message_loop; |
| 18 MockCallback callback; |
| 19 scoped_ptr<MemoryPressureListener> listener(new MemoryPressureListener( |
| 20 Bind(&MockCallback::Call, Unretained(&callback)))); |
| 21 |
| 22 // Memory pressure notifications are not suppressed by default. |
| 23 EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed()); |
| 24 EXPECT_CALL(callback, |
| 25 Call(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE)) |
| 26 .Times(1); |
| 27 MemoryPressureListener::NotifyMemoryPressure( |
| 28 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 29 message_loop.RunUntilIdle(); |
| 30 |
| 31 // Enable suppressing memory pressure notifications. |
| 32 MemoryPressureListener::SetNotificationsSuppressed(true); |
| 33 EXPECT_TRUE(MemoryPressureListener::AreNotificationsSuppressed()); |
| 34 EXPECT_CALL(callback, Call(testing::_)).Times(0); |
| 35 MemoryPressureListener::NotifyMemoryPressure( |
| 36 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 37 message_loop.RunUntilIdle(); |
| 38 |
| 39 // Disable suppressing memory pressure notifications. |
| 40 MemoryPressureListener::SetNotificationsSuppressed(false); |
| 41 EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed()); |
| 42 EXPECT_CALL(callback, |
| 43 Call(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL)) |
| 44 .Times(1); |
| 45 MemoryPressureListener::NotifyMemoryPressure( |
| 46 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 47 message_loop.RunUntilIdle(); |
| 48 |
| 49 listener.reset(); |
| 50 } |
| 51 |
| 52 } // namespace base |
OLD | NEW |