browser_drag_bookmarks_on_toolbar.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Places test.
15  *
16  * The Initial Developer of the Original Code is
17  * Mozilla Corporation
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Marco Bonardo <mak77@bonardo.net>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 const TEST_URL = "http://www.mozilla.org";
39 const TEST_TITLE = "example_title";
40 
41 var gBookmarksToolbar = window.document.getElementById("bookmarksBarContent");
42 var dragDirections = { LEFT: 0, UP: 1, RIGHT: 2, DOWN: 3 };
43 
61 function synthesizeDragWithDirection(aElement, aExpectedDragData, aDirection) {
62  var trapped = false;
63 
64  // Dragstart listener function.
65  var trapDrag = function(event) {
66  trapped = true;
67  var dataTransfer = event.dataTransfer;
68  is(dataTransfer.mozItemCount, aExpectedDragData.length,
69  "Number of dragged items should be the same.");
70 
71  for (var t = 0; t < dataTransfer.mozItemCount; t++) {
72  var types = dataTransfer.mozTypesAt(t);
73  var expecteditem = aExpectedDragData[t];
74  is(types.length, expecteditem.length,
75  "Number of flavors for item " + t + " should be the same.");
76 
77  for (var f = 0; f < types.length; f++) {
78  is(types[f], expecteditem[f].substring(0, types[f].length),
79  "Flavor " + types[f] + " for item " + t + " should be the same.");
80  is(dataTransfer.mozGetDataAt(types[f], t),
81  expecteditem[f].substring(types[f].length + 2),
82  "Contents for item " + t + " with flavor " + types[f] + " should be the same.");
83  }
84  }
85 
86  if (!aExpectedDragData.length)
87  ok(event.getPreventDefault(), "Drag has been canceled.");
88 
89  event.preventDefault();
90  event.stopPropagation();
91  }
92 
93  var prevent = function(aEvent) {aEvent.preventDefault();}
94 
95  var xIncrement = 0;
96  var yIncrement = 0;
97 
98  switch (aDirection) {
99  case dragDirections.LEFT:
100  xIncrement = -1;
101  break;
102  case dragDirections.RIGHT:
103  xIncrement = +1;
104  break;
105  case dragDirections.UP:
106  yIncrement = -1;
107  break;
108  case dragDirections.DOWN:
109  yIncrement = +1;
110  break;
111  }
112 
113  var rect = aElement.getBoundingClientRect();
114  var startingPoint = { x: (rect.right - rect.left)/2,
115  y: (rect.bottom - rect.top)/2 };
116 
117  EventUtils.synthesizeMouse(aElement,
118  startingPoint.x,
119  startingPoint.y,
120  { type: "mousedown" });
121  EventUtils.synthesizeMouse(aElement,
122  startingPoint.x + xIncrement * 1,
123  startingPoint.y + yIncrement * 1,
124  { type: "mousemove" });
125  gBookmarksToolbar.addEventListener("dragstart", trapDrag, false);
126  EventUtils.synthesizeMouse(aElement,
127  startingPoint.x + xIncrement * 9,
128  startingPoint.y + yIncrement * 9,
129  { type: "mousemove" });
130  ok(trapped, "A dragstart event has been trapped.");
131  gBookmarksToolbar.removeEventListener("dragstart", trapDrag, false);
132 
133  // This is likely to cause a click event, and, in case we are dragging a
134  // bookmark, an unwanted page visit. Prevent the click event.
135  aElement.addEventListener("click", prevent, false);
136  EventUtils.synthesizeMouse(aElement,
137  startingPoint.x + xIncrement * 9,
138  startingPoint.y + yIncrement * 9,
139  { type: "mouseup" });
140  aElement.removeEventListener("click", prevent, false);
141 
142  // Cleanup eventually opened menus.
143  if (aElement.localName == "menu" && aElement.open)
144  aElement.open = false;
145 }
146 
147 function getToolbarNodeForItemId(aItemId) {
148  var children = gBookmarksToolbar.childNodes;
149  var node = null;
150  for (var i = 0; i < children.length; i++) {
151  if (aItemId == children[i].node.itemId) {
152  node = children[i];
153  break;
154  }
155  }
156  return node;
157 }
158 
160  var wrappedNode = [];
161  var flavors = ["text/x-moz-place",
162  "text/x-moz-url",
163  "text/plain",
164  "text/html"];
165 
166  flavors.forEach(function(aFlavor) {
167  var wrappedFlavor = aFlavor + ": " +
168  PlacesUtils.wrapNode(aNode, aFlavor);
169  wrappedNode.push(wrappedFlavor);
170  });
171 
172  return [wrappedNode];
173 }
174 
175 var gTests = [
176 
177 //------------------------------------------------------------------------------
178 
179  {
180  desc: "Drag a folder on toolbar",
181  run: function() {
182  // Create a test folder to be dragged.
183  var folderId = PlacesUtils.bookmarks
184  .createFolder(PlacesUtils.toolbarFolderId,
185  TEST_TITLE,
186  PlacesUtils.bookmarks.DEFAULT_INDEX);
187  var element = getToolbarNodeForItemId(folderId);
188  isnot(element, null, "Found node on toolbar");
189 
190  isnot(element.node, null, "Toolbar node has an associated Places node.");
191  var expectedData = getExpectedDataForPlacesNode(element.node);
192 
193  ok(true, "Dragging left");
194  synthesizeDragWithDirection(element, expectedData, dragDirections.LEFT);
195  ok(true, "Dragging right");
196  synthesizeDragWithDirection(element, expectedData, dragDirections.RIGHT);
197  ok(true, "Dragging up");
198  synthesizeDragWithDirection(element, expectedData, dragDirections.UP);
199  ok(true, "Dragging down");
200  synthesizeDragWithDirection(element, new Array(), dragDirections.DOWN);
201 
202  // Cleanup.
203  PlacesUtils.bookmarks.removeItem(folderId);
204  }
205  },
206 
207 //------------------------------------------------------------------------------
208 
209  {
210  desc: "Drag a bookmark on toolbar",
211  run: function() {
212  // Create a test bookmark to be dragged.
213  var itemId = PlacesUtils.bookmarks
214  .insertBookmark(PlacesUtils.toolbarFolderId,
215  PlacesUtils._uri(TEST_URL),
216  PlacesUtils.bookmarks.DEFAULT_INDEX,
217  TEST_TITLE);
218  var element = getToolbarNodeForItemId(itemId);
219  isnot(element, null, "Found node on toolbar");
220 
221  isnot(element.node, null, "Toolbar node has an associated Places node.");
222  var expectedData = getExpectedDataForPlacesNode(element.node);
223 
224  ok(true, "Dragging left");
225  synthesizeDragWithDirection(element, expectedData, dragDirections.LEFT);
226  ok(true, "Dragging right");
227  synthesizeDragWithDirection(element, expectedData, dragDirections.RIGHT);
228  ok(true, "Dragging up");
229  synthesizeDragWithDirection(element, expectedData, dragDirections.UP);
230  ok(true, "Dragging down");
231  synthesizeDragWithDirection(element, expectedData, dragDirections.DOWN);
232 
233  // Cleanup.
234  PlacesUtils.bookmarks.removeItem(itemId);
235  }
236  },
237 ];
238 
239 function nextTest() {
240  if (gTests.length) {
241  var test = gTests.shift();
242  info("Start of test: " + test.desc);
243  test.run();
244 
245  setTimeout(nextTest, 0);
246  }
247  else
248  finish();
249 }
250 
251 function test() {
253 
254  nextTest();
255 }
256 
var event
function getExpectedDataForPlacesNode(aNode)
const char * types
let window
var t
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
function rect(ele) ele.getBoundingClientRect()
return null
Definition: FeedWriter.js:1143
function getToolbarNodeForItemId(aItemId)
let node
function synthesizeDragWithDirection(aElement, aExpectedDragData, aDirection)
_getSelectedPageStyle s i