summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_bookmarks_remove.js
blob: 8e8fc55f247aa12e2ee2da954b9495355d368bcb (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const UNVISITED_BOOKMARK_BONUS = 140;

function promiseRankingChanged() {
  return PlacesTestUtils.waitForNotification(
    "pages-rank-changed",
    () => true,
    "places"
  );
}

add_task(async function setup() {
  Services.prefs.setIntPref(
    "places.frecency.unvisitedBookmarkBonus",
    UNVISITED_BOOKMARK_BONUS
  );
});

add_task(async function invalid_input_throws() {
  Assert.throws(
    () => PlacesUtils.bookmarks.remove(),
    /Input should be a valid object/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.remove(null),
    /Input should be a valid object/
  );

  Assert.throws(
    () => PlacesUtils.bookmarks.remove("test"),
    /Invalid value for property 'guid'/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.remove(123),
    /Invalid value for property 'guid'/
  );

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

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

  Assert.throws(
    () => PlacesUtils.bookmarks.remove({ url: "http://te st/" }),
    /Invalid value for property 'url'/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.remove({ url: null }),
    /Invalid value for property 'url'/
  );
  Assert.throws(
    () => PlacesUtils.bookmarks.remove({ url: -10 }),
    /Invalid value for property 'url'/
  );
});

add_task(async function remove_nonexistent_guid() {
  try {
    await PlacesUtils.bookmarks.remove({ guid: "123456789012" });
    Assert.ok(false, "Should have thrown");
  } catch (ex) {
    Assert.ok(/No bookmarks found for the provided GUID/.test(ex));
  }
});

add_task(async function remove_roots_fail() {
  let guids = [
    PlacesUtils.bookmarks.rootGuid,
    PlacesUtils.bookmarks.unfiledGuid,
    PlacesUtils.bookmarks.menuGuid,
    PlacesUtils.bookmarks.toolbarGuid,
    PlacesUtils.bookmarks.tagsGuid,
    PlacesUtils.bookmarks.mobileGuid,
  ];
  for (let guid of guids) {
    Assert.throws(
      () => PlacesUtils.bookmarks.remove(guid),
      /It's not possible to remove Places root folders\./
    );
  }
});

add_task(async function remove_bookmark() {
  // When removing a bookmark we need to check the frecency. First we confirm
  // that there is a normal update when it is inserted.
  let promise = promiseRankingChanged();
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "a bookmark",
  });
  checkBookmarkObject(bm1);

  await promise;

  // This second one checks the frecency is changed when we remove the bookmark.
  promise = promiseRankingChanged();

  await PlacesUtils.bookmarks.remove(bm1.guid);

  await promise;
});

add_task(async function remove_multiple_bookmarks_simple() {
  // When removing a bookmark we need to check the frecency. First we confirm
  // that there is a normal update when it is inserted.
  const promise1 = promiseRankingChanged();
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "a bookmark",
  });
  checkBookmarkObject(bm1);

  const promise2 = promiseRankingChanged();
  let bm2 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example1.com/",
    title: "a bookmark",
  });
  checkBookmarkObject(bm2);

  await Promise.all([promise1, promise2]);

  // We should get a pages-rank-changed event with the removal of
  // multiple bookmarks.
  const promise3 = promiseRankingChanged();

  await PlacesUtils.bookmarks.remove([bm1, bm2]);

  await promise3;
});

add_task(async function remove_multiple_bookmarks_complex() {
  let bms = [];
  for (let i = 0; i < 10; i++) {
    bms.push(
      await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: `http://example.com/${i}`,
        title: `bookmark ${i}`,
      })
    );
  }

  // Remove bookmarks 2 and 3.
  let bmsToRemove = bms.slice(2, 4);
  let notifiedIndexes = [];
  let notificationPromise = PlacesTestUtils.waitForNotification(
    "bookmark-removed",
    events => {
      for (let event of events) {
        notifiedIndexes.push({ guid: event.guid, index: event.index });
      }
      return notifiedIndexes.length == bmsToRemove.length;
    },
    "places"
  );
  await PlacesUtils.bookmarks.remove(bmsToRemove);
  await notificationPromise;

  let indexModifier = 0;
  for (let i = 0; i < bmsToRemove.length; i++) {
    Assert.equal(
      notifiedIndexes[i].guid,
      bmsToRemove[i].guid,
      `Should have been notified of the correct guid for item ${i}`
    );
    Assert.equal(
      notifiedIndexes[i].index,
      bmsToRemove[i].index - indexModifier,
      `Should have been notified of the correct index for the item ${i}`
    );
    indexModifier++;
  }

  let expectedIndex = 0;
  for (let bm of [bms[0], bms[1], ...bms.slice(4)]) {
    const fetched = await PlacesUtils.bookmarks.fetch(bm.guid);
    Assert.equal(
      fetched.index,
      expectedIndex,
      "Should have the correct index after consecutive item removal"
    );
    bm.index = fetched.index;
    expectedIndex++;
  }

  // Remove some more including non-consecutive.
  bmsToRemove = [bms[1], bms[5], bms[6], bms[8]];
  notifiedIndexes = [];
  notificationPromise = PlacesTestUtils.waitForNotification(
    "bookmark-removed",
    events => {
      for (let event of events) {
        notifiedIndexes.push({ guid: event.guid, index: event.index });
      }
      return notifiedIndexes.length == bmsToRemove.length;
    },
    "places"
  );
  await PlacesUtils.bookmarks.remove(bmsToRemove);
  await notificationPromise;

  indexModifier = 0;
  for (let i = 0; i < bmsToRemove.length; i++) {
    Assert.equal(
      notifiedIndexes[i].guid,
      bmsToRemove[i].guid,
      `Should have been notified of the correct guid for item ${i}`
    );
    Assert.equal(
      notifiedIndexes[i].index,
      bmsToRemove[i].index - indexModifier,
      `Should have been notified of the correct index for the item ${i}`
    );
    indexModifier++;
  }

  expectedIndex = 0;
  const expectedRemaining = [bms[0], bms[4], bms[7], bms[9]];
  for (let bm of expectedRemaining) {
    const fetched = await PlacesUtils.bookmarks.fetch(bm.guid);
    Assert.equal(
      fetched.index,
      expectedIndex,
      "Should have the correct index after non-consecutive item removal"
    );
    expectedIndex++;
  }

  // Tidy up
  await PlacesUtils.bookmarks.remove(expectedRemaining);
  await PlacesTestUtils.promiseAsyncUpdates();
});

