|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
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"); |
|
9 |
|
10 function getBookmarks(folderId) { |
|
11 let bookmarks = []; |
|
12 |
|
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; |
|
19 |
|
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 } |
|
30 |
|
31 pos++; |
|
32 } |
|
33 |
|
34 return bookmarks; |
|
35 } |
|
36 |
|
37 function check(expected) { |
|
38 let bookmarks = getBookmarks(PlacesUtils.bookmarks.unfiledBookmarksFolder); |
|
39 |
|
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 } |
|
44 |
|
45 function run_test() { |
|
46 let store = new BookmarksEngine(Service)._store; |
|
47 initTestLogging("Trace"); |
|
48 |
|
49 _("Starting with a clean slate of no bookmarks"); |
|
50 store.wipe(); |
|
51 check([]); |
|
52 |
|
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 } |
|
62 |
|
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 } |
|
71 |
|
72 function apply(record) { |
|
73 store._childrenToOrder = {}; |
|
74 store.applyIncoming(record); |
|
75 store._orderChildren(); |
|
76 delete store._childrenToOrder; |
|
77 } |
|
78 |
|
79 _("basic add first bookmark"); |
|
80 apply(bookmark("10", "")); |
|
81 check(["10"]); |
|
82 |
|
83 _("basic append behind 10"); |
|
84 apply(bookmark("20", "")); |
|
85 check(["10", "20"]); |
|
86 |
|
87 _("basic create in folder"); |
|
88 apply(bookmark("31", "f30")); |
|
89 let f30 = folder("f30", "", ["31"]); |
|
90 apply(f30); |
|
91 check(["10", "20", ["31"]]); |
|
92 |
|
93 _("insert missing parent -> append to unfiled"); |
|
94 apply(bookmark("41", "f40")); |
|
95 check(["10", "20", ["31"], "41"]); |
|
96 |
|
97 _("insert another missing parent -> append"); |
|
98 apply(bookmark("42", "f40")); |
|
99 check(["10", "20", ["31"], "41", "42"]); |
|
100 |
|
101 _("insert folder -> move children and followers"); |
|
102 let f40 = folder("f40", "", ["41", "42"]); |
|
103 apply(f40); |
|
104 check(["10", "20", ["31"], ["41", "42"]]); |
|
105 |
|
106 _("Moving 41 behind 42 -> update f40"); |
|
107 f40.children = ["42", "41"]; |
|
108 apply(f40); |
|
109 check(["10", "20", ["31"], ["42", "41"]]); |
|
110 |
|
111 _("Moving 10 back to front -> update 10, 20"); |
|
112 f40.children = ["41", "42"]; |
|
113 apply(f40); |
|
114 check(["10", "20", ["31"], ["41", "42"]]); |
|
115 |
|
116 _("Moving 20 behind 42 in f40 -> update 50"); |
|
117 apply(bookmark("20", "f40")); |
|
118 check(["10", ["31"], ["41", "42", "20"]]); |
|
119 |
|
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"]]); |
|
125 |
|
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"]]); |
|
131 |
|
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"]); |
|
137 |
|
138 } |