Chromium Code Reviews| Index: content/browser/memory/memory_pressure_controller_browsertest.cc |
| diff --git a/content/browser/memory/memory_pressure_controller_browsertest.cc b/content/browser/memory/memory_pressure_controller_browsertest.cc |
| index b1452d84ae122324a94d6658f33307e0d9d5f477..36da0f866a2d33f24a4e3f9e9e09bde617b3f6ef 100644 |
| --- a/content/browser/memory/memory_pressure_controller_browsertest.cc |
| +++ b/content/browser/memory/memory_pressure_controller_browsertest.cc |
| @@ -15,6 +15,11 @@ |
| namespace content { |
| +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.
|
| + MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE; |
| +const auto MEMORY_PRESSURE_LEVEL_CRITICAL = |
| + MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| + |
| MATCHER_P(IsSetSuppressedMessage, suppressed, "") { |
| // Ensure that the message is deleted upon return. |
| scoped_ptr<IPC::Message> message(arg); |
| @@ -27,6 +32,17 @@ MATCHER_P(IsSetSuppressedMessage, suppressed, "") { |
| return suppressed == base::get<0>(param); |
| } |
| +MATCHER_P(IsSimulateMessage, level, "") { |
| + // Ensure that the message is deleted upon return. |
| + scoped_ptr<IPC::Message> message(arg); |
| + if (message == nullptr) |
| + return false; |
| + MemoryMsg_SimulatePressureNotification::Param param; |
| + if (!MemoryMsg_SimulatePressureNotification::Read(message.get(), ¶m)) |
| + return false; |
| + return level == base::get<0>(param); |
| +} |
| + |
| class MemoryMessageFilterForTesting : public MemoryMessageFilter { |
| public: |
| MOCK_METHOD1(Send, bool(IPC::Message* message)); |
| @@ -62,6 +78,9 @@ class MemoryMessageFilterForTesting : public MemoryMessageFilter { |
| }; |
| class MemoryPressureControllerBrowserTest : public ContentBrowserTest { |
| + public: |
| + MOCK_METHOD1(OnMemoryPressure, void(MemoryPressureLevel level)); |
| + |
| protected: |
| void SetPressureNotificationsSuppressedInAllProcessesAndWait( |
| bool suppressed) { |
| @@ -69,6 +88,13 @@ class MemoryPressureControllerBrowserTest : public ContentBrowserTest { |
| ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); |
| RunAllPendingInMessageLoop(BrowserThread::IO); |
| } |
| + |
| + void SimulatePressureNotificationInAllProcessesAndWait( |
| + MemoryPressureLevel level) { |
| + MemoryPressureController::GetInstance() |
| + ->SimulatePressureNotificationInAllProcesses(level); |
| + RunAllPendingInMessageLoop(BrowserThread::IO); |
| + } |
| }; |
| IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| @@ -128,4 +154,51 @@ IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| } |
| +IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| + SimulatePressureNotificationInAllProcesses) { |
| + scoped_refptr<MemoryMessageFilterForTesting> filter( |
| + new MemoryMessageFilterForTesting); |
| + scoped_ptr<base::MemoryPressureListener> listener( |
| + new base::MemoryPressureListener( |
| + base::Bind(&MemoryPressureControllerBrowserTest::OnMemoryPressure, |
| + base::Unretained(this)))); |
| + |
| + NavigateToURL(shell(), GetTestUrl("", "title.html")); |
| + |
| + filter->Add(); |
| + |
| + EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_CRITICAL))) |
| + .Times(1); |
| + EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_CRITICAL)).Times(1); |
| + SimulatePressureNotificationInAllProcessesAndWait( |
| + MEMORY_PRESSURE_LEVEL_CRITICAL); |
| + 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.
|
| + |
| + // Enable suppressing memory pressure notifications in all processes. This |
| + // should have no impact on simulating memory pressure notifications. |
| + EXPECT_CALL(*filter, Send(IsSetSuppressedMessage(true))).Times(1); |
| + SetPressureNotificationsSuppressedInAllProcessesAndWait(true); |
| + |
| + EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_MODERATE))) |
| + .Times(1); |
| + EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_MODERATE)).Times(1); |
| + SimulatePressureNotificationInAllProcessesAndWait( |
| + MEMORY_PRESSURE_LEVEL_MODERATE); |
| + RunAllPendingInMessageLoop(); // Force the listener to run. |
| + |
| + // Disable suppressing memory pressure notifications in all processes. This |
| + // should have no impact on simulating memory pressure notifications. |
| + EXPECT_CALL(*filter, Send(IsSetSuppressedMessage(false))).Times(1); |
| + SetPressureNotificationsSuppressedInAllProcessesAndWait(false); |
| + |
| + EXPECT_CALL(*filter, Send(IsSimulateMessage(MEMORY_PRESSURE_LEVEL_MODERATE))) |
| + .Times(1); |
| + EXPECT_CALL(*this, OnMemoryPressure(MEMORY_PRESSURE_LEVEL_MODERATE)).Times(1); |
| + SimulatePressureNotificationInAllProcessesAndWait( |
| + MEMORY_PRESSURE_LEVEL_MODERATE); |
| + RunAllPendingInMessageLoop(); // Force the listener to run. |
| + |
| + filter->Remove(); |
| +} |
| + |
| } // namespace content |