qunit_browser_test_runner.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 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/test/remoting/qunit_browser_test_runner.h"
6
7#include "base/files/file_util.h"
8#include "base/json/json_reader.h"
9#include "base/values.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "chrome/test/base/ui_test_utils.h"
13#include "content/public/test/browser_test_utils.h"
14#include "net/base/filename_util.h"
15
16void QUnitBrowserTestRunner::QUnitStart(content::WebContents* web_contents) {
17  std::string result;
18
19  // The test suite must include /third_party/qunit/src/browser_test_harness.js
20  // which exposes the entry point browserTestHarness.run().
21  ASSERT_TRUE(content::ExecuteScriptAndExtractString(
22      web_contents, "browserTestHarness.run();", &result));
23
24  // Read in the JSON.
25  base::JSONReader reader;
26  scoped_ptr<base::Value> value;
27  value.reset(reader.Read(result, base::JSON_ALLOW_TRAILING_COMMAS));
28
29  // Convert to dictionary.
30  base::DictionaryValue* dict_value = NULL;
31  ASSERT_TRUE(value->GetAsDictionary(&dict_value));
32
33  // Extract the fields.
34  bool passed;
35  ASSERT_TRUE(dict_value->GetBoolean("passed", &passed));
36  std::string error_message;
37  ASSERT_TRUE(dict_value->GetString("errorMessage", &error_message));
38
39  EXPECT_TRUE(passed) << error_message;
40}
41
42void QUnitBrowserTestRunner::RunTest(const base::FilePath& file) {
43  ASSERT_TRUE(PathExists(file)) << "Error: The QUnit test suite <"
44                                << file.value() << "> does not exist.";
45  ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(file));
46
47  content::WebContents* web_contents =
48      browser()->tab_strip_model()->GetActiveWebContents();
49  ASSERT_TRUE(web_contents);
50
51  std::string result;
52  QUnitStart(web_contents);
53}
54