Chromium Code Reviews| 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 "content/browser/media/session/media_session_visibility_browsertest_bas e.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "content/public/browser/web_contents.h" | |
| 9 #include "content/public/common/content_switches.h" | |
| 10 #include "content/public/test/browser_test_utils.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 MediaSessionVisibilityBrowserTestBase:: | |
| 26 MediaSessionVisibilityBrowserTestBase() { | |
|
DaleCurtis
2016/03/31 16:50:26
Is this the style that git cl format applied? It s
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
Done.
| |
| 27 } | |
| 28 | |
| 29 MediaSessionVisibilityBrowserTestBase:: | |
| 30 ~MediaSessionVisibilityBrowserTestBase() { | |
| 31 } | |
| 32 | |
| 33 void MediaSessionVisibilityBrowserTestBase::SetUpOnMainThread() { | |
| 34 ContentBrowserTest::SetUpOnMainThread(); | |
| 35 web_contents_ = shell()->web_contents(); | |
| 36 media_session_ = MediaSession::Get(web_contents_); | |
| 37 | |
| 38 media_session_state_loop_runners_[MediaSession::State::ACTIVE] = | |
| 39 new MessageLoopRunner; | |
|
DaleCurtis
2016/03/31 16:50:26
() after class name. ditto for all "new XXX" below
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
Done.
| |
| 40 media_session_state_loop_runners_[MediaSession::State::SUSPENDED] = | |
| 41 new MessageLoopRunner; | |
| 42 media_session_state_loop_runners_[MediaSession::State::INACTIVE] = | |
| 43 new MessageLoopRunner; | |
| 44 media_session_state_callback_subscription_ = | |
| 45 media_session_->RegisterMediaSessionStateChangedCallbackForTest( | |
| 46 base::Bind(&MediaSessionVisibilityBrowserTestBase:: | |
| 47 OnMediaSessionStateChanged, | |
| 48 base::Unretained(this))); | |
| 49 } | |
| 50 | |
| 51 void MediaSessionVisibilityBrowserTestBase::TearDownOnMainThread() { | |
| 52 // Unsubscribe the callback subscription before tearing down, so that the | |
| 53 // CallbackList in MediaSession will be empty when it is destroyed. | |
| 54 media_session_state_callback_subscription_.reset(); | |
| 55 } | |
| 56 | |
| 57 void MediaSessionVisibilityBrowserTestBase::SetUpCommandLine( | |
| 58 base::CommandLine* command_line) { | |
| 59 command_line->AppendSwitch( | |
| 60 switches::kDisableGestureRequirementForMediaPlayback); | |
| 61 #if !defined(OS_ANDROID) | |
| 62 command_line->AppendSwitch( | |
| 63 switches::kEnableDefaultMediaSession); | |
| 64 #endif // !defined(OS_ANDROID) | |
| 65 } | |
| 66 | |
| 67 void MediaSessionVisibilityBrowserTestBase::LoadTestPage() { | |
| 68 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | |
| 69 shell()->LoadURL(GetTestUrl("android/media", "media-session.html")); | |
| 70 navigation_observer.Wait(); | |
| 71 } | |
| 72 | |
| 73 void MediaSessionVisibilityBrowserTestBase::RunScript(std::string script) { | |
|
DaleCurtis
2016/03/31 16:50:26
const& for strings.
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
Done.
| |
| 74 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), script)); | |
| 75 } | |
| 76 | |
| 77 void MediaSessionVisibilityBrowserTestBase:: | |
| 78 ClearMediaSessionStateLoopRunners() { | |
| 79 for (auto& state_loop_runner : media_session_state_loop_runners_) | |
| 80 state_loop_runner.second = new MessageLoopRunner; | |
|
DaleCurtis
2016/03/31 16:50:26
()
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
Done.
| |
| 81 } | |
| 82 | |
| 83 void MediaSessionVisibilityBrowserTestBase::OnMediaSessionStateChanged( | |
| 84 MediaSession::State state) { | |
| 85 ASSERT_TRUE(media_session_state_loop_runners_.count(state)); | |
| 86 media_session_state_loop_runners_[state]->Quit(); | |
| 87 } | |
| 88 | |
| 89 | |
|
DaleCurtis
2016/03/31 16:50:26
Extra line.
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
Done.
| |
| 90 void MediaSessionVisibilityBrowserTestBase::Wait(base::TimeDelta timeout) { | |
|
DaleCurtis
2016/03/31 16:50:26
You don't really need this, but up to you. THe bro
Zhiqiang Zhang (Slow)
2016/03/31 19:58:39
I think we still need this since this is for verif
| |
| 91 base::RunLoop run_loop; | |
| 92 base::MessageLoop::current()->PostDelayedTask( | |
| 93 FROM_HERE, run_loop.QuitClosure(), timeout); | |
| 94 run_loop.Run(); | |
| 95 } | |
| 96 | |
| 97 void MediaSessionVisibilityBrowserTestBase::WaitForMediaSessionState( | |
| 98 MediaSession::State state) { | |
| 99 ASSERT_TRUE(media_session_state_loop_runners_.count(state)); | |
| 100 media_session_state_loop_runners_[state]->Run(); | |
| 101 } | |
| 102 | |
| 103 void MediaSessionVisibilityBrowserTestBase:: | |
| 104 TestSessionInactiveWhenHiddenAfterContentPause() { | |
| 105 LoadTestPage(); | |
| 106 | |
| 107 ClearMediaSessionStateLoopRunners(); | |
| 108 RunScript(kStartPlayerScript); | |
| 109 WaitForMediaSessionState(MediaSession::State::ACTIVE); | |
| 110 | |
| 111 ClearMediaSessionStateLoopRunners(); | |
| 112 RunScript(kPausePlayerScript); | |
| 113 WaitForMediaSessionState(MediaSession::State::SUSPENDED); | |
| 114 | |
| 115 ClearMediaSessionStateLoopRunners(); | |
| 116 web_contents_->WasHidden(); | |
| 117 WaitForMediaSessionState(MediaSession::State::INACTIVE); | |
| 118 } | |
| 119 | |
| 120 void MediaSessionVisibilityBrowserTestBase:: | |
| 121 TestSessionInactiveWhenHiddenWhilePlaying() { | |
| 122 LoadTestPage(); | |
| 123 | |
| 124 ClearMediaSessionStateLoopRunners(); | |
| 125 RunScript(kStartPlayerScript); | |
| 126 WaitForMediaSessionState(MediaSession::State::ACTIVE); | |
| 127 | |
| 128 ClearMediaSessionStateLoopRunners(); | |
| 129 web_contents_->WasHidden(); | |
| 130 WaitForMediaSessionState(MediaSession::State::INACTIVE); | |
| 131 } | |
| 132 | |
| 133 void MediaSessionVisibilityBrowserTestBase:: | |
| 134 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_, MediaSession::State::SUSPENDED); | |
| 150 } | |
| 151 | |
| 152 void MediaSessionVisibilityBrowserTestBase:: | |
| 153 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_, MediaSession::State::ACTIVE); | |
| 165 } | |
| 166 | |
| 167 } // namespace content | |
| OLD | NEW |