1// Copyright (c) 2013 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/media_browsertest.h"
6
7#include "base/strings/string_number_conversions.h"
8#include "base/strings/utf_string_conversions.h"
9#include "content/public/browser/web_contents.h"
10#include "content/public/test/browser_test_utils.h"
11#include "content/public/test/content_browser_test_utils.h"
12#include "content/shell/browser/shell.h"
13#include "media/base/test_data_util.h"
14#include "net/test/spawned_test_server/spawned_test_server.h"
15
16// TODO(wolenetz): Fix Media.YUV* tests on MSVS 2012 x64. crbug.com/180074
17#if defined(OS_WIN) && defined(ARCH_CPU_X86_64) && _MSC_VER == 1700
18#define MAYBE(x) DISABLED_##x
19#else
20#define MAYBE(x) x
21#endif
22
23namespace content {
24
25// Common test results.
26const char MediaBrowserTest::kEnded[] = "ENDED";
27const char MediaBrowserTest::kError[] = "ERROR";
28const char MediaBrowserTest::kFailed[] = "FAILED";
29
30void MediaBrowserTest::RunMediaTestPage(const std::string& html_page,
31                                        const media::QueryParams& query_params,
32                                        const std::string& expected_title,
33                                        bool http) {
34  GURL gurl;
35  std::string query = media::GetURLQueryString(query_params);
36  scoped_ptr<net::SpawnedTestServer> http_test_server;
37  if (http) {
38    http_test_server.reset(
39        new net::SpawnedTestServer(net::SpawnedTestServer::TYPE_HTTP,
40                                   net::SpawnedTestServer::kLocalhost,
41                                   media::GetTestDataPath()));
42    CHECK(http_test_server->Start());
43    gurl = http_test_server->GetURL("files/" + html_page + "?" + query);
44  } else {
45    gurl = content::GetFileUrlWithQuery(media::GetTestDataFilePath(html_page),
46                                        query);
47  }
48  std::string final_title = RunTest(gurl, expected_title);
49  EXPECT_EQ(expected_title, final_title);
50}
51
52std::string MediaBrowserTest::RunTest(const GURL& gurl,
53                                      const std::string& expected_title) {
54  VLOG(0) << "Running test URL: " << gurl;
55  TitleWatcher title_watcher(shell()->web_contents(),
56                             base::ASCIIToUTF16(expected_title));
57  AddWaitForTitles(&title_watcher);
58  NavigateToURL(shell(), gurl);
59  base::string16 result = title_watcher.WaitAndGetTitle();
60  return base::UTF16ToASCII(result);
61}
62
63void MediaBrowserTest::AddWaitForTitles(content::TitleWatcher* title_watcher) {
64  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kEnded));
65  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kError));
66  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kFailed));
67}
68
69// Tests playback and seeking of an audio or video file over file or http based
70// on a test parameter.  Test starts with playback, then, after X seconds or the
71// ended event fires, seeks near end of file; see player.html for details.  The
72// test completes when either the last 'ended' or an 'error' event fires.
73class MediaTest : public testing::WithParamInterface<bool>,
74                  public MediaBrowserTest {
75 public:
76  // Play specified audio over http:// or file:// depending on |http| setting.
77  void PlayAudio(const std::string& media_file, bool http) {
78    PlayMedia("audio", media_file, http);
79  }
80
81  // Play specified video over http:// or file:// depending on |http| setting.
82  void PlayVideo(const std::string& media_file, bool http) {
83    PlayMedia("video", media_file, http);
84  }
85
86  // Run specified color format test with the expected result.
87  void RunColorFormatTest(const std::string& media_file,
88                          const std::string& expected) {
89    base::FilePath test_file_path =
90        media::GetTestDataFilePath("blackwhite.html");
91    RunTest(GetFileUrlWithQuery(test_file_path, media_file), expected);
92  }
93
94  void PlayMedia(const std::string& tag,
95                 const std::string& media_file,
96                 bool http) {
97    media::QueryParams query_params;
98    query_params.push_back(std::make_pair(tag, media_file));
99    RunMediaTestPage("player.html", query_params, kEnded, http);
100  }
101
102  void RunVideoSizeTest(const char* media_file, int width, int height) {
103    std::string expected;
104    expected += base::IntToString(width);
105    expected += " ";
106    expected += base::IntToString(height);
107    media::QueryParams query_params;
108    query_params.push_back(std::make_pair("video", media_file));
109    RunMediaTestPage("player.html", query_params, expected, false);
110  }
111};
112
113IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearTheora) {
114  PlayVideo("bear.ogv", GetParam());
115}
116
117IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearSilentTheora) {
118  PlayVideo("bear_silent.ogv", GetParam());
119}
120
121IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWebm) {
122  PlayVideo("bear.webm", GetParam());
123}
124
125IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearOpusWebm) {
126  PlayVideo("bear-opus.webm", GetParam());
127}
128
129IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearOpusOgg) {
130  PlayVideo("bear-opus.ogg", GetParam());
131}
132
133IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearSilentWebm) {
134  PlayVideo("bear_silent.webm", GetParam());
135}
136
137#if defined(USE_PROPRIETARY_CODECS)
138IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMp4) {
139  PlayVideo("bear.mp4", GetParam());
140}
141
142IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearSilentMp4) {
143  PlayVideo("bear_silent.mp4", GetParam());
144}
145
146// While we support the big endian (be) PCM codecs on Chromium, Quicktime seems
147// to be the only creator of this format and only for .mov files.
148// TODO(dalecurtis/ihf): Find or create some .wav test cases for "be" format.
149IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMovPcmS16be) {
150  PlayVideo("bear_pcm_s16be.mov", GetParam());
151}
152
153IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearMovPcmS24be) {
154  PlayVideo("bear_pcm_s24be.mov", GetParam());
155}
156
157IN_PROC_BROWSER_TEST_F(MediaTest, VideoBearRotated0) {
158  RunVideoSizeTest("bear_rotate_0.mp4", 1280, 720);
159}
160
161IN_PROC_BROWSER_TEST_F(MediaTest, VideoBearRotated90) {
162  RunVideoSizeTest("bear_rotate_90.mp4", 720, 1280);
163}
164
165IN_PROC_BROWSER_TEST_F(MediaTest, VideoBearRotated180) {
166  RunVideoSizeTest("bear_rotate_180.mp4", 1280, 720);
167}
168
169IN_PROC_BROWSER_TEST_F(MediaTest, VideoBearRotated270) {
170  RunVideoSizeTest("bear_rotate_270.mp4", 720, 1280);
171}
172#endif  // defined(USE_PROPRIETARY_CODECS)
173
174#if defined(OS_CHROMEOS)
175#if defined(USE_PROPRIETARY_CODECS)
176IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearAviMp3Mpeg4) {
177  PlayVideo("bear_mpeg4_mp3.avi", GetParam());
178}
179
180IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearAviMp3Mpeg4Asp) {
181  PlayVideo("bear_mpeg4asp_mp3.avi", GetParam());
182}
183
184IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearAviMp3Divx) {
185  PlayVideo("bear_divx_mp3.avi", GetParam());
186}
187
188IN_PROC_BROWSER_TEST_P(MediaTest, VideoBear3gpAacH264) {
189  PlayVideo("bear_h264_aac.3gp", GetParam());
190}
191
192IN_PROC_BROWSER_TEST_P(MediaTest, VideoBear3gpAmrnbMpeg4) {
193  PlayVideo("bear_mpeg4_amrnb.3gp", GetParam());
194}
195
196IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavGsmms) {
197  PlayAudio("bear_gsm_ms.wav", GetParam());
198}
199
200IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearFlac) {
201  PlayAudio("bear.flac", GetParam());
202}
203#endif  // defined(USE_PROPRIETARY_CODECS)
204#endif  // defined(OS_CHROMEOS)
205
206
207IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavAlaw) {
208  PlayAudio("bear_alaw.wav", GetParam());
209}
210
211IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavMulaw) {
212  PlayAudio("bear_mulaw.wav", GetParam());
213}
214
215IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm) {
216  PlayAudio("bear_pcm.wav", GetParam());
217}
218
219IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm3kHz) {
220  PlayAudio("bear_3kHz.wav", GetParam());
221}
222
223IN_PROC_BROWSER_TEST_P(MediaTest, VideoBearWavPcm192kHz) {
224  PlayAudio("bear_192kHz.wav", GetParam());
225}
226
227IN_PROC_BROWSER_TEST_P(MediaTest, VideoTulipWebm) {
228  PlayVideo("tulip2.webm", GetParam());
229}
230
231// Covers tear-down when navigating away as opposed to browser exiting.
232IN_PROC_BROWSER_TEST_F(MediaTest, Navigate) {
233  PlayVideo("bear.ogv", false);
234  NavigateToURL(shell(), GURL(url::kAboutBlankURL));
235  EXPECT_FALSE(shell()->web_contents()->IsCrashed());
236}
237
238INSTANTIATE_TEST_CASE_P(File, MediaTest, ::testing::Values(false));
239INSTANTIATE_TEST_CASE_P(Http, MediaTest, ::testing::Values(true));
240
241IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv420pTheora)) {
242  RunColorFormatTest("yuv420p.ogv", kEnded);
243}
244
245IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv422pTheora)) {
246  RunColorFormatTest("yuv422p.ogv", kEnded);
247}
248
249IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv444pTheora)) {
250  RunColorFormatTest("yuv444p.ogv", kEnded);
251}
252
253IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv420pVp8)) {
254  RunColorFormatTest("yuv420p.webm", kEnded);
255}
256
257IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv444pVp9)) {
258  RunColorFormatTest("yuv444p.webm", "ENDED");
259}
260
261#if defined(USE_PROPRIETARY_CODECS)
262IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv420pH264)) {
263  RunColorFormatTest("yuv420p.mp4", kEnded);
264}
265
266IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuvj420pH264)) {
267  RunColorFormatTest("yuvj420p.mp4", kEnded);
268}
269
270IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv422pH264)) {
271  RunColorFormatTest("yuv422p.mp4", kEnded);
272}
273
274IN_PROC_BROWSER_TEST_F(MediaTest, MAYBE(Yuv444pH264)) {
275  RunColorFormatTest("yuv444p.mp4", kEnded);
276}
277
278#if defined(OS_CHROMEOS)
279IN_PROC_BROWSER_TEST_F(MediaTest, Yuv420pMpeg4) {
280  RunColorFormatTest("yuv420p.avi", kEnded);
281}
282#endif  // defined(OS_CHROMEOS)
283#endif  // defined(USE_PROPRIETARY_CODECS)
284
285}  // namespace content
286