summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/sync/test_bookmark_reconcile.js
blob: 218e84beb64058f30de2825f69b312c4b602710c (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Get bookmarks which aren't marked as normally syncing and with no pending
// changes.
async function getBookmarksNotMarkedAsSynced() {
  let db = await PlacesUtils.promiseDBConnection();
  let rows = await db.executeCached(
    `
    SELECT guid, syncStatus, syncChangeCounter FROM moz_bookmarks
    WHERE syncChangeCounter > 1 OR syncStatus != :syncStatus
    ORDER BY guid
    `,
    { syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NORMAL }
  );
  return rows.map(row => {
    return {
      guid: row.getResultByName("guid"),
      syncStatus: row.getResultByName("syncStatus"),
      syncChangeCounter: row.getResultByName("syncChangeCounter"),
    };
  });
}

add_task(async function test_reconcile_metadata() {
  let buf = await openMirror("test_reconcile_metadata");

  let olderDate = new Date(Date.now() - 100000);
  info("Set up local tree");
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.menuGuid,
    children: [
      {
        // this folder is going to reconcile exactly
        guid: "folderAAAAAA",
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
        title: "A",
        children: [
          {
            guid: "bookmarkBBBB",
            url: "http://example.com/b",
            title: "B",
          },
        ],
      },
      {
        // this folder's existing child isn't on the server (so will be
        // outgoing) and also will take a new child from the server.
        guid: "folderCCCCCC",
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
        title: "C",
        children: [
          {
            guid: "bookmarkEEEE",
            url: "http://example.com/e",
            title: "E",
          },
        ],
      },
      {
        // This bookmark is going to take the remote title.
        guid: "bookmarkFFFF",
        url: "http://example.com/f",
        title: "f",
        dateAdded: olderDate,
        lastModified: olderDate,
      },
    ],
  });
  // And a single, local-only bookmark in the toolbar.
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.toolbarGuid,
    children: [
      {
        guid: "bookmarkTTTT",
        url: "http://example.com/t",
        title: "in the toolbar",
        dateAdded: olderDate,
        lastModified: olderDate,
      },
    ],
  });
  // Reset to prepare for our reconciled sync.
  await PlacesSyncUtils.bookmarks.reset();
  // setup the mirror.
  await storeRecords(
    buf,
    shuffle([
      {
        id: "menu",
        parentid: "places",
        type: "folder",
        children: ["folderAAAAAA", "folderCCCCCC", "bookmarkFFFF"],
        modified: Date.now() / 1000 - 60,
      },
      {
        id: "folderAAAAAA",
        parentid: "menu",
        type: "folder",
        title: "A",
        children: ["bookmarkBBBB"],
        modified: Date.now() / 1000 - 60,
      },
      {
        id: "bookmarkBBBB",
        parentid: "folderAAAAAA",
        type: "bookmark",
        title: "B",
        bmkUri: "http://example.com/b",
        modified: Date.now() / 1000 - 60,
      },
      {
        id: "folderCCCCCC",
        parentid: "menu",
        type: "folder",
        title: "C",
        children: ["bookmarkDDDD"],
        modified: Date.now() / 1000 - 60,
      },
      {
        id: "bookmarkDDDD",
        parentid: "folderCCCCCC",
        type: "bookmark",
        title: "D",
        bmkUri: "http://example.com/d",
        modified: Date.now() / 1000 - 60,
      },
      {
        id: "bookmarkFFFF",
        parentid: "menu",
        type: "bookmark",
        title: "F",
        bmkUri: "http://example.com/f",
        dateAdded: olderDate,
        modified: Date.now() / 1000 + 60,
      },
      {
        id: "toolbar",
        parentid: "places",
        type: "folder",
        children: [],
        index: 1,
      },
      {
        id: "unfiled",
        parentid: "places",
        type: "folder",
        children: [],
        index: 3,
      },
    ])
  );
  info("Applying");
  let changesToUpload = await buf.apply();
  // We need to upload a bookmark and the parent as they didn't exist on the
  // server. Since we always use the local state for roots (bug 1472241), we'll
  // reupload them too.
  let idsToUpload = inspectChangeRecords(changesToUpload);
  deepEqual(
    idsToUpload,
    {
      updated: [
        "bookmarkEEEE",
        "bookmarkTTTT",
        "folderCCCCCC",
        "menu",
        "mobile",
        "toolbar",
        "unfiled",
      ],
      deleted: [],
    },
    "Should upload the 2 local-only bookmarks and their parents"
  );
  // Check it took the remote thing we were expecting.
  Assert.equal((await PlacesUtils.bookmarks.fetch("bookmarkFFFF")).title, "F");
  // Most things should be synced and have no change counter.
  let badGuids = await getBookmarksNotMarkedAsSynced();
  Assert.deepEqual(badGuids, [
    {
      // The bookmark that was only on the server. Still have SYNC_STATUS_NEW
      // as it's yet to be uploaded.
      guid: "bookmarkEEEE",
      syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NEW,
      syncChangeCounter: 1,
    },
    {
      // This bookmark is local only so is yet to be uploaded.
      guid: "bookmarkTTTT",
      syncStatus: PlacesUtils.bookmarks.SYNC_STATUS.NEW,
      syncChangeCounter: 1,
    },
  ]);
});