| Index: content/browser/media/session/media_session_visibility_browsertest.cc
|
| diff --git a/content/browser/media/session/media_session_visibility_browsertest.cc b/content/browser/media/session/media_session_visibility_browsertest.cc
|
| deleted file mode 100644
|
| index 01e42325551b75255788c6dfd83986b751ff2148..0000000000000000000000000000000000000000
|
| --- a/content/browser/media/session/media_session_visibility_browsertest.cc
|
| +++ /dev/null
|
| @@ -1,310 +0,0 @@
|
| -// Copyright 2016 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 <tuple>
|
| -
|
| -#include "base/command_line.h"
|
| -#include "base/location.h"
|
| -#include "base/logging.h"
|
| -#include "base/single_thread_task_runner.h"
|
| -#include "base/threading/thread_task_runner_handle.h"
|
| -#include "content/browser/media/session/media_session.h"
|
| -#include "content/public/browser/web_contents.h"
|
| -#include "content/public/common/content_switches.h"
|
| -#include "content/public/test/browser_test_utils.h"
|
| -#include "content/public/test/content_browser_test.h"
|
| -#include "content/public/test/content_browser_test_utils.h"
|
| -#include "content/public/test/test_navigation_observer.h"
|
| -#include "content/shell/browser/shell.h"
|
| -#include "media/base/media_switches.h"
|
| -#include "testing/gtest/include/gtest/gtest.h"
|
| -
|
| -namespace content {
|
| -namespace {
|
| -static const char kStartPlayerScript[] =
|
| - "document.getElementById('long-video').play()";
|
| -static const char kPausePlayerScript[] =
|
| - "document.getElementById('long-video').pause()";
|
| -
|
| -enum class MediaSuspend {
|
| - ENABLED,
|
| - DISABLED,
|
| -};
|
| -
|
| -enum class Pipeline {
|
| - WMPI,
|
| - WMPA,
|
| -};
|
| -
|
| -enum class BackgroundResuming {
|
| - ENABLED,
|
| - DISABLED,
|
| -};
|
| -
|
| -enum class SessionState {
|
| - ACTIVE,
|
| - SUSPENDED,
|
| - INACTIVE,
|
| -};
|
| -
|
| -struct VisibilityTestData {
|
| - MediaSuspend media_suspend;
|
| - BackgroundResuming background_resuming;
|
| - SessionState session_state_before_hide;
|
| - SessionState session_state_after_hide;
|
| -};
|
| -
|
| -}
|
| -
|
| -
|
| -// Base class of MediaSession visibility tests. The class is intended
|
| -// to be used to run tests under different configurations. Tests
|
| -// should inheret from this class, set up their own command line per
|
| -// their configuration, and use macro INCLUDE_TEST_FROM_BASE_CLASS to
|
| -// include required tests. See
|
| -// media_session_visibility_browsertest_instances.cc for examples.
|
| -class MediaSessionVisibilityBrowserTest
|
| - : public ContentBrowserTest,
|
| - public ::testing::WithParamInterface<
|
| - std::tr1::tuple<VisibilityTestData, Pipeline>> {
|
| - public:
|
| - MediaSessionVisibilityBrowserTest() = default;
|
| - ~MediaSessionVisibilityBrowserTest() override = default;
|
| -
|
| - void SetUpOnMainThread() override {
|
| - ContentBrowserTest::SetUpOnMainThread();
|
| - web_contents_ = shell()->web_contents();
|
| - media_session_ = MediaSession::Get(web_contents_);
|
| -
|
| - media_session_state_loop_runners_[MediaSession::State::ACTIVE] =
|
| - new MessageLoopRunner();
|
| - media_session_state_loop_runners_[MediaSession::State::SUSPENDED] =
|
| - new MessageLoopRunner();
|
| - media_session_state_loop_runners_[MediaSession::State::INACTIVE] =
|
| - new MessageLoopRunner();
|
| - media_session_state_callback_subscription_ =
|
| - media_session_->RegisterMediaSessionStateChangedCallbackForTest(
|
| - base::Bind(&MediaSessionVisibilityBrowserTest::
|
| - OnMediaSessionStateChanged,
|
| - base::Unretained(this)));
|
| - }
|
| -
|
| - void TearDownOnMainThread() override {
|
| - // Unsubscribe the callback subscription before tearing down, so that the
|
| - // CallbackList in MediaSession will be empty when it is destroyed.
|
| - media_session_state_callback_subscription_.reset();
|
| - }
|
| -
|
| - void EnableDisableResumingBackgroundVideos(bool enable) {
|
| - std::string enabled_features;
|
| - std::string disabled_features;
|
| - if (enable)
|
| - enabled_features = media::kResumeBackgroundVideo.name;
|
| - else
|
| - disabled_features = media::kResumeBackgroundVideo.name;
|
| -
|
| - std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
|
| - feature_list->InitializeFromCommandLine(
|
| - enabled_features, disabled_features);
|
| - base::FeatureList::ClearInstanceForTesting();
|
| - base::FeatureList::SetInstance(std::move(feature_list));
|
| - }
|
| -
|
| - void SetUpCommandLine(base::CommandLine* command_line) override {
|
| - command_line->AppendSwitch(
|
| - switches::kDisableGestureRequirementForMediaPlayback);
|
| -#if !defined(OS_ANDROID)
|
| - command_line->AppendSwitch(
|
| - switches::kEnableDefaultMediaSession);
|
| -#endif // !defined(OS_ANDROID)
|
| -
|
| - VisibilityTestData params = GetVisibilityTestData();
|
| -
|
| - if (params.media_suspend == MediaSuspend::ENABLED)
|
| - command_line->AppendSwitch(switches::kEnableMediaSuspend);
|
| - else
|
| - command_line->AppendSwitch(switches::kDisableMediaSuspend);
|
| -
|
| -#if defined(OS_ANDROID)
|
| - Pipeline pipeline = std::tr1::get<1>(GetParam());
|
| - if (pipeline == Pipeline::WMPA)
|
| - command_line->AppendSwitch(switches::kDisableUnifiedMediaPipeline);
|
| -#endif // defined(OS_ANDROID)
|
| -
|
| - if (params.background_resuming == BackgroundResuming::ENABLED) {
|
| - command_line->AppendSwitchASCII(switches::kEnableFeatures,
|
| - media::kResumeBackgroundVideo.name);
|
| - } else {
|
| - command_line->AppendSwitchASCII(switches::kDisableFeatures,
|
| - media::kResumeBackgroundVideo.name);
|
| - }
|
| - }
|
| -
|
| - const VisibilityTestData& GetVisibilityTestData() {
|
| - return std::tr1::get<0>(GetParam());
|
| - }
|
| -
|
| - void StartPlayer() {
|
| - LoadTestPage();
|
| -
|
| - LOG(INFO) << "Starting player";
|
| - ClearMediaSessionStateLoopRunners();
|
| - RunScript(kStartPlayerScript);
|
| - LOG(INFO) << "Waiting for session to be active";
|
| - WaitForMediaSessionState(MediaSession::State::ACTIVE);
|
| - }
|
| -
|
| - // Maybe pause the player depending on whether the session state before hide
|
| - // is SUSPENDED.
|
| - void MaybePausePlayer() {
|
| - ASSERT_TRUE(GetVisibilityTestData().session_state_before_hide
|
| - != SessionState::INACTIVE);
|
| - if (GetVisibilityTestData().session_state_before_hide
|
| - == SessionState::ACTIVE)
|
| - return;
|
| -
|
| - LOG(INFO) << "Pausing player";
|
| - ClearMediaSessionStateLoopRunners();
|
| - RunScript(kPausePlayerScript);
|
| - LOG(INFO) << "Waiting for session to be suspended";
|
| - WaitForMediaSessionState(MediaSession::State::SUSPENDED);
|
| - }
|
| -
|
| - void HideTab() {
|
| - LOG(INFO) << "Hiding the tab";
|
| - ClearMediaSessionStateLoopRunners();
|
| - web_contents_->WasHidden();
|
| - }
|
| -
|
| - void CheckSessionStateAfterHide() {
|
| - MediaSession::State state_before_hide =
|
| - ToMediaSessionState(GetVisibilityTestData().session_state_before_hide);
|
| - MediaSession::State state_after_hide =
|
| - ToMediaSessionState(GetVisibilityTestData().session_state_after_hide);
|
| -
|
| - if (state_before_hide == state_after_hide) {
|
| - LOG(INFO) << "Waiting for 1 second and check session state is unchanged";
|
| - Wait(base::TimeDelta::FromSeconds(1));
|
| - ASSERT_EQ(media_session_->audio_focus_state_, state_after_hide);
|
| - } else {
|
| - LOG(INFO) << "Waiting for Session to change";
|
| - WaitForMediaSessionState(state_after_hide);
|
| - }
|
| -
|
| - LOG(INFO) << "Test succeeded";
|
| - }
|
| -
|
| - private:
|
| - void LoadTestPage() {
|
| - TestNavigationObserver navigation_observer(shell()->web_contents(), 1);
|
| - shell()->LoadURL(GetTestUrl("media/session", "media-session.html"));
|
| - navigation_observer.Wait();
|
| - }
|
| -
|
| - void RunScript(const std::string& script) {
|
| - ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), script));
|
| - }
|
| -
|
| - void ClearMediaSessionStateLoopRunners() {
|
| - for (auto& state_loop_runner : media_session_state_loop_runners_)
|
| - state_loop_runner.second = new MessageLoopRunner();
|
| - }
|
| -
|
| - void OnMediaSessionStateChanged(MediaSession::State state) {
|
| - ASSERT_TRUE(media_session_state_loop_runners_.count(state));
|
| - media_session_state_loop_runners_[state]->Quit();
|
| - }
|
| -
|
| - // TODO(zqzhang): This method is shared with
|
| - // MediaRouterIntegrationTests. Move it into a general place.
|
| - void Wait(base::TimeDelta timeout) {
|
| - base::RunLoop run_loop;
|
| - base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
|
| - FROM_HERE, run_loop.QuitClosure(), timeout);
|
| - run_loop.Run();
|
| - }
|
| -
|
| - void WaitForMediaSessionState(MediaSession::State state) {
|
| - ASSERT_TRUE(media_session_state_loop_runners_.count(state));
|
| - media_session_state_loop_runners_[state]->Run();
|
| - }
|
| -
|
| - MediaSession::State ToMediaSessionState(SessionState state) {
|
| - switch (state) {
|
| - case SessionState::ACTIVE:
|
| - return MediaSession::State::ACTIVE;
|
| - break;
|
| - case SessionState::SUSPENDED:
|
| - return MediaSession::State::SUSPENDED;
|
| - break;
|
| - case SessionState::INACTIVE:
|
| - return MediaSession::State::INACTIVE;
|
| - break;
|
| - default:
|
| - ADD_FAILURE() << "invalid SessionState to convert";
|
| - return MediaSession::State::INACTIVE;
|
| - }
|
| - }
|
| -
|
| - WebContents* web_contents_;
|
| - MediaSession* media_session_;
|
| - // MessageLoopRunners for waiting MediaSession state to change. Note that the
|
| - // MessageLoopRunners can accept Quit() before calling Run(), thus the state
|
| - // change can still be captured before waiting. For example, the MediaSession
|
| - // might go active immediately after calling HTMLMediaElement.play(). A test
|
| - // can listen to the state change before calling play(), and then wait for the
|
| - // state change after play().
|
| - std::map<MediaSession::State, scoped_refptr<MessageLoopRunner> >
|
| - media_session_state_loop_runners_;
|
| - std::unique_ptr<base::CallbackList<void(MediaSession::State)>::Subscription>
|
| - media_session_state_callback_subscription_;
|
| -
|
| - DISALLOW_COPY_AND_ASSIGN(MediaSessionVisibilityBrowserTest);
|
| -};
|
| -
|
| -namespace {
|
| -
|
| -VisibilityTestData kTestParams[] = {
|
| - { MediaSuspend::ENABLED, BackgroundResuming::DISABLED,
|
| - SessionState::SUSPENDED, SessionState::INACTIVE },
|
| - { MediaSuspend::ENABLED, BackgroundResuming::DISABLED,
|
| - SessionState::ACTIVE, SessionState::INACTIVE },
|
| - { MediaSuspend::ENABLED, BackgroundResuming::ENABLED,
|
| - SessionState::ACTIVE, SessionState::SUSPENDED },
|
| - { MediaSuspend::ENABLED, BackgroundResuming::ENABLED,
|
| - SessionState::SUSPENDED, SessionState::SUSPENDED },
|
| - { MediaSuspend::DISABLED, BackgroundResuming::DISABLED,
|
| - SessionState::SUSPENDED, SessionState::SUSPENDED },
|
| - { MediaSuspend::DISABLED, BackgroundResuming::DISABLED,
|
| - SessionState::ACTIVE, SessionState::ACTIVE },
|
| - { MediaSuspend::DISABLED, BackgroundResuming::ENABLED,
|
| - SessionState::ACTIVE, SessionState::ACTIVE },
|
| - { MediaSuspend::DISABLED, BackgroundResuming::ENABLED,
|
| - SessionState::SUSPENDED, SessionState::SUSPENDED },
|
| -};
|
| -
|
| -Pipeline kPipelines[] = {
|
| - Pipeline::WMPI,
|
| -#if defined(OS_ANDROID)
|
| - // Disabling WMPA tests because of https://crbug.com/646312
|
| - // Pipeline::WMPA,
|
| -#endif // defined(OS_ANDROID)
|
| -};
|
| -
|
| -} // anonymous namespace
|
| -
|
| -IN_PROC_BROWSER_TEST_P(MediaSessionVisibilityBrowserTest,
|
| - TestEntryPoint) {
|
| - StartPlayer();
|
| - MaybePausePlayer();
|
| - HideTab();
|
| - CheckSessionStateAfterHide();
|
| -}
|
| -
|
| -INSTANTIATE_TEST_CASE_P(MediaSessionVisibilityBrowserTestInstances,
|
| - MediaSessionVisibilityBrowserTest,
|
| - ::testing::Combine(::testing::ValuesIn(kTestParams),
|
| - ::testing::ValuesIn(kPipelines)));
|
| -
|
| -} // namespace content
|
|
|