Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: base/chromeos/memory_pressure_observer_chromeos_unittest.cc

Issue 815183002: Using the new MemoryPressureListener instead of the LowMemoryObserver when the enhanced memory mana… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit test for memory_pressure_observer_chromeos and addressed Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // The memory in percent value which gets returned to the observer.
16 // Do not read/modify value directly.
17 int s_memory_in_percent_override_ = 0;
James Cook 2014/12/20 00:27:47 nit: I would name these like "g_memory_in_percent_
Mr4D (OOO till 08-26) 2014/12/20 03:01:04 Done.
18
19 // True if the memory notifier got called.
20 // Do not read/modify value directly.
21 bool s_on_memory_pressure_called_ = false;
22
23 // Call to set the memory in percent override.
24 void SetMemoryInPercentOverride(int override) {
25 s_memory_in_percent_override_ = override;
26 }
27
28 // Returns the memory pressure in percent.
29 int GetMemoryInPercentOverride() {
30 return s_memory_in_percent_override_;
31 }
32
33 // Processes OnMemoryPressure calls.
34 void OnMemoryPressure(MemoryPressureListener::MemoryPressureLevel level) {
35 s_on_memory_pressure_called_ = true;
36 if (s_memory_in_percent_override_ < 90)
37 EXPECT_EQ(level, MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
38 else
39 EXPECT_EQ(level, MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
40 }
41
42 // Resets the indicator for memory pressure.
43 void ResetOnMemoryPressureCalled() {
44 s_on_memory_pressure_called_ = false;
45 }
46
47 // Returns true when OnMemoryPressure was called (and resets it).
48 bool WasOnMemoryPressureCalled() {
49 bool b = s_on_memory_pressure_called_;
50 ResetOnMemoryPressureCalled();
51 return b;
52 }
53
54 } // namespace
55
56 // This test tests the various transition states from memory pressure, looking
57 // for the correct behavior on event reposting as well as state updates.
58 TEST(MemoryPressureObserverChromeOSTest, CheckMemoryPressure) {
59 base::MessageLoopForUI message_loop;
60 scoped_ptr<MemoryPressureObserverChromeOS> observer(
61 new MemoryPressureObserverChromeOS);
62 // Disable any timers which are going on and set a special memory reporting
63 // function.
64 observer->StopObserving();
65 observer->SetGetUsedMemoryInPercentCallbackForUnittest(
66 GetMemoryInPercentOverride);
67 scoped_ptr<MemoryPressureListener> listener(
68 new MemoryPressureListener(base::Bind(&OnMemoryPressure)));
69 // Checking the memory pressure while 0% are used should not produce any
70 // events.
71 SetMemoryInPercentOverride(0);
72 ResetOnMemoryPressureCalled();
73
74 observer->CheckMemoryPressure();
75 message_loop.RunUntilIdle();
76 EXPECT_FALSE(WasOnMemoryPressureCalled());
77 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE,
78 observer->GetCurrentPressureLevel());
79
80 // Setting the memory level to 80% should produce a moderate pressure level.
81 SetMemoryInPercentOverride(80);
82 observer->CheckMemoryPressure();
83 message_loop.RunUntilIdle();
84 EXPECT_TRUE(WasOnMemoryPressureCalled());
85 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
86 observer->GetCurrentPressureLevel());
87
88 // We need to check that the event gets reposted after a while.
89 int i = 0;
90 for (; i < 100; i++) {
91 observer->CheckMemoryPressure();
92 message_loop.RunUntilIdle();
93 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
94 observer->GetCurrentPressureLevel());
95 if (WasOnMemoryPressureCalled())
96 break;
97 }
98 // Should be more then 5 and less then 100.
99 EXPECT_LE(5, i);
100 EXPECT_GE(99, i);
101
102 // Setting the memory usage to 99% should produce critical levels.
103 SetMemoryInPercentOverride(99);
104 observer->CheckMemoryPressure();
105 message_loop.RunUntilIdle();
106 EXPECT_TRUE(WasOnMemoryPressureCalled());
107 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
108 observer->GetCurrentPressureLevel());
109
110 // Calling it again should immediately produce a second call.
111 observer->CheckMemoryPressure();
112 message_loop.RunUntilIdle();
113 EXPECT_TRUE(WasOnMemoryPressureCalled());
114 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
115 observer->GetCurrentPressureLevel());
116
117 // When lowering the pressure again we should not get an event, but the
118 // pressure should go back to moderate.
119 SetMemoryInPercentOverride(80);
120 observer->CheckMemoryPressure();
121 message_loop.RunUntilIdle();
122 EXPECT_FALSE(WasOnMemoryPressureCalled());
123 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
124 observer->GetCurrentPressureLevel());
125
126 // We should need exactly the same amount of calls as before, before the next
127 // call comes in.
128 int j = 0;
129 for (; j < 100; j++) {
130 observer->CheckMemoryPressure();
131 message_loop.RunUntilIdle();
132 EXPECT_EQ(MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
133 observer->GetCurrentPressureLevel());
134 if (WasOnMemoryPressureCalled())
135 break;
136 }
137 // We should have needed exactly the same amount of checks as before.
138 EXPECT_EQ(j, i);
139 }
James Cook 2014/12/20 00:27:47 nice test
Mr4D (OOO till 08-26) 2014/12/20 03:01:04 Thanks!
140
141 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698