summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/siteIdentity/browser_identityIcon_img_url.js
blob: 9ef8b6dfedc890b05277e7ec475b4726357ab602 (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
/* 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/. */
/**
 * Test Bug 1562881 - Ensuring the identity icon loads correct img in different
 *                    circumstances.
 */

const kBaseURI = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);

const kBaseURILocalhost = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://127.0.0.1"
);

const TEST_CASES = [
  {
    type: "http",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    testURL: "http://example.com",
    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
  },
  {
    type: "https",
    testURL: "https://example.com",
    img_url: `url("chrome://global/skin/icons/security.svg")`,
  },
  {
    type: "non-chrome about page",
    testURL: "about:about",
    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
  },
  {
    type: "chrome about page",
    testURL: "about:preferences",
    img_url: `url("chrome://branding/content/icon${
      window.devicePixelRatio > 1 ? 32 : 16
    }.png")`,
  },
  {
    type: "file",
    testURL: "dummy_page.html",
    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
  },
  {
    type: "resource",
    testURL: "resource://gre/modules/Log.sys.mjs",
    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
  },
  {
    type: "mixedPassiveContent",
    testURL: kBaseURI + "file_mixedPassiveContent.html",
    img_url: `url("chrome://global/skin/icons/security-warning.svg")`,
  },
  {
    type: "mixedActiveContent",
    testURL: kBaseURI + "file_csp_block_all_mixedcontent.html",
    img_url: `url("chrome://global/skin/icons/security.svg")`,
  },
  {
    type: "certificateError",
    testURL: "https://self-signed.example.com",
    img_url: `url("chrome://global/skin/icons/security-warning.svg")`,
  },
  {
    type: "localhost",
    testURL: "http://127.0.0.1",
    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
  },
  {
    type: "localhost + http frame",
    testURL: kBaseURILocalhost + "file_csp_block_all_mixedcontent.html",
    img_url: `url("chrome://global/skin/icons/page-portrait.svg")`,
  },
  {
    type: "data URI",
    testURL: "data:text/html,<div>",
    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
  },
  {
    type: "view-source HTTP",
    testURL: "view-source:http://example.com/",
    img_url: `url("chrome://global/skin/icons/security-broken.svg")`,
  },
  {
    type: "view-source HTTPS",
    testURL: "view-source:https://example.com/",
    img_url: `url("chrome://global/skin/icons/security.svg")`,
  },
];

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // By default, proxies don't apply to 127.0.0.1. We need them to for this test, though:
      ["network.proxy.allow_hijacking_localhost", true],
      ["security.mixed_content.upgrade_display_content", false],
    ],
  });

  for (let testData of TEST_CASES) {
    info(`Testing for ${testData.type}`);
    // Open the page for testing.
    let testURL = testData.testURL;

    // Overwrite the url if it is testing the file url.
    if (testData.type === "file") {
      let dir = getChromeDir(getResolvedURI(gTestPath));
      dir.append(testURL);
      dir.normalize();
      testURL = Services.io.newFileURI(dir).spec;
    }

    let pageLoaded;
    let tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      () => {
        gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, testURL);
        let browser = gBrowser.selectedBrowser;
        if (testData.type === "certificateError") {
          pageLoaded = BrowserTestUtils.waitForErrorPage(browser);
        } else {
          pageLoaded = BrowserTestUtils.browserLoaded(browser);
        }
      },
      false
    );
    await pageLoaded;

    let identityIcon = document.getElementById("identity-icon");

    // Get the image url from the identity icon.
    let identityIconImageURL = gBrowser.ownerGlobal
      .getComputedStyle(identityIcon)
      .getPropertyValue("list-style-image");

    is(
      identityIconImageURL,
      testData.img_url,
      "The identity icon has a correct image url."
    );

    BrowserTestUtils.removeTab(tab);
  }
});