add_task(async function remove_bookmark_empty_title() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "",
  });
  checkBookmarkObject(bm1);

  await PlacesUtils.bookmarks.remove(bm1.guid);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);
});

add_task(async function remove_folder() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  checkBookmarkObject(bm1);

  await PlacesUtils.bookmarks.remove(bm1.guid);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);

  // No wait for pages-rank-changed event in this test as the folder doesn't have
  // any children that would need updating.
});

add_task(async function test_contents_removed() {
  let folder1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: folder1.guid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "",
  });

  let skipDescendantsObserver = expectPlacesObserverNotifications(
    ["bookmark-removed"],
    false,
    true
  );
  let receiveAllObserver = expectPlacesObserverNotifications(
    ["bookmark-removed"],
    false,
    false
  );
  const promise = promiseRankingChanged();
  await PlacesUtils.bookmarks.remove(folder1);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(folder1.guid), null);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);

  await promise;

  let expectedNotifications = [
    {
      type: "bookmark-removed",
      guid: folder1.guid,
    },
  ];

  // If we're skipping descendents, we'll only be notified of the folder.
  skipDescendantsObserver.check(expectedNotifications);

  // Note: Items of folders get notified first.
  expectedNotifications.unshift({
    type: "bookmark-removed",
    guid: bm1.guid,
  });
  // If we don't skip descendents, we'll be notified of the folder and the
  // bookmark.
  receiveAllObserver.check(expectedNotifications);
});

add_task(async function test_nested_contents_removed() {
  let folder1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  let folder2 = await PlacesUtils.bookmarks.insert({
    parentGuid: folder1.guid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: folder2.guid,
    type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
    url: "http://example.com/",
    title: "",
  });

  const promise = promiseRankingChanged();
  await PlacesUtils.bookmarks.remove(folder1);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(folder1.guid), null);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(folder2.guid), null);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);

  await promise;
});

add_task(async function remove_folder_empty_title() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "",
  });
  checkBookmarkObject(bm1);

  await PlacesUtils.bookmarks.remove(bm1.guid);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);
});

add_task(async function remove_separator() {
  let bm1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
  });
  checkBookmarkObject(bm1);

  await PlacesUtils.bookmarks.remove(bm1.guid);
  Assert.strictEqual(await PlacesUtils.bookmarks.fetch(bm1.guid), null);
});

add_task(async function test_nested_content_fails_when_not_allowed() {
  let folder1 = await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  await PlacesUtils.bookmarks.insert({
    parentGuid: folder1.guid,
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    title: "a folder",
  });
  await Assert.rejects(
    PlacesUtils.bookmarks.remove(folder1, {
      preventRemovalOfNonEmptyFolders: true,
    }),
    /Cannot remove a non-empty folder./
  );
});

add_task(async function test_remove_bookmark_with_invalid_url() {
  let folder = await PlacesUtils.bookmarks.insert({
    type: PlacesUtils.bookmarks.TYPE_FOLDER,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title: "folder",
  });
  let guid = "invalid_____";
  let folderedGuid = "invalid____2";
  let url = "invalid-uri";
  await PlacesUtils.withConnectionWrapper("test_bookmarks_remove", async db => {
    await db.execute(
      `
      INSERT INTO moz_places(url, url_hash, title, rev_host, guid)
      VALUES (:url, hash(:url), 'Invalid URI', '.', GENERATE_GUID())
    `,
      { url }
    );
    await db.execute(
      `INSERT INTO moz_bookmarks (type, fk, parent, position, guid)
        VALUES (:type,
                (SELECT id FROM moz_places WHERE url = :url),
                (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid),
                (SELECT MAX(position) + 1 FROM moz_bookmarks WHERE parent = (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid)),
                :guid)
      `,
      {
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        url,
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        guid,
      }
    );
    await db.execute(
      `INSERT INTO moz_bookmarks (type, fk, parent, position, guid)
        VALUES (:type,
                (SELECT id FROM moz_places WHERE url = :url),
                (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid),
                (SELECT MAX(position) + 1 FROM moz_bookmarks WHERE parent = (SELECT id FROM moz_bookmarks WHERE guid = :parentGuid)),
                :guid)
      `,
      {
        type: PlacesUtils.bookmarks.TYPE_BOOKMARK,
        url,
        parentGuid: folder.guid,
        guid: folderedGuid,
      }
    );
  });
  await PlacesUtils.bookmarks.remove(guid);
  Assert.strictEqual(
    await PlacesUtils.bookmarks.fetch(guid),
    null,
    "Should not throw and not find the bookmark"
  );

  await PlacesUtils.bookmarks.remove(folder);
  Assert.strictEqual(
    await PlacesUtils.bookmarks.fetch(folderedGuid),
    null,
    "Should not throw and not find the bookmark"
  );
});