diff options
author | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 18:41:16 +0000 |
---|---|---|
committer | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-20 18:41:16 +0000 |
commit | 82aff3f9755be6176092244cc76ced588076dcdb (patch) | |
tree | a7fdc5dd00ba5ec76a898cae57388af61a5cce4c /chrome/browser/dom_ui | |
parent | 7edbf666d99d82ffe14daff4d38360f8636c4b6e (diff) | |
download | chromium_src-82aff3f9755be6176092244cc76ced588076dcdb.zip chromium_src-82aff3f9755be6176092244cc76ced588076dcdb.tar.gz chromium_src-82aff3f9755be6176092244cc76ced588076dcdb.tar.bz2 |
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
Diffstat (limited to 'chrome/browser/dom_ui')
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_browsertest.cc | 55 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_browsertest.h | 47 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_handler_browsertest.cc | 41 | ||||
-rw-r--r-- | chrome/browser/dom_ui/dom_ui_handler_browsertest.h | 40 |
4 files changed, 183 insertions, 0 deletions
diff --git a/chrome/browser/dom_ui/dom_ui_browsertest.cc b/chrome/browser/dom_ui/dom_ui_browsertest.cc new file mode 100644 index 0000000..01856f6 --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_browsertest.cc @@ -0,0 +1,55 @@ +// 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"))); +} diff --git a/chrome/browser/dom_ui/dom_ui_browsertest.h b/chrome/browser/dom_ui/dom_ui_browsertest.h new file mode 100644 index 0000000..4207b1a --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_browsertest.h @@ -0,0 +1,47 @@ +// 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_ diff --git a/chrome/browser/dom_ui/dom_ui_handler_browsertest.cc b/chrome/browser/dom_ui/dom_ui_handler_browsertest.cc new file mode 100644 index 0000000..89acbcb --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_handler_browsertest.cc @@ -0,0 +1,41 @@ +// 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_; +} diff --git a/chrome/browser/dom_ui/dom_ui_handler_browsertest.h b/chrome/browser/dom_ui/dom_ui_handler_browsertest.h new file mode 100644 index 0000000..07c3675 --- /dev/null +++ b/chrome/browser/dom_ui/dom_ui_handler_browsertest.h @@ -0,0 +1,40 @@ +// 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_ |