summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_bookmark_list.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:13:27 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:13:27 +0000
commit40a355a42d4a9444dc753c04c6608dade2f06a23 (patch)
tree871fc667d2de662f171103ce5ec067014ef85e61 /toolkit/components/places/tests/unit/test_bookmark_list.js
parentAdding upstream version 124.0.1. (diff)
downloadfirefox-adbda400be353e676059e335c3c0aaf99e719475.tar.xz
firefox-adbda400be353e676059e335c3c0aaf99e719475.zip
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/places/tests/unit/test_bookmark_list.js')
-rw-r--r--toolkit/components/places/tests/unit/test_bookmark_list.js115
1 files changed, 115 insertions, 0 deletions
diff --git a/toolkit/components/places/tests/unit/test_bookmark_list.js b/toolkit/components/places/tests/unit/test_bookmark_list.js
new file mode 100644
index 0000000000..b743a1281d
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_bookmark_list.js
@@ -0,0 +1,115 @@
+/* Any copyright is dedicated to the Public Domain.
+http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const { sinon } = ChromeUtils.importESModule(
+ "resource://testing-common/Sinon.sys.mjs"
+);
+const { BookmarkList } = ChromeUtils.importESModule(
+ "resource://gre/modules/BookmarkList.sys.mjs"
+);
+
+registerCleanupFunction(
+ async () => await PlacesUtils.bookmarks.eraseEverything()
+);
+
+add_task(async function test_url_tracking() {
+ const firstBookmark = await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.example.com/",
+ });
+ const secondBookmark = await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.reddit.com/",
+ });
+
+ let deferredUpdate;
+ const bookmarkList = new BookmarkList(
+ [
+ "https://www.example.com/",
+ "https://www.reddit.com/",
+ "https://www.youtube.com/",
+ ],
+ () => deferredUpdate?.resolve()
+ );
+
+ async function waitForUpdateBookmarksTask(updateTask) {
+ deferredUpdate = Promise.withResolvers();
+ await updateTask();
+ return deferredUpdate.promise;
+ }
+
+ info("Check bookmark status of tracked URLs.");
+ equal(await bookmarkList.isBookmark("https://www.example.com/"), true);
+ equal(await bookmarkList.isBookmark("https://www.reddit.com/"), true);
+ equal(await bookmarkList.isBookmark("https://www.youtube.com/"), false);
+
+ info("Add a bookmark.");
+ await waitForUpdateBookmarksTask(() =>
+ PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.youtube.com/",
+ })
+ );
+ equal(await bookmarkList.isBookmark("https://www.youtube.com/"), true);
+
+ info("Remove a bookmark.");
+ await waitForUpdateBookmarksTask(() =>
+ PlacesUtils.bookmarks.remove(firstBookmark.guid)
+ );
+ equal(await bookmarkList.isBookmark("https://www.example.com/"), false);
+
+ info("Update a bookmark's URL.");
+ await waitForUpdateBookmarksTask(() =>
+ PlacesUtils.bookmarks.update({
+ guid: secondBookmark.guid,
+ url: "https://www.wikipedia.org/",
+ })
+ );
+ equal(await bookmarkList.isBookmark("https://www.reddit.com/"), false);
+
+ info("Add a bookmark after removing listeners.");
+ bookmarkList.removeListeners();
+ await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.example.com/",
+ });
+
+ info("Reinitialize the list and validate bookmark status.");
+ bookmarkList.setTrackedUrls(["https://www.example.com/"]);
+ bookmarkList.addListeners();
+ equal(await bookmarkList.isBookmark("https://www.example.com/"), true);
+
+ info("Cleanup.");
+ bookmarkList.removeListeners();
+ await PlacesUtils.bookmarks.eraseEverything();
+});
+
+add_task(async function test_no_unnecessary_observer_notifications() {
+ const spy = sinon.spy();
+ const bookmarkList = new BookmarkList(
+ ["https://www.example.com/"],
+ spy,
+ 0,
+ 0
+ );
+
+ info("Add a bookmark with an untracked URL.");
+ await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.reddit.com/",
+ });
+ await new Promise(resolve => ChromeUtils.idleDispatch(resolve));
+ ok(spy.notCalled, "Observer was not notified.");
+ equal(await bookmarkList.isBookmark("https://www.reddit.com"), undefined);
+
+ info("Add a bookmark after removing listeners.");
+ bookmarkList.removeListeners();
+ await PlacesUtils.bookmarks.insert({
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: "https://www.example.com/",
+ });
+ await new Promise(resolve => ChromeUtils.idleDispatch(resolve));
+ ok(spy.notCalled, "Observer was not notified.");
+});