summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/bookmarks/test_insertTree_fixupOrSkipInvalidEntries.js
blob: bd71ce59e04768ee205350555baada9851bb4fa6 (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
function insertTree(tree) {
  return PlacesUtils.bookmarks.insertTree(tree, {
    fixupOrSkipInvalidEntries: true,
  });
}

add_task(async function() {
  let guid = PlacesUtils.bookmarks.unfiledGuid;
  await Assert.throws(
    () => insertTree({ guid, children: [] }),
    /Should have a non-zero number of children to insert./
  );
  await Assert.throws(
    () => insertTree({ guid: "invalid", children: [{}] }),
    /The parent guid is not valid/
  );

  let now = new Date();
  let url = "http://mozilla.com/";
  let obs = {
    count: 0,
    lastIndex: 0,
    handlePlacesEvent(events) {
      for (let event of events) {
        obs.count++;
        let lastIndex = obs.lastIndex;
        obs.lastIndex = event.index;
        if (event.itemType == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
          Assert.equal(event.url, url, "Found the expected url");
        }
        Assert.ok(
          event.index == 0 || event.index == lastIndex + 1,
          "Consecutive indices"
        );
        Assert.ok(event.dateAdded >= now, "Found a valid dateAdded");
        Assert.ok(PlacesUtils.isValidGuid(event.guid), "guid is valid");
      }
    },
  };
  PlacesUtils.observers.addListener(["bookmark-added"], obs.handlePlacesEvent);

  let tree = {
    guid,
    children: [
      {
        // Should be inserted, and the invalid guid should be replaced.
        guid: "test",
        url,
      },
      {
        // Should be skipped, since the url is invalid.
        url: "fake_url",
      },
      {
        // Should be skipped, since the type is invalid.
        url,
        type: 999,
      },
      {
        // Should be skipped, since the type is invalid.
        type: 999,
        children: [
          {
            url,
          },
        ],
      },
      {
        type: PlacesUtils.bookmarks.TYPE_FOLDER,
        title: "test",
        children: [
          {
            // Should fix lastModified and dateAdded.
            url,
            lastModified: null,
          },
          {
            // Should be skipped, since the url is invalid.
            url: "fake_url",
            dateAdded: null,
          },
          {
            // Should fix lastModified and dateAdded.
            url,
            dateAdded: undefined,
          },
          {
            // Should be skipped since it's a separator with a url
            url,
            type: PlacesUtils.bookmarks.TYPE_SEPARATOR,
          },
          {
            // Should fix lastModified and dateAdded.
            url,
            dateAdded: new Date(now - 86400000),
            lastModified: new Date(now - 172800000), // less than dateAdded
          },
        ],
      },
    ],
  };

  let bms = await insertTree(tree);
  for (let bm of bms) {
    checkBookmarkObject(bm);
  }
  Assert.equal(bms.length, 5);
  Assert.equal(obs.count, bms.length);

  PlacesUtils.observers.removeListener(
    ["bookmark-added"],
    obs.handlePlacesEvent
  );
});