summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/favicons/head.js
blob: ce16afd33f6dd3156a3394b4751f5e4f65fb888d (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

ChromeUtils.defineESModuleGetters(this, {
  LinkHandlerParent: "resource:///actors/LinkHandlerParent.sys.mjs",
  PlacesTestUtils: "resource://testing-common/PlacesTestUtils.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",

  XPCShellContentUtils:
    "resource://testing-common/XPCShellContentUtils.sys.mjs",
});

// Clear the network cache between every test to make sure we get a stable state
Services.cache2.clear();

function waitForFaviconMessage(isTabIcon = undefined, expectedURL = undefined) {
  return new Promise((resolve, reject) => {
    let listener = (name, data) => {
      if (name != "SetIcon" && name != "SetFailedIcon") {
        return; // Ignore unhandled messages
      }

      // If requested filter out loads of the wrong kind of icon.
      if (isTabIcon != undefined && isTabIcon != data.canUseForTab) {
        return;
      }

      if (expectedURL && data.originalURL != expectedURL) {
        return;
      }

      LinkHandlerParent.removeListenerForTests(listener);

      if (name == "SetIcon") {
        resolve({
          iconURL: data.originalURL,
          dataURL: data.iconURL,
          canUseForTab: data.canUseForTab,
        });
      } else {
        reject({
          iconURL: data.originalURL,
          canUseForTab: data.canUseForTab,
        });
      }
    };

    LinkHandlerParent.addListenerForTests(listener);
  });
}

function waitForFavicon(browser, url) {
  return new Promise(resolve => {
    let listener = {
      onLinkIconAvailable(b, dataURI, iconURI) {
        if (b !== browser || iconURI != url) {
          return;
        }

        gBrowser.removeTabsProgressListener(listener);
        resolve();
      },
    };

    gBrowser.addTabsProgressListener(listener);
  });
}

function waitForLinkAvailable(browser) {
  let resolve, reject;

  let listener = {
    onLinkIconAvailable(b, dataURI, iconURI) {
      // Ignore icons for other browsers or empty icons.
      if (browser !== b || !iconURI) {
        return;
      }

      gBrowser.removeTabsProgressListener(listener);
      resolve(iconURI);
    },
  };

  let promise = new Promise((res, rej) => {
    resolve = res;
    reject = rej;

    gBrowser.addTabsProgressListener(listener);
  });

  promise.cancel = () => {
    gBrowser.removeTabsProgressListener(listener);

    reject();
  };

  return promise;
}