summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/favicons/test_setAndFetchFaviconForPage.js
blob: 1b4ea87ec0bb1c9c78f7f1918d507c30109b3368 (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
/* 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 the normal operation of setAndFetchFaviconForPage.

let gTests = [
  {
    desc: "Normal test",
    href: "http://example.com/normal",
    loadType: PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    async setup() {
      await PlacesTestUtils.addVisits({
        uri: this.href,
        transition: TRANSITION_TYPED,
      });
    },
  },
  {
    desc: "Bookmarked about: uri",
    href: "about:testAboutURI_bookmarked",
    loadType: PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    async setup() {
      await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: this.href,
      });
    },
  },
  {
    desc: "Bookmarked in private window",
    href: "http://example.com/privateBrowsing_bookmarked",
    loadType: PlacesUtils.favicons.FAVICON_LOAD_PRIVATE,
    async setup() {
      await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: this.href,
      });
    },
  },
  {
    desc: "Bookmarked with disabled history",
    href: "http://example.com/disabledHistory_bookmarked",
    loadType: PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE,
    async setup() {
      await PlacesUtils.bookmarks.insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: this.href,
      });
      Services.prefs.setBoolPref("places.history.enabled", false);
    },
    clean() {
      Services.prefs.setBoolPref("places.history.enabled", true);
    },
  },
];

add_task(async function () {
  let faviconURI = SMALLPNG_DATA_URI;
  let faviconMimeType = "image/png";

  registerCleanupFunction(async function () {
    await PlacesUtils.bookmarks.eraseEverything();
    await PlacesUtils.history.clear();
  });

  for (let test of gTests) {
    info(test.desc);
    let pageURI = PlacesUtils.toURI(test.href);

    await test.setup();

    let pageGuid;
    let promise = PlacesTestUtils.waitForNotification(
      "favicon-changed",
      events =>
        events.some(e => {
          if (e.url == pageURI.spec && e.faviconUrl == faviconURI.spec) {
            pageGuid = e.pageGuid;
            return true;
          }
          return false;
        })
    );

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

    await promise;

    Assert.equal(
      pageGuid,
      await PlacesTestUtils.getDatabaseValue("moz_places", "guid", {
        url: pageURI,
      }),
      "Page guid is correct"
    );
    let { dataLen, data, mimeType } = await PlacesUtils.promiseFaviconData(
      pageURI.spec
    );
    Assert.equal(faviconMimeType, mimeType, "Check expected MimeType");
    Assert.equal(
      SMALLPNG_DATA_LEN,
      data.length,
      "Check favicon data for the given page matches the provided data"
    );
    Assert.equal(
      dataLen,
      data.length,
      "Check favicon dataLen for the given page matches the provided data"
    );

    if (test.clean) {
      await test.clean();
    }
  }
});