OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/public/browser/media_session.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/public/test/browser_test_utils.h" |
| 14 #include "content/public/test/content_browser_test.h" |
| 15 #include "content/public/test/content_browser_test_utils.h" |
| 16 #include "content/shell/browser/shell.h" |
| 17 #include "media/base/media_switches.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Integration tests for content::MediaSession that do not take into |
| 24 // consideration the implementation details contrary to |
| 25 // MediaSessionImplBrowserTest. |
| 26 class MediaSessionBrowserTest : public ContentBrowserTest { |
| 27 public: |
| 28 MediaSessionBrowserTest() = default; |
| 29 |
| 30 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 31 command_line->AppendSwitch(switches::kIgnoreAutoplayRestrictionsForTests); |
| 32 } |
| 33 |
| 34 void EnableInternalMediaSesion() { |
| 35 #if !defined(OS_ANDROID) |
| 36 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 37 switches::kEnableInternalMediaSession); |
| 38 #endif // !defined(OS_ANDROID) |
| 39 } |
| 40 |
| 41 void StartPlaybackAndWait(Shell* shell, const std::string& id) { |
| 42 shell->web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 43 base::ASCIIToUTF16("document.querySelector('#" + id + "').play();")); |
| 44 WaitForStart(shell); |
| 45 } |
| 46 |
| 47 void StopPlaybackAndWait(Shell* shell, const std::string& id) { |
| 48 shell->web_contents()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 49 base::ASCIIToUTF16("document.querySelector('#" + id + "').pause();")); |
| 50 WaitForStop(shell); |
| 51 } |
| 52 |
| 53 void WaitForStart(Shell* shell) { |
| 54 MediaStartStopObserver observer(shell->web_contents(), |
| 55 MediaStartStopObserver::Type::kStart); |
| 56 observer.Wait(); |
| 57 } |
| 58 |
| 59 void WaitForStop(Shell* shell) { |
| 60 MediaStartStopObserver observer(shell->web_contents(), |
| 61 MediaStartStopObserver::Type::kStop); |
| 62 observer.Wait(); |
| 63 } |
| 64 |
| 65 bool IsPlaying(Shell* shell, const std::string& id) { |
| 66 bool result; |
| 67 EXPECT_TRUE( |
| 68 ExecuteScriptAndExtractBool(shell->web_contents(), |
| 69 "window.domAutomationController.send(" |
| 70 "!document.querySelector('#" + |
| 71 id + "').paused);", |
| 72 &result)); |
| 73 return result; |
| 74 } |
| 75 |
| 76 private: |
| 77 class MediaStartStopObserver : public WebContentsObserver { |
| 78 public: |
| 79 enum class Type { kStart, kStop }; |
| 80 |
| 81 MediaStartStopObserver(WebContents* web_contents, Type type) |
| 82 : WebContentsObserver(web_contents), type_(type) {} |
| 83 |
| 84 void MediaStartedPlaying(const MediaPlayerInfo& info, |
| 85 const MediaPlayerId& id) override { |
| 86 if (type_ != Type::kStart) |
| 87 return; |
| 88 |
| 89 run_loop_.Quit(); |
| 90 } |
| 91 |
| 92 void MediaStoppedPlaying(const MediaPlayerInfo& info, |
| 93 const MediaPlayerId& id) override { |
| 94 if (type_ != Type::kStop) |
| 95 return; |
| 96 |
| 97 run_loop_.Quit(); |
| 98 } |
| 99 |
| 100 void Wait() { run_loop_.Run(); } |
| 101 |
| 102 private: |
| 103 base::RunLoop run_loop_; |
| 104 Type type_; |
| 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(MediaStartStopObserver); |
| 107 }; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(MediaSessionBrowserTest); |
| 110 }; |
| 111 |
| 112 } // anonymous namespace |
| 113 |
| 114 #if !defined(OS_ANDROID) |
| 115 // The feature can't be disabled on Android. |
| 116 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MediaSessionNoOpWhenDisabled) { |
| 117 NavigateToURL(shell(), GetTestUrl("media/session", "media-session.html")); |
| 118 |
| 119 MediaSession* media_session = MediaSession::Get(shell()->web_contents()); |
| 120 ASSERT_NE(nullptr, media_session); |
| 121 |
| 122 StartPlaybackAndWait(shell(), "long-video"); |
| 123 StartPlaybackAndWait(shell(), "long-audio"); |
| 124 |
| 125 media_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| 126 StopPlaybackAndWait(shell(), "long-audio"); |
| 127 |
| 128 // At that point, only "long-audio" is paused. |
| 129 EXPECT_FALSE(IsPlaying(shell(), "long-audio")); |
| 130 EXPECT_TRUE(IsPlaying(shell(), "long-video")); |
| 131 } |
| 132 #endif // !defined(OS_ANDROID) |
| 133 |
| 134 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, SimplePlayPause) { |
| 135 EnableInternalMediaSesion(); |
| 136 |
| 137 NavigateToURL(shell(), GetTestUrl("media/session", "media-session.html")); |
| 138 |
| 139 MediaSession* media_session = MediaSession::Get(shell()->web_contents()); |
| 140 ASSERT_NE(nullptr, media_session); |
| 141 |
| 142 StartPlaybackAndWait(shell(), "long-video"); |
| 143 |
| 144 media_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| 145 WaitForStop(shell()); |
| 146 EXPECT_FALSE(IsPlaying(shell(), "long-video")); |
| 147 |
| 148 media_session->Resume(MediaSession::SuspendType::SYSTEM); |
| 149 WaitForStart(shell()); |
| 150 EXPECT_TRUE(IsPlaying(shell(), "long-video")); |
| 151 } |
| 152 |
| 153 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MultiplePlayersPlayPause) { |
| 154 EnableInternalMediaSesion(); |
| 155 |
| 156 NavigateToURL(shell(), GetTestUrl("media/session", "media-session.html")); |
| 157 |
| 158 MediaSession* media_session = MediaSession::Get(shell()->web_contents()); |
| 159 ASSERT_NE(nullptr, media_session); |
| 160 |
| 161 StartPlaybackAndWait(shell(), "long-video"); |
| 162 StartPlaybackAndWait(shell(), "long-audio"); |
| 163 |
| 164 media_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| 165 WaitForStop(shell()); |
| 166 EXPECT_FALSE(IsPlaying(shell(), "long-video")); |
| 167 EXPECT_FALSE(IsPlaying(shell(), "long-audio")); |
| 168 |
| 169 media_session->Resume(MediaSession::SuspendType::SYSTEM); |
| 170 WaitForStart(shell()); |
| 171 EXPECT_TRUE(IsPlaying(shell(), "long-video")); |
| 172 EXPECT_TRUE(IsPlaying(shell(), "long-audio")); |
| 173 } |
| 174 |
| 175 #if !defined(OS_ANDROID) |
| 176 // On Android, System Audio Focus would break this test. |
| 177 IN_PROC_BROWSER_TEST_F(MediaSessionBrowserTest, MultipleTabsPlayPause) { |
| 178 EnableInternalMediaSesion(); |
| 179 |
| 180 Shell* other_shell = CreateBrowser(); |
| 181 |
| 182 NavigateToURL(shell(), GetTestUrl("media/session", "media-session.html")); |
| 183 NavigateToURL(other_shell, GetTestUrl("media/session", "media-session.html")); |
| 184 |
| 185 MediaSession* media_session = MediaSession::Get(shell()->web_contents()); |
| 186 MediaSession* other_media_session = |
| 187 MediaSession::Get(other_shell->web_contents()); |
| 188 ASSERT_NE(nullptr, media_session); |
| 189 ASSERT_NE(nullptr, other_media_session); |
| 190 |
| 191 StartPlaybackAndWait(shell(), "long-video"); |
| 192 StartPlaybackAndWait(other_shell, "long-video"); |
| 193 |
| 194 media_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| 195 WaitForStop(shell()); |
| 196 EXPECT_FALSE(IsPlaying(shell(), "long-video")); |
| 197 EXPECT_TRUE(IsPlaying(other_shell, "long-video")); |
| 198 |
| 199 other_media_session->Suspend(MediaSession::SuspendType::SYSTEM); |
| 200 WaitForStop(other_shell); |
| 201 EXPECT_FALSE(IsPlaying(shell(), "long-video")); |
| 202 EXPECT_FALSE(IsPlaying(other_shell, "long-video")); |
| 203 |
| 204 media_session->Resume(MediaSession::SuspendType::SYSTEM); |
| 205 WaitForStart(shell()); |
| 206 EXPECT_TRUE(IsPlaying(shell(), "long-video")); |
| 207 EXPECT_FALSE(IsPlaying(other_shell, "long-video")); |
| 208 |
| 209 other_media_session->Resume(MediaSession::SuspendType::SYSTEM); |
| 210 WaitForStart(other_shell); |
| 211 EXPECT_TRUE(IsPlaying(shell(), "long-video")); |
| 212 EXPECT_TRUE(IsPlaying(other_shell, "long-video")); |
| 213 } |
| 214 #endif // defined(OS_ANDROID) |
| 215 |
| 216 } // namespace content |
OLD | NEW |