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

add_task(async function test_fetchTags() {
  let tags = await PlacesUtils.bookmarks.fetchTags();
  Assert.deepEqual(tags, []);

  let bm = await PlacesUtils.bookmarks.insert({
    url: "http://page1.com/",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });

  PlacesUtils.tagging.tagURI(bm.url.URI, ["1", "2"]);
  tags = await PlacesUtils.bookmarks.fetchTags();
  Assert.deepEqual(tags, [
    { name: "1", count: 1 },
    { name: "2", count: 1 },
  ]);

  PlacesUtils.tagging.untagURI(bm.url.URI, ["1"]);
  tags = await PlacesUtils.bookmarks.fetchTags();
  Assert.deepEqual(tags, [{ name: "2", count: 1 }]);

  let bm2 = await PlacesUtils.bookmarks.insert({
    url: "http://page2.com/",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  PlacesUtils.tagging.tagURI(bm2.url.URI, ["2", "3"]);
  tags = await PlacesUtils.bookmarks.fetchTags();
  Assert.deepEqual(tags, [
    { name: "2", count: 2 },
    { name: "3", count: 1 },
  ]);
});

add_task(async function test_fetch_by_tags() {
  await Assert.rejects(
    PlacesUtils.bookmarks.fetch({ tags: "" }),
    /Invalid value for property 'tags'/
  );
  await Assert.rejects(
    PlacesUtils.bookmarks.fetch({ tags: [] }),
    /Invalid value for property 'tags'/
  );
  await Assert.rejects(
    PlacesUtils.bookmarks.fetch({ tags: null }),
    /Invalid value for property 'tags'/
  );
  await Assert.rejects(
    PlacesUtils.bookmarks.fetch({ tags: [""] }),
    /Invalid value for property 'tags'/
  );
  await Assert.rejects(
    PlacesUtils.bookmarks.fetch({ tags: ["valid", null] }),
    /Invalid value for property 'tags'/
  );

  info("Add bookmarks with tags.");
  let bm1 = await PlacesUtils.bookmarks.insert({
    url: "http://bacon.org/",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  PlacesUtils.tagging.tagURI(bm1.url.URI, ["egg", "ratafià"]);
  let bm2 = await PlacesUtils.bookmarks.insert({
    url: "http://mushroom.org/",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  PlacesUtils.tagging.tagURI(bm2.url.URI, ["egg"]);

  info("Fetch a single tag.");
  let bms = [];
  Assert.equal(
    (await PlacesUtils.bookmarks.fetch({ tags: ["egg"] }, b => bms.push(b)))
      .guid,
    bm2.guid,
    "Found the expected recent bookmark"
  );
  Assert.deepEqual(
    bms.map(b => b.guid),
    [bm2.guid, bm1.guid],
    "Found the expected bookmarks"
  );

  info("Fetch multiple tags.");
  bms = [];
  Assert.equal(
    (
      await PlacesUtils.bookmarks.fetch({ tags: ["egg", "ratafià"] }, b =>
        bms.push(b)
      )
    ).guid,
    bm1.guid,
    "Found the expected recent bookmark"
  );
  Assert.deepEqual(
    bms.map(b => b.guid),
    [bm1.guid],
    "Found the expected bookmarks"
  );

  info("Fetch a nonexisting tag.");
  bms = [];
  Assert.equal(
    await PlacesUtils.bookmarks.fetch({ tags: ["egg", "tomato"] }, b =>
      bms.push(b)
    ),
    null,
    "Should not find any bookmark"
  );
  Assert.deepEqual(bms, [], "Should not find any bookmark");

  info("Check case insensitive");
  bms = [];
  Assert.equal(
    (
      await PlacesUtils.bookmarks.fetch({ tags: ["eGg", "raTafiÀ"] }, b =>
        bms.push(b)
      )
    ).guid,
    bm1.guid,
    "Found the expected recent bookmark"
  );
  Assert.deepEqual(
    bms.map(b => b.guid),
    [bm1.guid],
    "Found the expected bookmarks"
  );
});