summaryrefslogtreecommitdiffstats
path: root/services/sync/tests/unit/test_bookmark_order.js
blob: 56806dba02e6657988a733c9bb1ff7c99c4a2461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

_("Making sure after processing incoming bookmarks, they show up in the right order");
Cu.import("resource://gre/modules/PlacesUtils.jsm", this);
Cu.import("resource://services-sync/engines/bookmarks.js");
Cu.import("resource://services-sync/service.js");
Cu.import("resource://services-sync/util.js");

function getBookmarks(folderId) {
  let bookmarks = [];

  let pos = 0;
  while (true) {
    let itemId = PlacesUtils.bookmarks.getIdForItemAt(folderId, pos);
    _("Got itemId", itemId, "under", folderId, "at", pos);
    if (itemId == -1)
      break;

    switch (PlacesUtils.bookmarks.getItemType(itemId)) {
      case PlacesUtils.bookmarks.TYPE_BOOKMARK:
        bookmarks.push(PlacesUtils.bookmarks.getItemTitle(itemId));
        break;
      case PlacesUtils.bookmarks.TYPE_FOLDER:
        bookmarks.push(getBookmarks(itemId));
        break;
      default:
        _("Unsupported item type..");
    }

    pos++;
  }

  return bookmarks;
}

function check(expected) {
  let bookmarks = getBookmarks(PlacesUtils.bookmarks.unfiledBookmarksFolder);

  _("Checking if the bookmark structure is", JSON.stringify(expected));
  _("Got bookmarks:", JSON.stringify(bookmarks));
  do_check_true(Utils.deepEquals(bookmarks, expected));
}

function run_test() {
  let store = new BookmarksEngine(Service)._store;
  initTestLogging("Trace");

  _("Starting with a clean slate of no bookmarks");
  store.wipe();
  check([]);

  function bookmark(name, parent) {
    let bookmark = new Bookmark("http://weave.server/my-bookmark");
    bookmark.id = name;
    bookmark.title = name;
    bookmark.bmkUri = "http://uri/";
    bookmark.parentid = parent || "unfiled";
    bookmark.tags = [];
    return bookmark;
  }

  function folder(name, parent, children) {
    let folder = new BookmarkFolder("http://weave.server/my-bookmark-folder");
    folder.id = name;
    folder.title = name;
    folder.parentid = parent || "unfiled";
    folder.children = children;
    return folder;
  }

  function apply(record) {
    store._childrenToOrder = {};
    store.applyIncoming(record);
    store._orderChildren();
    delete store._childrenToOrder;
  }

  _("basic add first bookmark");
  apply(bookmark("10", ""));
  check(["10"]);

  _("basic append behind 10");
  apply(bookmark("20", ""));
  check(["10", "20"]);

  _("basic create in folder");
  apply(bookmark("31", "f30"));
  let f30 = folder("f30", "", ["31"]);
  apply(f30);
  check(["10", "20", ["31"]]);

  _("insert missing parent -> append to unfiled");
  apply(bookmark("41", "f40"));
  check(["10", "20", ["31"], "41"]);

  _("insert another missing parent -> append");
  apply(bookmark("42", "f40"));
  check(["10", "20", ["31"], "41", "42"]);

  _("insert folder -> move children and followers");
  let f40 = folder("f40", "", ["41", "42"]);
  apply(f40);
  check(["10", "20", ["31"], ["41", "42"]]);

  _("Moving 41 behind 42 -> update f40");
  f40.children = ["42", "41"];
  apply(f40);
  check(["10", "20", ["31"], ["42", "41"]]);

  _("Moving 10 back to front -> update 10, 20");
  f40.children = ["41", "42"];
  apply(f40);
  check(["10", "20", ["31"], ["41", "42"]]);

  _("Moving 20 behind 42 in f40 -> update 50");
  apply(bookmark("20", "f40"));
  check(["10", ["31"], ["41", "42", "20"]]);

  _("Moving 10 in front of 31 in f30 -> update 10, f30");
  apply(bookmark("10", "f30"));
  f30.children = ["10", "31"];
  apply(f30);
  check([["10", "31"], ["41", "42", "20"]]);

  _("Moving 20 from f40 to f30 -> update 20, f30");
  apply(bookmark("20", "f30"));
  f30.children = ["10", "20", "31"];
  apply(f30);
  check([["10", "20", "31"], ["41", "42"]]);

  _("Move 20 back to front -> update 20, f30");
  apply(bookmark("20", ""));
  f30.children = ["10", "31"];
  apply(f30);
  check([["10", "31"], ["41", "42"], "20"]);

}