Commit 59510e33 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Reland "Make BrowserWithTestWindowTests work with single process Mash"

This relands commit 01dd4ead.

Breakage was due to in-flight conflict with 39536eb8
Fix is just to update a few more function names that were added in
that change.

Original change's description:
> Make BrowserWithTestWindowTests work with single process Mash
>
> The InputDeviceClient needs to only be created once, so remove it
> from AuraTestSuiteSetup. Otherwise, in BrowserWithTestWindowTest,
> it gets created twice: during test suite setup, and as part of
> OzonePlatform (due to AshTestHelper/ash::Shell).
>
> Many tests still need one though, especially those that create a
> WebContents, so add it to RenderViewHostTestEnabler, which should
> take care of all RenderViewHostTestHarness tests or tests that create
> the enabler manually.
>
> Also update a few other tests to either create their own
> RenderViewHostTestEnabler or InputDeviceClient as appropriate.
>
> Also the InputDeviceManager can be a true singleton now (not thread-
> singleton).

TBR=sky@chromium.org

Change-Id: I4eb8579404ab6c10fb9d6ac9ac8c8c6e7a9baa57
Reviewed-on: https://chromium-review.googlesource.com/c/1352969
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611837}
parent 88fac5a7
......@@ -14,7 +14,7 @@
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
#include "content/public/test/test_web_contents_factory.h"
#include "content/public/test/test_renderer_host.h"
#include "extensions/common/extension.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -135,7 +135,9 @@ class DeclarativeChromeContentRulesRegistryTest : public testing::Test {
private:
TestExtensionEnvironment env_;
content::TestWebContentsFactory factory_;
// Must come after |env_| so only one UI MessageLoop is created.
content::RenderViewHostTestEnabler rvh_enabler_;
DISALLOW_COPY_AND_ASSIGN(DeclarativeChromeContentRulesRegistryTest);
};
......
......@@ -131,6 +131,7 @@ class ParameterizedDeclarativeContentActionTest
TEST_P(ParameterizedDeclarativeContentActionTest, ShowPageAction) {
TestExtensionEnvironment env;
content::RenderViewHostTestEnabler rvh_enabler;
scoped_refptr<const Extension> extension =
ExtensionBuilder("extension")
......@@ -198,6 +199,7 @@ INSTANTIATE_TEST_CASE_P(
TEST(DeclarativeContentActionTest, SetIcon) {
TestExtensionEnvironment env;
content::RenderViewHostTestEnabler rvh_enabler;
// Simulate the process of passing ImageData to SetIcon::Create.
SkBitmap bitmap;
......
......@@ -17,6 +17,8 @@
#if defined(USE_OZONE) || defined(USE_X11)
#include "services/ws/public/cpp/input_devices/input_device_client_test_api.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/events/devices/input_device_manager.h"
#endif
namespace {
......@@ -33,6 +35,7 @@ class ChromeBrowserMainExtraPartsMetricsTest : public testing::Test {
protected:
#if defined(USE_OZONE) || defined(USE_X11)
std::unique_ptr<ui::InputDeviceManager> input_manager_;
ws::InputDeviceClientTestApi input_device_client_test_api_;
#endif
......@@ -50,6 +53,9 @@ class ChromeBrowserMainExtraPartsMetricsTest : public testing::Test {
ChromeBrowserMainExtraPartsMetricsTest::
ChromeBrowserMainExtraPartsMetricsTest() {
display::Screen::SetScreenInstance(&test_screen_);
#if defined(USE_OZONE) || defined(USE_X11)
input_manager_ = aura::test::CreateTestInputDeviceManager();
#endif
}
ChromeBrowserMainExtraPartsMetricsTest::
......
......@@ -34,6 +34,7 @@
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/metrics/public/cpp/ukm_source.h"
......@@ -193,6 +194,7 @@ class ManagePasswordsBubbleModelTest : public ::testing::Test {
private:
content::TestBrowserThreadBundle thread_bundle_;
content::RenderViewHostTestEnabler rvh_enabler_;
TestingProfile profile_;
std::unique_ptr<content::WebContents> test_web_contents_;
std::unique_ptr<ManagePasswordsBubbleModel> model_;
......
......@@ -14,7 +14,9 @@
#include "content/public/test/test_web_ui.h"
#include "services/ws/public/cpp/input_devices/input_device_client_test_api.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/events/devices/input_device.h"
#include "ui/events/devices/input_device_manager.h"
namespace chromeos {
namespace settings {
......@@ -32,6 +34,8 @@ class TestKeyboardHandler : public KeyboardHandler {
class KeyboardHandlerTest : public testing::Test {
public:
KeyboardHandlerTest() : handler_test_api_(&handler_) {
input_device_manager_ = aura::test::CreateTestInputDeviceManager();
handler_.set_web_ui(&web_ui_);
handler_.RegisterMessages();
handler_.AllowJavascriptForTesting();
......@@ -157,6 +161,7 @@ class KeyboardHandlerTest : public testing::Test {
return has_internal_search_key;
}
std::unique_ptr<ui::InputDeviceManager> input_device_manager_;
ws::InputDeviceClientTestApi input_device_client_test_api_;
content::TestWebUI web_ui_;
TestKeyboardHandler handler_;
......
......@@ -33,7 +33,6 @@
#if defined(OS_CHROMEOS)
#include "ash/test/ash_test_views_delegate.h"
#include "chrome/test/base/ash_test_environment_chrome.h"
#else
#include "ui/views/test/test_views_delegate.h"
#endif
......@@ -66,13 +65,11 @@ BrowserWithTestWindowTest::BrowserWithTestWindowTest(
bool hosted_app,
content::TestBrowserThreadBundle::Options thread_bundle_options)
: thread_bundle_(thread_bundle_options),
browser_type_(browser_type),
hosted_app_(hosted_app) {
#if defined(OS_CHROMEOS)
ash_test_environment_ = std::make_unique<AshTestEnvironmentChrome>();
ash_test_helper_ =
std::make_unique<ash::AshTestHelper>(ash_test_environment_.get());
ash_test_helper_(&ash_test_environment_),
#endif
browser_type_(browser_type),
hosted_app_(hosted_app) {
}
BrowserWithTestWindowTest::~BrowserWithTestWindowTest() {}
......@@ -80,11 +77,16 @@ BrowserWithTestWindowTest::~BrowserWithTestWindowTest() {}
void BrowserWithTestWindowTest::SetUp() {
testing::Test::SetUp();
#if defined(OS_CHROMEOS)
ash_test_helper_->SetUp(true);
ash_test_helper_->SetRunningOutsideAsh();
ash_test_helper_.SetUp(true);
ash_test_helper_.SetRunningOutsideAsh();
#elif defined(TOOLKIT_VIEWS)
views_test_helper_.reset(new views::ScopedViewsTestHelper());
#endif
// This must be created after ash_test_helper_ is set up so that it doesn't
// create an InputDeviceManager.
rvh_test_enabler_ = std::make_unique<content::RenderViewHostTestEnabler>();
#if defined(TOOLKIT_VIEWS)
SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient());
......@@ -131,7 +133,7 @@ void BrowserWithTestWindowTest::TearDown() {
profile_manager_.reset();
#if defined(OS_CHROMEOS)
ash_test_helper_->TearDown();
ash_test_helper_.TearDown();
#elif defined(TOOLKIT_VIEWS)
views_test_helper_.reset();
#endif
......@@ -146,7 +148,7 @@ void BrowserWithTestWindowTest::TearDown() {
gfx::NativeWindow BrowserWithTestWindowTest::GetContext() {
#if defined(OS_CHROMEOS)
return ash_test_helper_->CurrentContext();
return ash_test_helper_.CurrentContext();
#elif defined(TOOLKIT_VIEWS)
return views_test_helper_->GetContext();
#else
......
......@@ -20,6 +20,7 @@
#include "ash/test/ash_test_views_delegate.h"
#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
#include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
#include "chrome/test/base/ash_test_environment_chrome.h"
#else
#include "ui/views/test/scoped_views_test_helper.h"
#endif
......@@ -35,13 +36,6 @@ class GURL;
namespace views {
class TestViewsDelegate;
}
#if defined(OS_CHROMEOS)
namespace ash {
namespace test {
class AshTestEnvironment;
}
}
#endif
#endif
namespace content {
......@@ -124,7 +118,7 @@ class BrowserWithTestWindowTest : public testing::Test {
}
#if defined(OS_CHROMEOS)
ash::AshTestHelper* ash_test_helper() { return ash_test_helper_.get(); }
ash::AshTestHelper* ash_test_helper() { return &ash_test_helper_; }
#endif
// The context to help determine desktop type when creating new Widgets.
......@@ -175,7 +169,7 @@ class BrowserWithTestWindowTest : public testing::Test {
#if defined(TOOLKIT_VIEWS)
views::TestViewsDelegate* test_views_delegate() {
#if defined(OS_CHROMEOS)
return ash_test_helper_->test_views_delegate();
return ash_test_helper_.test_views_delegate();
#else
return views_test_helper_->test_views_delegate();
#endif
......@@ -197,17 +191,16 @@ class BrowserWithTestWindowTest : public testing::Test {
std::unique_ptr<BrowserWindow> window_; // Usually a TestBrowserWindow.
std::unique_ptr<Browser> browser_;
// The existence of this object enables tests via
// RenderViewHostTester.
content::RenderViewHostTestEnabler rvh_test_enabler_;
#if defined(OS_CHROMEOS)
std::unique_ptr<ash::AshTestEnvironment> ash_test_environment_;
std::unique_ptr<ash::AshTestHelper> ash_test_helper_;
AshTestEnvironmentChrome ash_test_environment_;
ash::AshTestHelper ash_test_helper_;
#elif defined(TOOLKIT_VIEWS)
std::unique_ptr<views::ScopedViewsTestHelper> views_test_helper_;
#endif
// The existence of this object enables tests via RenderViewHostTester.
std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_;
#if defined(OS_WIN)
ui::ScopedOleInitializer ole_initializer_;
#endif
......
......@@ -9,8 +9,7 @@
#include "base/test/metrics/user_action_tester.h"
#include "base/test/simple_test_tick_clock.h"
#include "content/browser/media/media_web_contents_observer.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -30,13 +29,13 @@ static const char* MAX_CONCURRENT_TAB_IN_SESSION_HISTOGRAM =
static const char* CONCURRENT_TABS_TIME_HISTOGRAM =
"Media.Audible.ConcurrentTabsTime";
class AudibleMetricsTest : public testing::Test {
class AudibleMetricsTest : public RenderViewHostTestHarness {
public:
AudibleMetricsTest() = default;
void SetUp() override {
RenderViewHostTestHarness::SetUp();
audible_metrics_ = std::make_unique<AudibleMetrics>();
browser_context_ = std::make_unique<TestBrowserContext>();
// Set the clock to a value different than 0 so the time it gives is
// recognized as initialized.
......@@ -46,7 +45,7 @@ class AudibleMetricsTest : public testing::Test {
void TearDown() override {
audible_metrics_.reset();
browser_context_.reset();
RenderViewHostTestHarness::TearDown();
}
base::SimpleTestTickClock* clock() { return &clock_; }
......@@ -62,16 +61,12 @@ class AudibleMetricsTest : public testing::Test {
return histogram_tester_.GetHistogramSamplesSinceCreation(name);
}
std::unique_ptr<WebContentsImpl> CreateWebContents() {
return TestWebContents::Create(
browser_context_.get(), SiteInstance::Create(browser_context_.get()));
TestWebContents* test_web_contents() {
return static_cast<TestWebContents*>(web_contents());
}
private:
TestBrowserThreadBundle test_browser_thread_bundle_;
std::unique_ptr<AudibleMetrics> audible_metrics_;
std::unique_ptr<TestBrowserContext> browser_context_;
base::SimpleTestTickClock clock_;
base::HistogramTester histogram_tester_;
......@@ -372,16 +367,17 @@ TEST_F(AudibleMetricsTest, ConcurrentTabsTimeRunsAsLongAsTwoAudibleTabs) {
}
TEST_F(AudibleMetricsTest, MediaWebContentsObserver_Audible_Muted) {
std::unique_ptr<WebContentsImpl> web_contents(CreateWebContents());
MediaWebContentsObserver media_observer(web_contents.get());
MediaWebContentsObserver media_observer(test_web_contents());
media_observer.SetAudibleMetricsForTest(audible_metrics());
web_contents->SetAudioMuted(true);
web_contents->audio_stream_monitor()->set_is_currently_audible_for_testing(
true);
test_web_contents()->SetAudioMuted(true);
test_web_contents()
->audio_stream_monitor()
->set_is_currently_audible_for_testing(true);
EXPECT_TRUE(web_contents->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_TRUE(web_contents->IsAudioMuted());
EXPECT_TRUE(
test_web_contents()->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_TRUE(test_web_contents()->IsAudioMuted());
EXPECT_EQ(0, audible_metrics()->GetAudibleWebContentsSizeForTest());
media_observer.MaybeUpdateAudibleState();
......@@ -389,15 +385,16 @@ TEST_F(AudibleMetricsTest, MediaWebContentsObserver_Audible_Muted) {
}
TEST_F(AudibleMetricsTest, MediaWebContentsObserver_Audible_NotMuted) {
std::unique_ptr<WebContentsImpl> web_contents(CreateWebContents());
MediaWebContentsObserver media_observer(web_contents.get());
MediaWebContentsObserver media_observer(test_web_contents());
media_observer.SetAudibleMetricsForTest(audible_metrics());
web_contents->audio_stream_monitor()->set_is_currently_audible_for_testing(
true);
test_web_contents()
->audio_stream_monitor()
->set_is_currently_audible_for_testing(true);
EXPECT_TRUE(web_contents->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_FALSE(web_contents->IsAudioMuted());
EXPECT_TRUE(
test_web_contents()->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_FALSE(test_web_contents()->IsAudioMuted());
EXPECT_EQ(0, audible_metrics()->GetAudibleWebContentsSizeForTest());
media_observer.MaybeUpdateAudibleState();
......@@ -405,14 +402,14 @@ TEST_F(AudibleMetricsTest, MediaWebContentsObserver_Audible_NotMuted) {
}
TEST_F(AudibleMetricsTest, MediaWebContentsObserver_NotAudible_Muted) {
std::unique_ptr<WebContentsImpl> web_contents(CreateWebContents());
MediaWebContentsObserver media_observer(web_contents.get());
MediaWebContentsObserver media_observer(test_web_contents());
media_observer.SetAudibleMetricsForTest(audible_metrics());
web_contents->SetAudioMuted(true);
test_web_contents()->SetAudioMuted(true);
EXPECT_FALSE(web_contents->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_TRUE(web_contents->IsAudioMuted());
EXPECT_FALSE(
test_web_contents()->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_TRUE(test_web_contents()->IsAudioMuted());
EXPECT_EQ(0, audible_metrics()->GetAudibleWebContentsSizeForTest());
media_observer.MaybeUpdateAudibleState();
......@@ -420,12 +417,12 @@ TEST_F(AudibleMetricsTest, MediaWebContentsObserver_NotAudible_Muted) {
}
TEST_F(AudibleMetricsTest, MediaWebContentsObserver_NotAudible_NotMuted) {
std::unique_ptr<WebContentsImpl> web_contents(CreateWebContents());
MediaWebContentsObserver media_observer(web_contents.get());
MediaWebContentsObserver media_observer(test_web_contents());
media_observer.SetAudibleMetricsForTest(audible_metrics());
EXPECT_FALSE(web_contents->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_FALSE(web_contents->IsAudioMuted());
EXPECT_FALSE(
test_web_contents()->audio_stream_monitor()->IsCurrentlyAudible());
EXPECT_FALSE(test_web_contents()->IsAudioMuted());
EXPECT_EQ(0, audible_metrics()->GetAudibleWebContentsSizeForTest());
media_observer.MaybeUpdateAudibleState();
......
......@@ -14,12 +14,10 @@
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/lock.h"
#include "base/test/scoped_command_line.h"
#include "base/test/test_message_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "content/browser/media/session/media_session_impl.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_service_manager_context.h"
#include "content/test/test_web_contents.h"
#include "media/base/audio_parameters.h"
......@@ -29,8 +27,6 @@
#include "services/media_session/public/cpp/switches.h"
#include "services/media_session/public/mojom/audio_focus.mojom.h"
#include "services/media_session/public/mojom/constants.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/size.h"
namespace {
......@@ -44,16 +40,18 @@ using media_session::mojom::AudioFocusRequestStatePtr;
// integer/string values.
class MediaInternalsTestBase {
public:
MediaInternalsTestBase() : media_internals_(nullptr) {
MediaInternalsTestBase() = default;
virtual ~MediaInternalsTestBase() = default;
void SetUpServiceManager() {
scoped_command_line_.GetProcessCommandLine()->AppendSwitch(
media_session::switches::kEnableAudioFocus);
service_manager_context_ =
std::make_unique<content::TestServiceManagerContext>();
media_internals_ = content::MediaInternals::GetInstance();
}
virtual ~MediaInternalsTestBase() {}
void TearDownServiceManager() { service_manager_context_.reset(); }
protected:
// Extracts and deserializes the JSON update data; merges into |update_data_|.
......@@ -107,14 +105,13 @@ class MediaInternalsTestBase {
}
}
const content::TestBrowserThreadBundle thread_bundle_;
base::DictionaryValue update_data_;
content::MediaInternals* media_internals() const { return media_internals_; }
content::MediaInternals* media_internals() const {
return content::MediaInternals::GetInstance();
}
private:
content::MediaInternals* media_internals_;
base::test::ScopedCommandLine scoped_command_line_;
std::unique_ptr<content::TestServiceManagerContext> service_manager_context_;
};
......@@ -132,14 +129,17 @@ class MediaInternalsVideoCaptureDeviceTest : public testing::Test,
: update_cb_(base::Bind(
&MediaInternalsVideoCaptureDeviceTest::UpdateCallbackImpl,
base::Unretained(this))) {
SetUpServiceManager();
media_internals()->AddUpdateCallback(update_cb_);
}
~MediaInternalsVideoCaptureDeviceTest() override {
media_internals()->RemoveUpdateCallback(update_cb_);
TearDownServiceManager();
}
protected:
const content::TestBrowserThreadBundle thread_bundle_;
MediaInternals::UpdateCallback update_cb_;
};
......@@ -228,11 +228,13 @@ class MediaInternalsAudioLogTest
test_component_(GetParam()),
audio_log_(media_internals()->CreateAudioLog(test_component_,
kTestComponentID)) {
SetUpServiceManager();
media_internals()->AddUpdateCallback(update_cb_);
}
virtual ~MediaInternalsAudioLogTest() {
media_internals()->RemoveUpdateCallback(update_cb_);
TearDownServiceManager();
}
protected:
......@@ -249,6 +251,8 @@ class MediaInternalsAudioLogTest
media::AudioParameters::DUCKING);
return params;
}
const content::TestBrowserThreadBundle thread_bundle_;
};
TEST_P(MediaInternalsAudioLogTest, AudioLogCreateStartStopErrorClose) {
......@@ -319,15 +323,19 @@ const char kTestTitle2[] = "Test Title 2";
} // namespace
class MediaInternalsAudioFocusTest : public testing::Test,
class MediaInternalsAudioFocusTest : public RenderViewHostTestHarness,
public MediaInternalsTestBase {
public:
MediaInternalsAudioFocusTest() = default;
~MediaInternalsAudioFocusTest() override = default;
void SetUp() override {
RenderViewHostTestHarness::SetUp();
SetUpServiceManager();
update_cb_ =
base::BindRepeating(&MediaInternalsAudioFocusTest::UpdateCallbackImpl,
base::Unretained(this));
browser_context_.reset(new TestBrowserContext());
run_loop_ = std::make_unique<base::RunLoop>();
content::ServiceManagerConnection::GetForProcess()
......@@ -339,7 +347,8 @@ class MediaInternalsAudioFocusTest : public testing::Test,
void TearDown() override {
content::MediaInternals::GetInstance()->RemoveUpdateCallback(update_cb_);
browser_context_.reset();
TearDownServiceManager();
RenderViewHostTestHarness::TearDown();
}
protected:
......@@ -374,11 +383,6 @@ class MediaInternalsAudioFocusTest : public testing::Test,
call_count_ = 0;
}
std::unique_ptr<TestWebContents> CreateWebContents() {
return TestWebContents::Create(
browser_context_.get(), SiteInstance::Create(browser_context_.get()));
}
void RemoveAllPlayersForTest(MediaSessionImpl* session) {
session->RemoveAllPlayersForTest();
}
......@@ -417,15 +421,15 @@ class MediaInternalsAudioFocusTest : public testing::Test,
base::Lock lock_;
std::unique_ptr<base::RunLoop> run_loop_;
std::unique_ptr<TestBrowserContext> browser_context_;
media_session::mojom::AudioFocusManagerPtr audio_focus_ptr_;
};
TEST_F(MediaInternalsAudioFocusTest, AudioFocusStateIsUpdated) {
// Create a test media session and request audio focus.
std::unique_ptr<TestWebContents> web_contents1 = CreateWebContents();
web_contents1->SetTitle(base::UTF8ToUTF16(kTestTitle1));
std::unique_ptr<WebContents> web_contents1 = CreateTestWebContents();
static_cast<TestWebContents*>(web_contents1.get())
->SetTitle(base::UTF8ToUTF16(kTestTitle1));
MediaSessionImpl* media_session1 = MediaSessionImpl::Get(web_contents1.get());
media_session1->RequestSystemAudioFocus(AudioFocusType::kGain);
WaitForCallbackCount(1);
......@@ -446,8 +450,9 @@ TEST_F(MediaInternalsAudioFocusTest, AudioFocusStateIsUpdated) {
}
// Create another media session.
std::unique_ptr<TestWebContents> web_contents2 = CreateWebContents();
web_contents2->SetTitle(base::UTF8ToUTF16(kTestTitle2));
std::unique_ptr<WebContents> web_contents2 = CreateTestWebContents();
static_cast<TestWebContents*>(web_contents2.get())
->SetTitle(base::UTF8ToUTF16(kTestTitle2));
MediaSessionImpl* media_session2 = MediaSessionImpl::Get(web_contents2.get());
media_session2->RequestSystemAudioFocus(
AudioFocusType::kGainTransientMayDuck);
......
......@@ -10,11 +10,9 @@
#include "build/build_config.h"
#include "content/browser/media/session/media_session_player_observer.h"
#include "content/browser/media/session/mock_media_session_player_observer.h"
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/common/service_manager_connection.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_service_manager_context.h"
#include "content/test/test_web_contents.h"
#include "media/base/media_content_type.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"
......@@ -74,7 +72,7 @@ class MockAudioFocusDelegate : public AudioFocusDelegate {
} // anonymous namespace
class MediaSessionImplTest : public testing::Test {
class MediaSessionImplTest : public RenderViewHostTestHarness {
public:
MediaSessionImplTest() = default;
......@@ -82,10 +80,8 @@ class MediaSessionImplTest : public testing::Test {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
media_session::switches::kEnableAudioFocus);
rph_factory_.reset(new MockRenderProcessHostFactory());
RenderProcessHostImpl::set_render_process_host_factory_for_testing(
rph_factory_.get());
browser_context_.reset(new TestBrowserContext());
RenderViewHostTestHarness::SetUp();
player_observer_.reset(new MockMediaSessionPlayerObserver());
// Connect to the Media Session service and bind |audio_focus_ptr_| to it.
......@@ -98,9 +94,8 @@ class MediaSessionImplTest : public testing::Test {
void TearDown() override {
service_manager_context_.reset();
browser_context_.reset();
RenderProcessHostImpl::set_render_process_host_factory_for_testing(nullptr);
rph_factory_.reset();
RenderViewHostTestHarness::TearDown();
}
void RequestAudioFocus(MediaSessionImpl* session,
......@@ -128,11 +123,6 @@ class MediaSessionImplTest : public testing::Test {
session->FlushForTesting();
}
std::unique_ptr<WebContents> CreateWebContents() {
return TestWebContents::Create(
browser_context_.get(), SiteInstance::Create(browser_context_.get()));
}
std::unique_ptr<TestAudioFocusObserver> CreateAudioFocusObserver() {
std::unique_ptr<TestAudioFocusObserver> observer =
std::make_unique<TestAudioFocusObserver>();
......@@ -153,17 +143,15 @@ class MediaSessionImplTest : public testing::Test {
}
private:
TestBrowserThreadBundle test_browser_thread_bundle_;
media_session::mojom::AudioFocusManagerPtr audio_focus_ptr_;
std::unique_ptr<TestServiceManagerContext> service_manager_context_;
std::unique_ptr<MockRenderProcessHostFactory> rph_factory_;
std::unique_ptr<TestBrowserContext> browser_context_;
DISALLOW_COPY_AND_ASSIGN(MediaSessionImplTest);
};
TEST_F(MediaSessionImplTest, SessionInfoState) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
EXPECT_EQ(MediaSessionInfo::SessionState::kInactive, GetState(media_session));
......@@ -229,7 +217,7 @@ TEST_F(MediaSessionImplTest, SessionInfoState) {
}
TEST_F(MediaSessionImplTest, NotifyDelegateOnStateChange) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
MockAudioFocusDelegate* delegate = new MockAudioFocusDelegate();
SetDelegateForTests(media_session, delegate);
......@@ -260,7 +248,7 @@ TEST_F(MediaSessionImplTest, NotifyDelegateOnStateChange) {
}
TEST_F(MediaSessionImplTest, PepperForcesDuckAndRequestsFocus) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
int player_id = player_observer_->StartNewPlayer();
......@@ -284,7 +272,7 @@ TEST_F(MediaSessionImplTest, PepperForcesDuckAndRequestsFocus) {
}
TEST_F(MediaSessionImplTest, RegisterMojoObserver) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
EXPECT_FALSE(HasMojoObservers(media_session));
......@@ -296,7 +284,7 @@ TEST_F(MediaSessionImplTest, RegisterMojoObserver) {
}
TEST_F(MediaSessionImplTest, SessionInfo_PlaybackState) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
EXPECT_EQ(MediaPlaybackState::kPaused,
......@@ -322,7 +310,7 @@ TEST_F(MediaSessionImplTest, SessionInfo_PlaybackState) {
#if !defined(OS_ANDROID)
TEST_F(MediaSessionImplTest, WebContentsDestroyed_ReleasesFocus) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
{
......@@ -346,7 +334,7 @@ TEST_F(MediaSessionImplTest, WebContentsDestroyed_ReleasesFocus) {
}
TEST_F(MediaSessionImplTest, WebContentsDestroyed_ReleasesTransients) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
{
......@@ -370,11 +358,11 @@ TEST_F(MediaSessionImplTest, WebContentsDestroyed_ReleasesTransients) {
}
TEST_F(MediaSessionImplTest, WebContentsDestroyed_StopsDucking) {
std::unique_ptr<WebContents> web_contents_1(CreateWebContents());
std::unique_ptr<WebContents> web_contents_1(CreateTestWebContents());
MediaSessionImpl* media_session_1 =
MediaSessionImpl::Get(web_contents_1.get());
std::unique_ptr<WebContents> web_contents_2(CreateWebContents());
std::unique_ptr<WebContents> web_contents_2(CreateTestWebContents());
MediaSessionImpl* media_session_2 =
MediaSessionImpl::Get(web_contents_2.get());
......@@ -417,7 +405,7 @@ TEST_F(MediaSessionImplTest, WebContentsDestroyed_StopsDucking) {
}
TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Active) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
MockAudioFocusDelegate* delegate = new MockAudioFocusDelegate();
SetDelegateForTests(media_session, delegate);
......@@ -435,7 +423,7 @@ TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Active) {
}
TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Inactive) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
MockAudioFocusDelegate* delegate = new MockAudioFocusDelegate();
SetDelegateForTests(media_session, delegate);
......@@ -447,7 +435,7 @@ TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Inactive) {
}
TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Suspended) {
std::unique_ptr<WebContents> web_contents(CreateWebContents());
std::unique_ptr<WebContents> web_contents(CreateTestWebContents());
MediaSessionImpl* media_session = MediaSessionImpl::Get(web_contents.get());
MockAudioFocusDelegate* delegate = new MockAudioFocusDelegate();
SetDelegateForTests(media_session, delegate);
......@@ -469,7 +457,7 @@ TEST_F(MediaSessionImplTest, RequestAudioFocus_OnFocus_Suspended) {
media_session->OnWebContentsFocused(nullptr);
EXPECT_EQ(1, delegate->request_audio_focus_count());
}
#endif // !defined(OS_ANDROID)
} // namespace content
......@@ -35,6 +35,7 @@
#include "content/test/test_web_contents.h"
#include "net/base/network_change_notifier.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/events/devices/input_device_manager.h"
#if defined(OS_ANDROID)
#include "ui/android/dummy_screen_android.h"
......@@ -47,6 +48,7 @@
#if defined(USE_AURA)
#include "ui/aura/test/aura_test_helper.h"
#include "ui/aura/test/aura_test_utils.h"
#include "ui/compositor/test/context_factories_for_test.h"
#include "ui/wm/core/default_activation_client.h"
#endif
......@@ -160,6 +162,9 @@ RenderViewHostTestEnabler::RenderViewHostTestEnabler()
if (base::ThreadTaskRunnerHandle::IsSet())
ui::WindowResizeHelperMac::Get()->Init(base::ThreadTaskRunnerHandle::Get());
#endif // OS_MACOSX
#if defined(USE_AURA)
input_device_client_ = aura::test::CreateTestInputDeviceManager();
#endif
}
RenderViewHostTestEnabler::~RenderViewHostTestEnabler() {
......
......@@ -39,6 +39,7 @@ class NetworkChangeNotifier;
}
namespace ui {
class InputDeviceManager;
class ScopedOleInitializer;
}
......@@ -179,6 +180,9 @@ class RenderViewHostTestEnabler {
#if defined(OS_ANDROID)
std::unique_ptr<display::Screen> screen_;
#endif
#if defined(USE_AURA)
std::unique_ptr<ui::InputDeviceManager> input_device_client_;
#endif
std::unique_ptr<base::test::ScopedTaskEnvironment> task_environment_;
std::unique_ptr<MockRenderProcessHostFactory> rph_factory_;
......
......@@ -10,32 +10,12 @@
#include "ui/aura/test/aura_test_context_factory.h"
#include "ui/base/ui_base_features.h"
#if defined(USE_OZONE)
#include "services/ws/public/cpp/input_devices/input_device_client.h"
#endif
#if BUILDFLAG(ENABLE_MUS)
#include "ui/aura/test/mus/test_window_tree_client_delegate.h"
#include "ui/aura/test/mus/test_window_tree_client_setup.h"
#endif
namespace aura {
namespace {
#if defined(USE_OZONE)
class TestInputDeviceClient : public ws::InputDeviceClient {
public:
TestInputDeviceClient() = default;
~TestInputDeviceClient() override = default;
using InputDeviceClient::GetIntefacePtr;
private:
DISALLOW_COPY_AND_ASSIGN(TestInputDeviceClient);
};
#endif
} // namespace
AuraTestSuiteSetup::AuraTestSuiteSetup() {
DCHECK(!Env::HasInstance());
......@@ -63,12 +43,11 @@ void AuraTestSuiteSetup::ConfigureMus() {
test_window_tree_client_delegate_.get());
env_->SetWindowTreeClient(window_tree_client_setup_->window_tree_client());
#if defined(USE_OZONE)
input_device_client_ = std::make_unique<TestInputDeviceClient>();
#endif
#if !defined(USE_OZONE)
context_factory_ = std::make_unique<test::AuraTestContextFactory>();
env_->set_context_factory(context_factory_.get());
env_->set_context_factory_private(nullptr);
#endif
}
#endif
......
......@@ -14,10 +14,6 @@ namespace ui {
class ContextFactory;
} // namespace ui
namespace ws {
class InputDeviceClient;
}
namespace aura {
class Env;
......@@ -48,10 +44,6 @@ class AuraTestSuiteSetup {
std::unique_ptr<TestWindowTreeClientSetup> window_tree_client_setup_;
#endif
#if defined(USE_OZONE)
std::unique_ptr<ws::InputDeviceClient> input_device_client_;
#endif
DISALLOW_COPY_AND_ASSIGN(AuraTestSuiteSetup);
};
......
......@@ -10,6 +10,14 @@
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#if defined(USE_OZONE)
#include "services/ws/public/cpp/input_devices/input_device_client.h"
#include "ui/aura/env.h"
#include "ui/base/ui_base_features.h"
#else
#include "ui/events/devices/input_device_manager.h"
#endif
namespace aura {
namespace test {
......@@ -48,5 +56,17 @@ void DisableIME(WindowTreeHost* host) {
WindowTreeHostTestApi(host).disable_ime();
}
std::unique_ptr<ui::InputDeviceManager> CreateTestInputDeviceManager() {
#if defined(USE_OZONE)
// Some tests may have already created an InputDeviceManager, e.g. through
// AshTestHelper. Some tests will not have created their Env yet, which may
// create an InputDeviceManager. So only create one if an Env has been created
// but it didn't create an InputDeviceManager.
if (aura::Env::HasInstance() && !ui::InputDeviceManager::HasInstance())
return std::make_unique<ws::InputDeviceClient>();
#endif
return nullptr;
}
} // namespace test
} // namespace aura
......@@ -13,6 +13,10 @@ namespace gfx {
class Point;
}
namespace ui {
class InputDeviceManager;
}
namespace aura {
class WindowEventDispatcher;
class WindowTreeHost;
......@@ -24,6 +28,8 @@ void SetHostDispatcher(WindowTreeHost* host,
std::unique_ptr<WindowEventDispatcher> dispatcher);
void DisableIME(WindowTreeHost* host);
std::unique_ptr<ui::InputDeviceManager> CreateTestInputDeviceManager();
} // namespace test
} // namespace aura
......
......@@ -3,41 +3,35 @@
// found in the LICENSE file.
#include "ui/events/devices/input_device_manager.h"
#include "base/lazy_instance.h"
#include "base/threading/thread_local.h"
namespace ui {
namespace {
// InputDeviceManager singleton is thread-local so that different instances can
// be used on different threads (eg. UI Service thread vs. browser UI thread).
base::LazyInstance<base::ThreadLocalPointer<InputDeviceManager>>::Leaky
lazy_tls_ptr = LAZY_INSTANCE_INITIALIZER;
InputDeviceManager* g_instance = nullptr;
} // namespace
// static
InputDeviceManager* InputDeviceManager::GetInstance() {
InputDeviceManager* instance = lazy_tls_ptr.Pointer()->Get();
DCHECK(instance) << "InputDeviceManager::SetInstance must be called before "
"getting the instance of InputDeviceManager.";
return instance;
DCHECK(g_instance) << "InputDeviceManager::SetInstance must be called before "
"getting the instance of InputDeviceManager.";
return g_instance;
}
// static
bool InputDeviceManager::HasInstance() {
return lazy_tls_ptr.Pointer()->Get() != nullptr;
return g_instance;
}
// static
void InputDeviceManager::SetInstance(InputDeviceManager* instance) {
DCHECK(!lazy_tls_ptr.Pointer()->Get());
lazy_tls_ptr.Pointer()->Set(instance);
DCHECK(!g_instance);
g_instance = instance;
}
// static
void InputDeviceManager::ClearInstance() {
lazy_tls_ptr.Pointer()->Set(nullptr);
g_instance = nullptr;
}
} // namespace ui
......@@ -15,13 +15,13 @@
namespace ui {
// Interface to query available input devices. Holds a thread-local pointer to
// an implementation that provides this service. The implementation could be
// DeviceDataManager or something that mirrors the necessary state if
// DeviceDataManager is in a different process.
// Interface to query available input devices. The implementation, which is a
// singleton, could be DeviceDataManager or something that mirrors the necessary
// state if DeviceDataManager is in a different process.
class EVENTS_DEVICES_EXPORT InputDeviceManager {
public:
InputDeviceManager() {}
InputDeviceManager() = default;
virtual ~InputDeviceManager() = default;
static InputDeviceManager* GetInstance();
static bool HasInstance();
......
......@@ -17,6 +17,7 @@
#include "content/public/test/mock_render_process_host.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "content/test/test_content_browser_client.h"
#include "ui/events/event.h"
......@@ -187,6 +188,7 @@ class WebViewUnitTest : public views::test::WidgetTest {
private:
content::TestBrowserThreadBundle test_browser_thread_bundle_;
content::RenderViewHostTestEnabler rvh_enabler_;
std::unique_ptr<content::TestBrowserContext> browser_context_;
content::TestContentBrowserClient test_browser_client_;
std::unique_ptr<views::WebView::ScopedWebContentsCreatorForTesting>
......
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