Commit 82aff3f9 authored by dtseng@chromium.org's avatar dtseng@chromium.org

Initial sketch of DOM UI test framework.

BUG=65911
TEST=manually run tests.
Review URL: http://codereview.chromium.org/6231004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71972 0039d316-1c4b-4281-b951-d872f2087c98
parent 7edbf666
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/dom_ui/dom_ui_browsertest.h"
#include "base/path_service.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/ui_test_utils.h"
static const FilePath::CharType* kDOMUILibraryJS =
FILE_PATH_LITERAL("test_api.js");
static const FilePath::CharType* kDOMUITestFolder =
FILE_PATH_LITERAL("dom_ui");
bool DOMUITest::RunDOMUITest(const FilePath::CharType* src_path) {
std::string content;
BuildJavaScriptTest(FilePath(src_path), &content);
handler_->Attach(
browser()->GetSelectedTabContents()->dom_ui());
return handler_->Execute(content);
}
DOMUITest::DOMUITest() : handler_(new DOMUITestHandler()) {}
void DOMUITest::SetUpInProcessBrowserTestFixture() {
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_));
test_data_directory_ = test_data_directory_.Append(kDOMUITestFolder);
}
void DOMUITest::BuildJavaScriptTest(const FilePath& src_path,
std::string* content) {
ASSERT_TRUE(content != NULL);
std::string library_content, src_content;
ASSERT_TRUE(file_util::ReadFileToString(
test_data_directory_.Append(FilePath(kDOMUILibraryJS)),
&library_content));
ASSERT_TRUE(file_util::ReadFileToString(
test_data_directory_.Append(src_path), &src_content));
content->append(library_content);
content->append(";\n");
content->append(src_content);
}
IN_PROC_BROWSER_TEST_F(DOMUITest, TestSamplePass) {
// Navigate to UI.
// TODO(dtseng): make accessor for subclasses to return?
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
ASSERT_TRUE(RunDOMUITest(FILE_PATH_LITERAL("sample_downloads.js")));
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_BROWSERTEST_H_
#define CHROME_BROWSER_DOM_UI_DOM_UI_BROWSERTEST_H_
#pragma once
#include <string>
#include "base/file_path.h"
#include "chrome/browser/dom_ui/dom_ui_handler_browsertest.h"
#include "chrome/test/in_process_browser_test.h"
// The runner of DOMUI javascript based tests.
// See chrome/test/data/dom_ui/test_api.js for the javascript side test API's.
//
// These tests should follow the form given in:
// chrome/test/data/dom_ui/sample_downloads.js.
// and the lone test within this class.
class DOMUITest : public InProcessBrowserTest {
public:
bool RunDOMUITest(const FilePath::CharType* src_path);
protected:
DOMUITest();
virtual void SetUpInProcessBrowserTestFixture();
private:
// Builds a javascript test in the form:
// <js_library> ...
// <src_path> ...
// runTests(function test1() {...},
// ...
// );
void BuildJavaScriptTest(const FilePath& src_path,
std::string* content);
// Handles test framework messages.
scoped_ptr<DOMUITestHandler> handler_;
// Location of test data (currently test/data/dom_ui).
FilePath test_data_directory_;
};
#endif // CHROME_BROWSER_DOM_UI_DOM_UI_BROWSERTEST_H_
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/dom_ui/dom_ui_handler_browsertest.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/test/ui_test_utils.h"
bool DOMUITestHandler::Execute(const std::string& js_test) {
dom_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(
std::wstring(), UTF8ToWide(js_test));
return WaitForResult();
}
void DOMUITestHandler::HandlePass(const ListValue* args) {
test_succeeded_ = true;
if (is_waiting_)
MessageLoopForUI::current()->Quit();
}
void DOMUITestHandler::HandleFail(const ListValue* args) {
test_succeeded_ = false;
if (is_waiting_)
MessageLoopForUI::current()->Quit();
}
void DOMUITestHandler::RegisterMessages() {
dom_ui_->RegisterMessageCallback("Pass",
NewCallback(this, &DOMUITestHandler::HandlePass));
dom_ui_->RegisterMessageCallback("Fail",
NewCallback(this, &DOMUITestHandler::HandleFail));
}
bool DOMUITestHandler::WaitForResult() {
is_waiting_ = true;
ui_test_utils::RunMessageLoop();
is_waiting_ = false;
return test_succeeded_;
}
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_DOM_UI_DOM_UI_HANDLER_BROWSERTEST_H_
#define CHROME_BROWSER_DOM_UI_DOM_UI_HANDLER_BROWSERTEST_H_
#pragma once
#include <string>
#include "chrome/browser/dom_ui/dom_ui.h"
// This class registers test framework specific handlers on DOMUI objects.
class DOMUITestHandler : public DOMMessageHandler {
public:
// Executes a string of javascript. Returns pass fail.
bool Execute(const std::string& js_test);
protected:
// DOMUI handlers which deliver results to any waiting message loops.
// |args| is currently ignored.
void HandlePass(const ListValue* args);
void HandleFail(const ListValue* args);
// DOMUIMessageHandler overrides.
// Add test handlers to the current DOMUI object.
virtual void RegisterMessages();
private:
// Runs a message loop until test finishes. Returns the result of the test.
bool WaitForResult();
// Pass fail result of current tests.
bool test_succeeded_;
// Waiting for a test to finish.
bool is_waiting_;
};
#endif // CHROME_BROWSER_DOM_UI_DOM_UI_HANDLER_BROWSERTEST_H_
......@@ -2033,6 +2033,10 @@
'browser/chromeos/update_browsertest.cc',
'browser/crash_recovery_browsertest.cc',
'browser/device_orientation/device_orientation_browsertest.cc',
'browser/dom_ui/dom_ui_browsertest.cc',
'browser/dom_ui/dom_ui_browsertest.h',
'browser/dom_ui/dom_ui_handler_browsertest.cc',
'browser/dom_ui/dom_ui_handler_browsertest.h',
'browser/dom_ui/file_browse_browsertest.cc',
'browser/dom_ui/mediaplayer_browsertest.cc',
'browser/download/download_browsertest.cc',
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Sample tests that exercise the test JS library and show how this framework
// could be used to test the downloads page.
// Call this method to run the tests.
// The method takes an array of functions (your tests).
runTests([
function testAssertFalse() {
assertFalse(false);
},
function testInitialFocus() {
assertTrue(document.activeElement.id == 'term', '');
}
]);
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Library providing basic test framework functionality.
(function() {
// Indicates a pass to the C++ backend.
function pass(message) {
chrome.send('Pass', []);
}
// Indicates a fail to the C++ backend.
function fail(message) {
chrome.send('Fail', []);
}
// Asserts.
// Use the following assertions to verify a condition within a test.
// If assertion fails, the C++ backend will be immediately notified.
// If assertion passes, no notification will be sent to the C++ backend.
function assertBool(test, expected, message) {
if (test !== expected) {
if (message)
message = test + '\n' + message;
else
message = test;
fail(message);
}
}
function assertTrue(test, message) {
assertBool(test, true, message);
}
function assertFalse(test, message) {
assertBool(test, false, message);
}
function assertEquals(expected, actual, message) {
if (expected !== actual) {
fail('Test Error in ' + testName(currentTest) +
'\nActual: ' + actual + '\nExpected: ' + expected + '\n' + message);
}
if (typeof expected != typeof actual) {
fail('Test Error in ' + testName(currentTest) +
' (type mismatch)\nActual Type: ' + typeof actual +
'\nExpected Type:' + typeof expected + '\n' + message);
}
}
function assertNotReached(message) {
fail(message);
}
// Call this method within your test script file to begin tests.
// Takes an array of functions; each function is a test.
function runTests(tests) {
var currentTest = tests.shift();
while (currentTest) {
try {
console.log('Running test ' + currentTest.name);
currentTest.call();
currentTest = tests.shift();
} catch (e) {
console.log(
'Failed: ' + currentTest.name + '\nwith exception: ' + e.message);
fail();
}
}
// All tests passed.
pass('');
}
// Exports.
window.assertTrue = assertTrue;
window.assertFalse = assertFalse;
window.assertEquals = assertEquals;
window.assertNotReached = assertNotReached;
window.runTests = runTests;
})();
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment