| OLD | NEW |
| 1 // Copyright 2014 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_chromeos.h" | 5 #include "base/memory/memory_pressure_monitor_linux.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.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 "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 namespace chromeos { | 13 namespace nix { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // True if the memory notifier got called. | 17 // True if the memory notifier got called. |
| 18 // Do not read/modify value directly. | 18 // Do not read/modify value directly. |
| 19 bool on_memory_pressure_called = false; | 19 bool on_memory_pressure_called = false; |
| 20 | 20 |
| 21 // If the memory notifier got called, this is the memory pressure reported. | 21 // If the memory notifier got called, this is the memory pressure reported. |
| 22 MemoryPressureListener::MemoryPressureLevel on_memory_pressure_level = | 22 MemoryPressureListener::MemoryPressureLevel on_memory_pressure_level = |
| 23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | 23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 bool b = on_memory_pressure_called; | 38 bool b = on_memory_pressure_called; |
| 39 ResetOnMemoryPressureCalled(); | 39 ResetOnMemoryPressureCalled(); |
| 40 return b; | 40 return b; |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 class TestMemoryPressureMonitor : public MemoryPressureMonitor { | 45 class TestMemoryPressureMonitor : public MemoryPressureMonitor { |
| 46 public: | 46 public: |
| 47 TestMemoryPressureMonitor() | 47 TestMemoryPressureMonitor() |
| 48 : MemoryPressureMonitor(THRESHOLD_DEFAULT), | 48 : MemoryPressureMonitor(), memory_in_percent_override_(0) { |
| 49 memory_in_percent_override_(0) { | |
| 50 // Disable any timers which are going on and set a special memory reporting | 49 // Disable any timers which are going on and set a special memory reporting |
| 51 // function. | 50 // function. |
| 52 StopObserving(); | 51 StopObserving(); |
| 53 } | 52 } |
| 54 ~TestMemoryPressureMonitor() override {} | 53 ~TestMemoryPressureMonitor() override {} |
| 55 | 54 |
| 56 void SetMemoryInPercentOverride(int percent) { | 55 void SetMemoryInPercentOverride(int percent) { |
| 57 memory_in_percent_override_ = percent; | 56 memory_in_percent_override_ = percent; |
| 58 } | 57 } |
| 59 | 58 |
| 60 void CheckMemoryPressureForTest() { | 59 void CheckMemoryPressureForTest() { CheckMemoryPressure(); } |
| 61 CheckMemoryPressure(); | |
| 62 } | |
| 63 | 60 |
| 64 private: | 61 private: |
| 65 int GetUsedMemoryInPercent() override { | 62 int GetUsedMemoryInPercent() override { return memory_in_percent_override_; } |
| 66 return memory_in_percent_override_; | |
| 67 } | |
| 68 | 63 |
| 69 int memory_in_percent_override_; | 64 int memory_in_percent_override_; |
| 70 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor); | 65 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor); |
| 71 }; | 66 }; |
| 72 | 67 |
| 73 // This test tests the various transition states from memory pressure, looking | 68 // This test tests the various transition states from memory pressure, looking |
| 74 // for the correct behavior on event reposting as well as state updates. | 69 // for the correct behavior on event reposting as well as state updates. |
| 75 TEST(ChromeOSMemoryPressureMonitorTest, CheckMemoryPressure) { | 70 TEST(LinuxMemoryPressureMonitorTest, CheckMemoryPressure) { |
| 76 base::MessageLoopForUI message_loop; | 71 base::MessageLoopForUI message_loop; |
| 77 scoped_ptr<TestMemoryPressureMonitor> monitor( | 72 scoped_ptr<TestMemoryPressureMonitor> monitor(new TestMemoryPressureMonitor); |
| 78 new TestMemoryPressureMonitor); | |
| 79 scoped_ptr<MemoryPressureListener> listener( | 73 scoped_ptr<MemoryPressureListener> listener( |
| 80 new MemoryPressureListener(base::Bind(&OnMemoryPressure))); | 74 new MemoryPressureListener(base::Bind(&OnMemoryPressure))); |
| 81 // Checking the memory pressure while 0% are used should not produce any | 75 // Checking the memory pressure while 0% are used should not produce any |
| 82 // events. | 76 // events. |
| 83 monitor->SetMemoryInPercentOverride(0); | 77 monitor->SetMemoryInPercentOverride(0); |
| 84 ResetOnMemoryPressureCalled(); | 78 ResetOnMemoryPressureCalled(); |
| 85 | 79 |
| 86 monitor->CheckMemoryPressureForTest(); | 80 monitor->CheckMemoryPressureForTest(); |
| 87 message_loop.RunUntilIdle(); | 81 message_loop.RunUntilIdle(); |
| 88 EXPECT_FALSE(WasOnMemoryPressureCalled()); | 82 EXPECT_FALSE(WasOnMemoryPressureCalled()); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 if (WasOnMemoryPressureCalled()) { | 149 if (WasOnMemoryPressureCalled()) { |
| 156 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, | 150 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE, |
| 157 on_memory_pressure_level); | 151 on_memory_pressure_level); |
| 158 break; | 152 break; |
| 159 } | 153 } |
| 160 } | 154 } |
| 161 // We should have needed exactly the same amount of checks as before. | 155 // We should have needed exactly the same amount of checks as before. |
| 162 EXPECT_EQ(j, i); | 156 EXPECT_EQ(j, i); |
| 163 } | 157 } |
| 164 | 158 |
| 165 } // namespace chromeos | 159 } // namespace nix |
| 166 } // namespace base | 160 } // namespace base |
| OLD | NEW |