Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: content/browser/memory/memory_coordinator_impl_unittest.cc

Issue 2565323002: Stop using callbacks in MemoryCoordinatorProxy (Closed)
Patch Set: fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/memory/memory_coordinator_impl.h" 5 #include "content/browser/memory/memory_coordinator_impl.h"
6 6
7 #include "base/memory/memory_coordinator_proxy.h" 7 #include "base/memory/memory_coordinator_proxy.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/scoped_feature_list.h" 9 #include "base/test/scoped_feature_list.h"
10 #include "base/test/test_mock_time_task_runner.h" 10 #include "base/test/test_mock_time_task_runner.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 53
54 class MemoryCoordinatorImplTest : public testing::Test { 54 class MemoryCoordinatorImplTest : public testing::Test {
55 public: 55 public:
56 void SetUp() override { 56 void SetUp() override {
57 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator); 57 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator);
58 58
59 task_runner_ = new base::TestMockTimeTaskRunner(); 59 task_runner_ = new base::TestMockTimeTaskRunner();
60 coordinator_.reset(new MemoryCoordinatorImpl( 60 coordinator_.reset(new MemoryCoordinatorImpl(
61 task_runner_, base::WrapUnique(new MockMemoryMonitor))); 61 task_runner_, base::WrapUnique(new MockMemoryMonitor)));
62 62
63 base::MemoryCoordinatorProxy::GetInstance()-> 63 base::MemoryCoordinatorProxy::GetInstance()->Set(
64 SetGetCurrentMemoryStateCallback(base::Bind( 64 coordinator_->GetWeakPtr());
65 &MemoryCoordinator::GetCurrentMemoryState,
66 base::Unretained(coordinator_.get())));
67 base::MemoryCoordinatorProxy::GetInstance()->
68 SetSetCurrentMemoryStateForTestingCallback(base::Bind(
69 &MemoryCoordinator::SetCurrentMemoryStateForTesting,
70 base::Unretained(coordinator_.get())));
71 } 65 }
72 66
73 MockMemoryMonitor* GetMockMemoryMonitor() { 67 MockMemoryMonitor* GetMockMemoryMonitor() {
74 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor()); 68 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor());
75 } 69 }
76 70
77 protected: 71 protected:
78 std::unique_ptr<MemoryCoordinatorImpl> coordinator_; 72 std::unique_ptr<MemoryCoordinatorImpl> coordinator_;
79 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; 73 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
80 base::MessageLoop message_loop_; 74 base::MessageLoop message_loop_;
81 base::test::ScopedFeatureList scoped_feature_list_; 75 base::test::ScopedFeatureList scoped_feature_list_;
82 }; 76 };
83 77
84 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) { 78 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) {
85 coordinator_->expected_renderer_size_ = 10; 79 coordinator_->expected_renderer_size_ = 10;
86 coordinator_->new_renderers_until_throttled_ = 4; 80 coordinator_->new_renderers_until_throttled_ = 4;
87 coordinator_->new_renderers_until_suspended_ = 2; 81 coordinator_->new_renderers_until_suspended_ = 2;
88 coordinator_->new_renderers_back_to_normal_ = 5; 82 coordinator_->new_renderers_back_to_normal_ = 5;
89 coordinator_->new_renderers_back_to_throttled_ = 3; 83 coordinator_->new_renderers_back_to_throttled_ = 3;
90 DCHECK(coordinator_->ValidateParameters()); 84 DCHECK(coordinator_->ValidateParameters());
91 85
92 // The default state is NORMAL. 86 // The default state is NORMAL.
93 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); 87 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetLocalMemoryState());
94 EXPECT_EQ(base::MemoryState::NORMAL, 88 EXPECT_EQ(base::MemoryState::NORMAL,
95 base::MemoryCoordinatorProxy::GetInstance()-> 89 base::MemoryCoordinatorProxy::GetInstance()->
96 GetCurrentMemoryState()); 90 GetLocalMemoryState());
97 91
98 // Transitions from NORMAL 92 // Transitions from NORMAL
99 coordinator_->current_state_ = base::MemoryState::NORMAL; 93 coordinator_->current_state_ = base::MemoryState::NORMAL;
100 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); 94 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetLocalMemoryState());
101 EXPECT_EQ(base::MemoryState::NORMAL, 95 EXPECT_EQ(base::MemoryState::NORMAL,
102 base::MemoryCoordinatorProxy::GetInstance()-> 96 base::MemoryCoordinatorProxy::GetInstance()->
103 GetCurrentMemoryState()); 97 GetLocalMemoryState());
104 98
105 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); 99 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
106 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); 100 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
107 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); 101 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
108 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); 102 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
109 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); 103 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
110 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); 104 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
111 105
112 // Transitions from THROTTLED 106 // Transitions from THROTTLED
113 coordinator_->current_state_ = base::MemoryState::THROTTLED; 107 coordinator_->current_state_ = base::MemoryState::THROTTLED;
114 EXPECT_EQ(base::MemoryState::THROTTLED, 108 EXPECT_EQ(base::MemoryState::THROTTLED,
115 coordinator_->GetCurrentMemoryState()); 109 coordinator_->GetLocalMemoryState());
116 EXPECT_EQ(base::MemoryState::THROTTLED, 110 EXPECT_EQ(base::MemoryState::THROTTLED,
117 base::MemoryCoordinatorProxy::GetInstance()-> 111 base::MemoryCoordinatorProxy::GetInstance()->
118 GetCurrentMemoryState()); 112 GetLocalMemoryState());
119 113
120 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); 114 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
121 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); 115 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
122 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); 116 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
123 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); 117 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
124 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); 118 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
125 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); 119 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
126 120
127 // Transitions from SUSPENDED 121 // Transitions from SUSPENDED
128 coordinator_->current_state_ = base::MemoryState::SUSPENDED; 122 coordinator_->current_state_ = base::MemoryState::SUSPENDED;
129 // GetCurrentMemoryState() returns THROTTLED state for the browser process 123 // GetLocalMemoryState() returns THROTTLED state for the browser process
130 // when the global state is SUSPENDED. 124 // when the global state is SUSPENDED.
131 EXPECT_EQ(base::MemoryState::THROTTLED, 125 EXPECT_EQ(base::MemoryState::THROTTLED,
132 coordinator_->GetCurrentMemoryState()); 126 coordinator_->GetLocalMemoryState());
133 EXPECT_EQ(base::MemoryState::THROTTLED, 127 EXPECT_EQ(base::MemoryState::THROTTLED,
134 base::MemoryCoordinatorProxy::GetInstance()-> 128 base::MemoryCoordinatorProxy::GetInstance()->
135 GetCurrentMemoryState()); 129 GetLocalMemoryState());
136 130
137 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); 131 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
138 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); 132 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
139 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(30); 133 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(30);
140 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); 134 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
141 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); 135 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
142 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); 136 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
143 } 137 }
144 138
145 TEST_F(MemoryCoordinatorImplTest, UpdateState) { 139 TEST_F(MemoryCoordinatorImplTest, UpdateState) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 TEST_F(MemoryCoordinatorImplTest, SetMemoryStateForTesting) { 176 TEST_F(MemoryCoordinatorImplTest, SetMemoryStateForTesting) {
183 coordinator_->expected_renderer_size_ = 10; 177 coordinator_->expected_renderer_size_ = 10;
184 coordinator_->new_renderers_until_throttled_ = 4; 178 coordinator_->new_renderers_until_throttled_ = 4;
185 coordinator_->new_renderers_until_suspended_ = 2; 179 coordinator_->new_renderers_until_suspended_ = 2;
186 coordinator_->new_renderers_back_to_normal_ = 5; 180 coordinator_->new_renderers_back_to_normal_ = 5;
187 coordinator_->new_renderers_back_to_throttled_ = 3; 181 coordinator_->new_renderers_back_to_throttled_ = 3;
188 DCHECK(coordinator_->ValidateParameters()); 182 DCHECK(coordinator_->ValidateParameters());
189 183
190 MockMemoryCoordinatorClient client; 184 MockMemoryCoordinatorClient client;
191 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(&client); 185 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(&client);
192 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); 186 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetLocalMemoryState());
193 EXPECT_EQ(base::MemoryState::NORMAL, 187 EXPECT_EQ(base::MemoryState::NORMAL,
194 base::MemoryCoordinatorProxy::GetInstance()-> 188 base::MemoryCoordinatorProxy::GetInstance()->GetLocalMemoryState());
195 GetCurrentMemoryState());
196 EXPECT_EQ(base::MemoryState::NORMAL, client.state()); 189 EXPECT_EQ(base::MemoryState::NORMAL, client.state());
197 190
198 base::MemoryCoordinatorProxy::GetInstance()->SetCurrentMemoryStateForTesting( 191 base::MemoryCoordinatorProxy::GetInstance()->SetMemoryStateForTesting(
199 base::MemoryState::SUSPENDED); 192 base::MemoryState::SUSPENDED);
200 // GetCurrentMemoryState() returns THROTTLED state for the browser process 193 // GetLocalMemoryState() returns THROTTLED state for the browser process
201 // when the global state is SUSPENDED. 194 // when the global state is SUSPENDED.
202 EXPECT_EQ(base::MemoryState::THROTTLED, 195 EXPECT_EQ(base::MemoryState::THROTTLED,
203 coordinator_->GetCurrentMemoryState()); 196 coordinator_->GetLocalMemoryState());
204 EXPECT_EQ(base::MemoryState::THROTTLED, 197 EXPECT_EQ(base::MemoryState::THROTTLED,
205 base::MemoryCoordinatorProxy::GetInstance()-> 198 base::MemoryCoordinatorProxy::GetInstance()->GetLocalMemoryState());
206 GetCurrentMemoryState());
207 199
208 base::MemoryCoordinatorProxy::GetInstance()->SetCurrentMemoryStateForTesting( 200 base::MemoryCoordinatorProxy::GetInstance()->SetMemoryStateForTesting(
209 base::MemoryState::THROTTLED); 201 base::MemoryState::THROTTLED);
210 EXPECT_EQ(base::MemoryState::THROTTLED, 202 EXPECT_EQ(base::MemoryState::THROTTLED,
211 coordinator_->GetCurrentMemoryState()); 203 coordinator_->GetLocalMemoryState());
212 EXPECT_EQ(base::MemoryState::THROTTLED, 204 EXPECT_EQ(base::MemoryState::THROTTLED,
213 base::MemoryCoordinatorProxy::GetInstance()-> 205 base::MemoryCoordinatorProxy::GetInstance()->GetLocalMemoryState());
214 GetCurrentMemoryState());
215 base::RunLoop loop; 206 base::RunLoop loop;
216 loop.RunUntilIdle(); 207 loop.RunUntilIdle();
217 EXPECT_TRUE(client.is_called()); 208 EXPECT_TRUE(client.is_called());
218 EXPECT_EQ(base::MemoryState::THROTTLED, client.state()); 209 EXPECT_EQ(base::MemoryState::THROTTLED, client.state());
219 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client); 210 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client);
220 } 211 }
221 212
222 TEST_F(MemoryCoordinatorImplTest, ForceSetGlobalState) { 213 TEST_F(MemoryCoordinatorImplTest, ForceSetGlobalState) {
223 coordinator_->expected_renderer_size_ = 10; 214 coordinator_->expected_renderer_size_ = 10;
224 coordinator_->new_renderers_until_throttled_ = 4; 215 coordinator_->new_renderers_until_throttled_ = 4;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 // Also make sure that the state is updated based on free avaiable memory. 249 // Also make sure that the state is updated based on free avaiable memory.
259 // Since the global state has changed in the previous task, we have to wait 250 // Since the global state has changed in the previous task, we have to wait
260 // for |minimum_transition|. 251 // for |minimum_transition|.
261 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); 252 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
262 task_runner_->FastForwardBy(minimum_transition); 253 task_runner_->FastForwardBy(minimum_transition);
263 task_runner_->RunUntilIdle(); 254 task_runner_->RunUntilIdle();
264 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->GetGlobalMemoryState()); 255 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->GetGlobalMemoryState());
265 } 256 }
266 257
267 } // namespace content 258 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698