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