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