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