Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 _("Making sure after processing incoming bookmarks, they show up in the right order");
5 Cu.import("resource://gre/modules/PlacesUtils.jsm", this);
6 Cu.import("resource://services-sync/engines/bookmarks.js");
7 Cu.import("resource://services-sync/service.js");
8 Cu.import("resource://services-sync/util.js");
10 function getBookmarks(folderId) {
11 let bookmarks = [];
13 let pos = 0;
14 while (true) {
15 let itemId = PlacesUtils.bookmarks.getIdForItemAt(folderId, pos);
16 _("Got itemId", itemId, "under", folderId, "at", pos);
17 if (itemId == -1)
18 break;
20 switch (PlacesUtils.bookmarks.getItemType(itemId)) {
21 case PlacesUtils.bookmarks.TYPE_BOOKMARK:
22 bookmarks.push(PlacesUtils.bookmarks.getItemTitle(itemId));
23 break;
24 case PlacesUtils.bookmarks.TYPE_FOLDER:
25 bookmarks.push(getBookmarks(itemId));
26 break;
27 default:
28 _("Unsupported item type..");
29 }
31 pos++;
32 }
34 return bookmarks;
35 }
37 function check(expected) {
38 let bookmarks = getBookmarks(PlacesUtils.bookmarks.unfiledBookmarksFolder);
40 _("Checking if the bookmark structure is", JSON.stringify(expected));
41 _("Got bookmarks:", JSON.stringify(bookmarks));
42 do_check_true(Utils.deepEquals(bookmarks, expected));
43 }
45 function run_test() {
46 let store = new BookmarksEngine(Service)._store;
47 initTestLogging("Trace");
49 _("Starting with a clean slate of no bookmarks");
50 store.wipe();
51 check([]);
53 function bookmark(name, parent) {
54 let bookmark = new Bookmark("http://weave.server/my-bookmark");
55 bookmark.id = name;
56 bookmark.title = name;
57 bookmark.bmkUri = "http://uri/";
58 bookmark.parentid = parent || "unfiled";
59 bookmark.tags = [];
60 return bookmark;
61 }
63 function folder(name, parent, children) {
64 let folder = new BookmarkFolder("http://weave.server/my-bookmark-folder");
65 folder.id = name;
66 folder.title = name;
67 folder.parentid = parent || "unfiled";
68 folder.children = children;
69 return folder;
70 }
72 function apply(record) {
73 store._childrenToOrder = {};
74 store.applyIncoming(record);
75 store._orderChildren();
76 delete store._childrenToOrder;
77 }
79 _("basic add first bookmark");
80 apply(bookmark("10", ""));
81 check(["10"]);
83 _("basic append behind 10");
84 apply(bookmark("20", ""));
85 check(["10", "20"]);
87 _("basic create in folder");
88 apply(bookmark("31", "f30"));
89 let f30 = folder("f30", "", ["31"]);
90 apply(f30);
91 check(["10", "20", ["31"]]);
93 _("insert missing parent -> append to unfiled");
94 apply(bookmark("41", "f40"));
95 check(["10", "20", ["31"], "41"]);
97 _("insert another missing parent -> append");
98 apply(bookmark("42", "f40"));
99 check(["10", "20", ["31"], "41", "42"]);
101 _("insert folder -> move children and followers");
102 let f40 = folder("f40", "", ["41", "42"]);
103 apply(f40);
104 check(["10", "20", ["31"], ["41", "42"]]);
106 _("Moving 41 behind 42 -> update f40");
107 f40.children = ["42", "41"];
108 apply(f40);
109 check(["10", "20", ["31"], ["42", "41"]]);
111 _("Moving 10 back to front -> update 10, 20");
112 f40.children = ["41", "42"];
113 apply(f40);
114 check(["10", "20", ["31"], ["41", "42"]]);
116 _("Moving 20 behind 42 in f40 -> update 50");
117 apply(bookmark("20", "f40"));
118 check(["10", ["31"], ["41", "42", "20"]]);
120 _("Moving 10 in front of 31 in f30 -> update 10, f30");
121 apply(bookmark("10", "f30"));
122 f30.children = ["10", "31"];
123 apply(f30);
124 check([["10", "31"], ["41", "42", "20"]]);
126 _("Moving 20 from f40 to f30 -> update 20, f30");
127 apply(bookmark("20", "f30"));
128 f30.children = ["10", "20", "31"];
129 apply(f30);
130 check([["10", "20", "31"], ["41", "42"]]);
132 _("Move 20 back to front -> update 20, f30");
133 apply(bookmark("20", ""));
134 f30.children = ["10", "31"];
135 apply(f30);
136 check([["10", "31"], ["41", "42"], "20"]);
138 }