toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/test_privbrowsing_perwindowpb.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,311 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=248970
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 248970</title>
    1.11 +  <script type="application/javascript"
    1.12 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.13 +  <script type="text/javascript" src="notification_common.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css"
    1.15 +        href="chrome://mochikit/content/tests/SimpleTest/test.css">
    1.16 +</head>
    1.17 +<body>
    1.18 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=248970">Mozilla Bug 248970</a>
    1.19 +<p id="display"></p>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript">
    1.22 +
    1.23 +/** Test for Bug 248970 **/
    1.24 +// based on test_notifications.html
    1.25 +
    1.26 +const Ci = SpecialPowers.Ci;
    1.27 +const Cc = SpecialPowers.Cc;
    1.28 +const Cr = SpecialPowers.Cr;
    1.29 +
    1.30 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.31 +
    1.32 +var testpath = "/tests/toolkit/components/passwordmgr/test/";
    1.33 +var prefix = "http://test2.example.com" + testpath;
    1.34 +var subtests = [
    1.35 +                   "subtst_privbrowsing_1.html", // 1
    1.36 +                   "subtst_privbrowsing_1.html", // 2
    1.37 +                   "subtst_privbrowsing_1.html", // 3
    1.38 +                   "subtst_privbrowsing_2.html", // 4
    1.39 +                   "subtst_privbrowsing_2.html", // 5
    1.40 +                   "subtst_privbrowsing_2.html", // 6
    1.41 +                   "subtst_privbrowsing_3.html", // 7
    1.42 +                   "subtst_privbrowsing_3.html", // 8
    1.43 +                   "subtst_privbrowsing_4.html", // 9
    1.44 +                   "subtst_privbrowsing_3.html" // 10
    1.45 +               ];
    1.46 +
    1.47 +var testNum = 0;
    1.48 +function loadNextTest() {
    1.49 +  // run the initialization code for each test
    1.50 +  switch (++ testNum) {
    1.51 +    case 1:
    1.52 +      popupNotifications = normalWindowPopupNotifications;
    1.53 +      iframe = normalWindowIframe;
    1.54 +      break;
    1.55 +
    1.56 +    case 2:
    1.57 +      popupNotifications = privateWindowPopupNotifications;
    1.58 +      iframe = privateWindowIframe;
    1.59 +      break;
    1.60 +
    1.61 +    case 3:
    1.62 +      popupNotifications = normalWindowPopupNotifications;
    1.63 +      iframe = normalWindowIframe;
    1.64 +      break;
    1.65 +
    1.66 +    case 4:
    1.67 +      pwmgr.addLogin(login);
    1.68 +      break;
    1.69 +
    1.70 +    case 5:
    1.71 +      popupNotifications = privateWindowPopupNotifications;
    1.72 +      iframe = privateWindowIframe;
    1.73 +      break;
    1.74 +
    1.75 +    case 6:
    1.76 +      popupNotifications = normalWindowPopupNotifications;
    1.77 +      iframe = normalWindowIframe;
    1.78 +      break;
    1.79 +
    1.80 +    case 7:
    1.81 +      pwmgr.addLogin(login);
    1.82 +      break;
    1.83 +
    1.84 +    case 8:
    1.85 +      popupNotifications = privateWindowPopupNotifications;
    1.86 +      iframe = privateWindowIframe;
    1.87 +      break;
    1.88 +
    1.89 +    case 9:
    1.90 +      break;
    1.91 +
    1.92 +    case 10:
    1.93 +      popupNotifications = normalWindowPopupNotifications;
    1.94 +      iframe = normalWindowIframe;
    1.95 +      break;
    1.96 +
    1.97 +    default:
    1.98 +      ok(false, "Unexpected call to loadNextTest for test #" + testNum);
    1.99 +  }
   1.100 +
   1.101 +  ok(true, "Starting test #" + testNum);
   1.102 +  iframe.src = prefix + subtests[testNum-1];
   1.103 +}
   1.104 +
   1.105 +
   1.106 +function checkTest() {
   1.107 +  var popup;
   1.108 +
   1.109 +  switch (testNum) {
   1.110 +    case 1:
   1.111 +      // run outside of private mode, popup notification should appear
   1.112 +      popup = getPopup(popupNotifications, "password-save");
   1.113 +      ok(popup, "got popup notification");
   1.114 +      popup.remove();
   1.115 +      break;
   1.116 +
   1.117 +    case 2:
   1.118 +      // run inside of private mode, popup notification should not appear
   1.119 +      popup = getPopup(popupNotifications, "password-save");
   1.120 +      ok(!popup, "checking for no popup notification");
   1.121 +      break;
   1.122 +
   1.123 +    case 3:
   1.124 +      // run outside of private mode, popup notification should appear
   1.125 +      popup = getPopup(popupNotifications, "password-save");
   1.126 +      ok(popup, "got popup notification");
   1.127 +      popup.remove();
   1.128 +      break;
   1.129 +
   1.130 +    case 4:
   1.131 +      // run outside of private mode, popup notification should appear
   1.132 +      popup = getPopup(popupNotifications, "password-change");
   1.133 +      ok(popup, "got popup notification");
   1.134 +      popup.remove();
   1.135 +      break;
   1.136 +
   1.137 +    case 5:
   1.138 +      // run inside of private mode, popup notification should not appear
   1.139 +      popup = getPopup(popupNotifications, "password-change");
   1.140 +      ok(!popup, "checking for no popup notification");
   1.141 +      break;
   1.142 +
   1.143 +    case 6:
   1.144 +      // run outside of private mode, popup notification should appear
   1.145 +      popup = getPopup(popupNotifications, "password-change");
   1.146 +      ok(popup, "got popup notification");
   1.147 +      popup.remove();
   1.148 +      pwmgr.removeLogin(login);
   1.149 +      break;
   1.150 +
   1.151 +    case 7:
   1.152 +      // verify that the user/pass pair was autofilled
   1.153 +      var gotUser = iframe.contentDocument.getElementById("user").textContent;
   1.154 +      var gotPass = iframe.contentDocument.getElementById("pass").textContent;
   1.155 +      is(gotUser, "notifyu1", "Checking submitted username");
   1.156 +      is(gotPass, "notifyp1", "Checking submitted password");
   1.157 +      break;
   1.158 +
   1.159 +    case 8:
   1.160 +      // verify that the user/pass pair was not autofilled
   1.161 +      var gotUser = iframe.contentDocument.getElementById("user").textContent;
   1.162 +      var gotPass = iframe.contentDocument.getElementById("pass").textContent;
   1.163 +      is(gotUser, "", "Checking submitted username");
   1.164 +      is(gotPass, "", "Checking submitted password");
   1.165 +      break;
   1.166 +
   1.167 +    case 9:
   1.168 +      // verify that the user/pass pair was available for autocomplete
   1.169 +      var gotUser = iframe.contentDocument.getElementById("user").textContent;
   1.170 +      var gotPass = iframe.contentDocument.getElementById("pass").textContent;
   1.171 +      is(gotUser, "notifyu1", "Checking submitted username");
   1.172 +      is(gotPass, "notifyp1", "Checking submitted password");
   1.173 +      break;
   1.174 +
   1.175 +    case 10:
   1.176 +      // verify that the user/pass pair was autofilled
   1.177 +      var gotUser = iframe.contentDocument.getElementById("user").textContent;
   1.178 +      var gotPass = iframe.contentDocument.getElementById("pass").textContent;
   1.179 +      is(gotUser, "notifyu1", "Checking submitted username");
   1.180 +      is(gotPass, "notifyp1", "Checking submitted password");
   1.181 +      pwmgr.removeLogin(login);
   1.182 +      break;
   1.183 +
   1.184 +    default:
   1.185 +      ok(false, "Unexpected call to checkTest for test #" + testNum);
   1.186 +
   1.187 +  }
   1.188 +}
   1.189 +
   1.190 +var mainWindow = window.QueryInterface(Ci.nsIInterfaceRequestor)
   1.191 +                    .getInterface(Ci.nsIWebNavigation)
   1.192 +                    .QueryInterface(Ci.nsIDocShellTreeItem)
   1.193 +                    .rootTreeItem
   1.194 +                    .QueryInterface(Ci.nsIInterfaceRequestor)
   1.195 +                    .getInterface(Ci.nsIDOMWindow);
   1.196 +var contentPage = "http://mochi.test:8888/tests/toolkit/components/passwordmgr/test/privbrowsing_perwindowpb_iframe.html";
   1.197 +var testWindows = [];
   1.198 +
   1.199 +function whenDelayedStartupFinished(aWindow, aCallback) {
   1.200 +  Services.obs.addObserver(function observer(aSubject, aTopic) {
   1.201 +    if (aWindow == aSubject) {
   1.202 +      Services.obs.removeObserver(observer, aTopic);
   1.203 +      setTimeout(aCallback, 0);
   1.204 +    }
   1.205 +  }, "browser-delayed-startup-finished", false);
   1.206 +}
   1.207 +
   1.208 +function testOnWindow(aIsPrivate, aCallback) {
   1.209 +  var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
   1.210 +  win.addEventListener("load", function onLoad() {
   1.211 +    win.removeEventListener("load", onLoad, false);
   1.212 +    whenDelayedStartupFinished(win, function() {
   1.213 +      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
   1.214 +        if (win.content.location.href != contentPage) {
   1.215 +          win.gBrowser.loadURI(contentPage);
   1.216 +          return;
   1.217 +        }
   1.218 +        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
   1.219 +
   1.220 +        win.content.addEventListener('load', function innerLoad2() {
   1.221 +          win.content.removeEventListener('load', innerLoad2, false);
   1.222 +          testWindows.push(win);
   1.223 +          SimpleTest.executeSoon(function() { aCallback(win); });
   1.224 +        }, false, true);
   1.225 +      }, true);
   1.226 +      SimpleTest.executeSoon(function() { win.gBrowser.loadURI(contentPage); });
   1.227 +    });
   1.228 +  }, true);
   1.229 +}
   1.230 +
   1.231 +var ignoreLoad = false;
   1.232 +function handleLoad(aEvent) {
   1.233 +  // ignore every other load event ... We get one for loading the subtest (which
   1.234 +  // we want to ignore), and another when the subtest's form submits itself
   1.235 +  // (which we want to handle, to start the next test).
   1.236 +  ignoreLoad = !ignoreLoad;
   1.237 +  if (ignoreLoad) {
   1.238 +    ok(true, "Ignoring load of subtest #" + testNum);
   1.239 +    return;
   1.240 +  }
   1.241 +  ok(true, "Processing submission of subtest #" + testNum);
   1.242 +
   1.243 +  checkTest();
   1.244 +
   1.245 +  if (testNum < subtests.length) {
   1.246 +    loadNextTest();
   1.247 +  } else {
   1.248 +    ok(true, "private browsing notification tests finished.");
   1.249 +
   1.250 +    testWindows.forEach(function(aWin) {
   1.251 +      aWin.close();
   1.252 +    });
   1.253 +
   1.254 +    SimpleTest.finish();
   1.255 +  }
   1.256 +}
   1.257 +
   1.258 +var pwmgr = Cc["@mozilla.org/login-manager;1"].
   1.259 +            getService(Ci.nsILoginManager);
   1.260 +ok(pwmgr != null, "Access pwmgr");
   1.261 +
   1.262 +// We need to make sure no logins have been stored by previous tests
   1.263 +// for forms in |url|, otherwise the change password notification
   1.264 +// would turn into a prompt, and the test will fail.
   1.265 +var url = "http://test2.example.com";
   1.266 +is(pwmgr.countLogins(url, "", null), 0, "No logins should be stored for " + url);
   1.267 +
   1.268 +var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1",
   1.269 +                                             Ci.nsILoginInfo, "init");
   1.270 +var login = new nsLoginInfo(url, url, null, "notifyu1", "notifyp1", "user", "pass");
   1.271 +
   1.272 +var normalWindow;
   1.273 +var privateWindow;
   1.274 +
   1.275 +var iframe;
   1.276 +var normalWindowIframe;
   1.277 +var privateWindowIframe;
   1.278 +
   1.279 +var popupNotifications;
   1.280 +var normalWindowPopupNotifications;
   1.281 +var privateWindowPopupNotifications;
   1.282 +
   1.283 +testOnWindow(false, function(aWin) {
   1.284 +  var selectedBrowser = aWin.gBrowser.selectedBrowser;
   1.285 +  normalWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
   1.286 +  normalWindowIframe.onload = handleLoad;
   1.287 +  selectedBrowser.focus();
   1.288 +
   1.289 +  normalWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
   1.290 +  ok(normalWindowPopupNotifications, "Got popupNotifications in normal window");
   1.291 +  // ignore the first load for this window;
   1.292 +  ignoreLoad = false;
   1.293 +
   1.294 +  testOnWindow(true, function(aPrivateWin) {
   1.295 +    selectedBrowser = aPrivateWin.gBrowser.selectedBrowser;
   1.296 +    privateWindowIframe = selectedBrowser.contentDocument.getElementById("iframe");
   1.297 +    privateWindowIframe.onload = handleLoad;
   1.298 +    selectedBrowser.focus();
   1.299 +
   1.300 +    privateWindowPopupNotifications = getPopupNotifications(selectedBrowser.contentWindow.top);
   1.301 +    ok(privateWindowPopupNotifications, "Got popupNotifications in private window");
   1.302 +    // ignore the first load for this window;
   1.303 +    ignoreLoad = false;
   1.304 +
   1.305 +    SimpleTest.executeSoon(loadNextTest);
   1.306 +  });
   1.307 +});
   1.308 +
   1.309 +SimpleTest.waitForExplicitFinish();
   1.310 +</script>
   1.311 +</pre>
   1.312 +</body>
   1.313 +</html>
   1.314 +

mercurial