summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_connection.js
blob: 01cdf571f20bfc09e4c725ab9f3200e9aa8f9945 (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
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* 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/. */

function test() {
  waitForExplicitFinish();

  // network.proxy.type needs to be backed up and restored because mochitest
  // changes this setting from the default
  let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type");
  registerCleanupFunction(function () {
    Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType);
    Services.prefs.clearUserPref("network.proxy.no_proxies_on");
    // On accepting the dialog, we also write TRR values, so we need to clear
    // them. They are tested separately in browser_privacy_dnsoverhttps.js.
    Services.prefs.clearUserPref("network.trr.mode");
    Services.prefs.clearUserPref("network.trr.uri");
  });

  let connectionURL =
    "chrome://browser/content/preferences/dialogs/connection.xhtml";

  /*
  The connection dialog alone won't save onaccept since it uses type="child",
  so it has to be opened as a sub dialog of the main pref tab.
  Open the main tab here.
  */
  open_preferences(async function tabOpened(aContentWindow) {
    is(
      gBrowser.currentURI.spec,
      "about:preferences",
      "about:preferences loaded"
    );
    let dialog = await openAndLoadSubDialog(connectionURL);
    let dialogElement = dialog.document.getElementById("ConnectionsDialog");
    let dialogClosingPromise = BrowserTestUtils.waitForEvent(
      dialogElement,
      "dialogclosing"
    );

    ok(dialog, "connection window opened");
    runConnectionTests(dialog);
    dialogElement.acceptDialog();

    let dialogClosingEvent = await dialogClosingPromise;
    ok(dialogClosingEvent, "connection window closed");
    // runConnectionTests will have changed this pref - make sure it was
    // sanitized correctly when the dialog was accepted
    is(
      Services.prefs.getCharPref("network.proxy.no_proxies_on"),
      ".a.com,.b.com,.c.com",
      "no_proxies_on pref has correct value"
    );
    gBrowser.removeCurrentTab();
    finish();
  });
}

// run a bunch of tests on the window containing connection.xul
function runConnectionTests(win) {
  let doc = win.document;
  let networkProxyNone = doc.getElementById("networkProxyNone");
  let networkProxyNonePref = win.Preferences.get("network.proxy.no_proxies_on");
  let networkProxyTypePref = win.Preferences.get("network.proxy.type");

  // make sure the networkProxyNone textbox is formatted properly
  is(networkProxyNone.localName, "textarea", "networkProxyNone is a textarea");
  is(
    networkProxyNone.getAttribute("rows"),
    "2",
    "networkProxyNone textbox has two rows"
  );

  // make sure manual proxy controls are disabled when the window is opened
  let networkProxyHTTP = doc.getElementById("networkProxyHTTP");
  is(networkProxyHTTP.disabled, true, "networkProxyHTTP textbox is disabled");

  // check if sanitizing the given input for the no_proxies_on pref results in
  // expected string
  function testSanitize(input, expected, errorMessage) {
    networkProxyNonePref.value = input;
    win.gConnectionsDialog.sanitizeNoProxiesPref();
    is(networkProxyNonePref.value, expected, errorMessage);
  }

  // change this pref so proxy exceptions are actually configurable
  networkProxyTypePref.value = 1;
  is(networkProxyNone.disabled, false, "networkProxyNone textbox is enabled");

  testSanitize(".a.com", ".a.com", "sanitize doesn't mess up single filter");
  testSanitize(
    ".a.com, .b.com, .c.com",
    ".a.com, .b.com, .c.com",
    "sanitize doesn't mess up multiple comma/space sep filters"
  );
  testSanitize(
    ".a.com\n.b.com",
    ".a.com,.b.com",
    "sanitize turns line break into comma"
  );
  testSanitize(
    ".a.com,\n.b.com",
    ".a.com,.b.com",
    "sanitize doesn't add duplicate comma after comma"
  );
  testSanitize(
    ".a.com\n,.b.com",
    ".a.com,.b.com",
    "sanitize doesn't add duplicate comma before comma"
  );
  testSanitize(
    ".a.com,\n,.b.com",
    ".a.com,,.b.com",
    "sanitize doesn't add duplicate comma surrounded by commas"
  );
  testSanitize(
    ".a.com, \n.b.com",
    ".a.com, .b.com",
    "sanitize doesn't add comma after comma/space"
  );
  testSanitize(
    ".a.com\n .b.com",
    ".a.com, .b.com",
    "sanitize adds comma before space"
  );
  testSanitize(
    ".a.com\n\n\n;;\n;\n.b.com",
    ".a.com,.b.com",
    "sanitize only adds one comma per substring of bad chars"
  );
  testSanitize(
    ".a.com,,.b.com",
    ".a.com,,.b.com",
    "duplicate commas from user are untouched"
  );
  testSanitize(
    ".a.com\n.b.com\n.c.com,\n.d.com,\n.e.com",
    ".a.com,.b.com,.c.com,.d.com,.e.com",
    "sanitize replaces things globally"
  );

  // will check that this was sanitized properly after window closes
  networkProxyNonePref.value = ".a.com;.b.com\n.c.com";
}