media_browsertest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/test/browser_test_utils.h"
15
16// Common test results.
17const char MediaBrowserTest::kEnded[] = "ENDED";
18const char MediaBrowserTest::kError[] = "ERROR";
19const char MediaBrowserTest::kFailed[] = "FAILED";
20
21MediaBrowserTest::MediaBrowserTest() {
22}
23
24MediaBrowserTest::~MediaBrowserTest() {
25}
26
27void MediaBrowserTest::RunMediaTestPage(
28    const char* html_page, std::vector<StringPair>* query_params,
29    const char* expected_title, bool http) {
30  GURL gurl;
31  std::string query = "";
32  if (query_params != NULL && !query_params->empty()) {
33    std::vector<StringPair>::const_iterator itr = query_params->begin();
34    query = base::StringPrintf("%s=%s", itr->first, itr->second);
35    ++itr;
36    for (; itr != query_params->end(); ++itr) {
37      query.append(base::StringPrintf("&%s=%s", itr->first, itr->second));
38    }
39  }
40  if (http) {
41    ASSERT_TRUE(test_server()->Start());
42    gurl = test_server()->GetURL(
43        base::StringPrintf("files/media/%s?%s", html_page, query.c_str()));
44  } else {
45    base::FilePath test_file_path;
46    PathService::Get(chrome::DIR_TEST_DATA, &test_file_path);
47    test_file_path = test_file_path.AppendASCII("media")
48                                   .AppendASCII(html_page);
49    gurl = content::GetFileUrlWithQuery(test_file_path, query);
50  }
51
52  base::string16 final_title = RunTest(gurl, expected_title);
53  EXPECT_EQ(ASCIIToUTF16(expected_title), final_title);
54}
55
56string16 MediaBrowserTest::RunTest(const GURL& gurl,
57                                   const char* expected_title) {
58  DVLOG(1) << "Running test URL: " << gurl;
59  content::TitleWatcher title_watcher(
60      browser()->tab_strip_model()->GetActiveWebContents(),
61      ASCIIToUTF16(expected_title));
62  AddWaitForTitles(&title_watcher);
63  ui_test_utils::NavigateToURL(browser(), gurl);
64
65  return title_watcher.WaitAndGetTitle();
66}
67
68void MediaBrowserTest::AddWaitForTitles(content::TitleWatcher* title_watcher) {
69  title_watcher->AlsoWaitForTitle(ASCIIToUTF16(kEnded));
70  title_watcher->AlsoWaitForTitle(ASCIIToUTF16(kError));
71  title_watcher->AlsoWaitForTitle(ASCIIToUTF16(kFailed));
72}
73