diff options
author | scr@chromium.org <scr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-17 08:00:03 +0000 |
---|---|---|
committer | scr@chromium.org <scr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-17 08:00:03 +0000 |
commit | ab5f20ac22869d842c473d8f22c488f7e4860cb4 (patch) | |
tree | 9f8bc0d2c01fdf61177726678f5db21d178c6668 | |
parent | b6081702c2cb8094e45c3a58ea92915d4b972ddf (diff) | |
download | chromium_src-ab5f20ac22869d842c473d8f22c488f7e4860cb4.zip chromium_src-ab5f20ac22869d842c473d8f22c488f7e4860cb4.tar.gz chromium_src-ab5f20ac22869d842c473d8f22c488f7e4860cb4.tar.bz2 |
Support automatic javascript test registry in gtest when creating WebUI tests.
The goal was to support something as simple as the following, where the tests in the
javascript files would be 1st class GTESTs, supporting FLAKY_, DISABLED_, etc, and
registered at linker_initialization time.
WEB_UI_BROWSER_TEST_JS(WebUIBrowserTest, TestJSPass,
FILE_PATH_LITERAL("sample_passing.js")) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
}
WEB_UI_BROWSER_TEST_JS_FALSE(WebUIBrowserTest, TestJSFail,
FILE_PATH_LITERAL("sample_failing.js")) {
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIDownloadsURL));
}
http://www.chromium.org/Home/domui-testing
In order to support linker init time, I had to modify path_service to support
Unregistration, and chrome_paths to allow multiple invocations.
BUG=82437
R=estade@chromium.org,jhawkins@chromium.org
TEST=browser_tests --gtest_filter=WebUIBrowserTest*
Review URL: http://codereview.chromium.org/7087014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@89453 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | chrome/browser/ui/webui/javascript2webui.js | 30 | ||||
-rw-r--r-- | chrome/browser/ui/webui/web_ui_browsertest.cc | 48 | ||||
-rw-r--r-- | chrome/browser/ui/webui/web_ui_browsertest.h | 6 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 21 | ||||
-rw-r--r-- | chrome/test/data/webui/sample_pass.js | 17 | ||||
-rwxr-xr-x | tools/gypv8sh.py | 74 |
7 files changed, 191 insertions, 6 deletions
@@ -46,6 +46,7 @@ v8.log /chrome/app/theme/google_chrome /chrome/browser/autofill/internal /chrome/browser/extensions/default_extensions/chromeos +/chrome/browser/ui/webui/web_ui_browsertest-inl.h /chrome/installer/linux /chrome/installer/mac/internal /chrome/installer/mac/third_party/xz/xz diff --git a/chrome/browser/ui/webui/javascript2webui.js b/chrome/browser/ui/webui/javascript2webui.js new file mode 100755 index 0000000..b4ad607 --- /dev/null +++ b/chrome/browser/ui/webui/javascript2webui.js @@ -0,0 +1,30 @@ +// 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. +if (arguments.length < 4) { + print('usage: ' + + arguments[0] + ' test_fixture path-to-testfile.js testfile.js'); + quit(); +} +var test_fixture = arguments[1]; +var js_file = arguments[2]; +var js_file_base = arguments[3]; +var prevfuncs = {}; +for (var func in this) { + if (this[func] instanceof Function) + prevfuncs[func] = func; +} +var js = load(js_file); +print('// GENERATED FILE'); +print('// ' + arguments.join(' ')); +print('// PLEASE DO NOT HAND EDIT!'); +print(); +for (var func in this) { + if (!prevfuncs[func] && this[func] instanceof Function) { + print('IN_PROC_BROWSER_TEST_F(' + test_fixture + ', ' + func + ') {'); + print(' AddLibrary(FilePath(FILE_PATH_LITERAL("' + js_file_base + '")));'); + print(' ASSERT_TRUE(RunJavascriptTest("' + func + '"));'); + print('}'); + print(); + } +} diff --git a/chrome/browser/ui/webui/web_ui_browsertest.cc b/chrome/browser/ui/webui/web_ui_browsertest.cc index 66815d0..4e8e959 100644 --- a/chrome/browser/ui/webui/web_ui_browsertest.cc +++ b/chrome/browser/ui/webui/web_ui_browsertest.cc @@ -11,6 +11,7 @@ #include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/webui/test_chrome_web_ui_factory.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/url_constants.h" #include "chrome/test/ui_test_utils.h" @@ -21,9 +22,8 @@ namespace { -const FilePath::StringType kWebUILibraryJS = - FILE_PATH_LITERAL("test_api.js"); -const FilePath::StringType kWebUITestFolder = FILE_PATH_LITERAL("webui"); +const FilePath::CharType kWebUILibraryJS[] = FILE_PATH_LITERAL("test_api.js"); +const FilePath::CharType kWebUITestFolder[] = FILE_PATH_LITERAL("webui"); base::LazyInstance<std::vector<std::string> > error_messages_( base::LINKER_INITIALIZED); @@ -244,3 +244,45 @@ IN_PROC_BROWSER_TEST_F(WebUIBrowserExpectFailTest, TestFailsFast) { EXPECT_FATAL_FAILURE(RunJavascriptTestNoReturn("FAILS_BogusFunctionName"), "WebUITestHandler::Observe"); } + + +// This test framework is used in the generated tests, which are included +// below. WebUIBrowserTest requires being on a page which is a WebUI page. Using +// the TestChromeWebUIFactory, we use a dummy URL |kChromeTestBrowserTestPass|, +// which we force to be a WebUI page. +class WebUIBrowserTestPass + : public WebUIBrowserTest, + public TestChromeWebUIFactory::WebUIProvider { + private: + // TestChromeWebUIFactory::WebUIProvider: + virtual WebUI* NewWebUI(TabContents* tab_contents, + const GURL& url) OVERRIDE { + return new WebUI(tab_contents); + } + + // InProcessBrowserTest: + virtual void SetUpOnMainThread() OVERRIDE { + WebUIBrowserTest::SetUpOnMainThread(); + ui_test_utils::NavigateToURL(browser(), + GURL(kChromeTestBrowserTestPass)); + } + + virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { + WebUIBrowserTest::SetUpInProcessBrowserTestFixture(); + TestChromeWebUIFactory::AddFactoryOverride( + GURL(kChromeTestBrowserTestPass).host(), this); + } + + virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { + WebUIBrowserTest::TearDownInProcessBrowserTestFixture(); + TestChromeWebUIFactory::RemoveFactoryOverride( + GURL(kChromeTestBrowserTestPass).host()); + } + + static const char kChromeTestBrowserTestPass[]; +}; + +const char WebUIBrowserTestPass::kChromeTestBrowserTestPass[] = + "chrome://WebUIBrowserTestPass"; + +#include "chrome/browser/ui/webui/web_ui_browsertest-inl.h" diff --git a/chrome/browser/ui/webui/web_ui_browsertest.h b/chrome/browser/ui/webui/web_ui_browsertest.h index 001ae7a..a8e9f96 100644 --- a/chrome/browser/ui/webui/web_ui_browsertest.h +++ b/chrome/browser/ui/webui/web_ui_browsertest.h @@ -2,8 +2,8 @@ // 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_ +#ifndef CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ +#define CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ #pragma once #include <string> @@ -98,4 +98,4 @@ class WebUIBrowserTest : public InProcessBrowserTest { std::vector<FilePath> user_libraries; }; -#endif // CONTENT_BROWSER_WEBUI_WEB_UI_BROWSERTEST_H_ +#endif // CHROME_BROWSER_UI_WEBUI_WEB_UI_BROWSERTEST_H_ diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index be0c1e3..c6da8dd 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -2291,6 +2291,7 @@ '../third_party/icu/icu.gyp:icui18n', '../third_party/icu/icu.gyp:icuuc', '../v8/tools/gyp/v8.gyp:v8', + '../v8/tools/gyp/v8.gyp:v8_shell', '../webkit/webkit.gyp:test_shell_test_support', # Runtime dependencies '../third_party/mesa/mesa.gyp:osmesa', @@ -2555,6 +2556,26 @@ '../content/renderer/render_widget_browsertest.h', '../content/renderer/v8_value_converter_browsertest.cc', ], + 'actions': [ + { + 'action_name': 'js2webui', + 'variables': { + 'gypv8sh': '../tools/gypv8sh.py', + 'js2webui': 'browser/ui/webui/javascript2webui.js', + }, + 'inputs': [ + '<(gypv8sh)', + '<(js2webui)', + '<!@(python <(gypv8sh) -i)', + ], + 'outputs': [ + '<!@(python <(gypv8sh) -o)', + ], + 'action': [ + 'python', '<(gypv8sh)', '-p', '<(PRODUCT_DIR)', '<(js2webui)', + ], + }, + ], 'conditions': [ ['chromeos==0', { 'sources/': [ diff --git a/chrome/test/data/webui/sample_pass.js b/chrome/test/data/webui/sample_pass.js new file mode 100644 index 0000000..ca9b88c4 --- /dev/null +++ b/chrome/test/data/webui/sample_pass.js @@ -0,0 +1,17 @@ +// 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. +function testAssertFalse() { + assertFalse(false); +} + +function testAssertTrue() { + assertTrue(true); +} + +function testAssertEquals() { + assertEquals(5, 5, "fives"); +} diff --git a/tools/gypv8sh.py b/tools/gypv8sh.py new file mode 100755 index 0000000..387b9e4 --- /dev/null +++ b/tools/gypv8sh.py @@ -0,0 +1,74 @@ +#!/usr/bin/python +# 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. +# +# This script is used by chrome_tests.gypi's js2webui action to maintain the +# argument lists and to generate inlinable tests. +# +# Usage: +# python tools/gypv8sh.py -p product_dir path/to/javascript2webui.js +# python tools/gypv8sh.py -t # print test_harnesses +# python tools/gypv8sh.py -i # print inputs +# python tools/gypv8sh.py -o # print outputs +import json +import optparse +import os +import subprocess +import sys + +# Please adjust the following to edit or add new javascript webui tests. +rules = [ + [ + 'WebUIBrowserTestPass', + 'test/data/webui/sample_pass.js', + 'browser/ui/webui/web_ui_browsertest-inl.h', + ], + ] + +def set_add(option, opt_str, value, parser, the_set, the_value): + the_set.add(the_value) + +# For options -t, -i, & -o, we print the "column" of the |rules|. We keep a set +# of indices to print in |print_rule_indices| and print them in sorted order if +# non-empty. +print_rule_indices = set() +parser = optparse.OptionParser(); +parser.set_usage( + "%prog [-v][-n] --product_dir PRODUCT_DIR -or- " + "%prog [-v][-n] (-i|-t|-o)"); +parser.add_option('-v', '--verbose', action='store_true', dest='verbose') +parser.add_option('-n', '--impotent', action='store_true', dest='impotent', + help="don't execute; just print (as if verbose)") +parser.add_option('-p', '--product_dir', action='store', type='string', + dest='product_dir', + help='for gyp to set the <(PRODUCT_DIR) for running v8_shell') +parser.add_option('-t', '--test_fixture', action='callback', callback=set_add, + callback_args=tuple([print_rule_indices, 0]), + help='print test_fixtures') +parser.add_option('-i', '--in', action='callback', callback=set_add, + callback_args=tuple([print_rule_indices, 1]), + help='print inputs') +parser.add_option('-o', '--out', action='callback', callback=set_add, + callback_args=tuple([print_rule_indices, 2]), + help='print outputs') +(opts, args) = parser.parse_args(); + +if len(print_rule_indices): + for rule in rules: + for index in sorted(print_rule_indices): + print rule[index], + print +else: + if not opts.product_dir: + parser.error("--product_dir option is required"); + v8_shell = os.path.join(opts.product_dir, 'v8_shell') + jsfilename = args[0] + for rule in rules: + arguments = [jsfilename, rule[0], rule[1], os.path.basename(rule[1])] + arguments = "arguments=" + json.dumps(arguments) + cmd = [v8_shell, '-e', arguments, jsfilename] + if opts.verbose or opts.impotent: + print cmd + if not opts.impotent: + sys.exit(subprocess.call(cmd, stdout=open(rule[2],'w+'))) |