| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/command_line.h" |
| 6 #include "content/browser/media/session/media_session.h" |
| 7 #include "content/public/browser/web_contents.h" |
| 8 #include "content/public/common/content_switches.h" |
| 9 #include "content/public/test/browser_test_utils.h" |
| 10 #include "content/public/test/content_browser_test.h" |
| 11 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/public/test/test_navigation_observer.h" |
| 13 #include "content/shell/browser/shell.h" |
| 14 #include "media/base/media_switches.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace content { |
| 18 namespace { |
| 19 static const char kStartPlayerScript[] = |
| 20 "document.getElementById('long-video').play()"; |
| 21 static const char kPausePlayerScript[] = |
| 22 "document.getElementById('long-video').pause()"; |
| 23 } |
| 24 |
| 25 |
| 26 // Base class of MediaSession visibility tests. The class is intended |
| 27 // to be used to run tests under different configurations. Tests |
| 28 // should inheret from this class, set up their own command line per |
| 29 // their configuration, and use macro INCLUDE_TEST_FROM_BASE_CLASS to |
| 30 // include required tests. See |
| 31 // media_session_visibility_browsertest_instances.cc for examples. |
| 32 class MediaSessionVisibilityBrowserTest |
| 33 : public ContentBrowserTest { |
| 34 public: |
| 35 MediaSessionVisibilityBrowserTest() = default; |
| 36 ~MediaSessionVisibilityBrowserTest() override = default; |
| 37 |
| 38 void SetUpOnMainThread() override { |
| 39 ContentBrowserTest::SetUpOnMainThread(); |
| 40 web_contents_ = shell()->web_contents(); |
| 41 media_session_ = MediaSession::Get(web_contents_); |
| 42 |
| 43 media_session_state_loop_runners_[MediaSession::State::ACTIVE] = |
| 44 new MessageLoopRunner(); |
| 45 media_session_state_loop_runners_[MediaSession::State::SUSPENDED] = |
| 46 new MessageLoopRunner(); |
| 47 media_session_state_loop_runners_[MediaSession::State::INACTIVE] = |
| 48 new MessageLoopRunner(); |
| 49 media_session_state_callback_subscription_ = |
| 50 media_session_->RegisterMediaSessionStateChangedCallbackForTest( |
| 51 base::Bind(&MediaSessionVisibilityBrowserTest:: |
| 52 OnMediaSessionStateChanged, |
| 53 base::Unretained(this))); |
| 54 } |
| 55 |
| 56 void TearDownOnMainThread() override { |
| 57 // Unsubscribe the callback subscription before tearing down, so that the |
| 58 // CallbackList in MediaSession will be empty when it is destroyed. |
| 59 media_session_state_callback_subscription_.reset(); |
| 60 } |
| 61 |
| 62 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 63 command_line->AppendSwitch( |
| 64 switches::kDisableGestureRequirementForMediaPlayback); |
| 65 #if !defined(OS_ANDROID) |
| 66 command_line->AppendSwitch( |
| 67 switches::kEnableDefaultMediaSession); |
| 68 #endif // !defined(OS_ANDROID) |
| 69 } |
| 70 |
| 71 void LoadTestPage() { |
| 72 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); |
| 73 shell()->LoadURL(GetTestUrl("android/media", "media-session.html")); |
| 74 navigation_observer.Wait(); |
| 75 } |
| 76 |
| 77 void RunScript(const std::string& script) { |
| 78 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), script)); |
| 79 } |
| 80 |
| 81 void ClearMediaSessionStateLoopRunners() { |
| 82 for (auto& state_loop_runner : media_session_state_loop_runners_) |
| 83 state_loop_runner.second = new MessageLoopRunner(); |
| 84 } |
| 85 |
| 86 void OnMediaSessionStateChanged(MediaSession::State state) { |
| 87 ASSERT_TRUE(media_session_state_loop_runners_.count(state)); |
| 88 media_session_state_loop_runners_[state]->Quit(); |
| 89 } |
| 90 |
| 91 // TODO(zqzhang): This method is shared with |
| 92 // MediaRouterIntegrationTests. Move it into a general place. |
| 93 void Wait(base::TimeDelta timeout) { |
| 94 base::RunLoop run_loop; |
| 95 base::MessageLoop::current()->PostDelayedTask( |
| 96 FROM_HERE, run_loop.QuitClosure(), timeout); |
| 97 run_loop.Run(); |
| 98 } |
| 99 |
| 100 void WaitForMediaSessionState(MediaSession::State state) { |
| 101 ASSERT_TRUE(media_session_state_loop_runners_.count(state)); |
| 102 media_session_state_loop_runners_[state]->Run(); |
| 103 } |
| 104 |
| 105 protected: |
| 106 void TestSessionInactiveWhenHiddenAfterContentPause() { |
| 107 LoadTestPage(); |
| 108 |
| 109 ClearMediaSessionStateLoopRunners(); |
| 110 RunScript(kStartPlayerScript); |
| 111 WaitForMediaSessionState(MediaSession::State::ACTIVE); |
| 112 |
| 113 ClearMediaSessionStateLoopRunners(); |
| 114 RunScript(kPausePlayerScript); |
| 115 WaitForMediaSessionState(MediaSession::State::SUSPENDED); |
| 116 |
| 117 ClearMediaSessionStateLoopRunners(); |
| 118 web_contents_->WasHidden(); |
| 119 WaitForMediaSessionState(MediaSession::State::INACTIVE); |
| 120 } |
| 121 |
| 122 void TestSessionInactiveWhenHiddenWhilePlaying() { |
| 123 LoadTestPage(); |
| 124 |
| 125 ClearMediaSessionStateLoopRunners(); |
| 126 RunScript(kStartPlayerScript); |
| 127 WaitForMediaSessionState(MediaSession::State::ACTIVE); |
| 128 |
| 129 ClearMediaSessionStateLoopRunners(); |
| 130 web_contents_->WasHidden(); |
| 131 WaitForMediaSessionState(MediaSession::State::INACTIVE); |
| 132 } |
| 133 |
| 134 void TestSessionSuspendedWhenHiddenAfterContentPause() { |
| 135 LoadTestPage(); |
| 136 |
| 137 ClearMediaSessionStateLoopRunners(); |
| 138 RunScript(kStartPlayerScript); |
| 139 WaitForMediaSessionState(MediaSession::State::ACTIVE); |
| 140 |
| 141 ClearMediaSessionStateLoopRunners(); |
| 142 RunScript(kPausePlayerScript); |
| 143 WaitForMediaSessionState(MediaSession::State::SUSPENDED); |
| 144 |
| 145 // Wait for 1 second and check the MediaSession state. |
| 146 // No better solution till now. |
| 147 web_contents_->WasHidden(); |
| 148 Wait(base::TimeDelta::FromSeconds(1)); |
| 149 ASSERT_EQ(media_session_->audio_focus_state_, |
| 150 MediaSession::State::SUSPENDED); |
| 151 } |
| 152 |
| 153 void TestSessionActiveWhenHiddenWhilePlaying() { |
| 154 LoadTestPage(); |
| 155 |
| 156 ClearMediaSessionStateLoopRunners(); |
| 157 RunScript(kStartPlayerScript); |
| 158 WaitForMediaSessionState(MediaSession::State::ACTIVE); |
| 159 |
| 160 // Wait for 1 second and check the MediaSession state. |
| 161 // No better solution till now. |
| 162 web_contents_->WasHidden(); |
| 163 Wait(base::TimeDelta::FromSeconds(1)); |
| 164 ASSERT_EQ(media_session_->audio_focus_state_, |
| 165 MediaSession::State::ACTIVE); |
| 166 } |
| 167 |
| 168 WebContents* web_contents_; |
| 169 MediaSession* media_session_; |
| 170 // MessageLoopRunners for waiting MediaSession state to change. Note that the |
| 171 // MessageLoopRunners can accept Quit() before calling Run(), thus the state |
| 172 // change can still be captured before waiting. For example, the MediaSession |
| 173 // might go active immediately after calling HTMLMediaElement.play(). A test |
| 174 // can listen to the state change before calling play(), and then wait for the |
| 175 // state change after play(). |
| 176 std::map<MediaSession::State, scoped_refptr<MessageLoopRunner> > |
| 177 media_session_state_loop_runners_; |
| 178 scoped_ptr<base::CallbackList<void(MediaSession::State)>::Subscription> |
| 179 media_session_state_callback_subscription_; |
| 180 |
| 181 private: |
| 182 DISALLOW_COPY_AND_ASSIGN(MediaSessionVisibilityBrowserTest); |
| 183 }; |
| 184 |
| 185 // Helper macro to include tests from the base class. |
| 186 #define INCLUDE_TEST_FROM_BASE_CLASS(test_fixture, test_name) \ |
| 187 IN_PROC_BROWSER_TEST_F(test_fixture, test_name) { \ |
| 188 test_name(); \ |
| 189 } |
| 190 |
| 191 /////////////////////////////////////////////////////////////////////////////// |
| 192 // Configuration instances. |
| 193 |
| 194 // UnifiedPipeline + SuspendOnHide |
| 195 class MediaSessionVisibilityBrowserTest_UnifiedPipeline_SuspendOnHide : |
| 196 public MediaSessionVisibilityBrowserTest { |
| 197 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 198 MediaSessionVisibilityBrowserTest::SetUpCommandLine(command_line); |
| 199 #if !defined(OS_ANDROID) |
| 200 command_line->AppendSwitch(switches::kEnableMediaSuspend); |
| 201 #endif // defined(OS_ANDROID) |
| 202 } |
| 203 }; |
| 204 |
| 205 INCLUDE_TEST_FROM_BASE_CLASS( |
| 206 MediaSessionVisibilityBrowserTest_UnifiedPipeline_SuspendOnHide, |
| 207 TestSessionInactiveWhenHiddenAfterContentPause) |
| 208 INCLUDE_TEST_FROM_BASE_CLASS( |
| 209 MediaSessionVisibilityBrowserTest_UnifiedPipeline_SuspendOnHide, |
| 210 TestSessionInactiveWhenHiddenWhilePlaying) |
| 211 |
| 212 // UnifiedPipeline + NosuspendOnHide |
| 213 class MediaSessionVisibilityBrowserTest_UnifiedPipeline_NosuspendOnHide : |
| 214 public MediaSessionVisibilityBrowserTest { |
| 215 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 216 MediaSessionVisibilityBrowserTest::SetUpCommandLine(command_line); |
| 217 #if defined(OS_ANDROID) |
| 218 command_line->AppendSwitch(switches::kDisableMediaSuspend); |
| 219 #endif // defined(OS_ANDROID) |
| 220 } |
| 221 }; |
| 222 |
| 223 INCLUDE_TEST_FROM_BASE_CLASS( |
| 224 MediaSessionVisibilityBrowserTest_UnifiedPipeline_NosuspendOnHide, |
| 225 TestSessionSuspendedWhenHiddenAfterContentPause) |
| 226 INCLUDE_TEST_FROM_BASE_CLASS( |
| 227 MediaSessionVisibilityBrowserTest_UnifiedPipeline_NosuspendOnHide, |
| 228 TestSessionActiveWhenHiddenWhilePlaying) |
| 229 |
| 230 #if defined(OS_ANDROID) |
| 231 // AndroidPipeline + SuspendOnHide |
| 232 class MediaSessionVisibilityBrowserTest_AndroidPipeline_SuspendOnHide : |
| 233 public MediaSessionVisibilityBrowserTest { |
| 234 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 235 MediaSessionVisibilityBrowserTest::SetUpCommandLine(command_line); |
| 236 command_line->AppendSwitch(switches::kDisableUnifiedMediaPipeline); |
| 237 } |
| 238 }; |
| 239 |
| 240 INCLUDE_TEST_FROM_BASE_CLASS( |
| 241 MediaSessionVisibilityBrowserTest_AndroidPipeline_SuspendOnHide, |
| 242 TestSessionInactiveWhenHiddenAfterContentPause) |
| 243 INCLUDE_TEST_FROM_BASE_CLASS( |
| 244 MediaSessionVisibilityBrowserTest_AndroidPipeline_SuspendOnHide, |
| 245 TestSessionInactiveWhenHiddenWhilePlaying) |
| 246 |
| 247 // AndroidPipeline + NosuspendOnHide |
| 248 class MediaSessionVisibilityBrowserTest_AndroidPipeline_NosuspendOnHide : |
| 249 public MediaSessionVisibilityBrowserTest { |
| 250 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 251 MediaSessionVisibilityBrowserTest::SetUpCommandLine(command_line); |
| 252 command_line->AppendSwitch(switches::kDisableUnifiedMediaPipeline); |
| 253 command_line->AppendSwitch(switches::kDisableMediaSuspend); |
| 254 } |
| 255 }; |
| 256 |
| 257 INCLUDE_TEST_FROM_BASE_CLASS( |
| 258 MediaSessionVisibilityBrowserTest_AndroidPipeline_NosuspendOnHide, |
| 259 TestSessionSuspendedWhenHiddenAfterContentPause) |
| 260 INCLUDE_TEST_FROM_BASE_CLASS( |
| 261 MediaSessionVisibilityBrowserTest_AndroidPipeline_NosuspendOnHide, |
| 262 TestSessionActiveWhenHiddenWhilePlaying) |
| 263 |
| 264 #endif // defined(OS_ANDROID) |
| 265 |
| 266 } // namespace content |
| OLD | NEW |