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