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