browser_bug321000.js (2396B)
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 const kTestString = " hello hello \n world\nworld "; 8 9 var gTests = [ 10 { 11 desc: "Urlbar strips newlines and surrounding whitespace", 12 element: gURLBar, 13 expected: kTestString.replace(/\s*\n\s*/g, ""), 14 }, 15 16 { 17 desc: "Searchbar replaces newlines with spaces", 18 element: document.getElementById("searchbar"), 19 expected: kTestString.replace(/\n/g, " "), 20 }, 21 ]; 22 23 // Test for bug 23485 and bug 321000. 24 // Urlbar should strip newlines, 25 // search bar should replace newlines with spaces. 26 function test() { 27 waitForExplicitFinish(); 28 29 let cbHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService( 30 Ci.nsIClipboardHelper 31 ); 32 33 // Put a multi-line string in the clipboard. 34 // Setting the clipboard value is an async OS operation, so we need to poll 35 // the clipboard for valid data before going on. 36 waitForClipboard( 37 kTestString, 38 function () { 39 cbHelper.copyString(kTestString); 40 }, 41 next_test, 42 finish 43 ); 44 } 45 46 function next_test() { 47 if (gTests.length) { 48 test_paste(gTests.shift()); 49 } else { 50 finish(); 51 } 52 } 53 54 function test_paste(aCurrentTest) { 55 var element = aCurrentTest.element; 56 57 // Register input listener. 58 var inputListener = { 59 test: aCurrentTest, 60 handleEvent(event) { 61 element.removeEventListener(event.type, this); 62 63 is(element.value, this.test.expected, this.test.desc); 64 65 // Clear the field and go to next test. 66 element.value = ""; 67 setTimeout(next_test, 0); 68 }, 69 }; 70 element.addEventListener("input", inputListener); 71 72 // Focus the window. 73 window.focus(); 74 gBrowser.selectedBrowser.focus(); 75 76 // Focus the element and wait for focus event. 77 info("About to focus " + element.id); 78 element.addEventListener( 79 "focus", 80 function () { 81 executeSoon(function () { 82 // Pasting is async because the Accel+V codepath ends up going through 83 // nsDocumentViewer::FireClipboardEvent. 84 info("Pasting into " + element.id); 85 EventUtils.synthesizeKey("v", { accelKey: true }); 86 }); 87 }, 88 { once: true } 89 ); 90 element.focus(); 91 }