summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_bookmarks_update.js
blob: d077fd6f3d5f19637a29c4660158177db686f411 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

add_task(function* invalid_input_throws() {
  Assert.throws(() => PlacesUtils.bookmarks.update(),
                /Input should be a valid object/);
  Assert.throws(() => PlacesUtils.bookmarks.update(null),
                /Input should be a valid object/);
  Assert.throws(() => PlacesUtils.bookmarks.update({}),
                /The following properties were expected/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ guid: "test" }),
                /Invalid value for property 'guid'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ guid: null }),
                /Invalid value for property 'guid'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ guid: 123 }),
                /Invalid value for property 'guid'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ parentGuid: "test" }),
                /Invalid value for property 'parentGuid'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ parentGuid: null }),
                /Invalid value for property 'parentGuid'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ parentGuid: 123 }),
                /Invalid value for property 'parentGuid'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ index: "1" }),
                /Invalid value for property 'index'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ index: -10 }),
                /Invalid value for property 'index'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ dateAdded: -10 }),
                /Invalid value for property 'dateAdded'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ dateAdded: "today" }),
                /Invalid value for property 'dateAdded'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ dateAdded: Date.now() }),
                /Invalid value for property 'dateAdded'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ lastModified: -10 }),
                /Invalid value for property 'lastModified'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ lastModified: "today" }),
                /Invalid value for property 'lastModified'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ lastModified: Date.now() }),
                /Invalid value for property 'lastModified'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ type: -1 }),
                /Invalid value for property 'type'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ type: 100 }),
                /Invalid value for property 'type'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ type: "bookmark" }),
                /Invalid value for property 'type'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ url: 10 }),
                /Invalid value for property 'url'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ url: "http://te st" }),
                /Invalid value for property 'url'/);
  let longurl = "http://www.example.com/";
  for (let i = 0; i < 65536; i++) {
    longurl += "a";
  }
  Assert.throws(() => PlacesUtils.bookmarks.update({ url: longurl }),
                /Invalid value for property 'url'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ url: NetUtil.newURI(longurl) }),
                /Invalid value for property 'url'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ url: "te st" }),
                /Invalid value for property 'url'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ title: -1 }),
                /Invalid value for property 'title'/);
  Assert.throws(() => PlacesUtils.bookmarks.update({ title: {} }),
                /Invalid value for property 'title'/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ guid: "123456789012" }),
                /Not enough properties to update/);

  Assert.throws(() => PlacesUtils.bookmarks.update({ guid: "123456789012",
                                                     parentGuid: "012345678901" }),
                /The following properties were expected: index/);
});

add_task(function* nonexisting_bookmark_throws() {
  try {
    yield PlacesUtils.bookmarks.update({ guid: "123456789012",
                                         title: "test" });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/No bookmarks found for the provided GUID/.test(ex));
  }
});

add_task(function* invalid_properties_for_existing_bookmark() {
  let bm = yield PlacesUtils.bookmarks.insert({ type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                url: "http://example.com/" });

  try {
    yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                         type: PlacesUtils.bookmarks.TYPE_FOLDER });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/The bookmark type cannot be changed/.test(ex));
  }

  try {
    yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                         dateAdded: new Date() });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/The bookmark dateAdded cannot be changed/.test(ex));
  }

  try {
    yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                         dateAdded: new Date() });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/The bookmark dateAdded cannot be changed/.test(ex));
  }

  try {
    yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                         parentGuid: "123456789012",
                                         index: 1 });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/No bookmarks found for the provided parentGuid/.test(ex));
  }

  let past = new Date(Date.now() - 86400000);
  try {
    yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                         lastModified: past });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Invalid value for property 'lastModified'/.test(ex));
  }

  let folder = yield PlacesUtils.bookmarks.insert({ type: PlacesUtils.bookmarks.TYPE_FOLDER,
                                                    parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  try {
    yield PlacesUtils.bookmarks.update({ guid: folder.guid,
                                         url: "http://example.com/" });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Invalid value for property 'url'/.test(ex));
  }

  let separator = yield PlacesUtils.bookmarks.insert({ type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
                                                       parentGuid: PlacesUtils.bookmarks.unfiledGuid });
  try {
    yield PlacesUtils.bookmarks.update({ guid: separator.guid,
                                         url: "http://example.com/" });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Invalid value for property 'url'/.test(ex));
  }
  try {
    yield PlacesUtils.bookmarks.update({ guid: separator.guid,
                                         title: "test" });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Invalid value for property 'title'/.test(ex));
  }
});

add_task(function* long_title_trim() {
  let longtitle = "a";
  for (let i = 0; i < 4096; i++) {
    longtitle += "a";
  }
  let bm = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                type: PlacesUtils.bookmarks.TYPE_FOLDER,
                                                title: "title" });
  checkBookmarkObject(bm);

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            title: longtitle });
  let newTitle = bm.title;
  Assert.equal(newTitle.length, 4096, "title should have been trimmed");

  bm = yield PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.equal(bm.title, newTitle);
});

