summaryrefslogtreecommitdiffstats
path: root/browser/components/places/tests/browser/browser_sort_in_library.js
blob: 2eea3a3f3183bf0c8d0496dbd86a5ce6f1f6875e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests the following bugs:
 *
 * Bug 443745 - View>Sort>of "alpha" sort items is default to Z>A instead of A>Z
 * https://bugzilla.mozilla.org/show_bug.cgi?id=443745
 *
 * Bug 444179 - Library>Views>Sort>Sort by Tags does nothing
 * https://bugzilla.mozilla.org/show_bug.cgi?id=444179
 *
 * Basically, fully tests sorting the placeContent tree in the Places Library
 * window.  Sorting is verified by comparing the nsINavHistoryResult returned by
 * placeContent.result to the expected sort values.
 */

// Two properties of nsINavHistoryResult control the sort of the tree:
// sortingMode.  sortingMode's value is one of the
// nsINavHistoryQueryOptions.SORT_BY_* constants.
//
// This lookup table maps the possible values of anonid's of the treecols to
// objects that represent the treecols' correct state after the user sorts the
// previously unsorted tree by selecting a column from the Views > Sort menu.
// sortingMode is constructed from the key and dir properties (i.e.,
// SORT_BY_<key>_<dir>).
const SORT_LOOKUP_TABLE = {
  title: { key: "TITLE", dir: "ASCENDING" },
  tags: { key: "TAGS", dir: "ASCENDING" },
  url: { key: "URI", dir: "ASCENDING" },
  date: { key: "DATE", dir: "DESCENDING" },
  visitCount: { key: "VISITCOUNT", dir: "DESCENDING" },
  dateAdded: { key: "DATEADDED", dir: "DESCENDING" },
  lastModified: { key: "LASTMODIFIED", dir: "DESCENDING" },
};

// This is the column that's sorted if one is not specified and the tree is
// currently unsorted.  Set it to a key substring in the name of one of the
// nsINavHistoryQueryOptions.SORT_BY_* constants, e.g., "TITLE", "URI".
// Method ViewMenu.setSortColumn in browser/components/places/content/places.js
// determines this value.
const DEFAULT_SORT_KEY = "TITLE";

// Part of the test is checking that sorts stick, so each time we sort we need
// to remember it.
var prevSortDir = null;
var prevSortKey = null;

/**
 * Ensures that the sort of aTree is aSortingMode
 *
 * @param {object} aTree
 *        the tree to check
 * @param {Ci.nsINavHistoryQueryOptions} aSortingMode
 *        one of the Ci.nsINavHistoryQueryOptions.SORT_BY_* constants
 */
function checkSort(aTree, aSortingMode) {
  // The placeContent tree's sort is determined by the nsINavHistoryResult it
  // stores.  Get it and check that the sort is what the caller expects.
  let res = aTree.result;
  isnot(res, null, "sanity check: placeContent.result should not return null");

  // Check sortingMode.
  is(
    res.sortingMode,
    aSortingMode,
    "column should now have sortingMode " + aSortingMode
  );
}

/**
 * Sets the sort of aTree.
 *
 * @param {object} aOrganizerWin
 *        the Places window
 * @param {object} aTree
 *        the tree to sort
 * @param {boolean} aUnsortFirst
 *        true if the sort should be set to SORT_BY_NONE before sorting by aCol
 *        and aDir
 * @param {boolean} aShouldFail
 *        true if setSortColumn should fail on aCol or aDir
 * @param {object} aCol
 *        the column of aTree by which to sort
 * @param {string} aDir
 *        either "ascending" or "descending"
 */
function setSort(aOrganizerWin, aTree, aUnsortFirst, aShouldFail, aCol, aDir) {
  if (aUnsortFirst) {
    aOrganizerWin.ViewMenu.setSortColumn();
    checkSort(aTree, Ci.nsINavHistoryQueryOptions.SORT_BY_NONE, "");

    // Remember the sort key and direction.
    prevSortKey = null;
    prevSortDir = null;
  }

  let failed = false;
  try {
    aOrganizerWin.ViewMenu.setSortColumn(aCol, aDir);

    // Remember the sort key and direction.
    if (!aCol && !aDir) {
      prevSortKey = null;
      prevSortDir = null;
    } else {
      if (aCol) {
        prevSortKey = SORT_LOOKUP_TABLE[aCol.getAttribute("anonid")].key;
      } else if (prevSortKey === null) {
        prevSortKey = DEFAULT_SORT_KEY;
      }

      if (aDir) {
        prevSortDir = aDir.toUpperCase();
      } else if (prevSortDir === null) {
        prevSortDir = SORT_LOOKUP_TABLE[aCol.getAttribute("anonid")].dir;
      }
    }
  } catch (exc) {
    failed = true;
  }

  is(
    failed,
    !!aShouldFail,
    "setSortColumn on column " +
      (aCol ? aCol.getAttribute("anonid") : "(no column)") +
      " with direction " +
      (aDir || "(no direction)") +
      " and table previously " +
      (aUnsortFirst ? "unsorted" : "sorted") +
      " should " +
      (aShouldFail ? "" : "not ") +
      "fail"
  );
}

