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