OLD | NEW |
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/run_loop.h" | 8 #include "base/run_loop.h" |
8 #include "content/browser/memory/memory_monitor.h" | 9 #include "content/browser/memory/memory_monitor.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 | 11 |
11 namespace content { | 12 namespace content { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 class MockMemoryCoordinatorClient : public base::MemoryCoordinatorClient { | 16 class MockMemoryCoordinatorClient : public base::MemoryCoordinatorClient { |
16 public: | 17 public: |
(...skipping 26 matching lines...) Expand all Loading... |
43 | 44 |
44 private: | 45 private: |
45 int free_memory_ = 0; | 46 int free_memory_ = 0; |
46 | 47 |
47 DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor); | 48 DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor); |
48 }; | 49 }; |
49 | 50 |
50 class MemoryCoordinatorImplTest : public testing::Test { | 51 class MemoryCoordinatorImplTest : public testing::Test { |
51 public: | 52 public: |
52 void SetUp() override { | 53 void SetUp() override { |
| 54 MemoryCoordinator::EnableFeaturesForTesting(); |
| 55 |
53 coordinator_.reset(new MemoryCoordinatorImpl( | 56 coordinator_.reset(new MemoryCoordinatorImpl( |
54 message_loop_.task_runner(), base::WrapUnique(new MockMemoryMonitor))); | 57 message_loop_.task_runner(), base::WrapUnique(new MockMemoryMonitor))); |
| 58 |
| 59 base::MemoryCoordinatorProxy::GetInstance()-> |
| 60 SetGetCurrentMemoryStateCallback(base::Bind( |
| 61 &MemoryCoordinator::GetCurrentMemoryState, |
| 62 base::Unretained(coordinator_.get()))); |
55 } | 63 } |
56 | 64 |
57 MockMemoryMonitor* GetMockMemoryMonitor() { | 65 MockMemoryMonitor* GetMockMemoryMonitor() { |
58 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor()); | 66 return static_cast<MockMemoryMonitor*>(coordinator_->memory_monitor()); |
59 } | 67 } |
60 | 68 |
61 protected: | 69 protected: |
62 std::unique_ptr<MemoryCoordinatorImpl> coordinator_; | 70 std::unique_ptr<MemoryCoordinatorImpl> coordinator_; |
63 base::MessageLoop message_loop_; | 71 base::MessageLoop message_loop_; |
64 }; | 72 }; |
65 | 73 |
66 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) { | 74 TEST_F(MemoryCoordinatorImplTest, CalculateNextState) { |
67 coordinator_->expected_renderer_size_ = 10; | 75 coordinator_->expected_renderer_size_ = 10; |
68 coordinator_->new_renderers_until_throttled_ = 4; | 76 coordinator_->new_renderers_until_throttled_ = 4; |
69 coordinator_->new_renderers_until_suspended_ = 2; | 77 coordinator_->new_renderers_until_suspended_ = 2; |
70 coordinator_->new_renderers_back_to_normal_ = 5; | 78 coordinator_->new_renderers_back_to_normal_ = 5; |
71 coordinator_->new_renderers_back_to_throttled_ = 3; | 79 coordinator_->new_renderers_back_to_throttled_ = 3; |
72 DCHECK(coordinator_->ValidateParameters()); | 80 DCHECK(coordinator_->ValidateParameters()); |
73 | 81 |
74 // The default state is NORMAL. | 82 // The default state is NORMAL. |
75 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); | 83 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); |
| 84 EXPECT_EQ(base::MemoryState::NORMAL, |
| 85 base::MemoryCoordinatorProxy::GetInstance()-> |
| 86 GetCurrentMemoryState()); |
76 | 87 |
77 // Transitions from NORMAL | 88 // Transitions from NORMAL |
78 coordinator_->current_state_ = base::MemoryState::NORMAL; | 89 coordinator_->current_state_ = base::MemoryState::NORMAL; |
79 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); | 90 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->GetCurrentMemoryState()); |
| 91 EXPECT_EQ(base::MemoryState::NORMAL, |
| 92 base::MemoryCoordinatorProxy::GetInstance()-> |
| 93 GetCurrentMemoryState()); |
| 94 |
80 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); | 95 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); |
81 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); | 96 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); |
82 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); | 97 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); |
83 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); | 98 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); |
84 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); | 99 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); |
85 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); | 100 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); |
86 | 101 |
87 // Transitions from THROTTLED | 102 // Transitions from THROTTLED |
88 coordinator_->current_state_ = base::MemoryState::THROTTLED; | 103 coordinator_->current_state_ = base::MemoryState::THROTTLED; |
89 EXPECT_EQ(base::MemoryState::THROTTLED, | 104 EXPECT_EQ(base::MemoryState::THROTTLED, |
90 coordinator_->GetCurrentMemoryState()); | 105 coordinator_->GetCurrentMemoryState()); |
| 106 EXPECT_EQ(base::MemoryState::THROTTLED, |
| 107 base::MemoryCoordinatorProxy::GetInstance()-> |
| 108 GetCurrentMemoryState()); |
| 109 |
91 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); | 110 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); |
92 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); | 111 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); |
93 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); | 112 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); |
94 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); | 113 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); |
95 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); | 114 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); |
96 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); | 115 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); |
97 | 116 |
98 // Transitions from SUSPENDED | 117 // Transitions from SUSPENDED |
99 coordinator_->current_state_ = base::MemoryState::SUSPENDED; | 118 coordinator_->current_state_ = base::MemoryState::SUSPENDED; |
100 EXPECT_EQ(base::MemoryState::SUSPENDED, | 119 EXPECT_EQ(base::MemoryState::SUSPENDED, |
101 coordinator_->GetCurrentMemoryState()); | 120 coordinator_->GetCurrentMemoryState()); |
| 121 EXPECT_EQ(base::MemoryState::SUSPENDED, |
| 122 base::MemoryCoordinatorProxy::GetInstance()-> |
| 123 GetCurrentMemoryState()); |
| 124 |
102 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); | 125 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(20); |
103 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); | 126 EXPECT_EQ(base::MemoryState::SUSPENDED, coordinator_->CalculateNextState()); |
104 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(30); | 127 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(30); |
105 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); | 128 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->CalculateNextState()); |
106 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); | 129 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(50); |
107 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); | 130 EXPECT_EQ(base::MemoryState::NORMAL, coordinator_->CalculateNextState()); |
108 } | 131 } |
109 | 132 |
110 TEST_F(MemoryCoordinatorImplTest, UpdateState) { | 133 TEST_F(MemoryCoordinatorImplTest, UpdateState) { |
111 coordinator_->expected_renderer_size_ = 10; | 134 coordinator_->expected_renderer_size_ = 10; |
(...skipping 26 matching lines...) Expand all Loading... |
138 coordinator_->UpdateState(); | 161 coordinator_->UpdateState(); |
139 base::RunLoop loop; | 162 base::RunLoop loop; |
140 loop.RunUntilIdle(); | 163 loop.RunUntilIdle(); |
141 EXPECT_FALSE(client.is_called()); | 164 EXPECT_FALSE(client.is_called()); |
142 EXPECT_EQ(base::MemoryState::NORMAL, client.state()); | 165 EXPECT_EQ(base::MemoryState::NORMAL, client.state()); |
143 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client); | 166 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(&client); |
144 } | 167 } |
145 } | 168 } |
146 | 169 |
147 } // namespace content | 170 } // namespace content |
OLD | NEW |