From f3e510837d0f3c27da1a7c6c1140cc6c045c2e53 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Wed, 2 Feb 2011 22:01:49 -0500 Subject: [PATCH] Manually run Ex completers rather than pushing them through the command line machinery. It's orders of magnitude faster, on the one hand, but also runs the display machinery for all results, which may catch more errors. Errors in the command line completion machinery should be tested elsewhere. Fix some sidebar test bugs. Move dactyl.js and utils.js files out of sandboxes and into modules so we can pass XML objects in and out of them. --HG-- rename : common/tests/functional/dactyl.js => common/tests/functional/dactyl.jsm rename : common/tests/functional/utils.js => common/tests/functional/utils.jsm --- common/modules/base.jsm | 4 +- .../functional/{dactyl.js => dactyl.jsm} | 99 ++++++++++++------- common/tests/functional/testCommands.js | 23 ++--- common/tests/functional/testEchoCommands.js | 2 +- common/tests/functional/testFindCommands.js | 2 +- common/tests/functional/testHelpCommands.js | 5 +- common/tests/functional/testShellCommands.js | 2 +- common/tests/functional/testVersionCommand.js | 2 +- common/tests/functional/utils.js | 49 +-------- common/tests/functional/utils.jsm | 48 +++++++++ 10 files changed, 132 insertions(+), 104 deletions(-) rename common/tests/functional/{dactyl.js => dactyl.jsm} (84%) create mode 100644 common/tests/functional/utils.jsm diff --git a/common/modules/base.jsm b/common/modules/base.jsm index c456a9d9..92f8655a 100644 --- a/common/modules/base.jsm +++ b/common/modules/base.jsm @@ -177,7 +177,9 @@ function require(obj, name, from) { if (arguments.length === 1) [obj, name] = [{}, obj]; - defineModule.loadLog.push((from || "require") + ": loading " + name + " into " + obj.NAME); + if (!loaded[name]) + defineModule.loadLog.push((from || "require") + ": loading " + name + (obj.NAME ? " into " + obj.NAME : "")); + JSMLoader.load(name + ".jsm", obj); return obj; } diff --git a/common/tests/functional/dactyl.js b/common/tests/functional/dactyl.jsm similarity index 84% rename from common/tests/functional/dactyl.js rename to common/tests/functional/dactyl.jsm index 0ece4c54..fe23a514 100644 --- a/common/tests/functional/dactyl.js +++ b/common/tests/functional/dactyl.jsm @@ -1,6 +1,16 @@ +function module(uri) { + if (!/^[a-z-]+:/.exec(uri)) + uri = /([^ ]+\/)[^\/]+$/.exec(Components.stack.filename)[1] + uri + ".jsm"; -var utils = require("utils"); -const { module, NS } = utils; + let obj = {}; + Components.utils.import(uri, obj); + return obj; +} + +var EXPORTED_SYMBOLS = ["Controller"]; + +var utils = module("utils"); +const { NS } = utils; var elementslib = module("resource://mozmill/modules/elementslib.js"); var frame = module("resource://mozmill/modules/frame.js"); @@ -175,7 +185,7 @@ Controller.prototype = { errorCount = this.modules.util.errorCount; try { - func.apply(self || this, args || []); + var returnVal = func.apply(self || this, args || []); } catch (e) { this.modules.util.reportError(e); @@ -193,9 +203,11 @@ Controller.prototype = { var errors = this.modules.util.errors.slice(errorCount - this.modules.util.errorCount) .join("\n"); - return utils.assertEqual('dactyl.assertNoErrors', - errorCount, this.modules.util.errorCount, - "Errors were reported during the execution of this test" + msg + "\n" + errors); + var res = utils.assertEqual('dactyl.assertNoErrors', + errorCount, this.modules.util.errorCount, + "Errors were reported during the execution of this test" + msg + "\n" + errors); + + return returnVal === undefined ? res : returnVal; }, /** @@ -377,39 +389,58 @@ Controller.prototype = { * * @param {string} cmd The Ex command string as entered on the command * line. + * @param {boolean} longWay Whether to test the completion by + * entering it into the command line and dispatching a key + * press. */ - runExCompletion: wrapAssertNoErrors(function (cmd) { - this.setExMode(); - - utils.assertEqual("dactyl.assertCommandLineFocused", - this.elements.commandInput, - this.elements.focused, - "Running Ex Completion: The command line is not focused"); - + runExCompletion: wrapAssertNoErrors(function (cmd, longWay) { // dump("runExCompletion " + cmd + "\n"); - if (true) { - let input = this.elements.commandInput; - input.value = cmd; + if (!longWay) { + var context = this.modules.CompletionContext(cmd); + context.tabPressed = true; + context.fork("ex", 0, this.modules.completion, "ex"); - var event = input.ownerDocument.createEvent("Events"); - event.initEvent("change", true, false); - input.dispatchEvent(event); + utils.assert("dactyl.runCompletions", context.wait(5000), + "Completion failed: " + cmd.quote()); + + for (var [, ctxt] in Iterator(context.contextList)) + for (var [, item] in Iterator(ctxt.items)) + ctxt.createRow(item); + + return context; } else { - this.controller.type(null, cmd); + this.setExMode(); - utils.assertEqual("dactyl.runExCompletion", cmd, - this.elements.commandInput.editor.rootElement.firstChild.textContent, - "Command line does not have the expected value: " + cmd); + utils.assertEqual("dactyl.assertCommandLineFocused", + this.elements.commandInput, + this.elements.focused, + "Running Ex Completion: The command line is not focused"); + + if (true) { + let input = this.elements.commandInput; + input.value = cmd; + + var event = input.ownerDocument.createEvent("Events"); + event.initEvent("change", true, false); + input.dispatchEvent(event); + } + else { + this.controller.type(null, cmd); + + utils.assertEqual("dactyl.runExCompletion", cmd, + this.elements.commandInput.editor.rootElement.firstChild.textContent, + "Command line does not have the expected value: " + cmd); + } + + this.controller.keypress(null, "VK_TAB", {}); + + // XXX + if (this.modules.commandline._tabTimer) + this.modules.commandline._tabTimer.flush(); + else if (this.modules.commandline.commandSession && this.modules.commandline.commandSession.completions) + this.modules.commandline.commandSession.completions.tabTimer.flush(); } - - this.controller.keypress(null, "VK_TAB", {}); - - // XXX - if (this.modules.commandline._tabTimer) - this.modules.commandline._tabTimer.flush(); - else if (this.modules.commandline.commandSession && this.modules.commandline.commandSession.completions) - this.modules.commandline.commandSession.completions.tabTimer.flush(); }), /** @@ -461,6 +492,4 @@ Controller.prototype = { get applicationName() this.modules.config.appName // XXX }; -exports.Controller = Controller; - -// vim: sw=4 ts=8 et: +// vim: sw=4 ts=8 et ft=javascript: diff --git a/common/tests/functional/testCommands.js b/common/tests/functional/testCommands.js index 430aad2c..48fb4d76 100644 --- a/common/tests/functional/testCommands.js +++ b/common/tests/functional/testCommands.js @@ -1,9 +1,9 @@ // Runs a slew of generic command tests -var dactyllib = require("dactyl"); var utils = require("utils"); - const { module } = utils; + +var dactyllib = module("dactyl"); var jumlib = module("resource://mozmill/modules/jum.js"); var setupModule = function (module) { @@ -29,8 +29,9 @@ function hasntNullItems(context) hasItems(context) && !context.allItems.items.some(function ({ text, description }) [text, description].some(function (text) /^\[object/.test(text))); function sidebarState(state) - function () typeof state == "string" ? $("#sidebar-title").value == state - : $("#sidebar-box").hidden == state; + function () utils.assertEqual("testCommand.sidebarState", state, + typeof state == "string" ? $("#sidebar-title").value + : !$("#sidebar-box").hidden); var tests = { "!": { @@ -503,7 +504,7 @@ var tests = { "Preferences"] .map(this.test)) .concat([ - ["!", sidebarState("Preferences")], + ["Preferences", sidebarState("Preferences")], ["!", sidebarState(false)] ]), completions: [ @@ -728,17 +729,9 @@ for (var val in Iterator(tests)) (function ([command, paramsList]) { dactyl.assertNoErrorMessages(function () { dump("COMPL: " + cmd + "\n"); - dactyl.runExCompletion(cmd); - if (test) { - /* Freezes. :( - var context = dactyl.modules.commandline.commandSession.completions.context; - */ - var context = dactyl.modules.CompletionContext(cmd); - context.tabPressed = true; - context.fork("ex", 0, dactyl.modules.completion, "ex"); - jumlib.assert(context.wait(5000), "Completion failed: " + cmd.quote()); + var context = dactyl.runExCompletion(cmd); + if (context && test) jumlib.assert(test(context), "Completion tests failed: " + cmd.quote() + " " + test); - } }); }); }); diff --git a/common/tests/functional/testEchoCommands.js b/common/tests/functional/testEchoCommands.js index d46694aa..39d4f08c 100644 --- a/common/tests/functional/testEchoCommands.js +++ b/common/tests/functional/testEchoCommands.js @@ -1,4 +1,4 @@ -var dactyllib = require("dactyl"); +var dactyllib = require("utils").module("dactyl"); var setupModule = function (module) { controller = mozmill.getBrowserController(); diff --git a/common/tests/functional/testFindCommands.js b/common/tests/functional/testFindCommands.js index f567cdfa..b1ac821c 100644 --- a/common/tests/functional/testFindCommands.js +++ b/common/tests/functional/testFindCommands.js @@ -1,4 +1,4 @@ -var dactyllib = require("dactyl"); +var dactyllib = require("utils").module("dactyl"); const FIND_TEST_PAGE = collector.addHttpResource("./data/") + "find.html"; diff --git a/common/tests/functional/testHelpCommands.js b/common/tests/functional/testHelpCommands.js index 898867ba..cf27044d 100644 --- a/common/tests/functional/testHelpCommands.js +++ b/common/tests/functional/testHelpCommands.js @@ -1,5 +1,6 @@ -var jumlib = {}; Components.utils.import("resource://mozmill/modules/jum.js", jumlib); -var dactyllib = require("dactyl"); +const { module } = require("utils"); +var jumlib = module("resource://mozmill/modules/jum.js"); +var dactyllib = module("dactyl"); var setupModule = function (module) { controller = mozmill.getBrowserController(); diff --git a/common/tests/functional/testShellCommands.js b/common/tests/functional/testShellCommands.js index db8a48e3..de4eb2b1 100644 --- a/common/tests/functional/testShellCommands.js +++ b/common/tests/functional/testShellCommands.js @@ -1,4 +1,4 @@ -var dactyllib = require("dactyl"); +var dactyllib = require("utils").module("dactyl"); var setupModule = function (module) { controller = mozmill.getBrowserController(); diff --git a/common/tests/functional/testVersionCommand.js b/common/tests/functional/testVersionCommand.js index 12bf4b8e..78a2f021 100644 --- a/common/tests/functional/testVersionCommand.js +++ b/common/tests/functional/testVersionCommand.js @@ -1,4 +1,4 @@ -var dactyllib = require("dactyl"); +var dactyllib = require("utils").module("dactyl"); var setupModule = function (module) { controller = mozmill.getBrowserController(); diff --git a/common/tests/functional/utils.js b/common/tests/functional/utils.js index 2de22ac9..03545537 100644 --- a/common/tests/functional/utils.js +++ b/common/tests/functional/utils.js @@ -1,50 +1,5 @@ -function module(uri) { - let obj = {}; - Components.utils.import(uri, obj); - return obj; -} -var elementslib = module("resource://mozmill/modules/elementslib.js"); -var frame = module("resource://mozmill/modules/frame.js"); -var jumlib = module("resource://mozmill/modules/jum.js"); - -function toJSON(val) { - if (typeof val == "function") - return val.toSource(); - if (val instanceof Ci.nsIDOMNode || val instanceof Ci.nsIDOMWindow) - return { DOMNode: String(val) }; - return val; -} - -function test(val, params) { - frame.events[val ? "pass" : "fail"](params); - return val; -} - -for (var [k, v] in Iterator({ - - NS: Namespace("dactyl", "http://vimperator.org/namespaces/liberator"), - - module: module, - - toJSON: toJSON, - - test: test, - - assert: function (funcName, value, comment) - test(value, { - function: funcName, - value: toJSON(value), - comment: toJSON(comment) - }), - - assertEqual: function (funcName, want, got, comment) - test(want == got, { - function: funcName, - want: toJSON(want), got: toJSON(got), - comment: toJSON(comment) - }) -})) - exports[k] = v; +// Work around these horrendous Sandbox issues. +Components.utils.import(/([^ ]+\/)[^\/]+$/.exec(Components.stack.filename)[1] + "utils.jsm", exports); // vim: sw=4 ts=8 et: diff --git a/common/tests/functional/utils.jsm b/common/tests/functional/utils.jsm new file mode 100644 index 00000000..75405085 --- /dev/null +++ b/common/tests/functional/utils.jsm @@ -0,0 +1,48 @@ + +var EXPORTED_SYMBOLS = ["NS", "assert", "assertEqual", "module", "test", "toJSON"]; + +const Ci = Components.interfaces; + +function module(uri) { + if (!/^[a-z-]+:/.exec(uri)) + uri = /([^ ]+\/)[^\/]+$/.exec(Components.stack.filename)[1] + uri + ".jsm"; + + let obj = {}; + Components.utils.import(uri, obj); + return obj; +} + +var elementslib = module("resource://mozmill/modules/elementslib.js"); +var frame = module("resource://mozmill/modules/frame.js"); +var jumlib = module("resource://mozmill/modules/jum.js"); + +function toJSON(val) { + if (typeof val == "function") + return val.toSource(); + if (val instanceof Ci.nsIDOMNode || val instanceof Ci.nsIDOMWindow) + return { DOMNode: String(val) }; + return val; +} + +function test(val, params) { + frame.events[val ? "pass" : "fail"](params); + return val; +} + +var NS = Namespace("dactyl", "http://vimperator.org/namespaces/liberator"); + +function assert(funcName, value, comment) + test(value, { + function: funcName, + value: toJSON(value), + comment: toJSON(comment) + }); + +function assertEqual(funcName, want, got, comment) + test(want == got, { + function: funcName, + want: toJSON(want), got: toJSON(got), + comment: toJSON(comment) + }); + +// vim: sw=4 ts=8 et ft=javascript: