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 | |
10 #include "base/command_line.h" | |
11 #include "base/memory/ptr_util.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "content/browser/media/android/media_web_contents_observer_android.h" | |
14 #include "content/public/browser/web_contents.h" | |
15 #include "content/public/common/content_switches.h" | |
16 #include "content/public/test/browser_test_utils.h" | |
17 #include "content/public/test/content_browser_test.h" | |
18 #include "content/public/test/test_utils.h" | |
19 #include "content/shell/browser/shell.h" | |
20 #include "testing/gmock/include/gmock/gmock.h" | |
21 | |
22 using ::testing::_; | |
23 using ::testing::InSequence; | |
24 using ::testing::InvokeWithoutArgs; | |
25 using ::testing::Ne; | |
26 | |
27 namespace content { | |
28 | |
29 namespace { | |
30 | |
31 // Helper function for build test javascripts. | |
32 std::string BuildSetMetadataScript(const MediaMetadata& metadata) { | |
33 std::ostringstream generated_script; | |
34 | |
35 generated_script << "var audio = document.createElement(\'audio\');" | |
36 << "audio.session = new MediaSession();" | |
37 << "audio.session.metadata = new MediaMetadata({" | |
38 << "title: \"" << metadata.title << "\", " | |
39 << "artist: \"" << metadata.artist << "\", " | |
40 << "album: \"" << metadata.album << "\", " | |
41 << "artwork: ["; | |
42 | |
43 std::string artwork_separator = ""; | |
44 for (const auto& artwork : metadata.artwork) { | |
45 generated_script << artwork_separator << "{" | |
46 << "src: \"" << artwork.src.spec() << "\", " | |
47 << "type: \"" << artwork.type.string() << "\", " | |
48 << "sizes: \""; | |
49 for (const auto& size : artwork.sizes) { | |
50 generated_script << size.width() << "x" << size.height() << " "; | |
51 } | |
52 generated_script << "\"}"; | |
53 artwork_separator = ", "; | |
54 } | |
55 generated_script << "]});"; | |
56 | |
57 return generated_script.str(); | |
58 } | |
59 | |
60 } // anonymous namespace | |
61 | |
62 // Helper function to be pretty-print error messages by GMock. | |
63 void PrintTo(const MediaMetadata& metadata, std::ostream* os) { | |
64 *os << "{ title=" << metadata.title << ", "; | |
65 *os << "artist=" << metadata.artist << ", "; | |
66 *os << "album=" << metadata.album << ", "; | |
67 *os << "artwork=["; | |
68 for (const auto& artwork : metadata.artwork) { | |
69 *os << "{ src=" << artwork.src.spec() << ", "; | |
70 *os << "type=" << artwork.type.string() << ", "; | |
71 *os << "sizes=["; | |
72 for (const auto& size : artwork.sizes) { | |
73 *os << size.width() << "x" << size.height() << " "; | |
74 } | |
75 *os << "]}"; | |
76 } | |
77 *os << "]}"; | |
78 } | |
79 | |
80 class MockBrowserMediaSessionManager : public BrowserMediaSessionManager { | |
81 public: | |
82 explicit MockBrowserMediaSessionManager(RenderFrameHost* render_frame_host) | |
83 : BrowserMediaSessionManager(render_frame_host) {} | |
84 | |
85 MOCK_METHOD2(OnActiveate, void(int session_id, int request_id)); | |
86 MOCK_METHOD2(OnDeactiveate, void(int session_id, int request_id)); | |
87 MOCK_METHOD2(OnSetMetadata, void(int session_id, | |
88 const MediaMetadata& metadata)); | |
89 | |
90 private: | |
91 DISALLOW_COPY_AND_ASSIGN(MockBrowserMediaSessionManager); | |
92 }; | |
93 | |
94 class BrowserMediaSessionManagerBrowserTest : public ContentBrowserTest { | |
95 public: | |
96 BrowserMediaSessionManagerBrowserTest() = default; | |
97 ~BrowserMediaSessionManagerBrowserTest() override = default; | |
98 | |
99 protected: | |
100 void SetUpOnMainThread() override { | |
101 ContentBrowserTest::SetUpOnMainThread(); | |
102 web_contents_ = shell()->web_contents(); | |
103 browser_media_session_manager_ = | |
104 new MockBrowserMediaSessionManager(web_contents_->GetMainFrame()); | |
105 | |
106 std::unique_ptr<BrowserMediaSessionManager> manager_ptr = | |
107 base::WrapUnique(browser_media_session_manager_); | |
dcheng
2016/07/06 02:34:15
A slightly shorter way to write this:
std::unique
Zhiqiang Zhang (Slow)
2016/07/06 15:38:38
Done for all the std::unique_ptr-related comments
| |
108 | |
109 MediaWebContentsObserverAndroid::FromWebContents(web_contents_) | |
110 ->SetMediaSessionManagerForTest( | |
111 web_contents_->GetMainFrame(), manager_ptr); | |
dcheng
2016/07/06 02:34:15
std::move(manager_ptr) (and make the function para
Zhiqiang Zhang (Slow)
2016/07/06 15:38:38
Done.
| |
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 EXPECT_CALL(*browser_media_session_manager_, OnSetMetadata(_, Ne(expected))) | |
dcheng
2016/07/06 02:34:15
I'm not sure I understand these expectations. Why
Zhiqiang Zhang (Slow)
2016/07/06 15:38:38
Yeah, this expect indeed don't make sense.
Removed
| |
205 .Times(0); | |
206 ASSERT_TRUE(ExecuteScript(web_contents_->GetMainFrame(), | |
207 BuildSetMetadataScript(dirty_metadata))); | |
208 message_loop_runner_->Run(); | |
209 } | |
210 | |
211 } // namespace content | |
OLD | NEW |