summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/siteIdentity/browser_identity_UI.js
blob: bda5e225b0a3e5c4a3f333353e7c92b6f361f523 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Tests for correct behaviour of getHostForDisplay on identity handler */

requestLongerTimeout(2);

// Greek IDN for 'example.test'.
var idnDomain =
  "\u03C0\u03B1\u03C1\u03AC\u03B4\u03B5\u03B9\u03B3\u03BC\u03B1.\u03B4\u03BF\u03BA\u03B9\u03BC\u03AE";
var tests = [
  {
    name: "normal domain",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    location: "http://test1.example.org/",
    hostForDisplay: "test1.example.org",
    hasSubview: true,
  },
  {
    name: "view-source",
    location: "view-source:http://example.com/",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    newURI: "http://example.com/",
    hostForDisplay: "example.com",
    hasSubview: true,
  },
  {
    name: "normal HTTPS",
    location: "https://example.com/",
    hostForDisplay: "example.com",
    hasSubview: true,
  },
  {
    name: "IDN subdomain",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    location: "http://sub1.xn--hxajbheg2az3al.xn--jxalpdlp/",
    hostForDisplay: "sub1." + idnDomain,
    hasSubview: true,
  },
  {
    name: "subdomain with port",
    // eslint-disable-next-line @microsoft/sdl/no-insecure-url
    location: "http://sub1.test1.example.org:8000/",
    hostForDisplay: "sub1.test1.example.org",
    hasSubview: true,
  },
  {
    name: "subdomain HTTPS",
    location: "https://test1.example.com/",
    hostForDisplay: "test1.example.com",
    hasSubview: true,
  },
  {
    name: "view-source HTTPS",
    location: "view-source:https://example.com/",
    newURI: "https://example.com/",
    hostForDisplay: "example.com",
    hasSubview: true,
  },
  {
    name: "IP address",
    location: "http://127.0.0.1:8888/",
    hostForDisplay: "127.0.0.1",
    hasSubview: false,
  },
  {
    name: "about:certificate",
    location:
      "about:certificate?cert=MIIHQjCCBiqgAwIBAgIQCgYwQn9bvO&cert=1pVzllk7ZFHzANBgkqhkiG9w0BAQ",
    hostForDisplay: "about:certificate",
    hasSubview: false,
  },
  {
    name: "about:reader",
    location: "about:reader?url=http://example.com",
    hostForDisplay: "example.com",
    hasSubview: false,
  },
  {
    name: "chrome:",
    location: "chrome://global/skin/in-content/info-pages.css",
    hostForDisplay: "chrome://global/skin/in-content/info-pages.css",
    hasSubview: false,
  },
];

add_task(async function test() {
  ok(gIdentityHandler, "gIdentityHandler should exist");

  await BrowserTestUtils.openNewForegroundTab(gBrowser);

  for (let i = 0; i < tests.length; i++) {
    await runTest(i, true);
  }

  gBrowser.removeCurrentTab();
  await BrowserTestUtils.openNewForegroundTab(gBrowser);

  for (let i = tests.length - 1; i >= 0; i--) {
    await runTest(i, false);
  }

  gBrowser.removeCurrentTab();
});

async function runTest(i, forward) {
  let currentTest = tests[i];
  let testDesc = "#" + i + " (" + currentTest.name + ")";
  if (!forward) {
    testDesc += " (second time)";
  }

  info("Running test " + testDesc);

  let popupHidden = null;
  if ((forward && i > 0) || (!forward && i < tests.length - 1)) {
    popupHidden = BrowserTestUtils.waitForEvent(
      gIdentityHandler._identityPopup,
      "popuphidden"
    );
  }

  let loaded = BrowserTestUtils.browserLoaded(
    gBrowser.selectedBrowser,
    false,
    currentTest.location
  );
  BrowserTestUtils.loadURIString(
    gBrowser.selectedBrowser,
    currentTest.location
  );
  await loaded;
  await popupHidden;
  ok(
    !gIdentityHandler._identityPopup ||
      BrowserTestUtils.is_hidden(gIdentityHandler._identityPopup),
    "Control Center is hidden"
  );

  // Sanity check other values, and the value of gIdentityHandler.getHostForDisplay()
  is(
    gIdentityHandler._uri.spec,
    currentTest.newURI || currentTest.location,
    "location matches for test " + testDesc
  );
  // getHostForDisplay can't be called for all modes
  if (currentTest.hostForDisplay !== null) {
    is(
      gIdentityHandler.getHostForDisplay(),
      currentTest.hostForDisplay,
      "hostForDisplay matches for test " + testDesc
    );
  }

  // Open the Control Center and make sure it closes after nav (Bug 1207542).
  let popupShown = BrowserTestUtils.waitForEvent(
    window,
    "popupshown",
    true,
    event => event.target == gIdentityHandler._identityPopup
  );
  gIdentityHandler._identityIconBox.click();
  info("Waiting for the Control Center to be shown");
  await popupShown;
  ok(
    !BrowserTestUtils.is_hidden(gIdentityHandler._identityPopup),
    "Control Center is visible"
  );
  let displayedHost = currentTest.hostForDisplay || currentTest.location;
  ok(
    gIdentityHandler._identityPopupMainViewHeaderLabel.textContent.includes(
      displayedHost
    ),
    "identity UI header shows the host for test " + testDesc
  );

  let securityButton = gBrowser.ownerDocument.querySelector(
    "#identity-popup-security-button"
  );
  is(
    securityButton.disabled,
    !currentTest.hasSubview,
    "Security button has correct disabled state"
  );
  if (currentTest.hasSubview) {
    // Show the subview, which is an easy way in automation to reproduce
    // Bug 1207542, where the CC wouldn't close on navigation.
    let promiseViewShown = BrowserTestUtils.waitForEvent(
      gIdentityHandler._identityPopup,
      "ViewShown"
    );
    securityButton.click();
    await promiseViewShown;
  }
}