michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const kTestString = " hello hello \n world\nworld "; michael@0: michael@0: var gTests = [ michael@0: michael@0: { desc: "Urlbar strips newlines and surrounding whitespace", michael@0: element: gURLBar, michael@0: expected: kTestString.replace(/\s*\n\s*/g,'') michael@0: }, michael@0: michael@0: { desc: "Searchbar replaces newlines with spaces", michael@0: element: document.getElementById('searchbar'), michael@0: expected: kTestString.replace('\n',' ','g') michael@0: }, michael@0: michael@0: ]; michael@0: michael@0: // Test for bug 23485 and bug 321000. michael@0: // Urlbar should strip newlines, michael@0: // search bar should replace newlines with spaces. michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let cbHelper = Cc["@mozilla.org/widget/clipboardhelper;1"]. michael@0: getService(Ci.nsIClipboardHelper); michael@0: michael@0: // Put a multi-line string in the clipboard. michael@0: // Setting the clipboard value is an async OS operation, so we need to poll michael@0: // the clipboard for valid data before going on. michael@0: waitForClipboard(kTestString, function() { cbHelper.copyString(kTestString, document); }, michael@0: next_test, finish); michael@0: } michael@0: michael@0: function next_test() { michael@0: if (gTests.length) michael@0: test_paste(gTests.shift()); michael@0: else michael@0: finish(); michael@0: } michael@0: michael@0: function test_paste(aCurrentTest) { michael@0: var element = aCurrentTest.element; michael@0: michael@0: // Register input listener. michael@0: var inputListener = { michael@0: test: aCurrentTest, michael@0: handleEvent: function(event) { michael@0: element.removeEventListener(event.type, this, false); michael@0: michael@0: is(element.value, this.test.expected, this.test.desc); michael@0: michael@0: // Clear the field and go to next test. michael@0: element.value = ""; michael@0: setTimeout(next_test, 0); michael@0: } michael@0: } michael@0: element.addEventListener("input", inputListener, false); michael@0: michael@0: // Focus the window. michael@0: window.focus(); michael@0: gBrowser.selectedBrowser.focus(); michael@0: michael@0: // Focus the element and wait for focus event. michael@0: info("About to focus " + element.id); michael@0: element.addEventListener("focus", function() { michael@0: element.removeEventListener("focus", arguments.callee, false); michael@0: executeSoon(function() { michael@0: // Pasting is async because the Accel+V codepath ends up going through michael@0: // nsDocumentViewer::FireClipboardEvent. michael@0: info("Pasting into " + element.id); michael@0: EventUtils.synthesizeKey("v", { accelKey: true }); michael@0: }); michael@0: }, false); michael@0: element.focus(); michael@0: }