diff options
author | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-05 20:37:34 +0000 |
---|---|---|
committer | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-05 20:37:34 +0000 |
commit | 80f4f96b7005479b9fd5ab1bafc971cd25c2952d (patch) | |
tree | c540372d9de76c9948de12711d38842db73574e5 /content | |
parent | 9834575d777dc4301838b2288b4042ee344dcd53 (diff) | |
download | chromium_src-80f4f96b7005479b9fd5ab1bafc971cd25c2952d.zip chromium_src-80f4f96b7005479b9fd5ab1bafc971cd25c2952d.tar.gz chromium_src-80f4f96b7005479b9fd5ab1bafc971cd25c2952d.tar.bz2 |
Move WebUI test framework code from content/ to chrome/
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/6921005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84306 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/webui/web_ui_bidi_checker_browsertest.cc | 38 | ||||
-rw-r--r-- | content/browser/webui/web_ui_bidi_checker_browsertest.h | 25 | ||||
-rw-r--r-- | content/browser/webui/web_ui_browsertest.cc | 205 | ||||
-rw-r--r-- | content/browser/webui/web_ui_browsertest.h | 101 | ||||
-rw-r--r-- | content/browser/webui/web_ui_test_handler.cc | 51 | ||||
-rw-r--r-- | content/browser/webui/web_ui_test_handler.h | 40 |
6 files changed, 0 insertions, 460 deletions
diff --git a/content/browser/webui/web_ui_bidi_checker_browsertest.cc b/content/browser/webui/web_ui_bidi_checker_browsertest.cc deleted file mode 100644 index 938a5a5..0000000 --- a/content/browser/webui/web_ui_bidi_checker_browsertest.cc +++ /dev/null @@ -1,38 +0,0 @@ -// 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 "content/browser/webui/web_ui_bidi_checker_browsertest.h" - -#include "base/base_paths.h" -#include "base/path_service.h" -#include "chrome/common/url_constants.h" -#include "chrome/test/ui_test_utils.h" -#include "googleurl/src/gurl.h" - -static const FilePath::CharType* kWebUIBidiCheckerLibraryJS = - FILE_PATH_LITERAL("third_party/bidichecker/bidichecker_packaged.js"); - -namespace { -FilePath WebUIBidiCheckerLibraryJSPath() { - FilePath src_root; - if (!PathService::Get(base::DIR_SOURCE_ROOT, &src_root)) - LOG(ERROR) << "Couldn't find source root"; - return src_root.Append(kWebUIBidiCheckerLibraryJS); -} -} - -WebUIBidiCheckerBrowserTest::~WebUIBidiCheckerBrowserTest() {} - -WebUIBidiCheckerBrowserTest::WebUIBidiCheckerBrowserTest() {} - -void WebUIBidiCheckerBrowserTest::SetUpInProcessBrowserTestFixture() { - WebUIBrowserTest::SetUpInProcessBrowserTestFixture(); - WebUIBrowserTest::AddLibrary(WebUIBidiCheckerLibraryJSPath()); -} - -IN_PROC_BROWSER_TEST_F(WebUIBidiCheckerBrowserTest, TestMainHistoryPageLTR) { - ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIHistoryURL)); - AddLibrary(FilePath(FILE_PATH_LITERAL("bidichecker_tests.js"))); - ASSERT_TRUE(RunJavascriptTest("runBidiCheckerLTR")); -} diff --git a/content/browser/webui/web_ui_bidi_checker_browsertest.h b/content/browser/webui/web_ui_bidi_checker_browsertest.h deleted file mode 100644 index 529294e..0000000 --- a/content/browser/webui/web_ui_bidi_checker_browsertest.h +++ /dev/null @@ -1,25 +0,0 @@ -// 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 CONTENT_BROWSER_WEBUI_WEB_UI_BIDI_CHECKER_BROWSERTEST_H_ -#define CONTENT_BROWSER_WEBUI_WEB_UI_BIDI_CHECKER_BROWSERTEST_H_ -#pragma once - -#include "content/browser/webui/web_ui_browsertest.h" -#include "chrome/test/in_process_browser_test.h" - -// Base class for BidiChecker-based tests. Preloads the BidiChecker JS library -// for each test. -class WebUIBidiCheckerBrowserTest : public WebUIBrowserTest { - public: - virtual ~WebUIBidiCheckerBrowserTest(); - - protected: - WebUIBidiCheckerBrowserTest(); - - // Setup test path. - virtual void SetUpInProcessBrowserTestFixture(); -}; - -#endif // CONTENT_BROWSER_WEBUI_WEB_UI_BIDI_CHECKER_BROWSERTEST_H_ diff --git a/content/browser/webui/web_ui_browsertest.cc b/content/browser/webui/web_ui_browsertest.cc deleted file mode 100644 index 36c9cc7..0000000 --- a/content/browser/webui/web_ui_browsertest.cc +++ /dev/null @@ -1,205 +0,0 @@ -// 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 "content/browser/webui/web_ui_browsertest.h" - -#include <string> -#include <vector> - -#include "base/path_service.h" -#include "base/values.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" -#include "content/browser/tab_contents/tab_contents.h" -#include "content/browser/webui/web_ui.h" -#include "ui/base/resource/resource_bundle.h" - -static const FilePath::CharType* kWebUILibraryJS = - FILE_PATH_LITERAL("test_api.js"); -static const FilePath::CharType* kWebUITestFolder = FILE_PATH_LITERAL("webui"); -static std::vector<std::string> error_messages_; - -// Intercepts all log messages. -bool LogHandler(int severity, - const char* file, - int line, - size_t message_start, - const std::string& str) { - if (severity == logging::LOG_ERROR) { - error_messages_.push_back(str); - return true; - } else { - // For debugging messages while developing tests. - return false; - } -} - -WebUIBrowserTest::~WebUIBrowserTest() {} - -bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name) { - return RunJavascriptFunction(function_name, ConstValueVector()); -} - -bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, - const Value& arg) { - ConstValueVector args; - args.push_back(&arg); - return RunJavascriptFunction(function_name, args); -} - -bool WebUIBrowserTest::RunJavascriptFunction(const std::string& function_name, - const Value& arg1, - const Value& arg2) { - ConstValueVector args; - args.push_back(&arg1); - args.push_back(&arg2); - return RunJavascriptFunction(function_name, args); -} - -bool WebUIBrowserTest::RunJavascriptFunction( - const std::string& function_name, - const ConstValueVector& function_arguments) { - return RunJavascriptUsingHandler(function_name, function_arguments, false); -} - -bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name) { - return RunJavascriptTest(test_name, ConstValueVector()); -} - -bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, - const Value& arg) { - ConstValueVector args; - args.push_back(&arg); - return RunJavascriptTest(test_name, args); -} - -bool WebUIBrowserTest::RunJavascriptTest(const std::string& test_name, - const Value& arg1, - const Value& arg2) { - ConstValueVector args; - args.push_back(&arg1); - args.push_back(&arg2); - return RunJavascriptTest(test_name, args); -} - -bool WebUIBrowserTest::RunJavascriptTest( - const std::string& test_name, - const ConstValueVector& test_arguments) { - return RunJavascriptUsingHandler(test_name, test_arguments, true); -} - -WebUIBrowserTest::WebUIBrowserTest() - : test_handler_(new WebUITestHandler()) {} - -void WebUIBrowserTest::SetUpInProcessBrowserTestFixture() { - ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); - test_data_directory_ = test_data_directory_.Append(kWebUITestFolder); - - // TODO(dtseng): should this be part of every BrowserTest or just WebUI test. - FilePath resources_pack_path; - PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path); - ResourceBundle::AddDataPackToSharedInstance(resources_pack_path); - - AddLibrary(FilePath(kWebUILibraryJS)); -} - -WebUIMessageHandler* WebUIBrowserTest::GetMockMessageHandler() { - return NULL; -} - -void WebUIBrowserTest::BuildJavascriptLibraries(std::string* content) { - ASSERT_TRUE(content != NULL); - std::string library_content, src_content; - - std::vector<FilePath>::iterator user_libraries_iterator; - for (user_libraries_iterator = user_libraries.begin(); - user_libraries_iterator != user_libraries.end(); - ++user_libraries_iterator) { - if (user_libraries_iterator->IsAbsolute()) { - ASSERT_TRUE(file_util::ReadFileToString(*user_libraries_iterator, - &library_content)); - } else { - ASSERT_TRUE(file_util::ReadFileToString( - test_data_directory_.Append(*user_libraries_iterator), - &library_content)); - } - content->append(library_content); - content->append(";\n"); - } -} - -string16 WebUIBrowserTest::BuildRunTestJSCall( - const std::string& function_name, - const WebUIBrowserTest::ConstValueVector& test_func_args) { - WebUIBrowserTest::ConstValueVector arguments; - StringValue function_name_arg(function_name); - arguments.push_back(&function_name_arg); - ListValue baked_argument_list; - WebUIBrowserTest::ConstValueVector::const_iterator arguments_iterator; - for (arguments_iterator = test_func_args.begin(); - arguments_iterator != test_func_args.end(); - ++arguments_iterator) { - baked_argument_list.Append((Value *)*arguments_iterator); - } - arguments.push_back(&baked_argument_list); - return WebUI::GetJavascriptCall(std::string("runTest"), arguments); -} - -bool WebUIBrowserTest::RunJavascriptUsingHandler( - const std::string& function_name, - const ConstValueVector& function_arguments, - bool is_test) { - std::string content; - BuildJavascriptLibraries(&content); - - if (!function_name.empty()) { - string16 called_function; - if (is_test) { - called_function = BuildRunTestJSCall(function_name, function_arguments); - } else { - called_function = WebUI::GetJavascriptCall(function_name, - function_arguments); - } - content.append(UTF16ToUTF8(called_function)); - } - SetupHandlers(); - logging::SetLogMessageHandler(&LogHandler); - bool result = test_handler_->RunJavascript(content, is_test); - logging::SetLogMessageHandler(NULL); - - if (error_messages_.size() > 0) { - LOG(ERROR) << "Encountered javascript console error(s)"; - result = false; - error_messages_.clear(); - } - return result; -} - -void WebUIBrowserTest::SetupHandlers() { - WebUI* web_ui_instance = - browser()->GetSelectedTabContents()->web_ui(); - ASSERT_TRUE(web_ui_instance != NULL); - web_ui_instance->register_callback_overwrites(true); - test_handler_->Attach(web_ui_instance); - - if (GetMockMessageHandler()) - GetMockMessageHandler()->Attach(web_ui_instance); -} - -void WebUIBrowserTest::AddLibrary(const FilePath& library_path) { - user_libraries.push_back(library_path); -} - -IN_PROC_BROWSER_TEST_F(WebUIBrowserTest, TestSamplePass) { - AddLibrary(FilePath(FILE_PATH_LITERAL("sample_downloads.js"))); - - // Navigate to UI. - // TODO(dtseng): make accessor for subclasses to return? - ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL)); - - ASSERT_TRUE(RunJavascriptTest("testAssertFalse")); - ASSERT_TRUE(RunJavascriptTest("testInitialFocus")); - ASSERT_FALSE(RunJavascriptTest("testConsoleError")); -} diff --git a/content/browser/webui/web_ui_browsertest.h b/content/browser/webui/web_ui_browsertest.h deleted file mode 100644 index 2e7ffc2..0000000 --- a/content/browser/webui/web_ui_browsertest.h +++ /dev/null @@ -1,101 +0,0 @@ -// 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 CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ -#define CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ -#pragma once - -#include <string> - -#include "base/file_path.h" -#include "chrome/test/in_process_browser_test.h" -#include "content/browser/webui/web_ui_test_handler.h" - -class Value; -class WebUIMessageHandler; - -// This macro simplifies the declaration of simple javascript unit tests. -// Use: -// WEB_UI_UNITTEST_F(MyWebUIPageTest, myJavascriptUnittest); -#define WEB_UI_UNITTEST_F(x, y) \ - IN_PROC_BROWSER_TEST_F(x, y) { \ - ASSERT_TRUE(RunJavascriptTest(#y)); \ - } - -// The runner of WebUI javascript based tests. -// See chrome/test/data/webui/test_api.js for the javascript side test API's. -// -// These tests should follow the form given in: -// chrome/test/data/webui/sample_downloads.js. -// and the lone test within this class. -class WebUIBrowserTest : public InProcessBrowserTest { - public: - typedef std::vector<const Value*> ConstValueVector; - virtual ~WebUIBrowserTest(); - - // Add a custom helper JS library for your test. - // If a relative path is specified, it'll be read - // as relative to the test data dir. - void AddLibrary(const FilePath& library_path); - - // Runs a javascript function in the context of all libraries. - // Note that calls to functions in test_api.js are not supported. - bool RunJavascriptFunction(const std::string& function_name); - bool RunJavascriptFunction(const std::string& function_name, - const Value& arg); - bool RunJavascriptFunction(const std::string& function_name, - const Value& arg1, - const Value& arg2); - bool RunJavascriptFunction(const std::string& function_name, - const ConstValueVector& function_arguments); - - // Runs a test that may include calls to functions in test_api.js. - bool RunJavascriptTest(const std::string& test_name); - bool RunJavascriptTest(const std::string& test_name, - const Value& arg); - bool RunJavascriptTest(const std::string& test_name, - const Value& arg1, - const Value& arg2); - bool RunJavascriptTest(const std::string& test_name, - const ConstValueVector& test_arguments); - - protected: - WebUIBrowserTest(); - - // Setup test path. - virtual void SetUpInProcessBrowserTestFixture(); - - // Returns a mock WebUI object under test (if any). - virtual WebUIMessageHandler* GetMockMessageHandler(); - - private: - // Builds a string containing all added javascript libraries. - void BuildJavascriptLibraries(std::string* content); - - // Builds a string with a call to the runTest JS function, passing the - // given test and its arguments. - string16 BuildRunTestJSCall(const std::string& test_name, - const WebUIBrowserTest::ConstValueVector& args); - - // Calls the specified function with all libraries available. If |is_test| - // is true, the framework listens for pass fail messages from javascript. - // The provided arguments vector is passed to |function_name|. - bool RunJavascriptUsingHandler(const std::string& function_name, - const ConstValueVector& function_arguments, - bool is_test); - - // Attaches mock and test handlers. - void SetupHandlers(); - - // Handles test framework messages. - scoped_ptr<WebUITestHandler> test_handler_; - - // Location of test data (currently test/data/webui). - FilePath test_data_directory_; - - // User added libraries - std::vector<FilePath> user_libraries; -}; - -#endif // CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ diff --git a/content/browser/webui/web_ui_test_handler.cc b/content/browser/webui/web_ui_test_handler.cc deleted file mode 100644 index 280774f..0000000 --- a/content/browser/webui/web_ui_test_handler.cc +++ /dev/null @@ -1,51 +0,0 @@ -// 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 "content/browser/webui/web_ui_test_handler.h" - -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/test/ui_test_utils.h" -#include "content/browser/renderer_host/render_view_host.h" - -bool WebUITestHandler::RunJavascript(const std::string& js_test, - bool is_test) { - web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame( - string16(), UTF8ToUTF16(js_test)); - - if (is_test) - return WaitForResult(); - else - return true; -} - -void WebUITestHandler::HandlePass(const ListValue* args) { - test_succeeded_ = true; - if (is_waiting_) - MessageLoopForUI::current()->Quit(); -} - -void WebUITestHandler::HandleFail(const ListValue* args) { - test_succeeded_ = false; - if (is_waiting_) - MessageLoopForUI::current()->Quit(); - - std::string message; - ASSERT_TRUE(args->GetString(0, &message)); - LOG(ERROR) << message; -} - -void WebUITestHandler::RegisterMessages() { - web_ui_->RegisterMessageCallback("Pass", - NewCallback(this, &WebUITestHandler::HandlePass)); - web_ui_->RegisterMessageCallback("Fail", - NewCallback(this, &WebUITestHandler::HandleFail)); -} - -bool WebUITestHandler::WaitForResult() { - is_waiting_ = true; - ui_test_utils::RunMessageLoop(); - is_waiting_ = false; - return test_succeeded_; -} diff --git a/content/browser/webui/web_ui_test_handler.h b/content/browser/webui/web_ui_test_handler.h deleted file mode 100644 index b0135d2..0000000 --- a/content/browser/webui/web_ui_test_handler.h +++ /dev/null @@ -1,40 +0,0 @@ -// 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 CONTENT_BROWSER_WEBUI_WEB_UI_HANDLER_BROWSERTEST_H_ -#define CONTENT_BROWSER_WEBUI_WEB_UI_HANDLER_BROWSERTEST_H_ -#pragma once - -#include <string> - -#include "content/browser/webui/web_ui.h" - -// This class registers test framework specific handlers on WebUI objects. -class WebUITestHandler : public WebUIMessageHandler { - public: - // Runs a string of javascript. Returns pass fail. - bool RunJavascript(const std::string& js_test, bool is_test); - - protected: - // WebUI handlers which deliver results to any waiting message loops. - // |args| is currently ignored. - void HandlePass(const ListValue* args); - void HandleFail(const ListValue* args); - - // WebUIMessageHandler overrides. - // Add test handlers to the current WebUI 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 // CONTENT_BROWSER_WEBUI_WEB_UI_HANDLER_BROWSERTEST_H_ |