1 // Copyright 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 "chrome/browser/media/media_browsertest.h"
6 
7 #include "base/command_line.h"
8 #include "base/i18n/time_formatting.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/content_features.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "media/audio/audio_features.h"
21 #include "media/base/media_switches.h"
22 #include "media/base/test_data_util.h"
23 #include "net/test/embedded_test_server/embedded_test_server.h"
24 
MediaBrowserTest()25 MediaBrowserTest::MediaBrowserTest() {}
26 
~MediaBrowserTest()27 MediaBrowserTest::~MediaBrowserTest() {}
28 
SetUpCommandLine(base::CommandLine * command_line)29 void MediaBrowserTest::SetUpCommandLine(base::CommandLine* command_line) {
30   command_line->AppendSwitchASCII(
31       switches::kAutoplayPolicy,
32       switches::autoplay::kNoUserGestureRequiredPolicy);
33 
34   std::vector<base::Feature> enabled_features = {
35 #if defined(OS_ANDROID)
36     features::kLogJsConsoleMessages,
37 #endif
38   };
39 
40   std::vector<base::Feature> disabled_features = {
41     // Disable fallback after decode error to avoid unexpected test pass on
42     // the fallback path.
43     media::kFallbackAfterDecodeError,
44 
45 #if defined(OS_LINUX) || defined(OS_CHROMEOS)
46     // Disable out of process audio on Linux due to process spawn
47     // failures. http://crbug.com/986021
48     features::kAudioServiceOutOfProcess,
49 #endif
50   };
51 
52   scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
53 }
54 
RunMediaTestPage(const std::string & html_page,const base::StringPairs & query_params,const std::string & expected_title,bool http)55 void MediaBrowserTest::RunMediaTestPage(const std::string& html_page,
56                                         const base::StringPairs& query_params,
57                                         const std::string& expected_title,
58                                         bool http) {
59   GURL gurl;
60   std::string query = media::GetURLQueryString(query_params);
61   std::unique_ptr<net::EmbeddedTestServer> http_test_server;
62   if (http) {
63     DVLOG(0) << base::TimeFormatTimeOfDayWithMilliseconds(base::Time::Now())
64              << " Starting HTTP server";
65     http_test_server.reset(new net::EmbeddedTestServer);
66     http_test_server->ServeFilesFromSourceDirectory(media::GetTestDataPath());
67     CHECK(http_test_server->Start());
68     gurl = http_test_server->GetURL("/" + html_page + "?" + query);
69   } else {
70     gurl = content::GetFileUrlWithQuery(media::GetTestDataFilePath(html_page),
71                                         query);
72   }
73   std::string final_title = RunTest(gurl, expected_title);
74   EXPECT_EQ(expected_title, final_title);
75 }
76 
RunTest(const GURL & gurl,const std::string & expected_title)77 std::string MediaBrowserTest::RunTest(const GURL& gurl,
78                                       const std::string& expected_title) {
79   DVLOG(0) << base::TimeFormatTimeOfDayWithMilliseconds(base::Time::Now())
80            << " Running test URL: " << gurl;
81 
82   content::TitleWatcher title_watcher(
83       browser()->tab_strip_model()->GetActiveWebContents(),
84       base::ASCIIToUTF16(expected_title));
85   AddWaitForTitles(&title_watcher);
86   ui_test_utils::NavigateToURL(browser(), gurl);
87   base::string16 result = title_watcher.WaitAndGetTitle();
88   return base::UTF16ToASCII(result);
89 }
90 
AddWaitForTitles(content::TitleWatcher * title_watcher)91 void MediaBrowserTest::AddWaitForTitles(content::TitleWatcher* title_watcher) {
92   title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kEnded));
93   title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kError));
94   title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(media::kFailed));
95 }
96