Chromium Code Reviews| 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 "base/command_line.h" | |
| 6 #include "base/strings/utf_string_conversions.h" | |
| 7 #include "content/browser/media/android/browser_media_session_manager.h" | |
| 8 #include "content/browser/media/android/media_web_contents_observer_android.h" | |
| 9 #include "content/public/browser/web_contents.h" | |
| 10 #include "content/public/common/content_switches.h" | |
| 11 #include "content/public/test/browser_test_utils.h" | |
| 12 #include "content/public/test/content_browser_test.h" | |
| 13 #include "content/public/test/test_utils.h" | |
| 14 #include "content/shell/browser/shell.h" | |
| 15 #include "testing/gmock/include/gmock/gmock.h" | |
| 16 | |
| 17 using ::testing::_; | |
| 18 using ::testing::InSequence; | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Beware that the artwork is being sanitize-checked, thus might be removed if | |
| 25 // it's invalid. | |
| 26 const char SET_METADATA_SCRIPT1[] = | |
| 27 "var audio = document.createElement(\'audio\');" | |
| 28 "audio.session = new MediaSession();" | |
| 29 "audio.session.metadata = new MediaMetadata({" | |
| 30 " title: 'title1', artist: 'artist1', album: 'album1'," | |
| 31 " artwork: [{" | |
| 32 " src: 'http://foo.com/bar.png', type: 'image/png', sizes: '128x128'" | |
| 33 " }]" | |
| 34 "});"; | |
| 35 | |
| 36 const char SET_METADATA_SCRIPT2[] = | |
| 37 "var audio = document.createElement(\'audio\');" | |
| 38 "audio.session = new MediaSession();" | |
| 39 "audio.session.metadata = new MediaMetadata({" | |
| 40 " title: 'title2', artist: 'artist2', album: 'album2'," | |
| 41 " artwork: [{" | |
| 42 " src: 'http://foo.com/bar.jpg', type: 'image/jpeg', sizes: 'any'" | |
|
dcheng
2016/07/05 02:36:55
Can we add an explicit test here for trying to loa
Zhiqiang Zhang (Slow)
2016/07/05 15:30:30
Done.
| |
| 43 " }]" | |
| 44 "});"; | |
| 45 | |
| 46 } // anonymous namespace | |
| 47 | |
| 48 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { | |
| 49 public: | |
| 50 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host) | |
| 51 : BrowserMediaSessionManager(render_frame_host) {} | |
| 52 | |
| 53 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); | |
| 54 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); | |
| 55 MOCK_METHOD2(OnSetMetadata, void(int session_id, | |
| 56 const MediaMetadata& metadata)); | |
| 57 | |
| 58 private: | |
| 59 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); | |
| 60 }; | |
| 61 | |
| 62 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { | |
| 63 public: | |
| 64 BrowserMediaSessionManagerBrowserTest() = default; | |
| 65 ~BrowserMediaSessionManagerBrowserTest() override = default; | |
| 66 | |
| 67 protected: | |
| 68 void SetUpOnMainThread() override { | |
| 69 ContentBrowserTest::SetUpOnMainThread(); | |
| 70 web_contents_ = shell()->web_contents(); | |
| 71 browser_media_session_manager_ = | |
| 72 new MockBrowserMediaSessionManager(web_contents_->GetMainFrame()); | |
| 73 MediaWebContentsObserverAndroid::FromWebContents(web_contents_) | |
| 74 ->SetMediaSessionManagerForTest( | |
| 75 web_contents_->GetMainFrame(), | |
| 76 browser_media_session_manager_); | |
| 77 shell()->LoadURL(GURL("about:blank")); | |
| 78 | |
| 79 ON_CALL(*browser_media_session_manager_, OnSetMetadata(_, _)) | |
| 80 .WillByDefault(InvokeWithoutArgs[&](message_loop_runner_->Quit())); | |
| 81 } | |
| 82 | |
| 83 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 84 command_line->AppendSwitchASCII( | |
| 85 switches::kEnableBlinkFeatures, "MediaSession"); | |
| 86 } | |
| 87 | |
| 88 WebContents* web_contents_; | |
| 89 MockBrowserMediaSessionManager* browser_media_session_manager_; | |
| 90 scoped_refptr<MessageLoopRunner> message_loop_runner_; | |
| 91 }; | |
| 92 | |
| 93 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | |
| 94 TestMetadataPropagated) { | |
| 95 MediaMetadata expected; | |
| 96 expected.title = base::ASCIIToUTF16("title1"); | |
| 97 expected.artist = base::ASCIIToUTF16("artist1"); | |
| 98 expected.album = base::ASCIIToUTF16("album1"); | |
| 99 MediaMetadata::Artwork artwork; | |
| 100 artwork.src = GURL("http://foo.com/bar.png"); | |
| 101 artwork.type = base::NullableString16(base::ASCIIToUTF16("image/png"), false); | |
| 102 artwork.sizes.push_back(gfx::Size(128, 128)); | |
| 103 expected.artwork.push_back(artwork); | |
| 104 | |
| 105 message_loop_runner_ = new MessageLoopRunner(); | |
| 106 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | |
| 107 .Times(1); | |
| 108 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | |
| 109 SET_METADATA_SCRIPT1)); | |
| 110 message_loop_runner_->Run(); | |
| 111 } | |
| 112 | |
| 113 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | |
| 114 TestSetMetadataTwice) { | |
| 115 // Make expectations ordered. | |
| 116 InSequence s; | |
| 117 | |
| 118 MediaMetadata expected; | |
| 119 expected.title = base::ASCIIToUTF16("title2"); | |
| 120 expected.artist = base::ASCIIToUTF16("artist2"); | |
| 121 expected.album = base::ASCIIToUTF16("album2"); | |
| 122 MediaMetadata::Artwork artwork; | |
| 123 artwork.src = GURL("http://foo.com/bar.jpg"); | |
| 124 artwork.type = base::NullableString16( | |
| 125 base::ASCIIToUTF16("image/jpeg"), false); | |
| 126 artwork.sizes.push_back(gfx::Size(0, 0)); | |
| 127 expected.artwork.push_back(artwork); | |
| 128 | |
| 129 // Set metadata for the first time. | |
| 130 message_loop_runner_ = new MessageLoopRunner(); | |
| 131 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, _)) | |
| 132 .Times(1); | |
| 133 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | |
| 134 SET_METADATA_SCRIPT1)); | |
| 135 message_loop_runner_->Run(); | |
| 136 | |
| 137 // Set metadata for the second time. | |
| 138 message_loop_runner_ = new MessageLoopRunner(); | |
| 139 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | |
| 140 .Times(1); | |
| 141 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | |
| 142 SET_METADATA_SCRIPT2)); | |
| 143 message_loop_runner_->Run(); | |
| 144 } | |
| 145 | |
| 146 } // namespace content | |
| OLD | NEW |