| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/chromeos/memory_pressure_monitor.h" | |
| 7 #include "base/memory/memory_pressure_listener.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace chromeos { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // True if the memory notifier got called. | |
| 17 // Do not read/modify value directly. | |
| 18 bool on_memory_pressure_called = false; | |
| 19 | |
| 20 // If the memory notifier got called, this is the memory pressure reported. | |
| 21 MemoryPressureListener::MemoryPressureLevel on_memory_pressure_level = | |
| 22 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
| 23 | |
| 24 // Processes OnMemoryPressure calls. | |
| 25 void OnMemoryPressure(MemoryPressureListener::MemoryPressureLevel level) { | |
| 26 on_memory_pressure_called = true; | |
| 27 on_memory_pressure_level = level; | |
| 28 } | |
| 29 | |
| 30 // Resets the indicator for memory pressure. | |
| 31 void ResetOnMemoryPressureCalled() { | |
| 32 on_memory_pressure_called = false; | |
| 33 } | |
| 34 | |
| 35 // Returns true when OnMemoryPressure was called (and resets it). | |
| 36 bool WasOnMemoryPressureCalled() { | |
| 37 bool b = on_memory_pressure_called; | |
| 38 ResetOnMemoryPressureCalled(); | |
| 39 return b; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 class TestMemoryPressureMonitor : public MemoryPressureMonitor { | |
| 45 public: | |
| 46 TestMemoryPressureMonitor() | |
| 47 : MemoryPressureMonitor(THRESHOLD_DEFAULT), | |
| 48 memory_in_percent_override_(0) { | |
| 49 // Disable any timers which are going on and set a special memory reporting | |
| 50 // function. | |
| 51 StopObserving(); | |
| 52 } | |
| 53 ~TestMemoryPressureMonitor() override {} | |
| 54 | |
| 55 void SetMemoryInPercentOverride(int percent) { | |
| 56 memory_in_percent_override_ = percent; | |
| 57 } | |
| 58 | |
| 59 void CheckMemoryPressureForTest() { | |
| 60 CheckMemoryPressure(); | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 int GetUsedMemoryInPercent() override { | |
| 65 return memory_in_percent_override_; | |
| 66 } | |
| 67 | |
| 68 int memory_in_percent_override_; | |
| 69 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor); | |
| 70 }; | |
| 71 | |
| 72 // This test tests the various transition states from memory pressure, looking | |
| 73 // for the correct behavior on event reposting as well as state updates. | |
| 74 TEST(ChromeOSMemoryPressureMonitorTest, CheckMemoryPressure) { | |
| 75 base::MessageLoopForUI message_loop; | |
| 76 scoped_ptr<TestMemoryPressureMonitor> monitor( | |
| 77 new TestMemoryPressureMonitor); | |
| 78 scoped_ptr<MemoryPressureListener> listener( | |
| 79 new MemoryPressureListener(base::Bind(&OnMemoryPressure))); | |
| 80 // Checking the memory pressure while 0% are used should not produce any | |
| 81 // events. | |
| 82 monitor->SetMemoryInPercentOverride(0); | |
| 83 ResetOnMemoryPressureCalled(); | |
| 84 | |
| 85 monitor->CheckMemoryPressureForTest(); | |
| 86 message_loop.RunUntilIdle(); | |
| 87 EXPECT_FALSE(WasOnMemoryPressureCalled()); | |
| 88 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, | |
| 89 monitor->GetCurrentPressureLevel()); | |
| 90 | |
| 91 // Setting the memory level to 80% should produce a moderate pressure level. | |
| 92 monitor->SetMemoryInPercentOverride(80); | |
| 93 monitor->CheckMemoryPressureForTest(); | |
| 94 message_loop.RunUntilIdle(); | |
| 95 EXPECT_TRUE(WasOnMemoryPressureCalled()); | |
| 96 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 97 monitor->GetCurrentPressureLevel()); | |
| 98 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 99 on_memory_pressure_level); | |
| 100 | |
| 101 // We need to check that the event gets reposted after a while. | |
| 102 int i = 0; | |
| 103 for (; i < 100; i++) { | |
| 104 monitor->CheckMemoryPressureForTest(); | |
| 105 message_loop.RunUntilIdle(); | |
| 106 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 107 monitor->GetCurrentPressureLevel()); | |
| 108 if (WasOnMemoryPressureCalled()) { | |
| 109 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 110 on_memory_pressure_level); | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 // Should be more then 5 and less then 100. | |
| 115 EXPECT_LE(5, i); | |
| 116 EXPECT_GE(99, i); | |
| 117 | |
| 118 // Setting the memory usage to 99% should produce critical levels. | |
| 119 monitor->SetMemoryInPercentOverride(99); | |
| 120 monitor->CheckMemoryPressureForTest(); | |
| 121 message_loop.RunUntilIdle(); | |
| 122 EXPECT_TRUE(WasOnMemoryPressureCalled()); | |
| 123 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | |
| 124 on_memory_pressure_level); | |
| 125 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | |
| 126 monitor->GetCurrentPressureLevel()); | |
| 127 | |
| 128 // Calling it again should immediately produce a second call. | |
| 129 monitor->CheckMemoryPressureForTest(); | |
| 130 message_loop.RunUntilIdle(); | |
| 131 EXPECT_TRUE(WasOnMemoryPressureCalled()); | |
| 132 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | |
| 133 on_memory_pressure_level); | |
| 134 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | |
| 135 monitor->GetCurrentPressureLevel()); | |
| 136 | |
| 137 // When lowering the pressure again we should not get an event, but the | |
| 138 // pressure should go back to moderate. | |
| 139 monitor->SetMemoryInPercentOverride(80); | |
| 140 monitor->CheckMemoryPressureForTest(); | |
| 141 message_loop.RunUntilIdle(); | |
| 142 EXPECT_FALSE(WasOnMemoryPressureCalled()); | |
| 143 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 144 monitor->GetCurrentPressureLevel()); | |
| 145 | |
| 146 // We should need exactly the same amount of calls as before, before the next | |
| 147 // call comes in. | |
| 148 int j = 0; | |
| 149 for (; j < 100; j++) { | |
| 150 monitor->CheckMemoryPressureForTest(); | |
| 151 message_loop.RunUntilIdle(); | |
| 152 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 153 monitor->GetCurrentPressureLevel()); | |
| 154 if (WasOnMemoryPressureCalled()) { | |
| 155 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | |
| 156 on_memory_pressure_level); | |
| 157 break; | |
| 158 } | |
| 159 } | |
| 160 // We should have needed exactly the same amount of checks as before. | |
| 161 EXPECT_EQ(j, i); | |
| 162 } | |
| 163 | |
| 164 } // namespace chromeos | |
| 165 } // namespace base | |
| OLD | NEW |