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/path_service.h"
8#include "base/strings/stringprintf.h"
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "chrome/common/chrome_paths.h"
13#include "chrome/test/base/ui_test_utils.h"
14#include "content/public/browser/navigation_controller.h"
15#include "content/public/browser/navigation_entry.h"
16#include "content/public/browser/web_contents.h"
17#include "content/public/test/browser_test_utils.h"
18
19// Common test results.
20const char MediaBrowserTest::kEnded[] = "ENDED";
21const char MediaBrowserTest::kError[] = "ERROR";
22const char MediaBrowserTest::kFailed[] = "FAILED";
23const char MediaBrowserTest::kPluginCrashed[] = "PLUGIN_CRASHED";
24
25MediaBrowserTest::MediaBrowserTest() : ignore_plugin_crash_(false) {
26}
27
28MediaBrowserTest::~MediaBrowserTest() {
29}
30
31void MediaBrowserTest::RunMediaTestPage(
32    const std::string& html_page, std::vector<StringPair>* query_params,
33    const std::string& expected_title, bool http) {
34  GURL gurl;
35  std::string query = "";
36  if (query_params != NULL && !query_params->empty()) {
37    std::vector<StringPair>::const_iterator itr = query_params->begin();
38    query = itr->first + "=" + itr->second;
39    ++itr;
40    for (; itr != query_params->end(); ++itr) {
41      query.append("&" + itr->first + "=" + itr->second);
42    }
43  }
44  if (http) {
45    ASSERT_TRUE(test_server()->Start());
46    gurl = test_server()->GetURL("files/media/" + html_page + "?" + query);
47  } else {
48    base::FilePath test_file_path;
49    PathService::Get(chrome::DIR_TEST_DATA, &test_file_path);
50    test_file_path = test_file_path.AppendASCII("media")
51                                   .AppendASCII(html_page);
52    gurl = content::GetFileUrlWithQuery(test_file_path, query);
53  }
54
55  base::string16 final_title = RunTest(gurl, expected_title);
56  EXPECT_EQ(base::ASCIIToUTF16(expected_title), final_title);
57}
58
59base::string16 MediaBrowserTest::RunTest(const GURL& gurl,
60                                         const std::string& expected_title) {
61  VLOG(0) << "Running test URL: " << gurl;
62  // Observe the web contents for plugin crashes.
63  Observe(browser()->tab_strip_model()->GetActiveWebContents());
64  content::TitleWatcher title_watcher(
65      browser()->tab_strip_model()->GetActiveWebContents(),
66      base::ASCIIToUTF16(expected_title));
67  AddWaitForTitles(&title_watcher);
68  ui_test_utils::NavigateToURL(browser(), gurl);
69
70  return title_watcher.WaitAndGetTitle();
71}
72
73void MediaBrowserTest::AddWaitForTitles(content::TitleWatcher* title_watcher) {
74  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kEnded));
75  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kError));
76  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kFailed));
77  title_watcher->AlsoWaitForTitle(base::ASCIIToUTF16(kPluginCrashed));
78}
79
80void MediaBrowserTest::PluginCrashed(const base::FilePath& plugin_path,
81                                     base::ProcessId plugin_pid) {
82  VLOG(0) << "Plugin crashed: " << plugin_path.value();
83  if (ignore_plugin_crash_)
84    return;
85  // Update document title to quit TitleWatcher early.
86  web_contents()->GetController().GetActiveEntry()
87      ->SetTitle(base::ASCIIToUTF16(kPluginCrashed));
88  ADD_FAILURE() << "Failing test due to plugin crash.";
89}
90
91void MediaBrowserTest::IgnorePluginCrash() {
92  ignore_plugin_crash_ = true;
93}
94
95