summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/favicons/test_setAndFetchFaviconForPage_failures.js
blob: d5b16bd67059ff7fc909cb24ec40f3e0d6839efa (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * This file tests setAndFetchFaviconForPage when it is called with invalid
 * arguments, and when no favicon is stored for the given arguments.
 */

let faviconURI = Services.io.newURI(
  "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal16.png"
);
add_task(async function () {
  registerCleanupFunction(async function () {
    await PlacesUtils.bookmarks.eraseEverything();
    await PlacesUtils.history.clear();
  });

  // We'll listen for favicon changes for the whole test, to ensure only the
  // last one will send a notification. Due to thread serialization, at that
  // point we can be sure previous calls didn't send a notification.
  let lastPageURI = Services.io.newURI("http://example.com/verification");
  let promiseIconChanged = PlacesTestUtils.waitForNotification(
    "favicon-changed",
    events =>
      events.some(
        e => e.url == lastPageURI.spec && e.faviconUrl == SMALLPNG_DATA_URI.spec
      )
  );

  info("Test null page uri");
  Assert.throws(
    () => {
      PlacesUtils.favicons.setAndFetchFaviconForPage(
        null,
        faviconURI,
        true,
        PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
        null,
        Services.scriptSecurityManager.getSystemPrincipal()
      );
    },
    /NS_ERROR_ILLEGAL_VALUE/,
    "Exception expected because aPageURI is null"
  );

  info("Test null favicon uri");
  Assert.throws(
    () => {
      PlacesUtils.favicons.setAndFetchFaviconForPage(
        Services.io.newURI("http://example.com/null_faviconURI"),
        null,
        true,
        PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
        null,
        Services.scriptSecurityManager.getSystemPrincipal()
      );
    },
    /NS_ERROR_ILLEGAL_VALUE/,
    "Exception expected because aFaviconURI is null."
  );

  info("Test about uri");
  PlacesUtils.favicons.setAndFetchFaviconForPage(
    Services.io.newURI("about:testAboutURI"),
    faviconURI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  info("Test private browsing non bookmarked uri");
  let pageURI = Services.io.newURI("http://example.com/privateBrowsing");
  await PlacesTestUtils.addVisits({
    uri: pageURI,
    transitionType: TRANSITION_TYPED,
  });
  PlacesUtils.favicons.setAndFetchFaviconForPage(
    pageURI,
    faviconURI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  info("Test disabled history");
  pageURI = Services.io.newURI("http://example.com/disabledHistory");
  await PlacesTestUtils.addVisits({
    uri: pageURI,
    transition: TRANSITION_TYPED,
  });
  Services.prefs.setBoolPref("places.history.enabled", false);

  PlacesUtils.favicons.setAndFetchFaviconForPage(
    pageURI,
    faviconURI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus
  // we can set the preference back to true immediately.
  Services.prefs.setBoolPref("places.history.enabled", true);

  info("Test error icon");
  // This error icon must stay in sync with FAVICON_ERRORPAGE_URL in
  // nsIFaviconService.idl and aboutNetError.html.
  let faviconErrorPageURI = Services.io.newURI(
    "chrome://global/skin/icons/info.svg"
  );
  pageURI = Services.io.newURI("http://example.com/errorIcon");
  await PlacesTestUtils.addVisits({
    uri: pageURI,
    transition: TRANSITION_TYPED,
  });
  PlacesUtils.favicons.setAndFetchFaviconForPage(
    pageURI,
    faviconErrorPageURI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  info("Test nonexisting page");
  PlacesUtils.favicons.setAndFetchFaviconForPage(
    Services.io.newURI("http://example.com/nonexistingPage"),
    faviconURI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  info("Final sanity check");
  // This is the only test that should cause the waitForFaviconChanged
  // callback to be invoked.
  await PlacesTestUtils.addVisits({
    uri: lastPageURI,
    transition: TRANSITION_TYPED,
  });
  PlacesUtils.favicons.setAndFetchFaviconForPage(
    lastPageURI,
    SMALLPNG_DATA_URI,
    true,
    PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    null,
    Services.scriptSecurityManager.getSystemPrincipal()
  );

  await promiseIconChanged;
});