OLD | NEW |
---|---|
1 // Copyright 2015 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/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/memory_pressure_listener.h" | 6 #include "base/memory/memory_pressure_listener.h" |
7 #include "content/browser/memory/memory_message_filter.h" | 7 #include "content/browser/memory/memory_message_filter.h" |
8 #include "content/browser/memory/memory_pressure_controller.h" | 8 #include "content/browser/memory/memory_pressure_controller.h" |
9 #include "content/common/memory_messages.h" | 9 #include "content/common/memory_messages.h" |
10 #include "content/public/test/content_browser_test.h" | 10 #include "content/public/test/content_browser_test.h" |
11 #include "content/public/test/content_browser_test_utils.h" | 11 #include "content/public/test/content_browser_test_utils.h" |
12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
13 #include "ipc/ipc_message.h" | 13 #include "ipc/ipc_message.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 14 #include "testing/gmock/include/gmock/gmock.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 const auto MEMORY_PRESSURE_LEVEL_MODERATE = | |
Primiano Tucci (use gerrit)
2015/09/28 12:47:32
you could move these two lines in the test that ne
petrcermak
2015/09/28 15:39:32
Done.
| |
19 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE; | |
20 const auto MEMORY_PRESSURE_LEVEL_CRITICAL = | |
21 MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
22 | |
18 MATCHER_P(IsSetSuppressedMessage, suppressed, "") { | 23 MATCHER_P(IsSetSuppressedMessage, suppressed, "") { |
19 // Ensure that the message is deleted upon return. | 24 // Ensure that the message is deleted upon return. |
20 scoped_ptr<IPC::Message> message(arg); | 25 scoped_ptr<IPC::Message> message(arg); |
21 if (message == nullptr) | 26 if (message == nullptr) |
22 return false; | 27 return false; |
23 MemoryMsg_SetPressureNotificationsSuppressed::Param param; | 28 MemoryMsg_SetPressureNotificationsSuppressed::Param param; |
24 if (!MemoryMsg_SetPressureNotificationsSuppressed::Read(message.get(), | 29 if (!MemoryMsg_SetPressureNotificationsSuppressed::Read(message.get(), |
25 ¶m)) | 30 ¶m)) |
26 return false; | 31 return false; |
27 return suppressed == base::get<0>(param); | 32 return suppressed == base::get<0>(param); |
28 } | 33 } |
29 | 34 |
35 MATCHER_P(IsSimulateMessage, level, "") { | |
36 // Ensure that the message is deleted upon return. | |
37 scoped_ptr<IPC::Message> message(arg); | |
38 if (message == nullptr) | |
39 return false; | |
40 MemoryMsg_SimulatePressureNotification::Param param; | |
41 if (!MemoryMsg_SimulatePressureNotification::Read(message.get(), ¶m)) | |
42 return false; | |
43 return level == base::get<0>(param); | |
44 } | |
45 | |
30 class MemoryMessageFilterForTesting : public MemoryMessageFilter { | 46 class MemoryMessageFilterForTesting : public MemoryMessageFilter { |
31 public: | 47 public: |
32 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 48 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
33 | 49 |
34 void Add() { | 50 void Add() { |
35 // The filter must be added on the IO thread. | 51 // The filter must be added on the IO thread. |
36 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 52 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
37 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 53 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
38 base::Bind(&MemoryMessageFilterForTesting::Add, | 54 base::Bind(&MemoryMessageFilterForTesting::Add, |
39 base::Unretained(this))); | 55 base::Unretained(this))); |
(...skipping 15 matching lines...) Expand all Loading... | |
55 } | 71 } |
56 OnChannelClosing(); | 72 OnChannelClosing(); |
57 OnFilterRemoved(); | 73 OnFilterRemoved(); |
58 } | 74 } |
59 | 75 |
60 protected: | 76 protected: |
61 ~MemoryMessageFilterForTesting() override {} | 77 ~MemoryMessageFilterForTesting() override {} |
62 }; | 78 }; |
63 | 79 |
64 class MemoryPressureControllerBrowserTest : public ContentBrowserTest { | 80 class MemoryPressureControllerBrowserTest : public ContentBrowserTest { |
81 public: | |
82 MOCK_METHOD1(OnMemoryPressure, void(MemoryPressureLevel level)); | |
83 | |
65 protected: | 84 protected: |
66 void SetPressureNotificationsSuppressedInAllProcessesAndWait( | 85 void SetPressureNotificationsSuppressedInAllProcessesAndWait( |
67 bool suppressed) { | 86 bool suppressed) { |
68 MemoryPressureController::GetInstance() | 87 MemoryPressureController::GetInstance() |
69 ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); | 88 ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); |
70 RunAllPendingInMessageLoop(BrowserThread::IO); | 89 RunAllPendingInMessageLoop(BrowserThread::IO); |
71 } | 90 } |
91 | |
92 void SimulatePressureNotificationInAllProcessesAndWait( | |
93 MemoryPressureLevel level) { | |
94 MemoryPressureController::GetInstance() | |
95 ->SimulatePressureNotificationInAllProcesses(level); | |
96 RunAllPendingInMessageLoop(BrowserThread::IO); | |
97 } | |
72 }; | 98 }; |
73 | 99 |
74 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, | 100 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
75 SetPressureNotificationsSuppressedInAllProcesses) { | 101 SetPressureNotificationsSuppressedInAllProcesses) { |
76 scoped_refptr<MemoryMessageFilterForTesting> filter1( | 102 scoped_refptr<MemoryMessageFilterForTesting> filter1( |
77 new MemoryMessageFilterForTesting); | 103 new MemoryMessageFilterForTesting); |
78 scoped_refptr<MemoryMessageFilterForTesting> filter2( | 104 scoped_refptr<MemoryMessageFilterForTesting> filter2( |
79 new MemoryMessageFilterForTesting); | 105 new MemoryMessageFilterForTesting); |
80 | 106 |
81 NavigateToURL(shell(), GetTestUrl("", "title.html")); | 107 NavigateToURL(shell(), GetTestUrl("", "title.html")); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 | 147 |
122 // Remove the second filter and disable suppressing memory pressure | 148 // Remove the second filter and disable suppressing memory pressure |
123 // notifications in all processes. No messages should be sent. | 149 // notifications in all processes. No messages should be sent. |
124 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); | 150 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
125 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); | 151 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
126 filter2->Remove(); | 152 filter2->Remove(); |
127 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); | 153 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); |
128 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); | 154 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
129 } | 155 } |
130 | 156 |
157 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, | |
158 SimulatePressureNotificationInAllProcesses) { | |
159 scoped_refptr<MemoryMessageFilterForTesting> filter( | |
160 new MemoryMessageFilterForTesting); | |
161 scoped_ptr<base::MemoryPressureListener> listener( | |
162 new base::MemoryPressureListener( | |
163 base::Bind(&MemoryPressureControllerBrowserTest::OnMemoryPressure, | |
164 base::Unretained(this)))); | |
165 | |
166 NavigateToURL(shell(), GetTestUrl("", "title.html")); | |
167 | |
168 filter->Add(); | |
169 | |
170 EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_CRITICAL))) | |
171 .Times(1); | |
172 EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_CRITICAL)).Times(1); | |
173 SimulatePressureNotificationInAllProcessesAndWait( | |
174 MEMORY_PRESSURE_LEVEL_CRITICAL); | |
175 RunAllPendingInMessageLoop(); // Force the listener to run. | |
Primiano Tucci (use gerrit)
2015/09/28 12:47:32
I'd reword the coomment to "Wait for the listener
petrcermak
2015/09/28 15:39:32
Done.
| |
176 | |
177 // Enable suppressing memory pressure notifications in all processes. This | |
178 // should have no impact on simulating memory pressure notifications. | |
179 EXPECT_CALL(*filter, Send(IsSetSuppressedMessage(true))).Times(1); | |
180 SetPressureNotificationsSuppressedInAllProcessesAndWait(true); | |
181 | |
182 EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_MODERATE))) | |
183 .Times(1); | |
184 EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_MODERATE)).Times(1); | |
185 SimulatePressureNotificationInAllProcessesAndWait( | |
186 MEMORY_PRESSURE_LEVEL_MODERATE); | |
187 RunAllPendingInMessageLoop(); // Force the listener to run. | |
188 | |
189 // Disable suppressing memory pressure notifications in all processes. This | |
190 // should have no impact on simulating memory pressure notifications. | |
191 EXPECT_CALL(*filter, Send(IsSetSuppressedMessage(false))).Times(1); | |
192 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); | |
193 | |
194 EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_MODERATE))) | |
195 .Times(1); | |
196 EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_MODERATE)).Times(1); | |
197 SimulatePressureNotificationInAllProcessesAndWait( | |
198 MEMORY_PRESSURE_LEVEL_MODERATE); | |
199 RunAllPendingInMessageLoop(); // Force the listener to run. | |
200 | |
201 filter->Remove(); | |
202 } | |
203 | |
131 } // namespace content | 204 } // namespace content |
OLD | NEW |