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_integration_browsertest_ba se.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/timer/elapsed_timer.h" | |
| 9 #include "content/browser/media/session/media_session.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 #include "content/public/common/content_switches.h" | |
| 12 #include "content/public/test/content_browser_test_utils.h" | |
| 13 #include "content/public/test/test_navigation_observer.h" | |
| 14 #include "content/shell/browser/shell.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 static const char kStartPlayerScript[] = | |
| 19 "document.getElementById('long-video').play()"; | |
| 20 static const char kPausePlayerScript[] = | |
| 21 "document.getElementById('long-video').pause()"; | |
| 22 } | |
| 23 | |
| 24 using content::MediaSessionIntegrationBrowserTestBase; | |
| 25 | |
| 26 void MediaSessionIntegrationBrowserTestBase::SetUpOnMainThread() { | |
| 27 ContentBrowserTest::SetUpOnMainThread(); | |
| 28 web_contents_ = shell()->web_contents(); | |
| 29 media_session_ = MediaSession::Get(web_contents_); | |
| 30 } | |
| 31 | |
| 32 void MediaSessionIntegrationBrowserTestBase::SetUpCommandLine( | |
| 33 base::CommandLine* command_line) { | |
| 34 command_line->AppendSwitch( | |
| 35 switches::kDisableGestureRequirementForMediaPlayback); | |
| 36 } | |
| 37 | |
| 38 void MediaSessionIntegrationBrowserTestBase::LoadTestPage() { | |
| 39 TestNavigationObserver navigation_observer(shell()->web_contents(), 1); | |
| 40 shell()->LoadURL(GetTestUrl("android/media", "media-session.html")); | |
|
mlamouri (slow - plz ping)
2016/03/23 15:10:16
Maybe you could move this file in a follow-up? Med
Zhiqiang Zhang (Slow)
2016/03/24 18:16:49
Sure, will do.
| |
| 41 navigation_observer.Wait(); | |
| 42 } | |
| 43 | |
| 44 void MediaSessionIntegrationBrowserTestBase::RunScript(std::string script) { | |
| 45 content::ExecuteScriptAndGetValue( | |
| 46 web_contents_->GetMainFrame(), script); | |
| 47 } | |
| 48 | |
| 49 // TODO(zqzhang): This method is shared with | |
| 50 // MediaIntegrationTests. Move it into a general place. | |
| 51 bool MediaSessionIntegrationBrowserTestBase::ConditionalWait( | |
| 52 base::TimeDelta timeout, | |
| 53 base::TimeDelta interval, | |
| 54 const base::Callback<bool(void)>& callback) { | |
| 55 base::ElapsedTimer timer; | |
| 56 do { | |
| 57 if (callback.Run()) | |
| 58 return true; | |
| 59 | |
| 60 base::RunLoop run_loop; | |
| 61 base::MessageLoop::current()->PostDelayedTask( | |
| 62 FROM_HERE, run_loop.QuitClosure(), interval); | |
| 63 run_loop.Run(); | |
| 64 } while (timer.Elapsed() < timeout); | |
| 65 | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 bool MediaSessionIntegrationBrowserTestBase::IsMediaSessionActive() { | |
| 70 return media_session_->audio_focus_state_for_test() == | |
| 71 MediaSession::State::ACTIVE; | |
| 72 } | |
| 73 | |
| 74 bool MediaSessionIntegrationBrowserTestBase::IsMediaSessionSuspended() { | |
| 75 return media_session_->audio_focus_state_for_test() == | |
| 76 MediaSession::State::SUSPENDED; | |
| 77 } | |
| 78 | |
| 79 bool MediaSessionIntegrationBrowserTestBase::IsMediaSessionInactive() { | |
| 80 return media_session_->audio_focus_state_for_test() == | |
| 81 MediaSession::State::INACTIVE; | |
| 82 } | |
| 83 | |
| 84 void MediaSessionIntegrationBrowserTestBase | |
| 85 ::TestSessionInactiveWhenHiddenAfterContentPause() { | |
| 86 LoadTestPage(); | |
| 87 RunScript(kStartPlayerScript); | |
| 88 ASSERT_TRUE(ConditionalWait( | |
| 89 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(50), | |
|
mlamouri (slow - plz ping)
2016/03/23 15:10:16
I'm not sure if there is a better way but you poin
DaleCurtis
2016/03/24 18:22:41
there are some testing::Timeout values that you sh
| |
| 90 base::Bind( | |
| 91 &MediaSessionIntegrationBrowserTestBase::IsMediaSessionActive, | |
| 92 base::Unretained(this)))); | |
| 93 RunScript(kPausePlayerScript); | |
| 94 ASSERT_TRUE(ConditionalWait( | |
| 95 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(50), | |
| 96 base::Bind( | |
| 97 &MediaSessionIntegrationBrowserTestBase::IsMediaSessionSuspended, | |
| 98 base::Unretained(this)))); | |
| 99 web_contents_->WasHidden(); | |
| 100 ASSERT_TRUE(ConditionalWait( | |
| 101 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(50), | |
| 102 base::Bind( | |
| 103 &MediaSessionIntegrationBrowserTestBase::IsMediaSessionInactive, | |
| 104 base::Unretained(this)))); | |
|
mlamouri (slow - plz ping)
2016/03/23 15:10:15
Could you improve the readability here? Maybe some
Zhiqiang Zhang (Slow)
2016/03/24 18:16:49
Done.
| |
| 105 } | |
| 106 | |
| 107 void MediaSessionIntegrationBrowserTestBase | |
| 108 ::TestSessionInactiveWhenHiddenWhilePlaying() { | |
| 109 LoadTestPage(); | |
| 110 RunScript(kStartPlayerScript); | |
| 111 ASSERT_TRUE(ConditionalWait( | |
| 112 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(50), | |
| 113 base::Bind( | |
| 114 &MediaSessionIntegrationBrowserTestBase::IsMediaSessionActive, | |
| 115 base::Unretained(this)))); | |
| 116 web_contents_->WasHidden(); | |
| 117 ASSERT_TRUE(ConditionalWait( | |
| 118 base::TimeDelta::FromSeconds(5), base::TimeDelta::FromMilliseconds(50), | |
| 119 base::Bind( | |
| 120 &MediaSessionIntegrationBrowserTestBase::IsMediaSessionInactive, | |
| 121 base::Unretained(this)))); | |
| 122 } | |
|
mlamouri (slow - plz ping)
2016/03/23 15:10:16
Could you have these tests with kEnableMediaSuspen
Zhiqiang Zhang (Slow)
2016/03/24 18:16:49
Done.
| |
| OLD | NEW |