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

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

Issue 2374343002: Add MemoryCoordinatorImpl (Closed)
Patch Set: Add comments and tests Created 4 years, 2 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/memory/memory_coordinator_impl.h"
6
7 #include "base/run_loop.h"
8 #include "content/browser/memory/memory_monitor.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace content {
12
13 namespace {
14
15 class MockMemoryCoordinatorClient : public base::MemoryCoordinatorClient {
16 public:
17 void OnMemoryStateChange(base::MemoryState state) override {
18 is_called_ = true;
19 state_ = state;
20 }
21
22 bool is_called() { return is_called_; }
23 base::MemoryState state() { return state_; }
24
25 private:
26 bool is_called_ = false;
27 base::MemoryState state_ = base::MemoryState::NORMAL;
28 };
29
30 } // namespace
31
32 class MockMemoryMonitor : public MemoryMonitor {
33 public:
34 MockMemoryMonitor() {}
35 ~MockMemoryMonitor() override {}
36
37 void SetFreeMemoryUntilCriticalMB(int free_memory) {
38 free_memory_ = free_memory;
39 }
40
41 // MemoryMonitor implementation
42 int GetFreeMemoryUntilCriticalMB() override { return free_memory_; }
43
44 private:
45 int free_memory_ = 0;
46
47 DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor);
48 };
49
50 class MemoryCoordinatorImplTest : public testing::Test {
51 public:
52 void SetUp() override {
53 coordinator_.reset(new MemoryCoordinatorImpl(
54 message_loop_.task_runner(), base::WrapUnique(new MockMemoryMonitor)));
55 }
56
57 MockMemoryMonitor* GetMockMemoryMonitor() {
58 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor());
59 }
60
61 protected:
62 std::unique_ptr<MemoryCoordinatorImpl> coordinator_;
63 base::MessageLoop message_loop_;
64 };
65
66 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) {
67 coordinator_->predicted_renderer_size_ = 10;
bashi 2016/10/07 02:10:20 Set parameters explicitly to make tests clear
68 coordinator_->new_renderers_until_throttled_ = 4;
69 coordinator_->new_renderers_until_suspended_ = 2;
70 coordinator_->new_renderers_back_to_normal_ = 5;
71 coordinator_->new_renderers_back_to_throttled_ = 3;
72 DCHECK(coordinator_->ValidateParameters());
73
74 // Transitions from NORMAL
75 coordinator_->current_state_ = base::MemoryState::NORMAL;
76 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
77 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
78 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
79 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
80 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
81 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
82
83 // Transitions from THROTTLED
84 coordinator_->current_state_ = base::MemoryState::THROTTLED;
85 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
86 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
87 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
88 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
89 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
90 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
91
92 // Transitions from SUSPENDED
93 coordinator_->current_state_ = base::MemoryState::SUSPENDED;
94 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20);
95 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState());
96 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(30);
97 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState());
98 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
99 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState());
100 }
101
102 TEST_F(MemoryCoordinatorImplTest, UpdateState) {
103 coordinator_->predicted_renderer_size_ = 10;
104 coordinator_->new_renderers_until_throttled_ = 4;
105 coordinator_->new_renderers_until_suspended_ = 2;
106 coordinator_->new_renderers_back_to_normal_ = 5;
107 coordinator_->new_renderers_back_to_throttled_ = 3;
108 DCHECK(coordinator_->ValidateParameters());
109
110 {
111 // Transition happens (NORMAL -> THROTTLED).
112 MockMemoryCoordinatorClient client;
113 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(&client);
114 coordinator_->current_state_ = base::MemoryState::NORMAL;
115 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
116 coordinator_->UpdateState();
117 base::RunLoop loop;
118 loop.RunUntilIdle();
119 EXPECT_TRUE(client.is_called());
120 EXPECT_EQ(base::MemoryState::THROTTLED, client.state());
121 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client);
122 }
123
124 {
125 // No transtion (NORMAL -> NORMAL). OnStateChange shouldn't be called.
126 MockMemoryCoordinatorClient client;
127 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(&client);
128 coordinator_->current_state_ = base::MemoryState::NORMAL;
129 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50);
130 coordinator_->UpdateState();
131 base::RunLoop loop;
132 loop.RunUntilIdle();
133 EXPECT_FALSE(client.is_called());
134 EXPECT_EQ(base::MemoryState::NORMAL, client.state());
135 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client);
136 }
137 }
138
139 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_impl.cc ('k') | content/browser/memory/memory_coordinator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698