summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_connection_bug388287.js
blob: d6f0c3c9d0619d3bcc02daaaa1d881c636f878b0 (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
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

function test() {
  waitForExplicitFinish();
  const connectionURL =
    "chrome://browser/content/preferences/dialogs/connection.xhtml";
  let closeable = false;
  let finalTest = false;

  // The changed preferences need to be backed up and restored because this mochitest
  // changes them 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.share_proxy_settings");
    for (let proxyType of ["http", "ssl", "socks"]) {
      Services.prefs.clearUserPref("network.proxy." + proxyType);
      Services.prefs.clearUserPref("network.proxy." + proxyType + "_port");
      if (proxyType == "http") {
        continue;
      }
      Services.prefs.clearUserPref("network.proxy.backup." + proxyType);
      Services.prefs.clearUserPref(
        "network.proxy.backup." + proxyType + "_port"
      );
    }
    // 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");
  });

  /*
   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) {
    let dialog, dialogClosingPromise, dialogElement;
    let proxyTypePref, sharePref, httpPref, httpPortPref;

    // Convenient function to reset the variables for the new window
    async function setDoc() {
      if (closeable) {
        let dialogClosingEvent = await dialogClosingPromise;
        ok(dialogClosingEvent, "Connection dialog closed");
      }

      if (finalTest) {
        gBrowser.removeCurrentTab();
        finish();
        return;
      }

      dialog = await openAndLoadSubDialog(connectionURL);
      dialogElement = dialog.document.getElementById("ConnectionsDialog");
      dialogClosingPromise = BrowserTestUtils.waitForEvent(
        dialogElement,
        "dialogclosing"
      );

      proxyTypePref = dialog.Preferences.get("network.proxy.type");
      sharePref = dialog.Preferences.get("network.proxy.share_proxy_settings");
      httpPref = dialog.Preferences.get("network.proxy.http");
      httpPortPref = dialog.Preferences.get("network.proxy.http_port");
    }

    // This batch of tests should not close the dialog
    await setDoc();

    // Testing HTTP port 0 with share on
    proxyTypePref.value = 1;
    sharePref.value = true;
    httpPref.value = "localhost";
    httpPortPref.value = 0;
    dialogElement.acceptDialog();

    // Testing HTTP port 0 + FTP port 80 with share off
    sharePref.value = false;
    dialogElement.acceptDialog();

    // Testing HTTP port 80 + FTP port 0 with share off
    httpPortPref.value = 80;
    dialogElement.acceptDialog();

    // From now on, the dialog should close since we are giving it legitimate inputs.
    // The test will timeout if the onbeforeaccept kicks in erroneously.
    closeable = true;

    // Both ports 80, share on
    httpPortPref.value = 80;
    dialogElement.acceptDialog();

    // HTTP 80, FTP 0, with share on
    await setDoc();
    proxyTypePref.value = 1;
    sharePref.value = true;
    httpPref.value = "localhost";
    httpPortPref.value = 80;
    dialogElement.acceptDialog();

    // HTTP host empty, port 0 with share on
    await setDoc();
    proxyTypePref.value = 1;
    sharePref.value = true;
    httpPref.value = "";
    httpPortPref.value = 0;
    dialogElement.acceptDialog();

    // HTTP 0, but in no proxy mode
    await setDoc();
    proxyTypePref.value = 0;
    sharePref.value = true;
    httpPref.value = "localhost";
    httpPortPref.value = 0;

    // This is the final test, don't spawn another connection window
    finalTest = true;
    dialogElement.acceptDialog();
    await setDoc();
  });
}