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