Commit d37e4c89 authored by bashi's avatar bashi Committed by Commit bot

Use MockRenderProcessHost in memory coordinator unittests

Calling RenderProcessHost::FromID() directly prevents us to write
unittests. Add a virtual method for getting RenderProcessHost so that
testing code can use MockRenderProcessHost to write unittests.

Also, this CL installs a MemoryCoordinatorDelegate in unittests so that
unittests can have wider coverage.

BUG=675811

Review-Url: https://codereview.chromium.org/2594513002
Cr-Commit-Position: refs/heads/master@{#439764}
parent 7209820b
...@@ -316,6 +316,17 @@ bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state, ...@@ -316,6 +316,17 @@ bool MemoryCoordinatorImpl::ChangeStateIfNeeded(base::MemoryState prev_state,
return true; return true;
} }
RenderProcessHost* MemoryCoordinatorImpl::GetRenderProcessHost(
int render_process_id) {
return RenderProcessHost::FromID(render_process_id);
}
void MemoryCoordinatorImpl::SetDelegateForTesting(
std::unique_ptr<MemoryCoordinatorDelegate> delegate) {
CHECK(!delegate_);
delegate_ = std::move(delegate);
}
void MemoryCoordinatorImpl::AddChildForTesting( void MemoryCoordinatorImpl::AddChildForTesting(
int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) { int dummy_render_process_id, mojom::ChildMemoryCoordinatorPtr child) {
mojom::MemoryCoordinatorHandlePtr mch; mojom::MemoryCoordinatorHandlePtr mch;
...@@ -332,14 +343,14 @@ void MemoryCoordinatorImpl::OnConnectionError(int render_process_id) { ...@@ -332,14 +343,14 @@ void MemoryCoordinatorImpl::OnConnectionError(int render_process_id) {
} }
bool MemoryCoordinatorImpl::CanSuspendRenderer(int render_process_id) { bool MemoryCoordinatorImpl::CanSuspendRenderer(int render_process_id) {
// If there is no delegate (i.e. unittests), renderers are always suspendable. auto* render_process_host = GetRenderProcessHost(render_process_id);
if (!delegate_)
return true;
auto* render_process_host = RenderProcessHost::FromID(render_process_id);
if (!render_process_host || !render_process_host->IsProcessBackgrounded()) if (!render_process_host || !render_process_host->IsProcessBackgrounded())
return false; return false;
if (render_process_host->GetWorkerRefCount() > 0) if (render_process_host->GetWorkerRefCount() > 0)
return false; return false;
// Assumes that we can't suspend renderers if there is no delegate.
if (!delegate_)
return false;
return delegate_->CanSuspendBackgroundedRenderer(render_process_id); return delegate_->CanSuspendBackgroundedRenderer(render_process_id);
} }
...@@ -366,12 +377,6 @@ base::MemoryState MemoryCoordinatorImpl::OverrideGlobalState( ...@@ -366,12 +377,6 @@ base::MemoryState MemoryCoordinatorImpl::OverrideGlobalState(
return memory_state; return memory_state;
} }
void MemoryCoordinatorImpl::SetDelegateForTesting(
std::unique_ptr<MemoryCoordinatorDelegate> delegate) {
CHECK(!delegate_);
delegate_ = std::move(delegate);
}
void MemoryCoordinatorImpl::CreateChildInfoMapEntry( void MemoryCoordinatorImpl::CreateChildInfoMapEntry(
int render_process_id, int render_process_id,
std::unique_ptr<MemoryCoordinatorHandleImpl> handle) { std::unique_ptr<MemoryCoordinatorHandleImpl> handle) {
...@@ -411,7 +416,7 @@ void MemoryCoordinatorImpl::RecordStateChange(MemoryState prev_state, ...@@ -411,7 +416,7 @@ void MemoryCoordinatorImpl::RecordStateChange(MemoryState prev_state,
total_private_kb += working_set.priv; total_private_kb += working_set.priv;
for (auto& iter : children()) { for (auto& iter : children()) {
auto* render_process_host = RenderProcessHost::FromID(iter.first); auto* render_process_host = GetRenderProcessHost(iter.first);
if (!render_process_host || if (!render_process_host ||
render_process_host->GetHandle() == base::kNullProcessHandle) render_process_host->GetHandle() == base::kNullProcessHandle)
continue; continue;
......
...@@ -28,6 +28,7 @@ class MemoryCoordinatorHandleImpl; ...@@ -28,6 +28,7 @@ class MemoryCoordinatorHandleImpl;
class MemoryCoordinatorImplTest; class MemoryCoordinatorImplTest;
class MemoryMonitor; class MemoryMonitor;
class MemoryStateUpdater; class MemoryStateUpdater;
class RenderProcessHost;
struct MemoryCoordinatorSingletonTraits; struct MemoryCoordinatorSingletonTraits;
// MemoryCoordinatorImpl is an implementation of MemoryCoordinator. // MemoryCoordinatorImpl is an implementation of MemoryCoordinator.
...@@ -96,6 +97,16 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver, ...@@ -96,6 +97,16 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state); bool ChangeStateIfNeeded(MemoryState prev_state, MemoryState next_state);
protected: protected:
// Returns the RenderProcessHost which is correspond to the given id.
// Returns nullptr if there is no corresponding RenderProcessHost.
// This is a virtual method so that we can write tests without having
// actual RenderProcessHost.
virtual RenderProcessHost* GetRenderProcessHost(int render_process_id);
// Sets a delegate for testing.
void SetDelegateForTesting(
std::unique_ptr<MemoryCoordinatorDelegate> delegate);
// Adds the given ChildMemoryCoordinator as a child of this coordinator. // Adds the given ChildMemoryCoordinator as a child of this coordinator.
void AddChildForTesting(int dummy_render_process_id, void AddChildForTesting(int dummy_render_process_id,
mojom::ChildMemoryCoordinatorPtr child); mojom::ChildMemoryCoordinatorPtr child);
...@@ -148,9 +159,6 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver, ...@@ -148,9 +159,6 @@ class CONTENT_EXPORT MemoryCoordinatorImpl : public NotificationObserver,
MemoryState OverrideGlobalState(MemoryState memroy_state, MemoryState OverrideGlobalState(MemoryState memroy_state,
const ChildInfo& child); const ChildInfo& child);
void SetDelegateForTesting(
std::unique_ptr<MemoryCoordinatorDelegate> delegate);
// Helper function of CreateHandle and AddChildForTesting. // Helper function of CreateHandle and AddChildForTesting.
void CreateChildInfoMapEntry( void CreateChildInfoMapEntry(
int render_process_id, int render_process_id,
......
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include "content/browser/memory/memory_monitor.h" #include "content/browser/memory/memory_monitor.h"
#include "content/browser/memory/memory_state_updater.h" #include "content/browser/memory/memory_state_updater.h"
#include "content/public/common/content_features.h" #include "content/public/common/content_features.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/binding.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -83,6 +85,19 @@ class MockMemoryMonitor : public MemoryMonitor { ...@@ -83,6 +85,19 @@ class MockMemoryMonitor : public MemoryMonitor {
DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor); DISALLOW_COPY_AND_ASSIGN(MockMemoryMonitor);
}; };
class TestMemoryCoordinatorDelegate : public MemoryCoordinatorDelegate {
public:
TestMemoryCoordinatorDelegate() {}
~TestMemoryCoordinatorDelegate() override {}
bool CanSuspendBackgroundedRenderer(int render_process_id) override {
return true;
}
private:
DISALLOW_COPY_AND_ASSIGN(TestMemoryCoordinatorDelegate);
};
// A MemoryCoordinatorImpl that can be directly constructed. // A MemoryCoordinatorImpl that can be directly constructed.
class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
public: public:
...@@ -100,7 +115,10 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { ...@@ -100,7 +115,10 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
TestMemoryCoordinatorImpl( TestMemoryCoordinatorImpl(
scoped_refptr<base::TestMockTimeTaskRunner> task_runner) scoped_refptr<base::TestMockTimeTaskRunner> task_runner)
: MemoryCoordinatorImpl(task_runner, : MemoryCoordinatorImpl(task_runner,
base::MakeUnique<MockMemoryMonitor>()) {} base::MakeUnique<MockMemoryMonitor>()) {
SetDelegateForTesting(base::MakeUnique<TestMemoryCoordinatorDelegate>());
}
~TestMemoryCoordinatorImpl() override {} ~TestMemoryCoordinatorImpl() override {}
using MemoryCoordinatorImpl::OnConnectionError; using MemoryCoordinatorImpl::OnConnectionError;
...@@ -111,9 +129,22 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { ...@@ -111,9 +129,22 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
mojom::ChildMemoryCoordinatorPtr cmc_ptr; mojom::ChildMemoryCoordinatorPtr cmc_ptr;
children_.push_back(std::unique_ptr<Child>(new Child(&cmc_ptr))); children_.push_back(std::unique_ptr<Child>(new Child(&cmc_ptr)));
AddChildForTesting(process_id, std::move(cmc_ptr)); AddChildForTesting(process_id, std::move(cmc_ptr));
render_process_hosts_[process_id] =
base::MakeUnique<MockRenderProcessHost>(&browser_context_);
return &children_.back()->cmc; return &children_.back()->cmc;
} }
RenderProcessHost* GetRenderProcessHost(int render_process_id) override {
return GetMockRenderProcessHost(render_process_id);
}
MockRenderProcessHost* GetMockRenderProcessHost(int render_process_id) {
auto iter = render_process_hosts_.find(render_process_id);
if (iter == render_process_hosts_.end())
return nullptr;
return iter->second.get();
}
// Wrapper of MemoryCoordinator::SetMemoryState that also calls RunUntilIdle. // Wrapper of MemoryCoordinator::SetMemoryState that also calls RunUntilIdle.
bool SetChildMemoryState( bool SetChildMemoryState(
int render_process_id, MemoryState memory_state) { int render_process_id, MemoryState memory_state) {
...@@ -123,7 +154,9 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl { ...@@ -123,7 +154,9 @@ class TestMemoryCoordinatorImpl : public MemoryCoordinatorImpl {
return result; return result;
} }
TestBrowserContext browser_context_;
std::vector<std::unique_ptr<Child>> children_; std::vector<std::unique_ptr<Child>> children_;
std::map<int, std::unique_ptr<MockRenderProcessHost>> render_process_hosts_;
}; };
} // namespace } // namespace
...@@ -210,10 +243,13 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateDelivered) { ...@@ -210,10 +243,13 @@ TEST_F(MemoryCoordinatorImplTest, SetMemoryStateDelivered) {
TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) { TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) {
auto cmc = coordinator_->CreateChildMemoryCoordinator(1); auto cmc = coordinator_->CreateChildMemoryCoordinator(1);
auto iter = coordinator_->children().find(1); auto iter = coordinator_->children().find(1);
auto* render_process_host = coordinator_->GetMockRenderProcessHost(1);
ASSERT_TRUE(iter != coordinator_->children().end()); ASSERT_TRUE(iter != coordinator_->children().end());
ASSERT_TRUE(render_process_host);
// Foreground // Foreground
iter->second.is_visible = true; iter->second.is_visible = true;
render_process_host->set_is_process_backgrounded(false);
EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL)); EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL));
EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state()); EXPECT_EQ(mojom::MemoryState::NORMAL, cmc->state());
EXPECT_TRUE( EXPECT_TRUE(
...@@ -225,6 +261,7 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) { ...@@ -225,6 +261,7 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) {
// Background // Background
iter->second.is_visible = false; iter->second.is_visible = false;
render_process_host->set_is_process_backgrounded(true);
EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL)); EXPECT_TRUE(coordinator_->SetChildMemoryState(1, MemoryState::NORMAL));
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state()); EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
...@@ -237,6 +274,16 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) { ...@@ -237,6 +274,16 @@ TEST_F(MemoryCoordinatorImplTest, SetChildMemoryState) {
EXPECT_TRUE( EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, MemoryState::SUSPENDED)); coordinator_->SetChildMemoryState(1, MemoryState::SUSPENDED));
EXPECT_EQ(mojom::MemoryState::SUSPENDED, cmc->state()); EXPECT_EQ(mojom::MemoryState::SUSPENDED, cmc->state());
// Background but there are workers
render_process_host->IncrementServiceWorkerRefCount();
EXPECT_TRUE(
coordinator_->SetChildMemoryState(1, MemoryState::THROTTLED));
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
EXPECT_FALSE(
coordinator_->SetChildMemoryState(1, MemoryState::SUSPENDED));
EXPECT_EQ(mojom::MemoryState::THROTTLED, cmc->state());
render_process_host->DecrementSharedWorkerRefCount();
} }
TEST_F(MemoryCoordinatorImplTest, CalculateNextState) { TEST_F(MemoryCoordinatorImplTest, CalculateNextState) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment