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