1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_bookmark_order.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,138 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +_("Making sure after processing incoming bookmarks, they show up in the right order"); 1.8 +Cu.import("resource://gre/modules/PlacesUtils.jsm", this); 1.9 +Cu.import("resource://services-sync/engines/bookmarks.js"); 1.10 +Cu.import("resource://services-sync/service.js"); 1.11 +Cu.import("resource://services-sync/util.js"); 1.12 + 1.13 +function getBookmarks(folderId) { 1.14 + let bookmarks = []; 1.15 + 1.16 + let pos = 0; 1.17 + while (true) { 1.18 + let itemId = PlacesUtils.bookmarks.getIdForItemAt(folderId, pos); 1.19 + _("Got itemId", itemId, "under", folderId, "at", pos); 1.20 + if (itemId == -1) 1.21 + break; 1.22 + 1.23 + switch (PlacesUtils.bookmarks.getItemType(itemId)) { 1.24 + case PlacesUtils.bookmarks.TYPE_BOOKMARK: 1.25 + bookmarks.push(PlacesUtils.bookmarks.getItemTitle(itemId)); 1.26 + break; 1.27 + case PlacesUtils.bookmarks.TYPE_FOLDER: 1.28 + bookmarks.push(getBookmarks(itemId)); 1.29 + break; 1.30 + default: 1.31 + _("Unsupported item type.."); 1.32 + } 1.33 + 1.34 + pos++; 1.35 + } 1.36 + 1.37 + return bookmarks; 1.38 +} 1.39 + 1.40 +function check(expected) { 1.41 + let bookmarks = getBookmarks(PlacesUtils.bookmarks.unfiledBookmarksFolder); 1.42 + 1.43 + _("Checking if the bookmark structure is", JSON.stringify(expected)); 1.44 + _("Got bookmarks:", JSON.stringify(bookmarks)); 1.45 + do_check_true(Utils.deepEquals(bookmarks, expected)); 1.46 +} 1.47 + 1.48 +function run_test() { 1.49 + let store = new BookmarksEngine(Service)._store; 1.50 + initTestLogging("Trace"); 1.51 + 1.52 + _("Starting with a clean slate of no bookmarks"); 1.53 + store.wipe(); 1.54 + check([]); 1.55 + 1.56 + function bookmark(name, parent) { 1.57 + let bookmark = new Bookmark("http://weave.server/my-bookmark"); 1.58 + bookmark.id = name; 1.59 + bookmark.title = name; 1.60 + bookmark.bmkUri = "http://uri/"; 1.61 + bookmark.parentid = parent || "unfiled"; 1.62 + bookmark.tags = []; 1.63 + return bookmark; 1.64 + } 1.65 + 1.66 + function folder(name, parent, children) { 1.67 + let folder = new BookmarkFolder("http://weave.server/my-bookmark-folder"); 1.68 + folder.id = name; 1.69 + folder.title = name; 1.70 + folder.parentid = parent || "unfiled"; 1.71 + folder.children = children; 1.72 + return folder; 1.73 + } 1.74 + 1.75 + function apply(record) { 1.76 + store._childrenToOrder = {}; 1.77 + store.applyIncoming(record); 1.78 + store._orderChildren(); 1.79 + delete store._childrenToOrder; 1.80 + } 1.81 + 1.82 + _("basic add first bookmark"); 1.83 + apply(bookmark("10", "")); 1.84 + check(["10"]); 1.85 + 1.86 + _("basic append behind 10"); 1.87 + apply(bookmark("20", "")); 1.88 + check(["10", "20"]); 1.89 + 1.90 + _("basic create in folder"); 1.91 + apply(bookmark("31", "f30")); 1.92 + let f30 = folder("f30", "", ["31"]); 1.93 + apply(f30); 1.94 + check(["10", "20", ["31"]]); 1.95 + 1.96 + _("insert missing parent -> append to unfiled"); 1.97 + apply(bookmark("41", "f40")); 1.98 + check(["10", "20", ["31"], "41"]); 1.99 + 1.100 + _("insert another missing parent -> append"); 1.101 + apply(bookmark("42", "f40")); 1.102 + check(["10", "20", ["31"], "41", "42"]); 1.103 + 1.104 + _("insert folder -> move children and followers"); 1.105 + let f40 = folder("f40", "", ["41", "42"]); 1.106 + apply(f40); 1.107 + check(["10", "20", ["31"], ["41", "42"]]); 1.108 + 1.109 + _("Moving 41 behind 42 -> update f40"); 1.110 + f40.children = ["42", "41"]; 1.111 + apply(f40); 1.112 + check(["10", "20", ["31"], ["42", "41"]]); 1.113 + 1.114 + _("Moving 10 back to front -> update 10, 20"); 1.115 + f40.children = ["41", "42"]; 1.116 + apply(f40); 1.117 + check(["10", "20", ["31"], ["41", "42"]]); 1.118 + 1.119 + _("Moving 20 behind 42 in f40 -> update 50"); 1.120 + apply(bookmark("20", "f40")); 1.121 + check(["10", ["31"], ["41", "42", "20"]]); 1.122 + 1.123 + _("Moving 10 in front of 31 in f30 -> update 10, f30"); 1.124 + apply(bookmark("10", "f30")); 1.125 + f30.children = ["10", "31"]; 1.126 + apply(f30); 1.127 + check([["10", "31"], ["41", "42", "20"]]); 1.128 + 1.129 + _("Moving 20 from f40 to f30 -> update 20, f30"); 1.130 + apply(bookmark("20", "f30")); 1.131 + f30.children = ["10", "20", "31"]; 1.132 + apply(f30); 1.133 + check([["10", "20", "31"], ["41", "42"]]); 1.134 + 1.135 + _("Move 20 back to front -> update 20, f30"); 1.136 + apply(bookmark("20", "")); 1.137 + f30.children = ["10", "31"]; 1.138 + apply(f30); 1.139 + check([["10", "31"], ["41", "42"], "20"]); 1.140 + 1.141 +}