summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_UrlbarInput_setURI.js
blob: 0fa365f8bc2c17bead9397703c54d75e3487cfb7 (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
/* 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();

  // avoid prompting about phishing
  Services.prefs.setIntPref(phishyUserPassPref, 32);
  registerCleanupFunction(function () {
    Services.prefs.clearUserPref(phishyUserPassPref);
  });

  nextTest();
}

const phishyUserPassPref = "network.http.phishy-userpass-length";

function nextTest() {
  let testCase = tests.shift();
  if (testCase) {
    testCase(function () {
      executeSoon(nextTest);
    });
  } else {
    executeSoon(finish);
  }
}

var tests = [
  function revert(next) {
    loadTabInWindow(window, function (tab) {
      gURLBar.handleRevert();
      is(
        gURLBar.value,
        "example.com",
        "URL bar had user/pass stripped after reverting"
      );
      gBrowser.removeTab(tab);
      next();
    });
  },
  function customize(next) {
    // Need to wait for delayedStartup for the customization part of the test,
    // since that's where BrowserToolboxCustomizeDone is set.
    BrowserTestUtils.openNewBrowserWindow().then(function (win) {
      loadTabInWindow(win, function () {
        openToolbarCustomizationUI(function () {
          closeToolbarCustomizationUI(function () {
            is(
              win.gURLBar.value,
              "example.com",
              "URL bar had user/pass stripped after customize"
            );
            win.close();
            next();
          }, win);
        }, win);
      });
    });
  },
  function pageloaderror(next) {
    loadTabInWindow(window, function (tab) {
      // Load a new URL and then immediately stop it, to simulate a page load
      // error.
      BrowserTestUtils.loadURIString(
        tab.linkedBrowser,
        "http://test1.example.com"
      );
      tab.linkedBrowser.stop();
      is(
        gURLBar.value,
        "example.com",
        "URL bar had user/pass stripped after load error"
      );
      gBrowser.removeTab(tab);
      next();
    });
  },
];

function loadTabInWindow(win, callback) {
  info("Loading tab");
  let url = "http://user:pass@example.com/";
  let tab = (win.gBrowser.selectedTab = BrowserTestUtils.addTab(
    win.gBrowser,
    url
  ));
  BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, url).then(() => {
    info("Tab loaded");
    is(
      win.gURLBar.value,
      "example.com",
      "URL bar had user/pass stripped initially"
    );
    callback(tab);
  }, true);
}

function openToolbarCustomizationUI(aCallback, aBrowserWin) {
  if (!aBrowserWin) {
    aBrowserWin = window;
  }

  aBrowserWin.gCustomizeMode.enter();

  aBrowserWin.gNavToolbox.addEventListener(
    "customizationready",
    function () {
      executeSoon(function () {
        aCallback(aBrowserWin);
      });
    },
    { once: true }
  );
}

function closeToolbarCustomizationUI(aCallback, aBrowserWin) {
  aBrowserWin.gNavToolbox.addEventListener(
    "aftercustomization",
    function () {
      executeSoon(aCallback);
    },
    { once: true }
  );

  aBrowserWin.gCustomizeMode.exit();
}