browser_bug321000.js
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Firefox Browser Test Code.
17  *
18  * The Initial Developer of the Original Code is
19  * Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2007
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Ted Mielczarek <ted.mielczarek@gmail.com> (Original Author)
25  * Marco Bonardo <mak77@bonardo.net>
26  *
27  * Alternatively, the contents of this file may be used under the terms of
28  * either the GNU General Public License Version 2 or later (the "GPL"), or
29  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30  * in which case the provisions of the GPL or the LGPL are applicable instead
31  * of those above. If you wish to allow use of your version of this file only
32  * under the terms of either the GPL or the LGPL, and not to allow others to
33  * use your version of this file under the terms of the MPL, indicate your
34  * decision by deleting the provisions above and replace them with the notice
35  * and other provisions required by the GPL or the LGPL. If you do not delete
36  * the provisions above, a recipient may use your version of this file under
37  * the terms of any one of the MPL, the GPL or the LGPL.
38  *
39  * ***** END LICENSE BLOCK ***** */
40 
41 const Ci = Components.interfaces;
42 const Cc = Components.classes;
43 
44 const kTestString = " hello hello \n world\nworld ";
45 
46 var gTests = [
47 
48  { desc: "Urlbar strips newlines and surrounding whitespace",
49  element: gURLBar,
50  expected: kTestString.replace(/\s*\n\s*/g,'')
51  },
52 
53  { desc: "Searchbar replaces newlines with spaces",
54  element: document.getElementById('searchbar'),
55  expected: kTestString.replace('\n',' ','g')
56  },
57 
58 ];
59 
60 // Test for bug 23485 and bug 321000.
61 // Urlbar should strip newlines,
62 // search bar should replace newlines with spaces.
63 function test() {
65 
66  // Put a multi-line string in the clipboard.
67  info("About to put a string in clipboard");
68  Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper)
69  .copyString(kTestString);
70 
71  // Setting the clipboard value is an async OS operation, so we need to poll
72  // the clipboard for valid data before going on.
74 }
75 
76 var runCount = 0;
77 function poll_clipboard() {
78  // Poll for a maximum of 5s (each run happens after 100ms).
79  if (++runCount > 50) {
80  // Log the failure.
81  ok(false, "Timed out while polling clipboard for pasted data");
82  // Cleanup and interrupt the test.
83  finish_test();
84  return;
85  }
86 
87  info("Polling clipboard cycle " + runCount);
88  var clip = Cc["@mozilla.org/widget/clipboard;1"].
89  getService(Ci.nsIClipboard);
90  var trans = Cc["@mozilla.org/widget/transferable;1"].
91  createInstance(Ci.nsITransferable);
92  trans.addDataFlavor("text/unicode");
93  var str = new Object();
94  try {
95  // This code could throw if the clipboard is not set yet.
96  clip.getData(trans, clip.kGlobalClipboard);
97  trans.getTransferData("text/unicode", str, {});
98  str = str.value.QueryInterface(Ci.nsISupportsString);
99  }
100  catch(ex) {}
101 
102  if (kTestString == str) {
103  next_test();
104  }
105  else
107 }
108 
109 function next_test() {
110  if (gTests.length) {
111  var currentTest = gTests.shift();
112  test_paste(currentTest);
113  }
114  else {
115  // No more tests to run.
116  // Clear the clipboard, emptyClipboard would not clear the native one, so
117  // we are setting it to an empty string.
118  Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper)
119  .copyString("");
120  finish();
121  }
122 }
123 
124 function test_paste(aCurrentTest) {
125  var element = aCurrentTest.element;
126 
127  // Register input listener.
128  var inputListener = {
129  test: aCurrentTest,
130  handleEvent: function(event) {
131  var element = event.target;
132  element.removeEventListener("input", this, false);
133 
134  is(element.value, this.test.expected, this.test.desc);
135 
136  // Clear the field and go to next test.
137  element.value = "";
138  setTimeout(next_test, 0);
139  }
140  }
141  element.addEventListener("input", inputListener, false);
142 
143  // Focus the window.
144  window.focus();
145  gBrowser.selectedBrowser.focus();
146 
147  // Focus the element and wait for focus event.
148  info("About to focus " + element.id);
149  element.addEventListener("focus", function() {
150  element.removeEventListener("focus", arguments.callee, false);
151  executeSoon(function() {
152  // Pasting is async because the Accel+V codepath ends up going through
153  // DocumentViewerImpl::FireClipboardEvent.
154  info("Pasting into " + element.id);
155  EventUtils.synthesizeKey("v", { accelKey: true });
156  });
157  }, false);
158  element.focus();
159 }
function test_paste(aCurrentTest)
var event
function poll_clipboard()
sidebarFactory createInstance
Definition: nsSidebar.js:351
const Ci
getService(Ci.sbIFaceplateManager)
let window
const Cc
function next_test()
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
var gTests
function test()
function finish_test()
var expected
return!aWindow arguments!aWindow arguments[0]
var runCount
const kTestString