summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_PlacesUtils_invalidateCachedGuids.js
blob: 33bf696a295efda4645364625af9f62db1f6a985 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

async function changeGuid(db, id, newGuid) {
  await db.execute(
    `UPDATE moz_bookmarks SET
       guid = :newGuid
     WHERE id = :id`,
    { id, newGuid }
  );
}

add_task(async function test_invalidateCachedGuids() {
  info("Add bookmarks");
  await PlacesUtils.bookmarks.insertTree({
    guid: PlacesUtils.bookmarks.unfiledGuid,
    children: [
      {
        guid: "bookmarkAAAA",
        url: "http://example.com/a",
        title: "A",
      },
      {
        guid: "bookmarkBBBB",
        url: "http://example.com/b",
        title: "B",
      },
    ],
  });

  let ids = await PlacesUtils.promiseManyItemIds([
    "bookmarkAAAA",
    "bookmarkBBBB",
  ]);
  Assert.equal(
    await PlacesUtils.promiseItemGuid(ids.get("bookmarkAAAA")),
    "bookmarkAAAA"
  );
  Assert.equal(
    await PlacesUtils.promiseItemGuid(ids.get("bookmarkBBBB")),
    "bookmarkBBBB"
  );

  info("Change GUIDs");
  await PlacesUtils.withConnectionWrapper(
    "test_invalidateCachedGuids",
    async function(db) {
      await db.executeTransaction(async function() {
        await changeGuid(db, ids.get("bookmarkAAAA"), "bookmarkCCCC");
        await changeGuid(db, ids.get("bookmarkBBBB"), "bookmarkDDDD");
      });
    }
  );
  Assert.equal(
    await PlacesUtils.promiseItemId("bookmarkAAAA"),
    ids.get("bookmarkAAAA")
  );
  Assert.equal(
    await PlacesUtils.promiseItemId("bookmarkBBBB"),
    ids.get("bookmarkBBBB")
  );

  info("Invalidate the cache");
  PlacesUtils.invalidateCachedGuids();

  let newIds = await PlacesUtils.promiseManyItemIds([
    "bookmarkCCCC",
    "bookmarkDDDD",
  ]);
  Assert.equal(
    await PlacesUtils.promiseItemGuid(newIds.get("bookmarkCCCC")),
    "bookmarkCCCC"
  );
  Assert.equal(
    await PlacesUtils.promiseItemGuid(newIds.get("bookmarkDDDD")),
    "bookmarkDDDD"
  );
  await Assert.rejects(
    PlacesUtils.promiseItemId("bookmarkAAAA"),
    /no item found for the given GUID/
  );
  await Assert.rejects(
    PlacesUtils.promiseItemId("bookmarkBBBB"),
    /no item found for the given GUID/
  );
});