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