| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/android/browser_media_session_manager.h" | 5 #include "content/browser/media/android/browser_media_session_manager.h" |
| 6 | 6 |
| 7 #include <iostream> | 7 #include <iostream> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "content/browser/media/android/media_web_contents_observer_android.h" | 14 #include "content/browser/media/android/media_web_contents_observer_android.h" |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 17 #include "content/public/common/media_metadata.h" |
| 17 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
| 18 #include "content/public/test/content_browser_test.h" | 19 #include "content/public/test/content_browser_test.h" |
| 19 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
| 20 #include "content/shell/browser/shell.h" | 21 #include "content/shell/browser/shell.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 | 23 |
| 23 using ::testing::_; | 24 using ::testing::_; |
| 24 using ::testing::InSequence; | 25 using ::testing::InSequence; |
| 25 using ::testing::InvokeWithoutArgs; | 26 using ::testing::InvokeWithoutArgs; |
| 26 using ::testing::Ne; | 27 using ::testing::Ne; |
| 27 | 28 |
| 28 namespace content { | 29 namespace content { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Helper function for build test javascripts. | 33 // Helper function for build test javascripts. |
| 33 std::string BuildSetMetadataScript(const MediaMetadata& metadata) { | 34 std::string BuildSetMetadataScript( |
| 35 const base::Optional<MediaMetadata>& metadata) { |
| 34 std::ostringstream generated_script; | 36 std::ostringstream generated_script; |
| 35 | 37 |
| 36 generated_script << "var audio = document.createElement(\'audio\');" | 38 generated_script |
| 37 << "audio.session = new MediaSession();" | 39 << "var audio = document.createElement(\'audio\');" |
| 38 << "audio.session.metadata = new MediaMetadata({" | 40 << "audio.session = new MediaSession();"; |
| 39 << "title: \"" << metadata.title << "\", " | 41 |
| 40 << "artist: \"" << metadata.artist << "\", " | 42 if (!metadata.has_value()) { |
| 41 << "album: \"" << metadata.album << "\", " | 43 generated_script << "audio.session.metadata = null;"; |
| 42 << "artwork: ["; | 44 return generated_script.str(); |
| 45 } |
| 46 |
| 47 generated_script |
| 48 << "audio.session.metadata = new MediaMetadata({" |
| 49 << "title: \"" << metadata->title << "\", " |
| 50 << "artist: \"" << metadata->artist << "\", " |
| 51 << "album: \"" << metadata->album << "\", " |
| 52 << "artwork: ["; |
| 43 | 53 |
| 44 std::string artwork_separator = ""; | 54 std::string artwork_separator = ""; |
| 45 for (const auto& artwork : metadata.artwork) { | 55 for (const auto& artwork : metadata->artwork) { |
| 46 generated_script << artwork_separator << "{" | 56 generated_script << artwork_separator << "{" |
| 47 << "src: \"" << artwork.src.spec() << "\", " | 57 << "src: \"" << artwork.src.spec() << "\", " |
| 48 << "type: \"" << artwork.type.string() << "\", " | 58 << "type: \"" << artwork.type.string() << "\", " |
| 49 << "sizes: \""; | 59 << "sizes: \""; |
| 50 for (const auto& size : artwork.sizes) { | 60 for (const auto& size : artwork.sizes) { |
| 51 generated_script << size.width() << "x" << size.height() << " "; | 61 generated_script << size.width() << "x" << size.height() << " "; |
| 52 } | 62 } |
| 53 generated_script << "\"}"; | 63 generated_script << "\"}"; |
| 54 artwork_separator = ", "; | 64 artwork_separator = ", "; |
| 55 } | 65 } |
| 56 generated_script << "]});"; | 66 generated_script << "]});"; |
| 57 | 67 |
| 58 return generated_script.str(); | 68 return generated_script.str(); |
| 59 } | 69 } |
| 60 | 70 |
| 61 } // anonymous namespace | 71 } // anonymous namespace |
| 62 | 72 |
| 63 // Helper function to be pretty-print error messages by GMock. | 73 // Helper function to be pretty-print error messages by GMock. |
| 64 void PrintTo(const MediaMetadata& metadata, std::ostream* os) { | 74 void PrintTo(const base::Optional<MediaMetadata>& metadata, std::ostream* os) { |
| 65 *os << "{ title=" << metadata.title << ", "; | 75 if (!metadata.has_value()) { |
| 66 *os << "artist=" << metadata.artist << ", "; | 76 *os << "<null MediaMetadata>"; |
| 67 *os << "album=" << metadata.album << ", "; | 77 return; |
| 78 } |
| 79 |
| 80 *os << "{ title=" << metadata->title << ", "; |
| 81 *os << "artist=" << metadata->artist << ", "; |
| 82 *os << "album=" << metadata->album << ", "; |
| 68 *os << "artwork=["; | 83 *os << "artwork=["; |
| 69 for (const auto& artwork : metadata.artwork) { | 84 for (const auto& artwork : metadata->artwork) { |
| 70 *os << "{ src=" << artwork.src.spec() << ", "; | 85 *os << "{ src=" << artwork.src.spec() << ", "; |
| 71 *os << "type=" << artwork.type.string() << ", "; | 86 *os << "type=" << artwork.type.string() << ", "; |
| 72 *os << "sizes=["; | 87 *os << "sizes=["; |
| 73 for (const auto& size : artwork.sizes) { | 88 for (const auto& size : artwork.sizes) { |
| 74 *os << size.width() << "x" << size.height() << " "; | 89 *os << size.width() << "x" << size.height() << " "; |
| 75 } | 90 } |
| 76 *os << "]}"; | 91 *os << "]}"; |
| 77 } | 92 } |
| 78 *os << "]}"; | 93 *os << "]}"; |
| 79 } | 94 } |
| 80 | 95 |
| 81 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { | 96 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { |
| 82 public: | 97 public: |
| 83 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host) | 98 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host) |
| 84 : BrowserMediaSessionManager(render_frame_host) {} | 99 : BrowserMediaSessionManager(render_frame_host) {} |
| 85 | 100 |
| 86 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); | 101 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); |
| 87 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); | 102 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); |
| 88 MOCK_METHOD2(OnSetMetadata, void(int session_id, | 103 MOCK_METHOD2(OnSetMetadata, void( |
| 89 const MediaMetadata& metadata)); | 104 int session_id, const base::Optional<MediaMetadata>& metadata)); |
| 90 | 105 |
| 91 private: | 106 private: |
| 92 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); | 107 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); |
| 93 }; | 108 }; |
| 94 | 109 |
| 95 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { | 110 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { |
| 96 public: | 111 public: |
| 97 BrowserMediaSessionManagerBrowserTest() = default; | 112 BrowserMediaSessionManagerBrowserTest() = default; |
| 98 ~BrowserMediaSessionManagerBrowserTest() override = default; | 113 ~BrowserMediaSessionManagerBrowserTest() override = default; |
| 99 | 114 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 122 switches::kEnableBlinkFeatures, "MediaSession"); | 137 switches::kEnableBlinkFeatures, "MediaSession"); |
| 123 } | 138 } |
| 124 | 139 |
| 125 WebContents* web_contents_; | 140 WebContents* web_contents_; |
| 126 MockBrowserMediaSessionManager* browser_media_session_manager_; | 141 MockBrowserMediaSessionManager* browser_media_session_manager_; |
| 127 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 142 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
| 128 }; | 143 }; |
| 129 | 144 |
| 130 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | 145 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, |
| 131 TestMetadataPropagated) { | 146 TestMetadataPropagated) { |
| 132 MediaMetadata expected; | 147 base::Optional<MediaMetadata> expected = MediaMetadata(); |
| 133 expected.title = base::ASCIIToUTF16("title1"); | 148 expected->title = base::ASCIIToUTF16("title1"); |
| 134 expected.artist = base::ASCIIToUTF16("artist1"); | 149 expected->artist = base::ASCIIToUTF16("artist1"); |
| 135 expected.album = base::ASCIIToUTF16("album1"); | 150 expected->album = base::ASCIIToUTF16("album1"); |
| 136 MediaMetadata::Artwork artwork; | 151 MediaMetadata::Artwork artwork; |
| 137 artwork.src = GURL("http://foo.com/bar.png"); | 152 artwork.src = GURL("http://foo.com/bar.png"); |
| 138 artwork.type = base::NullableString16(base::ASCIIToUTF16("image/png"), false); | 153 artwork.type = base::NullableString16(base::ASCIIToUTF16("image/png"), false); |
| 139 artwork.sizes.push_back(gfx::Size(128, 128)); | 154 artwork.sizes.push_back(gfx::Size(128, 128)); |
| 140 expected.artwork.push_back(artwork); | 155 expected->artwork.push_back(artwork); |
| 141 | 156 |
| 142 message_loop_runner_ = new MessageLoopRunner(); | 157 message_loop_runner_ = new MessageLoopRunner(); |
| 143 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | 158 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) |
| 144 .Times(1); | 159 .Times(1); |
| 145 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | 160 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 146 BuildSetMetadataScript(expected))); | 161 BuildSetMetadataScript(expected))); |
| 147 message_loop_runner_->Run(); | 162 message_loop_runner_->Run(); |
| 148 } | 163 } |
| 149 | 164 |
| 150 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | 165 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, |
| 151 TestSetMetadataTwice) { | 166 TestSetMetadataTwice) { |
| 152 // Make expectations ordered. | 167 // Make expectations ordered. |
| 153 InSequence s; | 168 InSequence s; |
| 154 | 169 |
| 155 MediaMetadata dont_care_metadata; | 170 base::Optional<MediaMetadata> dont_care_metadata = MediaMetadata(); |
| 156 | 171 |
| 157 MediaMetadata expected; | 172 base::Optional<MediaMetadata> expected = MediaMetadata(); |
| 158 expected.title = base::ASCIIToUTF16("title2"); | 173 expected->title = base::ASCIIToUTF16("title2"); |
| 159 expected.artist = base::ASCIIToUTF16("artist2"); | 174 expected->artist = base::ASCIIToUTF16("artist2"); |
| 160 expected.album = base::ASCIIToUTF16("album2"); | 175 expected->album = base::ASCIIToUTF16("album2"); |
| 161 MediaMetadata::Artwork artwork; | 176 MediaMetadata::Artwork artwork; |
| 162 artwork.src = GURL("http://foo.com/bar.jpg"); | 177 artwork.src = GURL("http://foo.com/bar.jpg"); |
| 163 artwork.type = base::NullableString16( | 178 artwork.type = base::NullableString16( |
| 164 base::ASCIIToUTF16("image/jpeg"), false); | 179 base::ASCIIToUTF16("image/jpeg"), false); |
| 165 artwork.sizes.push_back(gfx::Size(256, 256)); | 180 artwork.sizes.push_back(gfx::Size(256, 256)); |
| 166 expected.artwork.push_back(artwork); | 181 expected->artwork.push_back(artwork); |
| 167 | 182 |
| 168 // Set metadata for the first time. | 183 // Set metadata for the first time. |
| 169 message_loop_runner_ = new MessageLoopRunner(); | 184 message_loop_runner_ = new MessageLoopRunner(); |
| 185 EXPECT_CALL(*browser_media_session_manager_, |
| 186 OnSetMetadata(_, dont_care_metadata)) |
| 187 .Times(1); |
| 188 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 189 BuildSetMetadataScript(dont_care_metadata))); |
| 190 message_loop_runner_->Run(); |
| 191 |
| 192 // Set metadata for the second time. |
| 193 message_loop_runner_ = new MessageLoopRunner(); |
| 194 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) |
| 195 .Times(1); |
| 196 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 197 BuildSetMetadataScript(expected))); |
| 198 message_loop_runner_->Run(); |
| 199 } |
| 200 |
| 201 |
| 202 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, |
| 203 TestNullMetadata) { |
| 204 // Make expectations ordered. |
| 205 InSequence s; |
| 206 |
| 207 base::Optional<MediaMetadata> dont_care_metadata = MediaMetadata(); |
| 208 |
| 209 base::Optional<MediaMetadata> expected; |
| 210 |
| 211 // Set metadata for the first time. |
| 212 message_loop_runner_ = new MessageLoopRunner(); |
| 170 EXPECT_CALL(*browser_media_session_manager_, | 213 EXPECT_CALL(*browser_media_session_manager_, |
| 171 OnSetMetadata(_, dont_care_metadata)) | 214 OnSetMetadata(_, dont_care_metadata)) |
| 172 .Times(1); | 215 .Times(1); |
| 173 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | 216 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 174 BuildSetMetadataScript(dont_care_metadata))); | 217 BuildSetMetadataScript(dont_care_metadata))); |
| 175 message_loop_runner_->Run(); | 218 message_loop_runner_->Run(); |
| 176 | 219 |
| 177 // Set metadata for the second time. | 220 // Set metadata for the second time. |
| 178 message_loop_runner_ = new MessageLoopRunner(); | 221 message_loop_runner_ = new MessageLoopRunner(); |
| 179 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | 222 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) |
| 180 .Times(1); | 223 .Times(1); |
| 181 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | 224 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 182 BuildSetMetadataScript(expected))); | 225 BuildSetMetadataScript(expected))); |
| 183 message_loop_runner_->Run(); | 226 message_loop_runner_->Run(); |
| 184 } | 227 } |
| 185 | 228 |
| 186 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | 229 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, |
| 187 TestFileArtworkRemoved) { | 230 TestFileArtworkRemoved) { |
| 188 // Make expectations ordered. | 231 // Make expectations ordered. |
| 189 InSequence s; | 232 InSequence s; |
| 190 | 233 |
| 191 MediaMetadata dirty_metadata; | 234 base::Optional<MediaMetadata> dirty_metadata = MediaMetadata(); |
| 192 MediaMetadata::Artwork file_artwork; | 235 MediaMetadata::Artwork file_artwork; |
| 193 file_artwork.src = GURL("file:///foo/bar.jpg"); | 236 file_artwork.src = GURL("file:///foo/bar.jpg"); |
| 194 file_artwork.type = base::NullableString16( | 237 file_artwork.type = base::NullableString16( |
| 195 base::ASCIIToUTF16("image/jpeg"), false); | 238 base::ASCIIToUTF16("image/jpeg"), false); |
| 196 dirty_metadata.artwork.push_back(file_artwork); | 239 dirty_metadata->artwork.push_back(file_artwork); |
| 197 | 240 |
| 198 MediaMetadata expected; | 241 base::Optional<MediaMetadata> expected = MediaMetadata(); |
| 199 | 242 |
| 200 // Set metadata for the first time. | 243 // Set metadata for the first time. |
| 201 message_loop_runner_ = new MessageLoopRunner(); | 244 message_loop_runner_ = new MessageLoopRunner(); |
| 202 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | 245 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) |
| 203 .Times(1); | 246 .Times(1); |
| 204 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | 247 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
| 205 BuildSetMetadataScript(dirty_metadata))); | 248 BuildSetMetadataScript(dirty_metadata))); |
| 206 message_loop_runner_->Run(); | 249 message_loop_runner_->Run(); |
| 207 } | 250 } |
| 208 | 251 |
| 209 } // namespace content | 252 } // namespace content |
| OLD | NEW |