add_task(function* update_lastModified() {
  let yesterday = new Date(Date.now() - 86400000);
  let bm = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                type: PlacesUtils.bookmarks.TYPE_FOLDER,
                                                title: "title",
                                                dateAdded: yesterday });
  checkBookmarkObject(bm);
  Assert.deepEqual(bm.lastModified, yesterday);

  let time = new Date();
  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            lastModified: time });
  checkBookmarkObject(bm);
  Assert.deepEqual(bm.lastModified, time);

  bm = yield PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.deepEqual(bm.lastModified, time);

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            lastModified: yesterday });
  Assert.deepEqual(bm.lastModified, yesterday);

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            title: "title2" });
  Assert.ok(bm.lastModified >= time);

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            title: "" });
  Assert.ok(!("title" in bm));

  bm = yield PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.ok(!("title" in bm));
});

add_task(function* update_url() {
  let bm = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
                                                url: "http://example.com/",
                                                title: "title" });
  checkBookmarkObject(bm);
  let lastModified = bm.lastModified;
  let frecency = frecencyForUrl(bm.url);
  Assert.ok(frecency > 0, "Check frecency has been updated");

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            url: "http://mozilla.org/" });
  checkBookmarkObject(bm);
  Assert.ok(bm.lastModified >= lastModified);
  Assert.equal(bm.url.href, "http://mozilla.org/");

  bm = yield PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.equal(bm.url.href, "http://mozilla.org/");
  Assert.ok(bm.lastModified >= lastModified);

  Assert.equal(frecencyForUrl("http://example.com/"), frecency, "Check frecency for example.com");
  Assert.equal(frecencyForUrl("http://mozilla.org/"), frecency, "Check frecency for mozilla.org");
});

add_task(function* update_index() {
  let parent = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                    type: PlacesUtils.bookmarks.TYPE_FOLDER }) ;
  let f1 = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                type: PlacesUtils.bookmarks.TYPE_FOLDER });
  Assert.equal(f1.index, 0);
  let f2 = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                type: PlacesUtils.bookmarks.TYPE_FOLDER });
  Assert.equal(f2.index, 1);
  let f3 = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                type: PlacesUtils.bookmarks.TYPE_FOLDER });
  Assert.equal(f3.index, 2);
  let lastModified = f1.lastModified;

  f1 = yield PlacesUtils.bookmarks.update({ guid: f1.guid,
                                            parentGuid: f1.parentGuid,
                                            index: 1});
  checkBookmarkObject(f1);
  Assert.equal(f1.index, 1);
  Assert.ok(f1.lastModified >= lastModified);

  parent = yield PlacesUtils.bookmarks.fetch(f1.parentGuid);
  Assert.deepEqual(parent.lastModified, f1.lastModified);

  f2 = yield PlacesUtils.bookmarks.fetch(f2.guid);
  Assert.equal(f2.index, 0);

  f3 = yield PlacesUtils.bookmarks.fetch(f3.guid);
  Assert.equal(f3.index, 2);

  f3 = yield PlacesUtils.bookmarks.update({ guid: f3.guid,
                                            index: 0 });
  f1 = yield PlacesUtils.bookmarks.fetch(f1.guid);
  Assert.equal(f1.index, 2);

  f2 = yield PlacesUtils.bookmarks.fetch(f2.guid);
  Assert.equal(f2.index, 1);
});

add_task(function* update_move_folder_into_descendant_throws() {
  let parent = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                    type: PlacesUtils.bookmarks.TYPE_FOLDER }) ;
  let descendant = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                        type: PlacesUtils.bookmarks.TYPE_FOLDER });

  try {
    yield PlacesUtils.bookmarks.update({ guid: parent.guid,
                                         parentGuid: parent.guid,
                                         index: 0 });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Cannot insert a folder into itself or one of its descendants/.test(ex));
  }

  try {
    yield PlacesUtils.bookmarks.update({ guid: parent.guid,
                                         parentGuid: descendant.guid,
                                         index: 0 });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/Cannot insert a folder into itself or one of its descendants/.test(ex));
  }
});

