summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/favicons/test_copyFavicons.js
blob: 687b799a4b1b1c9400343735aadea34c416e7ed5 (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
const TEST_URI1 = Services.io.newURI("http://mozilla.com/");
const TEST_URI2 = Services.io.newURI("http://places.com/");
const TEST_URI3 = Services.io.newURI("http://bookmarked.com/");
const LOAD_NON_PRIVATE = PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE;
const LOAD_PRIVATE = PlacesUtils.favicons.FAVICON_LOAD_PRIVATE;

function copyFavicons(source, dest, inPrivate) {
  return new Promise(resolve => {
    PlacesUtils.favicons.copyFavicons(
      source,
      dest,
      inPrivate ? LOAD_PRIVATE : LOAD_NON_PRIVATE,
      resolve
    );
  });
}

function promisePageChanged(url) {
  return PlacesTestUtils.waitForNotification("favicon-changed", events =>
    events.some(e => e.url == url)
  );
}

add_task(async function test_copyFavicons_inputcheck() {
  Assert.throws(
    () => PlacesUtils.favicons.copyFavicons(null, TEST_URI2, LOAD_PRIVATE),
    /NS_ERROR_ILLEGAL_VALUE/
  );
  Assert.throws(
    () => PlacesUtils.favicons.copyFavicons(TEST_URI1, null, LOAD_PRIVATE),
    /NS_ERROR_ILLEGAL_VALUE/
  );
  Assert.throws(
    () => PlacesUtils.favicons.copyFavicons(TEST_URI1, TEST_URI2, 3),
    /NS_ERROR_ILLEGAL_VALUE/
  );
  Assert.throws(
    () => PlacesUtils.favicons.copyFavicons(TEST_URI1, TEST_URI2, -1),
    /NS_ERROR_ILLEGAL_VALUE/
  );
  Assert.throws(
    () => PlacesUtils.favicons.copyFavicons(TEST_URI1, TEST_URI2, null),
    /NS_ERROR_ILLEGAL_VALUE/
  );
});

add_task(async function test_copyFavicons_noop() {
  info("Unknown uris");
  Assert.equal(
    await copyFavicons(TEST_URI1, TEST_URI2, false),
    null,
    "Icon should not have been copied"
  );

  info("Unknown dest uri");
  await PlacesTestUtils.addVisits(TEST_URI1);
  Assert.equal(
    await copyFavicons(TEST_URI1, TEST_URI2, false),
    null,
    "Icon should not have been copied"
  );

  info("Unknown dest uri");
  await PlacesTestUtils.addVisits(TEST_URI1);
  Assert.equal(
    await copyFavicons(TEST_URI1, TEST_URI2, false),
    null,
    "Icon should not have been copied"
  );

  info("Unknown dest uri, source has icon");
  await setFaviconForPage(TEST_URI1, SMALLPNG_DATA_URI);
  Assert.equal(
    await copyFavicons(TEST_URI1, TEST_URI2, false),
    null,
    "Icon should not have been copied"
  );

  info("Known uris, source has icon, private");
  await PlacesTestUtils.addVisits(TEST_URI2);
  Assert.equal(
    await copyFavicons(TEST_URI1, TEST_URI2, true),
    null,
    "Icon should not have been copied"
  );

  PlacesUtils.favicons.expireAllFavicons();
  await PlacesUtils.history.clear();
});

add_task(async function test_copyFavicons() {
  info("Normal copy across 2 pages");
  await PlacesTestUtils.addVisits(TEST_URI1);
  await setFaviconForPage(TEST_URI1, SMALLPNG_DATA_URI);
  await setFaviconForPage(TEST_URI1, SMALLSVG_DATA_URI);
  await PlacesTestUtils.addVisits(TEST_URI2);
  let promiseChange = promisePageChanged(TEST_URI2.spec);
  Assert.equal(
    (await copyFavicons(TEST_URI1, TEST_URI2, false)).spec,
    SMALLSVG_DATA_URI.spec,
    "Icon should have been copied"
  );
  await promiseChange;
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI2, 1),
    SMALLPNG_DATA_URI.spec,
    "Small icon found"
  );
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI2),
    SMALLSVG_DATA_URI.spec,
    "Large icon found"
  );

  info("Private copy to a bookmarked page");
  await PlacesUtils.bookmarks.insert({
    url: TEST_URI3,
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
  });
  promiseChange = promisePageChanged(TEST_URI3.spec);
  Assert.equal(
    (await copyFavicons(TEST_URI1, TEST_URI3, true)).spec,
    SMALLSVG_DATA_URI.spec,
    "Icon should have been copied"
  );
  await promiseChange;
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI3, 1),
    SMALLPNG_DATA_URI.spec,
    "Small icon found"
  );
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI3),
    SMALLSVG_DATA_URI.spec,
    "Large icon found"
  );

  PlacesUtils.favicons.expireAllFavicons();
  await PlacesUtils.history.clear();
});

add_task(async function test_copyFavicons_overlap() {
  info("Copy to a page that has one of the favicons already");
  await PlacesTestUtils.addVisits(TEST_URI1);
  await setFaviconForPage(TEST_URI1, SMALLPNG_DATA_URI);
  await setFaviconForPage(TEST_URI1, SMALLSVG_DATA_URI);
  await PlacesTestUtils.addVisits(TEST_URI2);
  await setFaviconForPage(TEST_URI2, SMALLPNG_DATA_URI);
  let promiseChange = promisePageChanged(TEST_URI2.spec);
  Assert.equal(
    (await copyFavicons(TEST_URI1, TEST_URI2, false)).spec,
    SMALLSVG_DATA_URI.spec,
    "Icon should have been copied"
  );
  await promiseChange;
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI2, 1),
    SMALLPNG_DATA_URI.spec,
    "Small icon found"
  );
  Assert.equal(
    await getFaviconUrlForPage(TEST_URI2),
    SMALLSVG_DATA_URI.spec,
    "Large icon found"
  );
});