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