Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / memory / memory_pressure_monitor_chromeos_unittest.cc
blobe0afa448a412e80a24be8293716ecd403d954de9
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.
5 #include "base/memory/memory_pressure_monitor_chromeos.h"
7 #include "base/basictypes.h"
8 #include "base/memory/memory_pressure_listener.h"
9 #include "base/message_loop/message_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
13 namespace chromeos {
15 namespace {
17 // True if the memory notifier got called.
18 // Do not read/modify value directly.
19 bool on_memory_pressure_called = false;
21 // If the memory notifier got called, this is the memory pressure reported.
22 MemoryPressureListener::MemoryPressureLevel on_memory_pressure_level =
23 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
25 // Processes OnMemoryPressure calls.
26 void OnMemoryPressure(MemoryPressureListener::MemoryPressureLevel level) {
27 on_memory_pressure_called = true;
28 on_memory_pressure_level = level;
31 // Resets the indicator for memory pressure.
32 void ResetOnMemoryPressureCalled() {
33 on_memory_pressure_called = false;
36 // Returns true when OnMemoryPressure was called (and resets it).
37 bool WasOnMemoryPressureCalled() {
38 bool b = on_memory_pressure_called;
39 ResetOnMemoryPressureCalled();
40 return b;
43 } // namespace
45 class TestMemoryPressureMonitor : public MemoryPressureMonitor {
46 public:
47 TestMemoryPressureMonitor()
48 : MemoryPressureMonitor(THRESHOLD_DEFAULT),
49 memory_in_percent_override_(0) {
50 // Disable any timers which are going on and set a special memory reporting
51 // function.
52 StopObserving();
54 ~TestMemoryPressureMonitor() override {}
56 void SetMemoryInPercentOverride(int percent) {
57 memory_in_percent_override_ = percent;
60 void CheckMemoryPressureForTest() {
61 CheckMemoryPressure();
64 private:
65 int GetUsedMemoryInPercent() override {
66 return memory_in_percent_override_;
69 int memory_in_percent_override_;
70 DISALLOW_COPY_AND_ASSIGN(TestMemoryPressureMonitor);
73 // This test tests the various transition states from memory pressure, looking
74 // for the correct behavior on event reposting as well as state updates.
75 TEST(ChromeOSMemoryPressureMonitorTest, CheckMemoryPressure) {
76 base::MessageLoopForUI message_loop;
77 scoped_ptr<TestMemoryPressureMonitor> monitor(
78 new TestMemoryPressureMonitor);
79 scoped_ptr<MemoryPressureListener> listener(
80 new MemoryPressureListener(base::Bind(&OnMemoryPressure)));
81 // Checking the memory pressure while 0% are used should not produce any
82 // events.
83 monitor->SetMemoryInPercentOverride(0);
84 ResetOnMemoryPressureCalled();
86 monitor->CheckMemoryPressureForTest();
87 message_loop.RunUntilIdle();
88 EXPECT_FALSE(WasOnMemoryPressureCalled());
89 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
90 monitor->GetCurrentPressureLevel());
92 // Setting the memory level to 80% should produce a moderate pressure level.
93 monitor->SetMemoryInPercentOverride(80);
94 monitor->CheckMemoryPressureForTest();
95 message_loop.RunUntilIdle();
96 EXPECT_TRUE(WasOnMemoryPressureCalled());
97 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
98 monitor->GetCurrentPressureLevel());
99 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
100 on_memory_pressure_level);
102 // We need to check that the event gets reposted after a while.
103 int i = 0;
104 for (; i < 100; i++) {
105 monitor->CheckMemoryPressureForTest();
106 message_loop.RunUntilIdle();
107 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
108 monitor->GetCurrentPressureLevel());
109 if (WasOnMemoryPressureCalled()) {
110 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
111 on_memory_pressure_level);
112 break;
115 // Should be more then 5 and less then 100.
116 EXPECT_LE(5, i);
117 EXPECT_GE(99, i);
119 // Setting the memory usage to 99% should produce critical levels.
120 monitor->SetMemoryInPercentOverride(99);
121 monitor->CheckMemoryPressureForTest();
122 message_loop.RunUntilIdle();
123 EXPECT_TRUE(WasOnMemoryPressureCalled());
124 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
125 on_memory_pressure_level);
126 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
127 monitor->GetCurrentPressureLevel());
129 // Calling it again should immediately produce a second call.
130 monitor->CheckMemoryPressureForTest();
131 message_loop.RunUntilIdle();
132 EXPECT_TRUE(WasOnMemoryPressureCalled());
133 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
134 on_memory_pressure_level);
135 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
136 monitor->GetCurrentPressureLevel());
138 // When lowering the pressure again we should not get an event, but the
139 // pressure should go back to moderate.
140 monitor->SetMemoryInPercentOverride(80);
141 monitor->CheckMemoryPressureForTest();
142 message_loop.RunUntilIdle();
143 EXPECT_FALSE(WasOnMemoryPressureCalled());
144 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
145 monitor->GetCurrentPressureLevel());
147 // We should need exactly the same amount of calls as before, before the next
148 // call comes in.
149 int j = 0;
150 for (; j < 100; j++) {
151 monitor->CheckMemoryPressureForTest();
152 message_loop.RunUntilIdle();
153 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
154 monitor->GetCurrentPressureLevel());
155 if (WasOnMemoryPressureCalled()) {
156 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
157 on_memory_pressure_level);
158 break;
161 // We should have needed exactly the same amount of checks as before.
162 EXPECT_EQ(j, i);
165 } // namespace chromeos
166 } // namespace base