From b7ac4108ef0a214983a485897e351ace12d045dd Mon Sep 17 00:00:00 2001 From: Mihai Sucan Date: Mon, 11 Oct 2010 14:37:23 -0700 Subject: [PATCH] Bug 583816 - Tab should move to the page when there's nothing to complete in the Web Console r=sdwilsh a=dietrich --- .../console/hudservice/HUDService.jsm | 26 ++++---- .../hudservice/tests/browser/Makefile.in | 1 + ...browser_webconsole_bug_583816_tab_focus.js | 63 +++++++++++++++++++ 3 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_583816_tab_focus.js diff --git a/toolkit/components/console/hudservice/HUDService.jsm b/toolkit/components/console/hudservice/HUDService.jsm index 061d237da3d..4efd41a8881 100644 --- a/toolkit/components/console/hudservice/HUDService.jsm +++ b/toolkit/components/console/hudservice/HUDService.jsm @@ -4286,20 +4286,19 @@ JSTerm.prototype = { // If there are more than one possible completion, pressing tab // means taking the next completion, shift_tab means taking // the previous completion. + var completionResult; if (aEvent.shiftKey) { - self.complete(self.COMPLETE_BACKWARD); + completionResult = self.complete(self.COMPLETE_BACKWARD); } else { - self.complete(self.COMPLETE_FORWARD); + completionResult = self.complete(self.COMPLETE_FORWARD); } - var bool = aEvent.cancelable; - if (bool) { + if (completionResult) { + if (aEvent.cancelable) { aEvent.preventDefault(); } - else { - // noop - } aEvent.target.focus(); + } break; case 8: // backspace key @@ -4410,7 +4409,8 @@ JSTerm.prototype = { * the inputNode.value is set to this value and the selection is set * from the current cursor position to the end of the completed text. * - * @returns void + * @returns boolean true if there existed a completion for the current input, + * or false otherwise. */ complete: function JSTF_complete(type) { @@ -4418,7 +4418,7 @@ JSTerm.prototype = { let inputValue = inputNode.value; // If the inputNode has no value, then don't try to complete on it. if (!inputValue) { - return; + return false; } let selStart = inputNode.selectionStart, selEnd = inputNode.selectionEnd; @@ -4432,7 +4432,7 @@ JSTerm.prototype = { // Only complete if the selection is at the end of the input. if (selEnd != inputValue.length) { this.lastCompletion = null; - return; + return false; } // Remove the selected text from the inputValue. @@ -4460,7 +4460,7 @@ JSTerm.prototype = { // Look up possible completion values. let completion = this.propertyProvider(this.sandbox.window, inputValue); if (!completion) { - return; + return false; } matches = completion.matches; matchIndexToUse = 0; @@ -4500,7 +4500,11 @@ JSTerm.prototype = { else { inputNode.setSelectionRange(selEnd, selEnd); } + + return completionStr ? true : false; } + + return false; } }; diff --git a/toolkit/components/console/hudservice/tests/browser/Makefile.in b/toolkit/components/console/hudservice/tests/browser/Makefile.in index ba96b1c9739..78cdbdde010 100644 --- a/toolkit/components/console/hudservice/tests/browser/Makefile.in +++ b/toolkit/components/console/hudservice/tests/browser/Makefile.in @@ -89,6 +89,7 @@ _BROWSER_TEST_FILES = \ browser_webconsole_bug_582201_duplicate_errors.js \ browser_webconsole_bug_580454_timestamp_l10n.js \ browser_webconsole_netlogging.js \ + browser_webconsole_bug_583816_tab_focus.js \ head.js \ $(NULL) diff --git a/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_583816_tab_focus.js b/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_583816_tab_focus.js new file mode 100644 index 00000000000..201ee12461a --- /dev/null +++ b/toolkit/components/console/hudservice/tests/browser/browser_webconsole_bug_583816_tab_focus.js @@ -0,0 +1,63 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* ***** BEGIN LICENSE BLOCK ***** + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + * + * Contributor(s): + * Mihai Șucan + * + * ***** END LICENSE BLOCK ***** */ + +const TEST_URI = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-console.html"; + +let inputNode; + +function tabLoad(aEvent) { + browser.removeEventListener(aEvent.type, arguments.callee, true); + + waitForFocus(function() { + openConsole(); + + let hudId = HUDService.getHudIdByWindow(content); + HUD = HUDService.hudWeakReferences[hudId].get(); + + let display = HUDService.getOutputNodeById(hudId); + inputNode = display.querySelector(".jsterm-input-node"); + + inputNode.focus(); + executeSoon(function() { + is(inputNode.getAttribute("focused"), "true", "inputNode is focused"); + HUD.jsterm.setInputValue("doc"); + inputNode.addEventListener("keyup", firstTab, false); + EventUtils.synthesizeKey("VK_TAB", {}); + }); + }, content); +} + +function firstTab(aEvent) { + this.removeEventListener(aEvent.type, arguments.callee, false); + + is(inputNode.getAttribute("focused"), "true", "inputNode is still focused"); + isnot(this.value, "doc", "input autocompleted"); + + HUD.jsterm.setInputValue("foobarbaz" + Date.now()); + + EventUtils.synthesizeKey("VK_TAB", {}); + + executeSoon(secondTab); +} + +function secondTab() { + isnot(inputNode.getAttribute("focused"), "true", + "inputNode is no longer focused"); + + HUD = inputNode = null; + HUDService.deactivateHUDForContext(gBrowser.selectedTab); + executeSoon(finish); +} + +function test() { + addTab(TEST_URI); + browser.addEventListener("load", tabLoad, true); +} +