/**
 * Tries sorting by an invalid column and sort direction.
 *
 * @param {object} aOrganizerWin
 *        the Places window
 * @param {object} aPlaceContentTree
 *        the placeContent tree in aOrganizerWin
 */
function testInvalid(aOrganizerWin, aPlaceContentTree) {
  // Invalid column should fail by throwing an exception.
  let bogusCol = document.createXULElement("treecol");
  bogusCol.setAttribute("anonid", "bogusColumn");
  setSort(aOrganizerWin, aPlaceContentTree, true, true, bogusCol, "ascending");

  // Invalid direction reverts to SORT_BY_NONE.
  setSort(aOrganizerWin, aPlaceContentTree, false, false, null, "bogus dir");
  checkSort(aPlaceContentTree, Ci.nsINavHistoryQueryOptions.SORT_BY_NONE, "");
}

/**
 * Tests sorting aPlaceContentTree by column only and then by both column
 * and direction.
 *
 * @param {object} aOrganizerWin
 *        the Places window
 * @param {object} aPlaceContentTree
 *        the placeContent tree in aOrganizerWin
 * @param {boolean} aUnsortFirst
 *        true if, before each sort we try, we should sort to SORT_BY_NONE
 */
function testSortByColAndDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst) {
  let cols = aPlaceContentTree.getElementsByTagName("treecol");
  ok(!!cols.length, "sanity check: placeContent should contain columns");

  for (let i = 0; i < cols.length; i++) {
    let col = cols.item(i);
    ok(
      col.hasAttribute("anonid"),
      "sanity check: column " + col.id + " should have anonid"
    );

    let colId = col.getAttribute("anonid");
    ok(
      colId in SORT_LOOKUP_TABLE,
      "sanity check: unexpected placeContent column anonid"
    );

    let sortStr =
      "SORT_BY_" +
      SORT_LOOKUP_TABLE[colId].key +
      "_" +
      (aUnsortFirst ? SORT_LOOKUP_TABLE[colId].dir : prevSortDir);
    let expectedSortMode = Ci.nsINavHistoryQueryOptions[sortStr];

    // Test sorting by only a column.
    setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, col);
    checkSort(aPlaceContentTree, expectedSortMode);

    // Test sorting by both a column and a direction.
    ["ascending", "descending"].forEach(function (dir) {
      sortStr =
        "SORT_BY_" + SORT_LOOKUP_TABLE[colId].key + "_" + dir.toUpperCase();
      expectedSortMode = Ci.nsINavHistoryQueryOptions[sortStr];
      setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, col, dir);
      checkSort(aPlaceContentTree, expectedSortMode);
    });
  }
}

/**
 * Tests sorting aPlaceContentTree by direction only.
 *
 * @param {object} aOrganizerWin
 *        the Places window
 * @param {object} aPlaceContentTree
 *        the placeContent tree in aOrganizerWin
 * @param {boolean} aUnsortFirst
 *        true if, before each sort we try, we should sort to SORT_BY_NONE
 */
function testSortByDir(aOrganizerWin, aPlaceContentTree, aUnsortFirst) {
  ["ascending", "descending"].forEach(function (dir) {
    let key = aUnsortFirst ? DEFAULT_SORT_KEY : prevSortKey;
    let sortStr = "SORT_BY_" + key + "_" + dir.toUpperCase();
    let expectedSortMode = Ci.nsINavHistoryQueryOptions[sortStr];
    setSort(aOrganizerWin, aPlaceContentTree, aUnsortFirst, false, null, dir);
    checkSort(aPlaceContentTree, expectedSortMode, "");
  });
}

function test() {
  waitForExplicitFinish();

  openLibrary(function (win) {
    let tree = win.document.getElementById("placeContent");
    isnot(tree, null, "sanity check: placeContent tree should exist");
    // Run the tests.
    testSortByColAndDir(win, tree, true);
    testSortByColAndDir(win, tree, false);
    testSortByDir(win, tree, true);
    testSortByDir(win, tree, false);
    testInvalid(win, tree);
    // Reset the sort to SORT_BY_NONE.
    setSort(win, tree, false, false);
    // Close the window and finish.
    win.close();
    finish();
  });
}