OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/memory/memory_pressure_listener.h" |
| 7 #include "content/browser/memory/memory_message_filter.h" |
| 8 #include "content/browser/memory/memory_pressure_controller.h" |
| 9 #include "content/common/memory_messages.h" |
| 10 #include "content/public/test/content_browser_test.h" |
| 11 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/public/test/test_utils.h" |
| 13 #include "ipc/ipc_message.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 |
| 16 namespace content { |
| 17 namespace { |
| 18 |
| 19 MATCHER_P(IsSetSuppressedMessage, suppressed, "") { |
| 20 if (arg == nullptr) |
| 21 return false; |
| 22 MemoryMsg_SetPressureNotificationsSuppressed::Param param; |
| 23 if (!MemoryMsg_SetPressureNotificationsSuppressed::Read(arg, ¶m)) |
| 24 return false; |
| 25 return suppressed == base::get<0>(param); |
| 26 } |
| 27 |
| 28 class MemoryMessageFilterForTesting : public MemoryMessageFilter { |
| 29 public: |
| 30 MOCK_METHOD1(Send, bool(IPC::Message* message)); |
| 31 |
| 32 void Add() { |
| 33 // The filter must be added on the IO thread. |
| 34 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 35 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 36 base::Bind(&MemoryMessageFilterForTesting::Add, |
| 37 base::Unretained(this))); |
| 38 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 39 return; |
| 40 } |
| 41 OnChannelConnected(0); |
| 42 OnFilterAdded(nullptr); |
| 43 } |
| 44 |
| 45 void Remove() { |
| 46 // The filter must be removed on the IO thread. |
| 47 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 48 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 49 base::Bind(&MemoryMessageFilterForTesting::Remove, |
| 50 base::Unretained(this))); |
| 51 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 52 return; |
| 53 } |
| 54 OnChannelClosing(); |
| 55 OnFilterRemoved(); |
| 56 } |
| 57 |
| 58 protected: |
| 59 ~MemoryMessageFilterForTesting() override {} |
| 60 }; |
| 61 |
| 62 class MemoryPressureControllerBrowserTest : public ContentBrowserTest { |
| 63 protected: |
| 64 void SetPressureNotificationsSuppressedInAllProcessesAndWait( |
| 65 bool suppressed) { |
| 66 MemoryPressureController::GetInstance() |
| 67 ->SetPressureNotificationsSuppressedInAllProcesses(suppressed); |
| 68 RunAllPendingInMessageLoop(BrowserThread::IO); |
| 69 } |
| 70 }; |
| 71 |
| 72 IN_PROC_BROWSER_TEST_F(MemoryPressureControllerBrowserTest, |
| 73 SetPressureNotificationsSuppressedInAllProcesses) { |
| 74 scoped_refptr<MemoryMessageFilterForTesting> filter1( |
| 75 new MemoryMessageFilterForTesting); |
| 76 scoped_refptr<MemoryMessageFilterForTesting> filter2( |
| 77 new MemoryMessageFilterForTesting); |
| 78 |
| 79 NavigateToURL(shell(), GetTestUrl("", "title.html")); |
| 80 |
| 81 // Add the first filter. No messages should be sent because notifications are |
| 82 // not suppressed. |
| 83 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 84 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
| 85 filter1->Add(); |
| 86 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 87 |
| 88 // Enable suppressing memory pressure notifications in all processes. The |
| 89 // first filter should send a message. |
| 90 EXPECT_CALL(*filter1, Send(IsSetSuppressedMessage(true))).Times(1); |
| 91 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
| 92 SetPressureNotificationsSuppressedInAllProcessesAndWait(true); |
| 93 EXPECT_TRUE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 94 |
| 95 // Add the second filter. It should send a message because notifications are |
| 96 // suppressed. |
| 97 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 98 EXPECT_CALL(*filter2, Send(IsSetSuppressedMessage(true))).Times(1); |
| 99 filter2->Add(); |
| 100 |
| 101 // Disable suppressing memory pressure notifications in all processes. Both |
| 102 // filters should send a message. |
| 103 EXPECT_CALL(*filter1, Send(IsSetSuppressedMessage(false))).Times(1); |
| 104 EXPECT_CALL(*filter2, Send(IsSetSuppressedMessage(false))).Times(1); |
| 105 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); |
| 106 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 107 |
| 108 // Remove the first filter. No messages should be sent. |
| 109 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 110 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
| 111 filter1->Remove(); |
| 112 |
| 113 // Enable suppressing memory pressure notifications in all processes. The |
| 114 // second filter should send a message. |
| 115 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 116 EXPECT_CALL(*filter2, Send(IsSetSuppressedMessage(true))).Times(1); |
| 117 SetPressureNotificationsSuppressedInAllProcessesAndWait(true); |
| 118 EXPECT_TRUE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 119 |
| 120 // Remove the second filter and disable suppressing memory pressure |
| 121 // notifications in all processes. No messages should be sent. |
| 122 EXPECT_CALL(*filter1, Send(testing::_)).Times(0); |
| 123 EXPECT_CALL(*filter2, Send(testing::_)).Times(0); |
| 124 filter2->Remove(); |
| 125 SetPressureNotificationsSuppressedInAllProcessesAndWait(false); |
| 126 EXPECT_FALSE(base::MemoryPressureListener::AreNotificationsSuppressed()); |
| 127 } |
| 128 |
| 129 } // namespace |
| 130 } // namespace content |
OLD | NEW |