browser_sort_in_library.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
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 Places test code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Drew Willcoxon <adw@mozilla.com> (Original Author)
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
53 // Two properties of nsINavHistoryResult control the sort of the tree:
54 // sortingMode and sortingAnnotation. sortingMode's value is one of the
55 // nsINavHistoryQueryOptions.SORT_BY_* constants. sortingAnnotation is the
56 // annotation used to sort for SORT_BY_ANNOTATION_* mode.
57 //
58 // This lookup table maps the possible values of anonid's of the treecols to
59 // objects that represent the treecols' correct state after the user sorts the
60 // previously unsorted tree by selecting a column from the Views > Sort menu.
61 // sortingMode is constructed from the key and dir properties (i.e.,
62 // SORT_BY_<key>_<dir>) and sortingAnnotation is checked against anno. anno
63 // may be undefined if key is not "ANNOTATION".
65  title: { key: "TITLE", dir: "ASCENDING" },
66  tags: { key: "TAGS", dir: "ASCENDING" },
67  url: { key: "URI", dir: "ASCENDING" },
68  date: { key: "DATE", dir: "DESCENDING" },
69  visitCount: { key: "VISITCOUNT", dir: "DESCENDING" },
70  keyword: { key: "KEYWORD", dir: "ASCENDING" },
71  dateAdded: { key: "DATEADDED", dir: "DESCENDING" },
72  lastModified: { key: "LASTMODIFIED", dir: "DESCENDING" },
73  description: { key: "ANNOTATION",
74  dir: "ASCENDING",
75  anno: "bookmarkProperties/description" }
76 };
77 
78 // This is the column that's sorted if one is not specified and the tree is
79 // currently unsorted. Set it to a key substring in the name of one of the
80 // nsINavHistoryQueryOptions.SORT_BY_* constants, e.g., "TITLE", "URI".
81 // Method ViewMenu.setSortColumn in browser/components/places/content/places.js
82 // determines this value.
83 const DEFAULT_SORT_KEY = "TITLE";
84 
85 // Part of the test is checking that sorts stick, so each time we sort we need
86 // to remember it.
89 
91 
103 function checkSort(aTree, aSortingMode, aSortingAnno) {
104  // The placeContent tree's sort is determined by the nsINavHistoryResult it
105  // stores. Get it and check that the sort is what the caller expects.
106  let res = aTree.getResult();
107  isnot(res, null,
108  "sanity check: placeContent.getResult() should not return null");
109 
110  // Check sortingMode.
111  is(res.sortingMode, aSortingMode,
112  "column should now have sortingMode " + aSortingMode);
113 
114  // Check sortingAnnotation, but only if sortingMode is ANNOTATION.
115  if ([Ci.nsINavHistoryQueryOptions.SORT_BY_ANNOTATION_ASCENDING,
116  Ci.nsINavHistoryQueryOptions.SORT_BY_ANNOTATION_DESCENDING].
117  indexOf(aSortingMode) >= 0) {
118  is(res.sortingAnnotation, aSortingAnno,
119  "column should now have sorting annotation " + aSortingAnno);
120  }
121 }
122 
140 function setSort(aOrganizerWin, aTree, aUnsortFirst, aShouldFail, aCol, aDir) {
141  if (aUnsortFirst) {
142  aOrganizerWin.ViewMenu.setSortColumn();
143  checkSort(aTree, Ci.nsINavHistoryQueryOptions.SORT_BY_NONE, "");
144 
145  // Remember the sort key and direction.
146  prevSortKey = null;
147  prevSortDir = null;
148  }
149 
150  let failed = false;
151  try {
152  aOrganizerWin.ViewMenu.setSortColumn(aCol, aDir);
153 
154  // Remember the sort key and direction.
155  if (!aCol && !aDir) {
156  prevSortKey = null;
157  prevSortDir = null;
158  }
159  else {
160  if (aCol)
161  prevSortKey = SORT_LOOKUP_TABLE[aCol.getAttribute("anonid")].key;
162  else if (prevSortKey === null)
164 
165  if (aDir)
166  prevSortDir = aDir.toUpperCase();
167  else if (prevSortDir === null)
168  prevSortDir = SORT_LOOKUP_TABLE[aCol.getAttribute("anonid")].dir;
169  }
170  } catch (exc) {
171  failed = true;
172  }
173 
174  is(failed, !!aShouldFail,
175  "setSortColumn on column " +
176  (aCol ? aCol.getAttribute("anonid") : "(no column)") +
177  " with direction " + (aDir || "(no direction)") +
178  " and table previously " + (aUnsortFirst ? "unsorted" : "sorted") +
179  " should " + (aShouldFail ? "" : "not ") + "fail");
180 }
181 
190 function testInvalid(aOrganizerWin, aPlaceContentTree) {
191  // Invalid column should fail by throwing an exception.
192  let bogusCol = document.createElement("treecol");
193  bogusCol.setAttribute("anonid", "bogusColumn");
194  setSort(aOrganizerWin, aPlaceContentTree, true, true, bogusCol, "ascending");
195 
196  // Invalid direction reverts to SORT_BY_NONE.
197  setSort(aOrganizerWin, aPlaceContentTree, false, false, null, "bogus dir");
198  checkSort(aPlaceContentTree, Ci.nsINavHistoryQueryOptions.SORT_BY_NONE, "");
199 }
200 
212 function testSortByColAndDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst) {
213  let cols = aPlaceContentTree.getElementsByTagName("treecol");
214  ok(cols.length > 0, "sanity check: placeContent should contain columns");
215 
216  for (let i = 0; i < cols.length; i++) {
217  let col = cols.item(i);
218  ok(col.hasAttribute("anonid"),
219  "sanity check: column " + col.id + " should have anonid");
220 
221  let colId = col.getAttribute("anonid");
222  ok(colId in SORT_LOOKUP_TABLE,
223  "sanity check: unexpected placeContent column anonid");
224 
225  let sortConst =
226  "SORT_BY_" + SORT_LOOKUP_TABLE[colId].key + "_" +
227  (aUnsortFirst ? SORT_LOOKUP_TABLE[colId].dir : prevSortDir);
228  let expectedSortMode = Ci.nsINavHistoryQueryOptions[sortConst];
229  let expectedAnno = SORT_LOOKUP_TABLE[colId].anno || "";
230 
231  // Test sorting by only a column.
232  setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, col);
233  checkSort(aPlaceContentTree, expectedSortMode, expectedAnno);
234 
235  // Test sorting by both a column and a direction.
236  ["ascending", "descending"].forEach(function (dir) {
237  let sortConst =
238  "SORT_BY_" + SORT_LOOKUP_TABLE[colId].key + "_" + dir.toUpperCase();
239  let expectedSortMode = Ci.nsINavHistoryQueryOptions[sortConst];
240  setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, col, dir);
241  checkSort(aPlaceContentTree, expectedSortMode, expectedAnno);
242  });
243  }
244 }
245 
256 function testSortByDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst) {
257  ["ascending", "descending"].forEach(function (dir) {
258  let key = (aUnsortFirst ? DEFAULT_SORT_KEY : prevSortKey);
259  let sortConst = "SORT_BY_" + key + "_" + dir.toUpperCase();
260  let expectedSortMode = Ci.nsINavHistoryQueryOptions[sortConst];
261  setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, null, dir);
262  checkSort(aPlaceContentTree, expectedSortMode, "");
263  });
264 }
265 
267 
268 function test() {
270 
271  let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
272  getService(Ci.nsIWindowWatcher);
273 
274  let windowObserver = {
275  observe: function(aSubject, aTopic, aData) {
276  if (aTopic === "domwindowopened") {
277  ww.unregisterNotification(this);
278  let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
279  win.addEventListener("load", function onLoad(event) {
280  win.removeEventListener("load", onLoad, false);
281  executeSoon(function () {
282  let tree = win.document.getElementById("placeContent");
283  isnot(tree, null, "sanity check: placeContent tree should exist");
284  // Run the tests.
285  testSortByColAndDir(win, tree, true);
286  testSortByColAndDir(win, tree, false);
287  testSortByDir(win, tree, true);
288  testSortByDir(win, tree, false);
289  testInvalid(win, tree);
290  // Reset the sort to SORT_BY_NONE.
291  setSort(win, tree, false, false);
292  // Close the window and finish.
293  win.close();
294  finish();
295  });
296  }, false);
297  }
298  }
299  };
300 
301  ww.registerNotification(windowObserver);
302  ww.openWindow(null,
303  "chrome://browser/content/places/places.xul",
304  "",
305  "chrome,toolbar=yes,dialog=no,resizable",
306  null);
307 }
const Cc
_setDateDatepicker date
const SORT_LOOKUP_TABLE
function test()
var event
let prevSortKey
getService(Ci.sbIFaceplateManager)
return elem filter &&elem filter indexOf("opacity=")>=0?(parseFloat(elem.filter.match(/opacity
function setSort(aOrganizerWin, aTree, aUnsortFirst, aShouldFail, aCol, aDir)
let prevSortDir
function testSortByColAndDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst)
Lastfm onLoad
Definition: mini.js:36
waitForExplicitFinish()
function testSortByDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst)
return null
Definition: FeedWriter.js:1143
function checkSort(aTree, aSortingMode, aSortingAnno)
function url(spec)
const Ci
const DEFAULT_SORT_KEY
var failed
_getSelectedPageStyle s i
function testInvalid(aOrganizerWin, aPlaceContentTree)
_updateTextAndScrollDataForFrame aData
sbDeviceFirmwareAutoCheckForUpdate prototype observe