michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: _("Making sure after processing incoming bookmarks, they show up in the right order"); michael@0: Cu.import("resource://gre/modules/PlacesUtils.jsm", this); michael@0: Cu.import("resource://services-sync/engines/bookmarks.js"); michael@0: Cu.import("resource://services-sync/service.js"); michael@0: Cu.import("resource://services-sync/util.js"); michael@0: michael@0: function getBookmarks(folderId) { michael@0: let bookmarks = []; michael@0: michael@0: let pos = 0; michael@0: while (true) { michael@0: let itemId = PlacesUtils.bookmarks.getIdForItemAt(folderId, pos); michael@0: _("Got itemId", itemId, "under", folderId, "at", pos); michael@0: if (itemId == -1) michael@0: break; michael@0: michael@0: switch (PlacesUtils.bookmarks.getItemType(itemId)) { michael@0: case PlacesUtils.bookmarks.TYPE_BOOKMARK: michael@0: bookmarks.push(PlacesUtils.bookmarks.getItemTitle(itemId)); michael@0: break; michael@0: case PlacesUtils.bookmarks.TYPE_FOLDER: michael@0: bookmarks.push(getBookmarks(itemId)); michael@0: break; michael@0: default: michael@0: _("Unsupported item type.."); michael@0: } michael@0: michael@0: pos++; michael@0: } michael@0: michael@0: return bookmarks; michael@0: } michael@0: michael@0: function check(expected) { michael@0: let bookmarks = getBookmarks(PlacesUtils.bookmarks.unfiledBookmarksFolder); michael@0: michael@0: _("Checking if the bookmark structure is", JSON.stringify(expected)); michael@0: _("Got bookmarks:", JSON.stringify(bookmarks)); michael@0: do_check_true(Utils.deepEquals(bookmarks, expected)); michael@0: } michael@0: michael@0: function run_test() { michael@0: let store = new BookmarksEngine(Service)._store; michael@0: initTestLogging("Trace"); michael@0: michael@0: _("Starting with a clean slate of no bookmarks"); michael@0: store.wipe(); michael@0: check([]); michael@0: michael@0: function bookmark(name, parent) { michael@0: let bookmark = new Bookmark("http://weave.server/my-bookmark"); michael@0: bookmark.id = name; michael@0: bookmark.title = name; michael@0: bookmark.bmkUri = "http://uri/"; michael@0: bookmark.parentid = parent || "unfiled"; michael@0: bookmark.tags = []; michael@0: return bookmark; michael@0: } michael@0: michael@0: function folder(name, parent, children) { michael@0: let folder = new BookmarkFolder("http://weave.server/my-bookmark-folder"); michael@0: folder.id = name; michael@0: folder.title = name; michael@0: folder.parentid = parent || "unfiled"; michael@0: folder.children = children; michael@0: return folder; michael@0: } michael@0: michael@0: function apply(record) { michael@0: store._childrenToOrder = {}; michael@0: store.applyIncoming(record); michael@0: store._orderChildren(); michael@0: delete store._childrenToOrder; michael@0: } michael@0: michael@0: _("basic add first bookmark"); michael@0: apply(bookmark("10", "")); michael@0: check(["10"]); michael@0: michael@0: _("basic append behind 10"); michael@0: apply(bookmark("20", "")); michael@0: check(["10", "20"]); michael@0: michael@0: _("basic create in folder"); michael@0: apply(bookmark("31", "f30")); michael@0: let f30 = folder("f30", "", ["31"]); michael@0: apply(f30); michael@0: check(["10", "20", ["31"]]); michael@0: michael@0: _("insert missing parent -> append to unfiled"); michael@0: apply(bookmark("41", "f40")); michael@0: check(["10", "20", ["31"], "41"]); michael@0: michael@0: _("insert another missing parent -> append"); michael@0: apply(bookmark("42", "f40")); michael@0: check(["10", "20", ["31"], "41", "42"]); michael@0: michael@0: _("insert folder -> move children and followers"); michael@0: let f40 = folder("f40", "", ["41", "42"]); michael@0: apply(f40); michael@0: check(["10", "20", ["31"], ["41", "42"]]); michael@0: michael@0: _("Moving 41 behind 42 -> update f40"); michael@0: f40.children = ["42", "41"]; michael@0: apply(f40); michael@0: check(["10", "20", ["31"], ["42", "41"]]); michael@0: michael@0: _("Moving 10 back to front -> update 10, 20"); michael@0: f40.children = ["41", "42"]; michael@0: apply(f40); michael@0: check(["10", "20", ["31"], ["41", "42"]]); michael@0: michael@0: _("Moving 20 behind 42 in f40 -> update 50"); michael@0: apply(bookmark("20", "f40")); michael@0: check(["10", ["31"], ["41", "42", "20"]]); michael@0: michael@0: _("Moving 10 in front of 31 in f30 -> update 10, f30"); michael@0: apply(bookmark("10", "f30")); michael@0: f30.children = ["10", "31"]; michael@0: apply(f30); michael@0: check([["10", "31"], ["41", "42", "20"]]); michael@0: michael@0: _("Moving 20 from f40 to f30 -> update 20, f30"); michael@0: apply(bookmark("20", "f30")); michael@0: f30.children = ["10", "20", "31"]; michael@0: apply(f30); michael@0: check([["10", "20", "31"], ["41", "42"]]); michael@0: michael@0: _("Move 20 back to front -> update 20, f30"); michael@0: apply(bookmark("20", "")); michael@0: f30.children = ["10", "31"]; michael@0: apply(f30); michael@0: check([["10", "31"], ["41", "42"], "20"]); michael@0: michael@0: }