| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/session/media_session_service_impl.h" |
| 6 |
| 5 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/run_loop.h" |
| 9 #include "content/browser/media/session/media_session_impl.h" |
| 10 #include "content/browser/media/session/media_session_player_observer.h" |
| 11 #include "content/public/browser/web_contents_observer.h" |
| 6 #include "content/public/test/content_browser_test.h" | 12 #include "content/public/test/content_browser_test.h" |
| 7 #include "content/public/test/content_browser_test_utils.h" | 13 #include "content/public/test/content_browser_test_utils.h" |
| 8 #include "content/shell/browser/shell.h" | 14 #include "content/shell/browser/shell.h" |
| 15 #include "media/base/media_content_type.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 17 |
| 10 namespace content { | 18 namespace content { |
| 11 | 19 |
| 20 namespace { |
| 21 |
| 22 class MockMediaSessionObserver : public MediaSessionObserver { |
| 23 public: |
| 24 explicit MockMediaSessionObserver( |
| 25 MediaSession* session, |
| 26 const base::Closure& closure_on_actions_change) |
| 27 : MediaSessionObserver(session), |
| 28 closure_on_actions_change_(closure_on_actions_change) {} |
| 29 |
| 30 void MediaSessionActionsChanged( |
| 31 const std::set<blink::mojom::MediaSessionAction>& actions) override { |
| 32 // The actions might be empty when the service becomes routed for the first |
| 33 // time. |
| 34 if (actions.size() == 1) |
| 35 closure_on_actions_change_.Run(); |
| 36 } |
| 37 |
| 38 private: |
| 39 base::Closure closure_on_actions_change_; |
| 40 }; |
| 41 |
| 42 class MockWebContentsObserver : public WebContentsObserver { |
| 43 public: |
| 44 explicit MockWebContentsObserver(WebContents* contents, |
| 45 const base::Closure& closure_on_navigate) |
| 46 : WebContentsObserver(contents), |
| 47 closure_on_navigate_(closure_on_navigate) {} |
| 48 |
| 49 void DidFinishNavigation(NavigationHandle* navigation_handle) override { |
| 50 closure_on_navigate_.Run(); |
| 51 } |
| 52 |
| 53 private: |
| 54 base::Closure closure_on_navigate_; |
| 55 }; |
| 56 |
| 57 class MockMediaSessionPlayerObserver : public MediaSessionPlayerObserver { |
| 58 public: |
| 59 explicit MockMediaSessionPlayerObserver(RenderFrameHost* rfh) |
| 60 : render_frame_host_(rfh) {} |
| 61 |
| 62 ~MockMediaSessionPlayerObserver() override = default; |
| 63 |
| 64 void OnSuspend(int player_id) override {} |
| 65 void OnResume(int player_id) override {} |
| 66 void OnSetVolumeMultiplier(int player_id, double volume_multiplier) override { |
| 67 } |
| 68 |
| 69 RenderFrameHost* GetRenderFrameHost() const override { |
| 70 return render_frame_host_; |
| 71 } |
| 72 |
| 73 private: |
| 74 RenderFrameHost* render_frame_host_; |
| 75 }; |
| 76 |
| 77 void NavigateToURLAndWaitForFinish(Shell* window, const GURL& url) { |
| 78 base::RunLoop run_loop; |
| 79 MockWebContentsObserver observer(window->web_contents(), |
| 80 run_loop.QuitClosure()); |
| 81 |
| 82 NavigateToURL(window, url); |
| 83 run_loop.Run(); |
| 84 } |
| 85 |
| 86 char kSetUpMediaSessionScript[] = |
| 87 "navigator.mediaSession.playbackState = \"playing\";\n" |
| 88 "navigator.mediaSession.metadata = new MediaMetadata({ title: \"foo\" });\n" |
| 89 "navigator.mediaSession.setActionHandler(\"play\", _ => {});"; |
| 90 |
| 91 const int kPlayerId = 0; |
| 92 |
| 93 } // anonymous namespace |
| 94 |
| 12 class MediaSessionServiceImplBrowserTest : public ContentBrowserTest { | 95 class MediaSessionServiceImplBrowserTest : public ContentBrowserTest { |
| 13 protected: | 96 protected: |
| 14 void SetUpCommandLine(base::CommandLine* command_line) override { | 97 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 15 ContentBrowserTest::SetUpCommandLine(command_line); | 98 ContentBrowserTest::SetUpCommandLine(command_line); |
| 16 command_line->AppendSwitchASCII("--enable-blink-features", "MediaSession"); | 99 command_line->AppendSwitchASCII("--enable-blink-features", "MediaSession"); |
| 17 } | 100 } |
| 101 |
| 102 void EnsurePlayer() { |
| 103 if (player_) |
| 104 return; |
| 105 |
| 106 player_.reset(new MockMediaSessionPlayerObserver( |
| 107 shell()->web_contents()->GetMainFrame())); |
| 108 |
| 109 MediaSessionImpl::Get(shell()->web_contents()) |
| 110 ->AddPlayer(player_.get(), kPlayerId, |
| 111 media::MediaContentType::Persistent); |
| 112 } |
| 113 |
| 114 MediaSessionImpl* GetSession() { |
| 115 return MediaSessionImpl::Get(shell()->web_contents()); |
| 116 } |
| 117 |
| 118 MediaSessionServiceImpl* GetService() { |
| 119 RenderFrameHost* main_frame = shell()->web_contents()->GetMainFrame(); |
| 120 if (GetSession()->services_.count(main_frame)) |
| 121 return GetSession()->services_[main_frame]; |
| 122 |
| 123 return nullptr; |
| 124 } |
| 125 |
| 126 bool ExecuteScriptToSetUpMediaSessionSync() { |
| 127 // Using the actions change as the signal of completion. |
| 128 base::RunLoop run_loop; |
| 129 MockMediaSessionObserver observer(GetSession(), run_loop.QuitClosure()); |
| 130 bool result = ExecuteScript(shell(), kSetUpMediaSessionScript); |
| 131 run_loop.Run(); |
| 132 return result; |
| 133 } |
| 134 |
| 135 private: |
| 136 std::unique_ptr<MockMediaSessionPlayerObserver> player_; |
| 18 }; | 137 }; |
| 19 | 138 |
| 20 // Two windows from the same BrowserContext. | 139 // Two windows from the same BrowserContext. |
| 21 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest, | 140 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest, |
| 22 CrashMessageOnUnload) { | 141 CrashMessageOnUnload) { |
| 23 NavigateToURL(shell(), GetTestUrl("media/session", "embedder.html")); | 142 NavigateToURL(shell(), GetTestUrl("media/session", "embedder.html")); |
| 24 // Navigate to a chrome:// URL to avoid render process re-use. | 143 // Navigate to a chrome:// URL to avoid render process re-use. |
| 25 NavigateToURL(shell(), GURL("chrome://flags")); | 144 NavigateToURL(shell(), GURL("chrome://flags")); |
| 26 // Should not crash. | 145 // Should not crash. |
| 27 } | 146 } |
| 28 | 147 |
| 148 // Tests for checking if the media session service members are correctly reset |
| 149 // when navigating. Due to the mojo services have different message queues, it's |
| 150 // hard to wait for the messages to arrive. Temporarily, the tests are using |
| 151 // observers to wait for the message to be processed on the MediaSessionObserver |
| 152 // side. |
| 153 |
| 154 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest, |
| 155 ResetServiceWhenNavigatingAway) { |
| 156 NavigateToURL(shell(), GetTestUrl(".", "title1.html")); |
| 157 EnsurePlayer(); |
| 158 |
| 159 EXPECT_TRUE(ExecuteScriptToSetUpMediaSessionSync()); |
| 160 |
| 161 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::PLAYING, |
| 162 GetService()->playback_state()); |
| 163 EXPECT_TRUE(GetService()->metadata().has_value()); |
| 164 EXPECT_EQ(1u, GetService()->actions().size()); |
| 165 |
| 166 // Start a non-same-page navigation and check the playback state, metadata, |
| 167 // actions are reset. |
| 168 NavigateToURLAndWaitForFinish(shell(), GetTestUrl(".", "title2.html")); |
| 169 |
| 170 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::NONE, |
| 171 GetService()->playback_state()); |
| 172 EXPECT_FALSE(GetService()->metadata().has_value()); |
| 173 EXPECT_EQ(0u, GetService()->actions().size()); |
| 174 } |
| 175 |
| 176 IN_PROC_BROWSER_TEST_F(MediaSessionServiceImplBrowserTest, |
| 177 DontResetServiceForSamePageNavigation) { |
| 178 NavigateToURL(shell(), GetTestUrl(".", "title1.html")); |
| 179 EnsurePlayer(); |
| 180 |
| 181 EXPECT_TRUE(ExecuteScriptToSetUpMediaSessionSync()); |
| 182 |
| 183 // Start a same-page navigation and check the playback state, metadata, |
| 184 // actions are not reset. |
| 185 GURL same_page_url = GetTestUrl(".", "title1.html"); |
| 186 same_page_url = GURL(same_page_url.spec() + "#some-anchor"); |
| 187 NavigateToURLAndWaitForFinish(shell(), same_page_url); |
| 188 |
| 189 EXPECT_EQ(blink::mojom::MediaSessionPlaybackState::PLAYING, |
| 190 GetService()->playback_state()); |
| 191 EXPECT_TRUE(GetService()->metadata().has_value()); |
| 192 EXPECT_EQ(1u, GetService()->actions().size()); |
| 193 } |
| 194 |
| 29 } // namespace content | 195 } // namespace content |
| OLD | NEW |