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/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/test/base/ui_test_utils.h"
11 #include "content/public/browser/navigation_controller.h"
12 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "media/base/test_data_util.h"
16 #include "net/test/spawned_test_server/spawned_test_server.h"
17
18 // Common test results.
19 const char MediaBrowserTest::kEnded[] = "ENDED";
20 const char MediaBrowserTest::kError[] = "ERROR";
21 const char MediaBrowserTest::kFailed[] = "FAILED";
22 const char MediaBrowserTest::kPluginCrashed[] = "PLUGIN_CRASHED";
23
MediaBrowserTest()24 MediaBrowserTest::MediaBrowserTest() : ignore_plugin_crash_(false) {
25 }
26
~MediaBrowserTest()27 MediaBrowserTest::~MediaBrowserTest() {
28 }
29
RunMediaTestPage(const std::string & html_page,const media::QueryParams & query_params,const std::string & expected_title,bool http)30 void 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
RunTest(const GURL & gurl,const std::string & expected_title)52 std::string MediaBrowserTest::RunTest(const GURL& gurl,
53 const std::string& expected_title) {
54 DVLOG(0) << "Running test URL: " << gurl;
55 // Observe the web contents for plugin crashes.
56 Observe(browser()->tab_strip_model()->GetActiveWebContents());
57 content::TitleWatcher title_watcher(
58 browser()->tab_strip_model()->GetActiveWebContents(),
59 base::ASCIIToUTF16(expected_title));
60 AddWaitForTitles(&title_watcher);
61 ui_test_utils::NavigateToURL(browser(), gurl);
62 base::string16 result = title_watcher.WaitAndGetTitle();
63 return base::UTF16ToASCII(result);
64 }
65
AddWaitForTitles(content::TitleWatcher * title_watcher)66 void MediaBrowserTest::AddWaitForTitles(content::TitleWatcher* title_watcher) {
67 title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kEnded));
68 title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kError));
69 title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kFailed));
70 title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kPluginCrashed));
71 }
72
PluginCrashed(const base::FilePath & plugin_path,base::ProcessId plugin_pid)73 void MediaBrowserTest::PluginCrashed(const base::FilePath& plugin_path,
74 base::ProcessId plugin_pid) {
75 VLOG(0) << "Plugin crashed: " << plugin_path.value();
76 if (ignore_plugin_crash_)
77 return;
78 // Update document title to quit TitleWatcher early.
79 web_contents()->GetController().GetActiveEntry()
80 ->SetTitle(base::ASCIIToUTF16(kPluginCrashed));
81 ADD_FAILURE() << "Failing test due to plugin crash.";
82 }
83
IgnorePluginCrash()84 void MediaBrowserTest::IgnorePluginCrash() {
85 ignore_plugin_crash_ = true;
86 }
87
88