Commit c351e82b authored by rdevlin.cronin's avatar rdevlin.cronin Committed by Commit bot

Add TestWebContentsFactory to content/public/test

TestWebContentsFactory is a class that can create WebContents for unittests,
but can be created on the stack. This gives it the advantage of being able to
be used in tests that are already inheriting from something other than
RenderViewTestHarness.

Review URL: https://codereview.chromium.org/962393002

Cr-Commit-Position: refs/heads/master@{#318740}
parent d83ea6db
...@@ -11,84 +11,13 @@ ...@@ -11,84 +11,13 @@
#include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "content/public/test/test_browser_thread.h" #include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_renderer_host.h" #include "content/public/test/test_web_contents_factory.h"
#include "content/public/test/web_contents_tester.h"
#include "ui/accessibility/ax_view_state.h" #include "ui/accessibility/ax_view_state.h"
#include "ui/events/test/event_generator.h" #include "ui/events/test/event_generator.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#if defined(USE_AURA)
#include "ui/aura/env.h"
#endif
namespace { namespace {
// A helper class to create test web contents (tabs) for unit tests, without
// inheriting from RenderViewTestHarness. Can create web contents, and will
// clean up after itself upon destruction. Owns all created web contents.
// A few notes:
// - Works well allocated on the stack, because it should be destroyed before
// associated browser context.
// - Doesn't play nice with web contents created any other way (because of
// the implementation of RenderViewHostTestEnabler). But if you are creating
// web contents already, what do you need this for? ;)
// TODO(devlin): Look around and see if this class is needed elsewhere; if so,
// move it there and expand the API a bit (methods to, e.g., delete/close a
// web contents, access existing web contents, etc).
class TestWebContentsFactory {
public:
// |init_aura| initializes the aura environment (and cleans it up at
// shutdown, which is necessary for web contents. Since this method should
// only be called once, this should only be true if no other part of the test
// has initialized the environment.
explicit TestWebContentsFactory(bool init_aura);
~TestWebContentsFactory();
// Creates a new WebContents with the given |context|, and returns it.
content::WebContents* CreateWebContents(content::BrowserContext* context);
private:
// The test factory (and friends) for creating test web contents.
scoped_ptr<content::RenderViewHostTestEnabler> rvh_enabler_;
// The vector of web contents that this class created.
ScopedVector<content::WebContents> web_contents_;
// True if the factory initialized aura (and should thus tear it down).
bool init_aura_;
DISALLOW_COPY_AND_ASSIGN(TestWebContentsFactory);
};
TestWebContentsFactory::TestWebContentsFactory(bool init_aura)
: rvh_enabler_(new content::RenderViewHostTestEnabler()),
init_aura_(init_aura) {
#if defined(USE_AURA)
if (init_aura)
aura::Env::CreateInstance(true);
#endif
}
TestWebContentsFactory::~TestWebContentsFactory() {
web_contents_.clear();
// Let any posted tasks for web contents deletion run.
base::RunLoop().RunUntilIdle();
rvh_enabler_.reset();
// Let any posted tasks for RenderProcess/ViewHost deletion run.
base::RunLoop().RunUntilIdle();
#if defined(USE_AURA)
if (init_aura_)
aura::Env::DeleteInstance();
#endif
}
content::WebContents* TestWebContentsFactory::CreateWebContents(
content::BrowserContext* context) {
scoped_ptr<content::WebContents> web_contents(
content::WebContentsTester::CreateTestWebContents(context, nullptr));
DCHECK(web_contents);
web_contents_.push_back(web_contents.release());
return web_contents_.back();
}
// A test delegate for a toolbar action view. // A test delegate for a toolbar action view.
class TestToolbarActionViewDelegate : public ToolbarActionView::Delegate { class TestToolbarActionViewDelegate : public ToolbarActionView::Delegate {
public: public:
...@@ -181,7 +110,7 @@ TEST_F(ToolbarActionViewUnitTest, BasicToolbarActionViewTest) { ...@@ -181,7 +110,7 @@ TEST_F(ToolbarActionViewUnitTest, BasicToolbarActionViewTest) {
TestingProfile profile; TestingProfile profile;
// ViewsTestBase initializees the aura environment, so the factory shouldn't. // ViewsTestBase initializees the aura environment, so the factory shouldn't.
TestWebContentsFactory web_contents_factory(false); content::TestWebContentsFactory web_contents_factory;
TestToolbarActionViewController controller("fake controller"); TestToolbarActionViewController controller("fake controller");
TestToolbarActionViewDelegate action_view_delegate; TestToolbarActionViewDelegate action_view_delegate;
......
...@@ -99,6 +99,7 @@ ...@@ -99,6 +99,7 @@
'public/test/test_synchronous_compositor_android.h', 'public/test/test_synchronous_compositor_android.h',
'public/test/test_utils.cc', 'public/test/test_utils.cc',
'public/test/test_utils.h', 'public/test/test_utils.h',
'public/test/test_web_contents_factory.h',
'public/test/unittest_test_suite.cc', 'public/test/unittest_test_suite.cc',
'public/test/unittest_test_suite.h', 'public/test/unittest_test_suite.h',
'public/test/web_contents_observer_sanity_checker.cc', 'public/test/web_contents_observer_sanity_checker.cc',
...@@ -164,6 +165,7 @@ ...@@ -164,6 +165,7 @@
'test/test_render_view_host_factory.h', 'test/test_render_view_host_factory.h',
'test/test_web_contents.cc', 'test/test_web_contents.cc',
'test/test_web_contents.h', 'test/test_web_contents.h',
'test/test_web_contents_factory.cc',
'test/web_gesture_curve_mock.cc', 'test/web_gesture_curve_mock.cc',
'test/web_gesture_curve_mock.h', 'test/web_gesture_curve_mock.h',
'test/web_layer_tree_view_impl_for_testing.cc', 'test/web_layer_tree_view_impl_for_testing.cc',
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
#define CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
namespace content {
class BrowserContext;
class RenderViewHostTestEnabler;
class WebContents;
// A helper class to create test web contents (tabs) for unit tests, without
// inheriting from RenderViewTestHarness. Can create web contents, and will
// clean up after itself upon destruction. Owns all created web contents.
// A few notes:
// - Works well allocated on the stack, because it should be destroyed before
// associated browser context.
// - Doesn't play nice with web contents created any other way (because of
// the implementation of RenderViewHostTestEnabler). But if you are creating
// web contents already, what do you need this for? ;)
// TODO(devlin): The API is currently a bit sparse; there may need to be methods
// to, e.g., delete/close a web contents, access existing web contents, etc.
// These can be added as-needed.
class TestWebContentsFactory {
public:
TestWebContentsFactory();
~TestWebContentsFactory();
// Creates a new WebContents with the given |context|, and returns it.
// Ownership remains with the TestWebContentsFactory.
WebContents* CreateWebContents(BrowserContext* context);
private:
// The test factory (and friends) for creating test web contents.
scoped_ptr<RenderViewHostTestEnabler> rvh_enabler_;
// The vector of web contents that this class created.
ScopedVector<WebContents> web_contents_;
// True if the factory initialized aura (and should thus tear it down).
bool tear_down_aura_;
DISALLOW_COPY_AND_ASSIGN(TestWebContentsFactory);
};
} // namespace content
#endif // CONTENT_PUBLIC_TEST_TEST_WEB_CONTENTS_FACTORY_H_
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/test/test_web_contents_factory.h"
#include "base/run_loop.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#if defined(USE_AURA)
#include "ui/aura/env.h"
#endif
namespace content {
TestWebContentsFactory::TestWebContentsFactory()
: rvh_enabler_(new content::RenderViewHostTestEnabler()),
tear_down_aura_(false) {
#if defined(USE_AURA)
if (aura::Env::GetInstanceDontCreate() == nullptr) {
aura::Env::CreateInstance(true);
tear_down_aura_ = true;
}
#endif
}
TestWebContentsFactory::~TestWebContentsFactory() {
// We explicitly clear the vector to force destruction of any created web
// contents so that we can properly handle their cleanup (running posted
// tasks, etc).
web_contents_.clear();
// Let any posted tasks for web contents deletion run.
base::RunLoop().RunUntilIdle();
rvh_enabler_.reset();
// Let any posted tasks for RenderProcess/ViewHost deletion run.
base::RunLoop().RunUntilIdle();
#if defined(USE_AURA)
if (tear_down_aura_)
aura::Env::DeleteInstance();
#endif
}
WebContents* TestWebContentsFactory::CreateWebContents(
BrowserContext* context) {
web_contents_.push_back(
WebContentsTester::CreateTestWebContents(context, nullptr));
DCHECK(web_contents_.back());
return web_contents_.back();
}
} // namespace content
...@@ -42,6 +42,11 @@ Env* Env::GetInstance() { ...@@ -42,6 +42,11 @@ Env* Env::GetInstance() {
return env; return env;
} }
// static
Env* Env::GetInstanceDontCreate() {
return lazy_tls_ptr.Pointer()->Get();
}
// static // static
void Env::DeleteInstance() { void Env::DeleteInstance() {
delete lazy_tls_ptr.Pointer()->Get(); delete lazy_tls_ptr.Pointer()->Get();
......
...@@ -37,6 +37,7 @@ class AURA_EXPORT Env : public ui::EventTarget, public base::SupportsUserData { ...@@ -37,6 +37,7 @@ class AURA_EXPORT Env : public ui::EventTarget, public base::SupportsUserData {
// nativeviewportservice lives in the same process as the viewmanager. // nativeviewportservice lives in the same process as the viewmanager.
static void CreateInstance(bool create_event_source); static void CreateInstance(bool create_event_source);
static Env* GetInstance(); static Env* GetInstance();
static Env* GetInstanceDontCreate();
static void DeleteInstance(); static void DeleteInstance();
void AddObserver(EnvObserver* observer); void AddObserver(EnvObserver* observer);
......
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