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

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

Issue 2561863003: Add test for MemoryCoordinatorImpl::ForceSetGlobalState() (Closed)
Patch Set: 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
« no previous file with comments | « content/browser/memory/memory_coordinator_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/browser/memory/memory_monitor.h" 11 #include "content/browser/memory/memory_monitor.h"
11 #include "content/public/common/content_features.h" 12 #include "content/public/common/content_features.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace content { 15 namespace content {
15 16
16 namespace { 17 namespace {
17 18
18 class MockMemoryCoordinatorClient : public base::MemoryCoordinatorClient { 19 class MockMemoryCoordinatorClient : public base::MemoryCoordinatorClient {
19 public: 20 public:
(...skipping 28 matching lines...) Expand all
48 int free_memory_ = 0; 49 int free_memory_ = 0;
49 50
50 DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor); 51 DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor);
51 }; 52 };
52 53
53 class MemoryCoordinatorImplTest : public testing::Test { 54 class MemoryCoordinatorImplTest : public testing::Test {
54 public: 55 public:
55 void SetUp() override { 56 void SetUp() override {
56 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator); 57 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator);
57 58
59 task_runner_ = new base::TestMockTimeTaskRunner();
58 coordinator_.reset(new MemoryCoordinatorImpl( 60 coordinator_.reset(new MemoryCoordinatorImpl(
59 message_loop_.task_runner(), base::WrapUnique(new MockMemoryMonitor))); 61 task_runner_, base::WrapUnique(new MockMemoryMonitor)));
60 62
61 base::MemoryCoordinatorProxy::GetInstance()-> 63 base::MemoryCoordinatorProxy::GetInstance()->
62 SetGetCurrentMemoryStateCallback(base::Bind( 64 SetGetCurrentMemoryStateCallback(base::Bind(
63 &MemoryCoordinator::GetCurrentMemoryState, 65 &MemoryCoordinator::GetCurrentMemoryState,
64 base::Unretained(coordinator_.get()))); 66 base::Unretained(coordinator_.get())));
65 base::MemoryCoordinatorProxy::GetInstance()-> 67 base::MemoryCoordinatorProxy::GetInstance()->
66 SetSetCurrentMemoryStateForTestingCallback(base::Bind( 68 SetSetCurrentMemoryStateForTestingCallback(base::Bind(
67 &MemoryCoordinator::SetCurrentMemoryStateForTesting, 69 &MemoryCoordinator::SetCurrentMemoryStateForTesting,
68 base::Unretained(coordinator_.get()))); 70 base::Unretained(coordinator_.get())));
69 } 71 }
70 72
71 MockMemoryMonitor* GetMockMemoryMonitor() { 73 MockMemoryMonitor* GetMockMemoryMonitor() {
72 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor()); 74 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor());
73 } 75 }
74 76
75 protected: 77 protected:
76 std::unique_ptr<MemoryCoordinatorImpl> coordinator_; 78 std::unique_ptr<MemoryCoordinatorImpl> coordinator_;
79 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
77 base::MessageLoop message_loop_; 80 base::MessageLoop message_loop_;
78 base::test::ScopedFeatureList scoped_feature_list_; 81 base::test::ScopedFeatureList scoped_feature_list_;
79 }; 82 };
80 83
81 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) { 84 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) {
82 coordinator_->expected_renderer_size_ = 10; 85 coordinator_->expected_renderer_size_ = 10;
83 coordinator_->new_renderers_until_throttled_ = 4; 86 coordinator_->new_renderers_until_throttled_ = 4;
84 coordinator_->new_renderers_until_suspended_ = 2; 87 coordinator_->new_renderers_until_suspended_ = 2;
85 coordinator_->new_renderers_back_to_normal_ = 5; 88 coordinator_->new_renderers_back_to_normal_ = 5;
86 coordinator_->new_renderers_back_to_throttled_ = 3; 89 coordinator_->new_renderers_back_to_throttled_ = 3;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 EXPECT_EQ(base::MemoryState::THROTTLED, 212 EXPECT_EQ(base::MemoryState::THROTTLED,
210 base::MemoryCoordinatorProxy::GetInstance()-> 213 base::MemoryCoordinatorProxy::GetInstance()->
211 GetCurrentMemoryState()); 214 GetCurrentMemoryState());
212 base::RunLoop loop; 215 base::RunLoop loop;
213 loop.RunUntilIdle(); 216 loop.RunUntilIdle();
214 EXPECT_TRUE(client.is_called()); 217 EXPECT_TRUE(client.is_called());
215 EXPECT_EQ(base::MemoryState::THROTTLED, client.state()); 218 EXPECT_EQ(base::MemoryState::THROTTLED, client.state());
216 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client); 219 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client);
217 } 220 }
218 221
222 TEST_F(MemoryCoordinatorImplTest, ForceSetGlobalState) {
223 coordinator_->expected_renderer_size_ = 10;
224 coordinator_->new_renderers_until_throttled_ = 4;
225 coordinator_->new_renderers_until_suspended_ = 2;
226 coordinator_->new_renderers_back_to_normal_ = 5;
227 coordinator_->new_renderers_back_to_throttled_ = 3;
228 DCHECK(coordinator_->ValidateParameters());
229 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
230
231 base::TimeDelta interval = base::TimeDelta::FromSeconds(5);
232 base::TimeDelta minimum_transition = base::TimeDelta::FromSeconds(30);
233 coordinator_->monitoring_interval_ = interval;
234 coordinator_->minimum_transition_period_ = minimum_transition;
235
236 // Starts updating states. The initial state should be NORMAL with above
237 // configuration.
238 coordinator_->Start();
239 task_runner_->RunUntilIdle();
240 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetGlobalMemoryState());
241
242 base::TimeDelta force_set_duration = interval * 3;
243 coordinator_->ForceSetGlobalState(base::MemoryState::SUSPENDED,
244 force_set_duration);
245 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->GetGlobalMemoryState());
246
247 // The state should remain SUSPENDED even after some monitoring period are
248 // passed.
249 task_runner_->FastForwardBy(interval * 2);
250 task_runner_->RunUntilIdle();
251 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->GetGlobalMemoryState());
252
253 // The state should be updated after |force_set_duration| is passed.
254 task_runner_->FastForwardBy(force_set_duration);
255 task_runner_->RunUntilIdle();
256 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetGlobalMemoryState());
257
258 // 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
260 // for |minimum_transition|.
261 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
262 task_runner_->FastForwardBy(minimum_transition);
263 task_runner_->RunUntilIdle();
264 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->GetGlobalMemoryState());
265 }
266
219 } // namespace content 267 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698