add_task(function* update_move() {
  let parent = yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                                    type: PlacesUtils.bookmarks.TYPE_FOLDER }) ;
  let bm = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                url: "http://example.com/",
                                                type: PlacesUtils.bookmarks.TYPE_BOOKMARK }) ;
  let descendant = yield PlacesUtils.bookmarks.insert({ parentGuid: parent.guid,
                                                        type: PlacesUtils.bookmarks.TYPE_FOLDER });
  Assert.equal(descendant.index, 1);
  let lastModified = bm.lastModified;

  // This is moving to a nonexisting index by purpose, it will be appended.
  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            parentGuid: descendant.guid,
                                            index: 1 });
  checkBookmarkObject(bm);
  Assert.equal(bm.parentGuid, descendant.guid);
  Assert.equal(bm.index, 0);
  Assert.ok(bm.lastModified >= lastModified);

  parent = yield PlacesUtils.bookmarks.fetch(parent.guid);
  descendant = yield PlacesUtils.bookmarks.fetch(descendant.guid);
  Assert.deepEqual(parent.lastModified, bm.lastModified);
  Assert.deepEqual(descendant.lastModified, bm.lastModified);
  Assert.equal(descendant.index, 0);

  bm = yield PlacesUtils.bookmarks.fetch(bm.guid);
  Assert.equal(bm.parentGuid, descendant.guid);
  Assert.equal(bm.index, 0);

  bm = yield PlacesUtils.bookmarks.update({ guid: bm.guid,
                                            parentGuid: parent.guid,
                                            index: 0 });
  Assert.equal(bm.parentGuid, parent.guid);
  Assert.equal(bm.index, 0);

  descendant = yield PlacesUtils.bookmarks.fetch(descendant.guid);
  Assert.equal(descendant.index, 1);
});

add_task(function* update_move_append() {
  let folder_a =
    yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                         type: PlacesUtils.bookmarks.TYPE_FOLDER });
  checkBookmarkObject(folder_a);
  let folder_b =
    yield PlacesUtils.bookmarks.insert({ parentGuid: PlacesUtils.bookmarks.unfiledGuid,
                                         type: PlacesUtils.bookmarks.TYPE_FOLDER });
  checkBookmarkObject(folder_b);

  /* folder_a: [sep_1, sep_2, sep_3], folder_b: [] */
  let sep_1 = yield PlacesUtils.bookmarks.insert({ parentGuid: folder_a.guid,
                                                   type: PlacesUtils.bookmarks.TYPE_SEPARATOR });
  checkBookmarkObject(sep_1);
  let sep_2 = yield PlacesUtils.bookmarks.insert({ parentGuid: folder_a.guid,
                                                   type: PlacesUtils.bookmarks.TYPE_SEPARATOR });
  checkBookmarkObject(sep_2);
  let sep_3 = yield PlacesUtils.bookmarks.insert({ parentGuid: folder_a.guid,
                                                   type: PlacesUtils.bookmarks.TYPE_SEPARATOR });
  checkBookmarkObject(sep_3);

  function ensurePosition(info, parentGuid, index) {
    checkBookmarkObject(info);
    Assert.equal(info.parentGuid, parentGuid);
    Assert.equal(info.index, index);
  }

  // folder_a: [sep_2, sep_3, sep_1], folder_b: []
  sep_1.index = PlacesUtils.bookmarks.DEFAULT_INDEX;
  // Note sep_1 includes parentGuid even though we're not moving the item to
  // another folder
  sep_1 = yield PlacesUtils.bookmarks.update(sep_1);
  ensurePosition(sep_1, folder_a.guid, 2);
  sep_2 = yield PlacesUtils.bookmarks.fetch(sep_2.guid);
  ensurePosition(sep_2, folder_a.guid, 0);
  sep_3 = yield PlacesUtils.bookmarks.fetch(sep_3.guid);
  ensurePosition(sep_3, folder_a.guid, 1);
  sep_1 = yield PlacesUtils.bookmarks.fetch(sep_1.guid);
  ensurePosition(sep_1, folder_a.guid, 2);

  // folder_a: [sep_2, sep_1], folder_b: [sep_3]
  sep_3.index = PlacesUtils.bookmarks.DEFAULT_INDEX;
  sep_3.parentGuid = folder_b.guid;
  sep_3 = yield PlacesUtils.bookmarks.update(sep_3);
  ensurePosition(sep_3, folder_b.guid, 0);
  sep_2 = yield PlacesUtils.bookmarks.fetch(sep_2.guid);
  ensurePosition(sep_2, folder_a.guid, 0);
  sep_1 = yield PlacesUtils.bookmarks.fetch(sep_1.guid);
  ensurePosition(sep_1, folder_a.guid, 1);
  sep_3 = yield PlacesUtils.bookmarks.fetch(sep_3.guid);
  ensurePosition(sep_3, folder_b.guid, 0);

  // folder_a: [sep_1], folder_b: [sep_3, sep_2]
  sep_2.index = Number.MAX_SAFE_INTEGER;
  sep_2.parentGuid = folder_b.guid;
  sep_2 = yield PlacesUtils.bookmarks.update(sep_2);
  ensurePosition(sep_2, folder_b.guid, 1);
  sep_1 = yield PlacesUtils.bookmarks.fetch(sep_1.guid);
  ensurePosition(sep_1, folder_a.guid, 0);
  sep_3 = yield PlacesUtils.bookmarks.fetch(sep_3.guid);
  ensurePosition(sep_3, folder_b.guid, 0);
  sep_2 = yield PlacesUtils.bookmarks.fetch(sep_2.guid);
  ensurePosition(sep_2, folder_b.guid, 1);
});

function run_test() {
  run_next_test();
}