commit 8af71fddd509ec43dfa57e2841c06b483388a4a0
parent 05697b6a50a35ac152373f762d93b045d4d5a9e0
Author: James Teow <jteow@mozilla.com>
Date: Fri, 5 Dec 2025 14:03:05 +0000
Bug 510467 - Finish todos in test_result_sort.js - r=places-reviewers,Standard8
- Added more conditions mentioned in TODO
- Added more bookmark types to check how sort affects them
Differential Revision: https://phabricator.services.mozilla.com/D275080
Diffstat:
2 files changed, 100 insertions(+), 17 deletions(-)
diff --git a/toolkit/components/places/tests/head_common.js b/toolkit/components/places/tests/head_common.js
@@ -900,3 +900,9 @@ async function assertNoOrphanPageAnnotations() {
WHERE id NOT IN (SELECT anno_attribute_id FROM moz_annos) AND
id NOT IN (SELECT anno_attribute_id FROM moz_items_annos)`);
}
+
+function daysAgo(days) {
+ let date = new Date();
+ date.setDate(date.getDate() - days);
+ return date;
+}
diff --git a/toolkit/components/places/tests/unit/test_result_sort.js b/toolkit/components/places/tests/unit/test_result_sort.js
@@ -20,15 +20,32 @@ add_task(async function test() {
{
title: "b",
url: uri1,
+ dateAdded: daysAgo(3),
+ lastModified: daysAgo(2),
},
{
title: "a",
url: uri2,
+ dateAdded: daysAgo(2),
+ lastModified: daysAgo(1),
},
{
// url of the first child, title of second
title: "a",
url: uri1,
+ dateAdded: daysAgo(1),
+ lastModified: daysAgo(0),
+ },
+ {
+ title: "c",
+ type: PlacesUtils.bookmarks.TYPE_FOLDER,
+ dateAdded: daysAgo(6),
+ lastModified: daysAgo(5),
+ },
+ {
+ type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
+ dateAdded: daysAgo(6),
+ lastModified: daysAgo(5),
},
],
},
@@ -38,37 +55,65 @@ add_task(async function test() {
let guid1 = bookmarks[1].guid;
let guid2 = bookmarks[2].guid;
let guid3 = bookmarks[3].guid;
+ let folder = bookmarks[4].guid;
+ let separator = bookmarks[5].guid;
// query with natural order
let result = PlacesUtils.getFolderContents(bookmarks[0].guid);
let root = result.root;
- Assert.equal(root.childCount, 3);
+ Assert.equal(root.childCount, 5);
- function checkOrder(a, b, c) {
- Assert.equal(root.getChild(0).bookmarkGuid, a);
- Assert.equal(root.getChild(1).bookmarkGuid, b);
- Assert.equal(root.getChild(2).bookmarkGuid, c);
+ let guidNames = {
+ [guid1]: "guid1 (title='b', uri=uri1)",
+ [guid2]: "guid2 (title='a', uri=uri2)",
+ [guid3]: "guid3 (title='a', uri=uri1)",
+ [folder]: "guid4 (title='c', folder)",
+ [separator]: "guid5 (separator)",
+ };
+
+ function formatGuid(guid) {
+ return guidNames[guid] || guid;
+ }
+
+ function checkOrder(a, b, c, d, e) {
+ let expected = [a, b, c, d, e];
+ let actual = [
+ root.getChild(0).bookmarkGuid,
+ root.getChild(1).bookmarkGuid,
+ root.getChild(2).bookmarkGuid,
+ root.getChild(3).bookmarkGuid,
+ root.getChild(4).bookmarkGuid,
+ ];
+ Assert.deepEqual(
+ actual.map(formatGuid),
+ expected.map(formatGuid),
+ "Bookmark order"
+ );
}
// natural order
info("Natural order");
- checkOrder(guid1, guid2, guid3);
+ checkOrder(guid1, guid2, guid3, folder, separator);
// title: guid3 should precede guid2 since we fall-back to URI-based sorting
info("Sort by title asc");
result.sortingMode = NHQO.SORT_BY_TITLE_ASCENDING;
- checkOrder(guid3, guid2, guid1);
+ checkOrder(separator, guid3, guid2, guid1, folder);
// In reverse
info("Sort by title desc");
result.sortingMode = NHQO.SORT_BY_TITLE_DESCENDING;
- checkOrder(guid1, guid2, guid3);
+ checkOrder(folder, guid1, guid2, guid3, separator);
+
+ info("Sort by uri descending.");
+ result.sortingMode = NHQO.SORT_BY_URI_DESCENDING;
+ checkOrder(guid2, guid3, guid1, separator, folder);
// uri sort: guid1 should precede guid3 since we fall-back to natural order
info("Sort by uri asc");
result.sortingMode = NHQO.SORT_BY_URI_ASCENDING;
- checkOrder(guid1, guid3, guid2);
+ checkOrder(folder, separator, guid1, guid3, guid2);
// test live update
info("Change bookmark uri liveupdate");
@@ -76,18 +121,50 @@ add_task(async function test() {
guid: guid1,
url: uri2,
});
- checkOrder(guid3, guid1, guid2);
+ checkOrder(folder, separator, guid3, guid1, guid2);
await PlacesUtils.bookmarks.update({
guid: guid1,
url: uri1,
});
- checkOrder(guid1, guid3, guid2);
+ checkOrder(folder, separator, guid1, guid3, guid2);
+
+ await PlacesTestUtils.addVisits([
+ { uri: uri1, visitDate: daysAgo(0) },
+ { uri: uri2, visitDate: daysAgo(1) },
+ { uri: uri2, visitDate: daysAgo(1) },
+ ]);
+
+ info("Sort by visit count descending.");
+ result.sortingMode = NHQO.SORT_BY_VISITCOUNT_DESCENDING;
+ checkOrder(guid2, guid3, guid1, separator, folder);
+
+ info("Sort by visit count ascending.");
+ result.sortingMode = NHQO.SORT_BY_VISITCOUNT_ASCENDING;
+ checkOrder(folder, separator, guid1, guid3, guid2);
+
+ info("Sort by date descending.");
+ result.sortingMode = NHQO.SORT_BY_DATE_DESCENDING;
+ checkOrder(guid1, guid3, guid2, folder, separator);
+
+ info("Sort by date ascending.");
+ result.sortingMode = NHQO.SORT_BY_DATE_ASCENDING;
+ checkOrder(separator, folder, guid2, guid3, guid1);
+
+ info("Sort by date added descending.");
+ result.sortingMode = NHQO.SORT_BY_DATEADDED_DESCENDING;
+ checkOrder(guid3, guid2, guid1, folder, separator);
+
+ info("Sort by date added ascending.");
+ result.sortingMode = NHQO.SORT_BY_DATEADDED_ASCENDING;
+ checkOrder(separator, folder, guid1, guid2, guid3);
- // XXXtodo: test history sortings (visit count, visit date)
- // XXXtodo: test different item types once folderId and bookmarkId are merged.
+ info("Sort by last modified descending.");
+ result.sortingMode = NHQO.SORT_BY_LASTMODIFIED_DESCENDING;
+ checkOrder(guid1, guid3, guid2, folder, separator);
- // XXXtodo: test dateAdded sort
- // XXXtodo: test lastModified sort
+ info("Sort by last modified ascending.");
+ result.sortingMode = NHQO.SORT_BY_LASTMODIFIED_ASCENDING;
+ checkOrder(separator, folder, guid2, guid3, guid1);
// Add a visit, then check frecency ordering.
@@ -100,13 +177,13 @@ add_task(async function test() {
}
// For guid1 and guid3, since they have same frecency and no visits, fallback
// to sort by the newest bookmark.
- checkOrder(guid2, guid3, guid1);
+ checkOrder(guid2, guid3, guid1, separator, folder);
info("Sort by frecency asc");
result.sortingMode = NHQO.SORT_BY_FRECENCY_ASCENDING;
for (let i = 0; i < root.childCount; ++i) {
print(root.getChild(i).uri + " " + root.getChild(i).title);
}
- checkOrder(guid1, guid3, guid2);
+ checkOrder(folder, separator, guid1, guid3, guid2);
root.containerOpen = false;
});