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/browser/web_contents/web_contents_impl.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/common/media_metadata.h" |
18 #include "content/public/test/browser_test_utils.h" | 18 #include "content/public/test/browser_test_utils.h" |
19 #include "content/public/test/content_browser_test.h" | 19 #include "content/public/test/content_browser_test.h" |
20 #include "content/public/test/test_utils.h" | 20 #include "content/public/test/test_utils.h" |
21 #include "content/shell/browser/shell.h" | 21 #include "content/shell/browser/shell.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 | 23 |
24 using ::testing::_; | 24 using ::testing::_; |
25 using ::testing::InSequence; | 25 using ::testing::InSequence; |
26 using ::testing::InvokeWithoutArgs; | 26 using ::testing::InvokeWithoutArgs; |
27 using ::testing::Ne; | 27 using ::testing::Ne; |
28 | 28 |
29 namespace content { | 29 namespace content { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 // Helper function for build test javascripts. | 33 // Helper function for build test javascripts. |
34 std::string BuildSetMetadataScript( | 34 std::string BuildSetMetadataScript( |
35 const base::Optional<MediaMetadata>& metadata) { | 35 const base::Optional<MediaMetadata>& metadata) { |
36 std::ostringstream generated_script; | 36 std::ostringstream generated_script; |
37 | 37 |
38 generated_script | |
39 << "var audio = document.createElement(\'audio\');" | |
40 << "audio.session = new MediaSession();"; | |
41 | |
42 if (!metadata.has_value()) { | 38 if (!metadata.has_value()) { |
43 generated_script << "audio.session.metadata = null;"; | 39 generated_script << "window.navigator.mediaSession.metadata = null;"; |
44 return generated_script.str(); | 40 return generated_script.str(); |
45 } | 41 } |
46 | 42 |
47 generated_script | 43 generated_script |
48 << "audio.session.metadata = new MediaMetadata({" | 44 << "window.navigator.mediaSession.metadata = new MediaMetadata({" |
49 << "title: \"" << metadata->title << "\", " | 45 << "title: \"" << metadata->title << "\", " |
50 << "artist: \"" << metadata->artist << "\", " | 46 << "artist: \"" << metadata->artist << "\", " |
51 << "album: \"" << metadata->album << "\", " | 47 << "album: \"" << metadata->album << "\", " |
52 << "artwork: ["; | 48 << "artwork: ["; |
53 | 49 |
54 std::string artwork_separator = ""; | 50 std::string artwork_separator = ""; |
55 for (const auto& artwork : metadata->artwork) { | 51 for (const auto& artwork : metadata->artwork) { |
56 generated_script << artwork_separator << "{" | 52 generated_script << artwork_separator << "{" |
57 << "src: \"" << artwork.src.spec() << "\", " | 53 << "src: \"" << artwork.src.spec() << "\", " |
58 << "type: \"" << artwork.type.string() << "\", " | 54 << "type: \"" << artwork.type.string() << "\", " |
(...skipping 29 matching lines...) Expand all Loading... |
88 for (const auto& size : artwork.sizes) { | 84 for (const auto& size : artwork.sizes) { |
89 *os << size.width() << "x" << size.height() << " "; | 85 *os << size.width() << "x" << size.height() << " "; |
90 } | 86 } |
91 *os << "]}"; | 87 *os << "]}"; |
92 } | 88 } |
93 *os << "]}"; | 89 *os << "]}"; |
94 } | 90 } |
95 | 91 |
96 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { | 92 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { |
97 public: | 93 public: |
98 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host) | 94 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host, |
99 : BrowserMediaSessionManager(render_frame_host) {} | 95 WebContentsImpl* contents) |
| 96 : BrowserMediaSessionManager(render_frame_host, contents) {} |
100 | 97 |
101 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); | 98 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); |
102 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); | 99 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); |
103 MOCK_METHOD2(OnSetMetadata, void( | 100 MOCK_METHOD2(OnSetMetadata, void( |
104 int session_id, const base::Optional<MediaMetadata>& metadata)); | 101 int session_id, const base::Optional<MediaMetadata>& metadata)); |
105 | 102 |
106 private: | 103 private: |
107 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); | 104 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); |
108 }; | 105 }; |
109 | 106 |
110 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { | 107 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { |
111 public: | 108 public: |
112 BrowserMediaSessionManagerBrowserTest() = default; | 109 BrowserMediaSessionManagerBrowserTest() = default; |
113 ~BrowserMediaSessionManagerBrowserTest() override = default; | 110 ~BrowserMediaSessionManagerBrowserTest() override = default; |
114 | 111 |
115 protected: | 112 protected: |
116 void SetUpOnMainThread() override { | 113 void SetUpOnMainThread() override { |
117 ContentBrowserTest::SetUpOnMainThread(); | 114 ContentBrowserTest::SetUpOnMainThread(); |
118 web_contents_ = shell()->web_contents(); | 115 web_contents_ = static_cast<WebContentsImpl*>(shell()->web_contents()); |
119 | 116 |
120 std::unique_ptr<MockBrowserMediaSessionManager> manager( | 117 std::unique_ptr<MockBrowserMediaSessionManager> manager( |
121 new MockBrowserMediaSessionManager(web_contents_->GetMainFrame())); | 118 new MockBrowserMediaSessionManager( |
| 119 web_contents_->GetMainFrame(), web_contents_)); |
122 browser_media_session_manager_ = manager.get(); | 120 browser_media_session_manager_ = manager.get(); |
123 MediaWebContentsObserverAndroid::FromWebContents(web_contents_) | 121 MediaWebContentsObserverAndroid::FromWebContents(web_contents_) |
124 ->SetMediaSessionManagerForTest( | 122 ->SetMediaSessionManagerForTest( |
125 web_contents_->GetMainFrame(), std::move(manager)); | 123 web_contents_->GetMainFrame(), std::move(manager)); |
126 | 124 |
127 shell()->LoadURL(GURL("about:blank")); | 125 shell()->LoadURL(GURL("about:blank")); |
128 | 126 |
129 ON_CALL(*browser_media_session_manager_, OnSetMetadata(_, _)) | 127 ON_CALL(*browser_media_session_manager_, OnSetMetadata(_, _)) |
130 .WillByDefault(InvokeWithoutArgs([&]{ | 128 .WillByDefault(InvokeWithoutArgs([&]{ |
131 message_loop_runner_->Quit(); | 129 message_loop_runner_->Quit(); |
132 })); | 130 })); |
133 } | 131 } |
134 | 132 |
135 void SetUpCommandLine(base::CommandLine* command_line) override { | 133 void SetUpCommandLine(base::CommandLine* command_line) override { |
136 command_line->AppendSwitchASCII( | 134 command_line->AppendSwitchASCII( |
137 switches::kEnableBlinkFeatures, "MediaSession"); | 135 switches::kEnableBlinkFeatures, "MediaSession"); |
138 } | 136 } |
139 | 137 |
140 WebContents* web_contents_; | 138 WebContentsImpl* web_contents_; |
141 MockBrowserMediaSessionManager* browser_media_session_manager_; | 139 MockBrowserMediaSessionManager* browser_media_session_manager_; |
142 scoped_refptr<MessageLoopRunner> message_loop_runner_; | 140 scoped_refptr<MessageLoopRunner> message_loop_runner_; |
143 }; | 141 }; |
144 | 142 |
145 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, | 143 IN_PROC_BROWSER_TEST_F(BrowserMediaSessionManagerBrowserTest, |
146 TestMetadataPropagated) { | 144 TestMetadataPropagated) { |
147 base::Optional<MediaMetadata> expected = MediaMetadata(); | 145 base::Optional<MediaMetadata> expected = MediaMetadata(); |
148 expected->title = base::ASCIIToUTF16("title1"); | 146 expected->title = base::ASCIIToUTF16("title1"); |
149 expected->artist = base::ASCIIToUTF16("artist1"); | 147 expected->artist = base::ASCIIToUTF16("artist1"); |
150 expected->album = base::ASCIIToUTF16("album1"); | 148 expected->album = base::ASCIIToUTF16("album1"); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 // Set metadata for the first time. | 241 // Set metadata for the first time. |
244 message_loop_runner_ = new MessageLoopRunner(); | 242 message_loop_runner_ = new MessageLoopRunner(); |
245 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) | 243 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, expected)) |
246 .Times(1); | 244 .Times(1); |
247 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | 245 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), |
248 BuildSetMetadataScript(dirty_metadata))); | 246 BuildSetMetadataScript(dirty_metadata))); |
249 message_loop_runner_->Run(); | 247 message_loop_runner_->Run(); |
250 } | 248 } |
251 | 249 |
252 } // namespace content | 250 } // namespace content |
OLD | NEW |