browser_sidebarpanels_click.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 code.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2009
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or 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 // This test makes sure that the items in the bookmarks and history sidebar
39 // panels are clickable in both LTR and RTL modes.
40 
41 function test() {
42  const BOOKMARKS_SIDEBAR_ID = "viewBookmarksSidebar";
43  const BOOKMARKS_SIDEBAR_TREE_ID = "bookmarks-view";
44  const HISTORY_SIDEBAR_ID = "viewHistorySidebar";
45  const HISTORY_SIDEBAR_TREE_ID = "historyTree";
46 
47  // Initialization.
48  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
49  getService(Ci.nsIWindowWatcher);
50  let bs = PlacesUtils.bookmarks;
51  let hs = PlacesUtils.history;
52  let sidebarBox = document.getElementById("sidebar-box");
53  let sidebar = document.getElementById("sidebar");
55 
56  // If a sidebar is already open, close it.
57  if (!sidebarBox.hidden) {
58  info("Unexpected sidebar found - a previous test failed to cleanup correctly");
59  toggleSidebar();
60  }
61 
62  const TEST_URL = "javascript:alert(\"test\");";
63 
64  let tests = [];
65  tests.push({
66  _itemID: null,
67  init: function() {
68  // Add a bookmark to the Unfiled Bookmarks folder.
69  this._itemID = bs.insertBookmark(bs.unfiledBookmarksFolder,
70  PlacesUtils._uri(TEST_URL),
71  bs.DEFAULT_INDEX, "test");
72  },
73  prepare: function() {
74  },
75  selectNode: function(tree) {
76  tree.selectItems([this._itemID]);
77  },
78  cleanup: function() {
79  bs.removeFolderChildren(bs.unfiledBookmarksFolder);
80  },
81  sidebarName: BOOKMARKS_SIDEBAR_ID,
82  treeName: BOOKMARKS_SIDEBAR_TREE_ID,
83  desc: "Bookmarks sidebar test"
84  });
85 
86  tests.push({
87  init: function() {
88  // Add a history entry.
89  this.cleanup();
90  hs.addVisit(PlacesUtils._uri(TEST_URL), Date.now() * 1000,
91  null, hs.TRANSITION_TYPED, false, 0);
92  },
93  prepare: function() {
94  sidebar.contentDocument.getElementById("byvisited").doCommand();
95  },
96  selectNode: function(tree) {
97  tree.selectNode(tree.view.nodeForTreeIndex(0));
98  is(tree.selectedNode.uri, TEST_URL, "The correct visit has been selected");
99  is(tree.selectedNode.itemId, -1, "The selected node is not bookmarked");
100  },
101  cleanup: function() {
102  hs.QueryInterface(Ci.nsIBrowserHistory)
103  .removeAllPages();
104  },
105  sidebarName: HISTORY_SIDEBAR_ID,
106  treeName: HISTORY_SIDEBAR_TREE_ID,
107  desc: "History sidebar test"
108  });
109 
110  let currentTest;
111 
112  function testPlacesPanel(preFunc, postFunc) {
113  currentTest.init();
114 
115  sidebar.addEventListener("load", function() {
116  sidebar.removeEventListener("load", arguments.callee, true);
117 
118  let doc = sidebar.contentDocument;
119  let tree = doc.getElementById(currentTest.treeName);
120  let tbo = tree.treeBoxObject;
121 
122  executeSoon(function() {
123  currentTest.prepare();
124  if (preFunc)
125  preFunc();
126 
127  let observer = {
128  observe: function(aSubject, aTopic, aData) {
129  if (aTopic === "domwindowopened") {
130  ww.unregisterNotification(this);
131  let alertDialog = aSubject.QueryInterface(Ci.nsIDOMWindow);
132  alertDialog.addEventListener("load", function() {
133  alertDialog.removeEventListener("load", arguments.callee, false);
134  info("alert dialog observed as expected");
135  executeSoon(function() {
136  alertDialog.close();
137  toggleSidebar(currentTest.sidebarName);
138  currentTest.cleanup();
139  postFunc();
140  });
141  }, false);
142  }
143  }
144  };
145  ww.registerNotification(observer);
146 
147  // Select the inserted places item.
148  currentTest.selectNode(tree);
149  is(tbo.view.selection.count, 1,
150  "The test node should be successfully selected");
151  // Get its row ID.
152  let min = {}, max = {};
153  tbo.view.selection.getRangeAt(0, min, max);
154  let rowID = min.value;
155  tbo.ensureRowIsVisible(rowID);
156 
157  // Calculate the click coordinates.
158  let x = {}, y = {}, width = {}, height = {};
159  tbo.getCoordsForCellItem(rowID, tree.columns[0], "text",
160  x, y, width, height);
161  x = x.value + width.value / 2;
162  y = y.value + height.value / 2;
163  // Simulate the click.
164  EventUtils.synthesizeMouse(tree.body, x, y, {}, doc.defaultView);
165  // Now, wait for the domwindowopened observer to catch the alert dialog.
166  // If something goes wrong, the test will time out at this stage.
167  // Note that for the history sidebar, the URL itself is not opened,
168  // and Places will show the load-js-data-url-error prompt as an alert
169  // box, which means that the click actually worked, so it's good enough
170  // for the purpose of this test.
171  });
172  }, true);
173  toggleSidebar(currentTest.sidebarName);
174  }
175 
176  function changeSidebarDirection(aDirection) {
177  document.getElementById("sidebar")
178  .contentDocument
179  .documentElement
180  .style.direction = aDirection;
181  }
182 
183  function runNextTest() {
184  if (tests.length == 0)
185  finish();
186  else {
187  currentTest = tests.shift();
188  testPlacesPanel(function() {
189  changeSidebarDirection("ltr");
190  info("Running " + currentTest.desc + " in LTR mode");
191  }, function() {
192  executeSoon(function() {
193  testPlacesPanel(function() {
194  // Run the test in RTL mode.
195  changeSidebarDirection("rtl");
196  info("Running " + currentTest.desc + " in RTL mode");
197  }, function() {
198  executeSoon(runNextTest);
199  });
200  });
201  });
202  }
203  }
204 
205  runNextTest();
206 }
const Cc
function test()
function doc() browser.contentDocument
getService(Ci.sbIFaceplateManager)
function width(ele) rect(ele).width
_window init
Definition: FeedWriter.js:1144
waitForExplicitFinish()
return null
Definition: FeedWriter.js:1143
_updateDatepicker height
function cleanup()
return!aWindow arguments!aWindow arguments[0]
const Ci
#define min(a, b)
function runNextTest()
let observer
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe