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

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

Issue 2590773002: Add a public interface for content::MemoryCoordinator (Closed)
Patch Set: rebase Created 3 years, 11 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
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_client_registry.h" 7 #include "base/memory/memory_coordinator_client_registry.h"
8 #include "base/memory/memory_coordinator_proxy.h" 8 #include "base/memory/memory_coordinator_proxy.h"
9 #include "base/memory/memory_pressure_monitor.h" 9 #include "base/memory/memory_pressure_monitor.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/test/multiprocess_test.h"
13 #include "base/test/scoped_feature_list.h" 14 #include "base/test/scoped_feature_list.h"
14 #include "base/test/test_mock_time_task_runner.h" 15 #include "base/test/test_mock_time_task_runner.h"
15 #include "content/browser/memory/memory_monitor.h" 16 #include "content/browser/memory/memory_monitor.h"
16 #include "content/browser/memory/memory_state_updater.h" 17 #include "content/browser/memory/memory_state_updater.h"
17 #include "content/public/common/content_features.h" 18 #include "content/public/common/content_features.h"
18 #include "content/public/test/mock_render_process_host.h" 19 #include "content/public/test/mock_render_process_host.h"
19 #include "content/public/test/test_browser_context.h" 20 #include "content/public/test/test_browser_context.h"
20 #include "mojo/public/cpp/bindings/binding.h" 21 #include "mojo/public/cpp/bindings/binding.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 return result; 156 return result;
156 } 157 }
157 158
158 TestBrowserContext browser_context_; 159 TestBrowserContext browser_context_;
159 std::vector<std::unique_ptr<Child>> children_; 160 std::vector<std::unique_ptr<Child>> children_;
160 std::map<int, std::unique_ptr<MockRenderProcessHost>> render_process_hosts_; 161 std::map<int, std::unique_ptr<MockRenderProcessHost>> render_process_hosts_;
161 }; 162 };
162 163
163 } // namespace 164 } // namespace
164 165
165 class MemoryCoordinatorImplTest : public testing::Test { 166 class MemoryCoordinatorImplTest : public base::MultiProcessTest {
166 public: 167 public:
167 using MemoryState = base::MemoryState; 168 using MemoryState = base::MemoryState;
168 169
169 void SetUp() override { 170 void SetUp() override {
171 base::MultiProcessTest::SetUp();
170 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator); 172 scoped_feature_list_.InitAndEnableFeature(features::kMemoryCoordinator);
171 173
172 task_runner_ = new base::TestMockTimeTaskRunner(); 174 task_runner_ = new base::TestMockTimeTaskRunner();
173 coordinator_.reset(new TestMemoryCoordinatorImpl(task_runner_)); 175 coordinator_.reset(new TestMemoryCoordinatorImpl(task_runner_));
174 176
175 base::MemoryCoordinatorProxy::GetInstance()-> 177 base::MemoryCoordinatorProxy::GetInstance()->
176 SetGetCurrentMemoryStateCallback(base::Bind( 178 SetGetCurrentMemoryStateCallback(base::Bind(
177 &MemoryCoordinatorImpl::GetCurrentMemoryState, 179 &MemoryCoordinatorImpl::GetCurrentMemoryState,
178 base::Unretained(coordinator_.get()))); 180 base::Unretained(coordinator_.get())));
179 base::MemoryCoordinatorProxy::GetInstance()-> 181 base::MemoryCoordinatorProxy::GetInstance()->
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 466
465 // Also make sure that the state is updated based on free avaiable memory. 467 // Also make sure that the state is updated based on free avaiable memory.
466 // Since the global state has changed in the previous task, we have to wait 468 // Since the global state has changed in the previous task, we have to wait
467 // for |minimum_transition|. 469 // for |minimum_transition|.
468 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40); 470 GetMockMemoryMonitor()->SetFreeMemoryUntilCriticalMB(40);
469 task_runner_->FastForwardBy(minimum_transition); 471 task_runner_->FastForwardBy(minimum_transition);
470 task_runner_->RunUntilIdle(); 472 task_runner_->RunUntilIdle();
471 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->GetGlobalMemoryState()); 473 EXPECT_EQ(base::MemoryState::THROTTLED, coordinator_->GetGlobalMemoryState());
472 } 474 }
473 475
476 TEST_F(MemoryCoordinatorImplTest, GetStateForProcess) {
477 EXPECT_EQ(base::MemoryState::UNKNOWN,
478 coordinator_->GetStateForProcess(base::kNullProcessHandle));
479 EXPECT_EQ(base::MemoryState::NORMAL,
480 coordinator_->GetStateForProcess(base::GetCurrentProcessHandle()));
481
482 coordinator_->CreateChildMemoryCoordinator(1);
483 coordinator_->CreateChildMemoryCoordinator(2);
484 base::Process process1 = SpawnChild("process1");
485 base::Process process2 = SpawnChild("process2");
486 coordinator_->GetMockRenderProcessHost(1)->SetProcessHandle(
487 base::MakeUnique<base::ProcessHandle>(process1.Handle()));
488 coordinator_->GetMockRenderProcessHost(2)->SetProcessHandle(
489 base::MakeUnique<base::ProcessHandle>(process2.Handle()));
490
491 EXPECT_EQ(base::MemoryState::NORMAL,
492 coordinator_->GetStateForProcess(process1.Handle()));
493 EXPECT_EQ(base::MemoryState::NORMAL,
494 coordinator_->GetStateForProcess(process2.Handle()));
495
496 EXPECT_TRUE(
497 coordinator_->SetChildMemoryState(1, MemoryState::THROTTLED));
498 EXPECT_EQ(base::MemoryState::THROTTLED,
499 coordinator_->GetStateForProcess(process1.Handle()));
500 EXPECT_EQ(base::MemoryState::NORMAL,
501 coordinator_->GetStateForProcess(process2.Handle()));
502 }
503
474 } // namespace content 504 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/memory/memory_coordinator_impl_browsertest.cc ('k') | content/public/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698