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.h" | 5 #include "content/browser/memory/memory_coordinator.h" |
6 | 6 |
7 #include "base/memory/memory_pressure_monitor.h" | 7 #include "base/memory/memory_pressure_monitor.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
11 | 11 |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 void RunUntilIdle() { | 18 void RunUntilIdle() { |
19 base::RunLoop loop; | 19 base::RunLoop loop; |
20 loop.RunUntilIdle(); | 20 loop.RunUntilIdle(); |
21 } | 21 } |
22 | 22 |
23 } // namespace | 23 } // namespace |
24 | 24 |
25 // A mock ChildMemoryCoordinator, for testing interaction between MC and CMC. | 25 // A mock ChildMemoryCoordinator, for testing interaction between MC and CMC. |
26 class MockChildMemoryCoordinator : public mojom::ChildMemoryCoordinator { | 26 class MockChildMemoryCoordinator : public mojom::ChildMemoryCoordinator { |
27 public: | 27 public: |
28 MockChildMemoryCoordinator() | 28 MockChildMemoryCoordinator() |
29 : state_(mojom::MemoryState::UNKNOWN), | 29 : state_(mojom::MemoryState::NORMAL), |
30 on_state_change_calls_(0) {} | 30 on_state_change_calls_(0) {} |
31 | 31 |
32 ~MockChildMemoryCoordinator() override {} | 32 ~MockChildMemoryCoordinator() override {} |
33 | 33 |
34 void OnStateChange(mojom::MemoryState state) override { | 34 void OnStateChange(mojom::MemoryState state) override { |
35 state_ = state; | 35 state_ = state; |
36 ++on_state_change_calls_; | 36 ++on_state_change_calls_; |
37 } | 37 } |
38 | 38 |
39 mojom::MemoryState state() const { return state_; } | 39 mojom::MemoryState state() const { return state_; } |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 EXPECT_EQ(1, cmc1->on_state_change_calls()); | 131 EXPECT_EQ(1, cmc1->on_state_change_calls()); |
132 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc1->state()); | 132 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc1->state()); |
133 | 133 |
134 EXPECT_TRUE(mc.SetChildMemoryState(2, mojom::MemoryState::SUSPENDED)); | 134 EXPECT_TRUE(mc.SetChildMemoryState(2, mojom::MemoryState::SUSPENDED)); |
135 EXPECT_EQ(1, cmc2->on_state_change_calls()); | 135 EXPECT_EQ(1, cmc2->on_state_change_calls()); |
136 // Child processes are considered as visible (foreground) by default, | 136 // Child processes are considered as visible (foreground) by default, |
137 // and visible ones won't be suspended but throttled. | 137 // and visible ones won't be suspended but throttled. |
138 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc2->state()); | 138 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc2->state()); |
139 } | 139 } |
140 | 140 |
| 141 TEST_F(MemoryCoordinatorTest, SetChildMemoryState) { |
| 142 TestMemoryCoordinator mc; |
| 143 auto cmc = mc.CreateChildMemoryCoordinator(1); |
| 144 auto iter = mc.children().find(1); |
| 145 ASSERT_TRUE(iter != mc.children().end()); |
| 146 |
| 147 // Foreground |
| 148 iter->second.is_visible = true; |
| 149 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::NORMAL)); |
| 150 EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state()); |
| 151 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::THROTTLED)); |
| 152 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); |
| 153 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::SUSPENDED)); |
| 154 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); |
| 155 |
| 156 // Background |
| 157 iter->second.is_visible = false; |
| 158 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::NORMAL)); |
| 159 #if defined(OS_ANDROID) |
| 160 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); |
| 161 #else |
| 162 EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state()); |
| 163 #endif |
| 164 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::THROTTLED)); |
| 165 EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); |
| 166 EXPECT_TRUE(mc.SetChildMemoryState(1, mojom::MemoryState::SUSPENDED)); |
| 167 EXPECT_EQ(mojom::MemoryState::SUSPENDED, cmc->state()); |
| 168 } |
| 169 |
141 } // namespace content | 170 } // namespace content |
OLD | NEW |