Commit 5e0fd9d7 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Clean up content::RenderViewTest.

- Explain |view_| member's life time.
- Remove an old comment that does not make sense.
- std::make_unique() everywhere.

Change-Id: I42360b80ea66fbd38705223a162f1683b49ab5b3
Reviewed-on: https://chromium-review.googlesource.com/740709
Commit-Queue: Nick Carter <nick@chromium.org>
Reviewed-by: default avatarNick Carter <nick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#513254}
parent 6fcd0ed3
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include <cctype> #include <cctype>
#include "base/location.h" #include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
...@@ -152,8 +151,9 @@ RenderViewTest::RendererBlinkPlatformImplTestOverride::Get() const { ...@@ -152,8 +151,9 @@ RenderViewTest::RendererBlinkPlatformImplTestOverride::Get() const {
void RenderViewTest::RendererBlinkPlatformImplTestOverride::Initialize() { void RenderViewTest::RendererBlinkPlatformImplTestOverride::Initialize() {
renderer_scheduler_ = blink::scheduler::RendererScheduler::Create(); renderer_scheduler_ = blink::scheduler::RendererScheduler::Create();
blink_platform_impl_.reset( blink_platform_impl_ =
new RendererBlinkPlatformImplTestOverrideImpl(renderer_scheduler_.get())); std::make_unique<RendererBlinkPlatformImplTestOverrideImpl>(
renderer_scheduler_.get());
} }
void RenderViewTest::RendererBlinkPlatformImplTestOverride::Shutdown() { void RenderViewTest::RendererBlinkPlatformImplTestOverride::Shutdown() {
...@@ -161,7 +161,7 @@ void RenderViewTest::RendererBlinkPlatformImplTestOverride::Shutdown() { ...@@ -161,7 +161,7 @@ void RenderViewTest::RendererBlinkPlatformImplTestOverride::Shutdown() {
blink_platform_impl_->Shutdown(); blink_platform_impl_->Shutdown();
} }
RenderViewTest::RenderViewTest() : view_(nullptr) { RenderViewTest::RenderViewTest() {
RenderFrameImpl::InstallCreateHook(&TestRenderFrame::CreateTestRenderFrame); RenderFrameImpl::InstallCreateHook(&TestRenderFrame::CreateTestRenderFrame);
} }
...@@ -234,17 +234,18 @@ void RenderViewTest::GoForward(const GURL& url, const PageState& state) { ...@@ -234,17 +234,18 @@ void RenderViewTest::GoForward(const GURL& url, const PageState& state) {
} }
void RenderViewTest::SetUp() { void RenderViewTest::SetUp() {
test_io_thread_.reset(new base::TestIOThread(base::TestIOThread::kAutoStart)); test_io_thread_ =
ipc_support_.reset(new mojo::edk::ScopedIPCSupport( std::make_unique<base::TestIOThread>(base::TestIOThread::kAutoStart);
ipc_support_ = std::make_unique<mojo::edk::ScopedIPCSupport>(
test_io_thread_->task_runner(), test_io_thread_->task_runner(),
mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST)); mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST);
// Subclasses can set render_thread_ with their own implementation before // Subclasses can set render_thread_ with their own implementation before
// calling RenderViewTest::SetUp(). // calling RenderViewTest::SetUp().
// The render thread needs to exist before blink::Initialize. It also mirrors // The render thread needs to exist before blink::Initialize. It also mirrors
// the order on Chromium initialization. // the order on Chromium initialization.
if (!render_thread_) if (!render_thread_)
render_thread_.reset(new MockRenderThread()); render_thread_ = std::make_unique<MockRenderThread>();
render_thread_->set_routing_id(kRouteId); render_thread_->set_routing_id(kRouteId);
render_thread_->set_new_window_routing_id(kNewWindowRouteId); render_thread_->set_new_window_routing_id(kNewWindowRouteId);
render_thread_->set_new_window_main_frame_widget_routing_id( render_thread_->set_new_window_main_frame_widget_routing_id(
...@@ -274,15 +275,16 @@ void RenderViewTest::SetUp() { ...@@ -274,15 +275,16 @@ void RenderViewTest::SetUp() {
#endif #endif
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool()); autorelease_pool_ = std::make_unique<base::mac::ScopedNSAutoreleasePool>();
#endif #endif
command_line_.reset(new base::CommandLine(base::CommandLine::NO_PROGRAM)); command_line_ =
field_trial_list_.reset(new base::FieldTrialList(nullptr)); std::make_unique<base::CommandLine>(base::CommandLine::NO_PROGRAM);
field_trial_list_ = std::make_unique<base::FieldTrialList>(nullptr);
// We don't use the descriptor here anyways so it's ok to pass -1. // We don't use the descriptor here anyways so it's ok to pass -1.
base::FieldTrialList::CreateTrialsFromCommandLine( base::FieldTrialList::CreateTrialsFromCommandLine(
*command_line_, switches::kFieldTrialHandle, -1); *command_line_, switches::kFieldTrialHandle, -1);
params_.reset(new MainFunctionParams(*command_line_)); params_ = std::make_unique<MainFunctionParams>(*command_line_);
platform_.reset(new RendererMainPlatformDelegate(*params_)); platform_ = std::make_unique<RendererMainPlatformDelegate>(*params_);
platform_->PlatformInitialize(); platform_->PlatformInitialize();
// Setting flags and really doing anything with WebKit is fairly fragile and // Setting flags and really doing anything with WebKit is fairly fragile and
...@@ -301,12 +303,13 @@ void RenderViewTest::SetUp() { ...@@ -301,12 +303,13 @@ void RenderViewTest::SetUp() {
// ResourceBundle isn't initialized (since we have to use a diferent test // ResourceBundle isn't initialized (since we have to use a diferent test
// suite implementation than for content_unittests). For browser_tests, this // suite implementation than for content_unittests). For browser_tests, this
// is already initialized. // is already initialized.
if (!ui::ResourceBundle::HasSharedInstance()) if (!ui::ResourceBundle::HasSharedInstance()) {
ui::ResourceBundle::InitSharedInstanceWithLocale( ui::ResourceBundle::InitSharedInstanceWithLocale(
"en-US", nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES); "en-US", nullptr, ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
}
compositor_deps_.reset(new FakeCompositorDependencies); compositor_deps_ = std::make_unique<FakeCompositorDependencies>();
mock_process_.reset(new MockRenderProcess); mock_process_ = std::make_unique<MockRenderProcess>();
mojom::CreateViewParamsPtr view_params = mojom::CreateViewParams::New(); mojom::CreateViewParamsPtr view_params = mojom::CreateViewParams::New();
view_params->opener_frame_route_id = MSG_ROUTING_NONE; view_params->opener_frame_route_id = MSG_ROUTING_NONE;
...@@ -328,11 +331,8 @@ void RenderViewTest::SetUp() { ...@@ -328,11 +331,8 @@ void RenderViewTest::SetUp() {
view_params->min_size = gfx::Size(); view_params->min_size = gfx::Size();
view_params->max_size = gfx::Size(); view_params->max_size = gfx::Size();
// This needs to pass the mock render thread to the view. view_ = RenderViewImpl::Create(compositor_deps_.get(), std::move(view_params),
RenderViewImpl* view = RenderWidget::ShowCallback());
RenderViewImpl::Create(compositor_deps_.get(), std::move(view_params),
RenderWidget::ShowCallback());
view_ = view;
} }
void RenderViewTest::TearDown() { void RenderViewTest::TearDown() {
...@@ -346,6 +346,8 @@ void RenderViewTest::TearDown() { ...@@ -346,6 +346,8 @@ void RenderViewTest::TearDown() {
leak_detector->PrepareForLeakDetection(view_->GetWebView()->MainFrame()); leak_detector->PrepareForLeakDetection(view_->GetWebView()->MainFrame());
// |view_| is ref-counted and deletes itself during the RunUntilIdle() call
// below.
view_ = nullptr; view_ = nullptr;
mock_process_.reset(); mock_process_.reset();
...@@ -357,7 +359,7 @@ void RenderViewTest::TearDown() { ...@@ -357,7 +359,7 @@ void RenderViewTest::TearDown() {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
autorelease_pool_.reset(NULL); autorelease_pool_.reset();
#endif #endif
leak_detector->CollectGarbageAndReport(); leak_detector->CollectGarbageAndReport();
...@@ -412,27 +414,27 @@ void RenderViewTest::SendWebGestureEvent( ...@@ -412,27 +414,27 @@ void RenderViewTest::SendWebGestureEvent(
SendInputEvent(gesture_event); SendInputEvent(gesture_event);
} }
const char* const kGetCoordinatesScript =
"(function() {"
" function GetCoordinates(elem) {"
" if (!elem)"
" return [ 0, 0];"
" var coordinates = [ elem.offsetLeft, elem.offsetTop];"
" var parent_coordinates = GetCoordinates(elem.offsetParent);"
" coordinates[0] += parent_coordinates[0];"
" coordinates[1] += parent_coordinates[1];"
" return [ Math.round(coordinates[0]),"
" Math.round(coordinates[1])];"
" };"
" var elem = document.getElementById('$1');"
" if (!elem)"
" return null;"
" var bounds = GetCoordinates(elem);"
" bounds[2] = Math.round(elem.offsetWidth);"
" bounds[3] = Math.round(elem.offsetHeight);"
" return bounds;"
"})();";
gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) { gfx::Rect RenderViewTest::GetElementBounds(const std::string& element_id) {
static constexpr char kGetCoordinatesScript[] =
"(function() {"
" function GetCoordinates(elem) {"
" if (!elem)"
" return [ 0, 0];"
" var coordinates = [ elem.offsetLeft, elem.offsetTop];"
" var parent_coordinates = GetCoordinates(elem.offsetParent);"
" coordinates[0] += parent_coordinates[0];"
" coordinates[1] += parent_coordinates[1];"
" return [ Math.round(coordinates[0]),"
" Math.round(coordinates[1])];"
" };"
" var elem = document.getElementById('$1');"
" if (!elem)"
" return null;"
" var bounds = GetCoordinates(elem);"
" bounds[2] = Math.round(elem.offsetWidth);"
" bounds[3] = Math.round(elem.offsetHeight);"
" return bounds;"
"})();";
std::vector<std::string> params; std::vector<std::string> params;
params.push_back(element_id); params.push_back(element_id);
std::string script = std::string script =
......
...@@ -190,14 +190,13 @@ class RenderViewTest : public testing::Test, blink::WebLeakDetectorClient { ...@@ -190,14 +190,13 @@ class RenderViewTest : public testing::Test, blink::WebLeakDetectorClient {
// blink::WebLeakDetectorClient implementation. // blink::WebLeakDetectorClient implementation.
void OnLeakDetectionComplete(const Result& result) override; void OnLeakDetectionComplete(const Result& result) override;
protected:
base::test::ScopedTaskEnvironment scoped_task_environment_; base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<FakeCompositorDependencies> compositor_deps_; std::unique_ptr<FakeCompositorDependencies> compositor_deps_;
std::unique_ptr<MockRenderProcess> mock_process_; std::unique_ptr<MockRenderProcess> mock_process_;
// We use a naked pointer because we don't want to expose RenderViewImpl in // We use a naked pointer because we don't want to expose RenderViewImpl in
// the embedder's namespace. // the embedder's namespace.
RenderView* view_; RenderView* view_ = nullptr;
RendererBlinkPlatformImplTestOverride blink_platform_impl_; RendererBlinkPlatformImplTestOverride blink_platform_impl_;
std::unique_ptr<ContentClient> content_client_; std::unique_ptr<ContentClient> content_client_;
std::unique_ptr<ContentBrowserClient> content_browser_client_; std::unique_ptr<ContentBrowserClient> content_browser_client_;
......
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