| 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 MATCHER_P(IsSetSuppressedMessage, suppressed, "") { | 18 MATCHER_P(IsSetSuppressedMessage, suppressed, "") { |
| 19 // Ensure that the message is deleted upon return. | 19 // Ensure that the message is deleted upon return. |
| 20 scoped_ptr<IPC::Message> message(arg); | 20 scoped_ptr<IPC::Message> message(arg); |
| 21 if (message == nullptr) | 21 if (message == nullptr) |
| 22 return false; | 22 return false; |
| 23 MemoryMsg_SetPressureNotificationsSuppressed::Param param; | 23 MemoryMsg_SetPressureNotificationsSuppressed::Param param; |
| 24 if (!MemoryMsg_SetPressureNotificationsSuppressed::Read(message.get(), | 24 if (!MemoryMsg_SetPressureNotificationsSuppressed::Read(message.get(), |
| 25 ¶m)) | 25 ¶m)) |
| 26 return false; | 26 return false; |
| 27 return suppressed == base::get<0>(param); | 27 return suppressed == base::get<0>(param); |
| 28 } | 28 } |
| 29 | 29 |
| 30 MATCHER_P(IsSimulateMessage, level, "") { |
| 31 // Ensure that the message is deleted upon return. |
| 32 scoped_ptr<IPC::Message> message(arg); |
| 33 if (message == nullptr) |
| 34 return false; |
| 35 MemoryMsg_SimulatePressureNotification::Param param; |
| 36 if (!MemoryMsg_SimulatePressureNotification::Read(message.get(), ¶m)) |
| 37 return false; |
| 38 return level == base::get<0>(param); |
| 39 } |
| 40 |
| 30 class MemoryMessageFilterForTesting : public MemoryMessageFilter { | 41 class MemoryMessageFilterForTesting : public MemoryMessageFilter { |
| 31 public: | 42 public: |
| 32 MOCK_METHOD1(Send, bool(IPC::Message* message)); | 43 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
| 33 | 44 |
| 34 void Add() { | 45 void Add() { |
| 35 // The filter must be added on the IO thread. | 46 // The filter must be added on the IO thread. |
| 36 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 47 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 37 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 38 base::Bind(&MemoryMessageFilterForTesting::Add, | 49 base::Bind(&MemoryMessageFilterForTesting::Add, |
| 39 base::Unretained(this))); | 50 base::Unretained(this))); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 } | 66 } |
| 56 OnChannelClosing(); | 67 OnChannelClosing(); |
| 57 OnFilterRemoved(); | 68 OnFilterRemoved(); |
| 58 } | 69 } |
| 59 | 70 |
| 60 protected: | 71 protected: |
| 61 ~MemoryMessageFilterForTesting() override {} | 72 ~MemoryMessageFilterForTesting() override {} |
| 62 }; | 73 }; |
| 63 | 74 |
| 64 class MemoryPressureControllerBrowserTest : public ContentBrowserTest { | 75 class MemoryPressureControllerBrowserTest : public ContentBrowserTest { |
| 76 public: |
| 77 MOCK_METHOD1(OnMemoryPressure, void(MemoryPressureLevel level)); |
| 78 |
| 65 protected: | 79 protected: |
| 66 void SetPressureNotificationsSuppressedInAllProcessesAndWait( | 80 void SetPressureNotificationsSuppressedInAllProcessesAndWait( |
| 67 bool suppressed) { | 81 bool suppressed) { |
| 68 MemoryPressureController::GetInstance() | 82 MemoryPressureController::GetInstance() |
| 69 ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); | 83 ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); |
| 70 RunAllPendingInMessageLoop(BrowserThread::IO); | 84 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 71 } | 85 } |
| 86 |
| 87 void SimulatePressureNotificationInAllProcessesAndWait( |
| 88 MemoryPressureLevel level) { |
| 89 MemoryPressureController::GetInstance() |
| 90 ->SimulatePressureNotificationInAllProcesses(level); |
| 91 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 92 } |
| 72 }; | 93 }; |
| 73 | 94 |
| 74 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, | 95 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| 75 SetPressureNotificationsSuppressedInAllProcesses) { | 96 SetPressureNotificationsSuppressedInAllProcesses) { |
| 76 scoped_refptr<MemoryMessageFilterForTesting> filter1( | 97 scoped_refptr<MemoryMessageFilterForTesting> filter1( |
| 77 new MemoryMessageFilterForTesting); | 98 new MemoryMessageFilterForTesting); |
| 78 scoped_refptr<MemoryMessageFilterForTesting> filter2( | 99 scoped_refptr<MemoryMessageFilterForTesting> filter2( |
| 79 new MemoryMessageFilterForTesting); | 100 new MemoryMessageFilterForTesting); |
| 80 | 101 |
| 81 NavigateToURL(shell(), GetTestUrl("", "title.html")); | 102 NavigateToURL(shell(), GetTestUrl("", "title.html")); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 | 142 |
| 122 // Remove the second filter and disable suppressing memory pressure | 143 // Remove the second filter and disable suppressing memory pressure |
| 123 // notifications in all processes. No messages should be sent. | 144 // notifications in all processes. No messages should be sent. |
| 124 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); | 145 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 125 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); | 146 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
| 126 filter2->Remove(); | 147 filter2->Remove(); |
| 127 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); | 148 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); |
| 128 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); | 149 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 129 } | 150 } |
| 130 | 151 |
| 152 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| 153 SimulatePressureNotificationInAllProcesses) { |
| 154 const auto MEMORY_PRESSURE_LEVEL_MODERATE = |
| 155 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| 156 const auto MEMORY_PRESSURE_LEVEL_CRITICAL = |
| 157 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 158 |
| 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(); // Wait for the listener to run. |
| 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(); // Wait for 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(); // Wait for the listener to run. |
| 200 |
| 201 filter->Remove(); |
| 202 } |
| 203 |
| 131 } // namespace content | 204 } // namespace content |
| OLD | NEW |