Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Unified Diff: base/memory/memory_pressure_listener_unittest.cc

Issue 1370713002: Add method for simulating MemoryPressureListener notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Lei Zhang's comment Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/memory/memory_pressure_listener.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/memory_pressure_listener_unittest.cc
diff --git a/base/memory/memory_pressure_listener_unittest.cc b/base/memory/memory_pressure_listener_unittest.cc
index 1bcab2fd041f2707115608850de01c064fa705ac..38d429d31560c5d8ba29c31d5d6df1c5bb3514fb 100644
--- a/base/memory/memory_pressure_listener_unittest.cc
+++ b/base/memory/memory_pressure_listener_unittest.cc
@@ -10,43 +10,69 @@
namespace base {
-using MockCallback =
- testing::MockFunction<void(MemoryPressureListener::MemoryPressureLevel)>;
+using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
-TEST(MemoryPressureListenerTest, NotifyMemoryPressure) {
- MessageLoopForUI message_loop;
- MockCallback callback;
- scoped_ptr<MemoryPressureListener> listener(new MemoryPressureListener(
- Bind(&MockCallback::Call, Unretained(&callback))));
+class MemoryPressureListenerTest : public testing::Test {
+ public:
+ void SetUp() override {
+ message_loop_.reset(new MessageLoopForUI());
+ listener_.reset(new MemoryPressureListener(
+ Bind(&MemoryPressureListenerTest::OnMemoryPressure, Unretained(this))));
+ }
+ void TearDown() override {
+ listener_.reset();
+ message_loop_.reset();
+ }
+
+ protected:
+ void ExpectNotification(
+ void (*notification_function)(MemoryPressureLevel),
+ MemoryPressureLevel level) {
+ EXPECT_CALL(*this, OnMemoryPressure(level)).Times(1);
+ notification_function(level);
+ message_loop_->RunUntilIdle();
+ }
+
+ void ExpectNoNotification(
+ void (*notification_function)(MemoryPressureLevel),
+ MemoryPressureLevel level) {
+ EXPECT_CALL(*this, OnMemoryPressure(testing::_)).Times(0);
+ notification_function(level);
+ message_loop_->RunUntilIdle();
+ }
+
+ private:
+ MOCK_METHOD1(OnMemoryPressure,
+ void(MemoryPressureListener::MemoryPressureLevel));
+
+ scoped_ptr<MessageLoopForUI> message_loop_;
+ scoped_ptr<MemoryPressureListener> listener_;
+};
+
+TEST_F(MemoryPressureListenerTest, NotifyMemoryPressure) {
// Memory pressure notifications are not suppressed by default.
EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed());
- EXPECT_CALL(callback,
- Call(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE))
- .Times(1);
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
- message_loop.RunUntilIdle();
+ ExpectNotification(&MemoryPressureListener::NotifyMemoryPressure,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
+ ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
// Enable suppressing memory pressure notifications.
MemoryPressureListener::SetNotificationsSuppressed(true);
EXPECT_TRUE(MemoryPressureListener::AreNotificationsSuppressed());
- EXPECT_CALL(callback, Call(testing::_)).Times(0);
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
- message_loop.RunUntilIdle();
+ ExpectNoNotification(&MemoryPressureListener::NotifyMemoryPressure,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
+ ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE);
// Disable suppressing memory pressure notifications.
MemoryPressureListener::SetNotificationsSuppressed(false);
EXPECT_FALSE(MemoryPressureListener::AreNotificationsSuppressed());
- EXPECT_CALL(callback,
- Call(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL))
- .Times(1);
- MemoryPressureListener::NotifyMemoryPressure(
- MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
- message_loop.RunUntilIdle();
-
- listener.reset();
+ ExpectNotification(&MemoryPressureListener::NotifyMemoryPressure,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);
+ ExpectNotification(&MemoryPressureListener::SimulatePressureNotification,
+ MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL);
}
} // namespace base
« no previous file with comments | « base/memory/memory_pressure_listener.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698