toolkit/components/passwordmgr/test/test_notifications.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/test_notifications.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,436 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <title>Test for Login Manager</title>
     1.8 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>  
     1.9 +  <script type="text/javascript" src="pwmgr_common.js"></script>
    1.10 +  <script type="text/javascript" src="notification_common.js"></script>
    1.11 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.12 +</head>
    1.13 +<body>
    1.14 +Login Manager test: notifications
    1.15 +<p id="display"></p>
    1.16 +
    1.17 +<div id="content" style="display: none">
    1.18 +  <iframe id="iframe"></iframe>
    1.19 +</div>
    1.20 +
    1.21 +<pre id="test">
    1.22 +<script class="testbody" type="text/javascript">
    1.23 +
    1.24 +SimpleTest.expectAssertions(23);
    1.25 +
    1.26 +/** Test for Login Manager: notifications. **/
    1.27 +
    1.28 +// Set testpath to the directory where we live. Used to load tests from
    1.29 +// alternate Mochitest servers (different hostnames, same content).
    1.30 +var testpath = document.location.pathname + "/../";
    1.31 +
    1.32 +var subtests = [
    1.33 +                   "subtst_notifications_1.html", // 1
    1.34 +                   "subtst_notifications_1.html", // 2
    1.35 +                   "subtst_notifications_1.html", // 3
    1.36 +                   "subtst_notifications_1.html", // 4
    1.37 +                   "subtst_notifications_1.html", // 5
    1.38 +                   "subtst_notifications_1.html", // 6
    1.39 +                   "subtst_notifications_1.html", // 7
    1.40 +                   "subtst_notifications_1.html", // 8
    1.41 +                   "subtst_notifications_2.html", // 9
    1.42 +                   "subtst_notifications_3.html", // 10
    1.43 +                   "subtst_notifications_4.html", // 11
    1.44 +                   "subtst_notifications_5.html", // 12
    1.45 +                   "subtst_notifications_1.html", // 13
    1.46 +                   "subtst_notifications_6.html", // 14
    1.47 +                   "subtst_notifications_1.html", // 15
    1.48 +                   "subtst_notifications_6.html", // 16
    1.49 +                   "subtst_notifications_8.html", // 17
    1.50 +                   "subtst_notifications_8.html", // 18
    1.51 +                   "subtst_notifications_9.html", // 19
    1.52 +                   "subtst_notifications_10.html",  // 20
    1.53 +                   "http://test1.example.org:80" + testpath + "subtst_notifications_1.html", // 21
    1.54 +                   "http://test1.example.org:80" + testpath + "subtst_notifications_7.html", // 22
    1.55 +                   "http://test1.example.org:80" + testpath + "subtst_notifications_6.html", // 23
    1.56 +               ];
    1.57 +
    1.58 +
    1.59 +var ignoreLoad = false;
    1.60 +function handleLoad(aEvent) {
    1.61 +    // ignore every other load event ... We get one for loading the subtest (which
    1.62 +    // we want to ignore), and another when the subtest's form submits itself
    1.63 +    // (which we want to handle, to start the next test).
    1.64 +    ignoreLoad = !ignoreLoad;
    1.65 +    if (ignoreLoad) {
    1.66 +        ok(true, "Ignoring load of subtest #" + testNum);
    1.67 +        return;
    1.68 +    }
    1.69 +    ok(true, "Processing submission of subtest #" + testNum);
    1.70 +
    1.71 +    checkTest();
    1.72 +
    1.73 +    testNum++;
    1.74 +
    1.75 +    if (testNum <= subtests.length) {
    1.76 +        ok(true, "Starting test #" + testNum);
    1.77 +        iframe.src = subtests[testNum-1];
    1.78 +    } else {
    1.79 +        ok(true, "notification tests finished.");
    1.80 +        SimpleTest.finish();
    1.81 +    }
    1.82 +}
    1.83 +
    1.84 +
    1.85 +// Remember, Never for This Site, Not Now
    1.86 +function checkTest() {
    1.87 +    var popup, notificationText, expectedText;
    1.88 +
    1.89 +    // The document generated from formsubmit.sjs contains the user/pass it
    1.90 +    // received inside <span id="blah">value</span>
    1.91 +    var gotUser = SpecialPowers.wrap(iframe).contentDocument.getElementById("user").textContent;
    1.92 +    var gotPass = SpecialPowers.wrap(iframe).contentDocument.getElementById("pass").textContent;
    1.93 +
    1.94 +
    1.95 +    switch(testNum) {
    1.96 +
    1.97 +      /* Basic Yes/No/Never tests... */
    1.98 +
    1.99 +      case 1:
   1.100 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.101 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.102 +        popup = getPopup(popupNotifications, "password-save");
   1.103 +        ok(popup, "got notification popup");
   1.104 +        popup.remove();
   1.105 +        break;
   1.106 +
   1.107 +      case 2:
   1.108 +        // Same subtest, this time click Never
   1.109 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.110 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.111 +        popup = getPopup(popupNotifications, "password-save");
   1.112 +        ok(popup, "got notification popup");
   1.113 +        is(true, pwmgr.getLoginSavingEnabled("http://mochi.test:8888"),
   1.114 +           "Checking for login saving enabled");
   1.115 +        clickPopupButton(popup, kNeverButton);
   1.116 +        break;
   1.117 +
   1.118 +      case 3:
   1.119 +        // Same subtest, make sure Never took effect
   1.120 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.121 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.122 +        popup = getPopup(popupNotifications, "password-save");
   1.123 +        ok(!popup, "checking for no notification popup");
   1.124 +        is(false, pwmgr.getLoginSavingEnabled("http://mochi.test:8888"),
   1.125 +           "Checking for login saving disabled");
   1.126 +        // reenable login saving.
   1.127 +        pwmgr.setLoginSavingEnabled("http://mochi.test:8888", true);
   1.128 +        break;
   1.129 +
   1.130 +      case 4:
   1.131 +        // Same subtest, this time click Remember
   1.132 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.133 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.134 +        popup = getPopup(popupNotifications, "password-save");
   1.135 +        ok(popup, "got notification popup");
   1.136 +
   1.137 +        // Sanity check, no logins should exist yet.
   1.138 +        var logins = pwmgr.getAllLogins();
   1.139 +        is(logins.length, 0, "Should not have any logins yet");
   1.140 +
   1.141 +        clickPopupButton(popup, kRememberButton);
   1.142 +        break;
   1.143 +
   1.144 +      case 5:
   1.145 +        // Same subtest, make sure we didn't prompt for an existing login.
   1.146 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.147 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.148 +        popup = getPopup(popupNotifications, "password-save");
   1.149 +        ok(!popup, "checking for no notification popup");
   1.150 +
   1.151 +        // Check to make sure we updated the timestamps and use count on the
   1.152 +        // existing loging that was submitted for this form.
   1.153 +        var logins = pwmgr.getAllLogins();
   1.154 +        is(logins.length, 1, "Should only have 1 login");
   1.155 +        ok(SpecialPowers.call_Instanceof(logins[0], Ci.nsILoginMetaInfo), "metainfo QI");
   1.156 +        is(logins[0].timesUsed, 2, "check .timesUsed for existing login submission");
   1.157 +        ok(logins[0].timeLastUsed > logins[0].timeCreated, "timeLastUsed bumped");
   1.158 +        ok(logins[0].timeCreated == logins[0].timePasswordChanged, "timeChanged not updated");
   1.159 +
   1.160 +        // remove that login
   1.161 +        pwmgr.removeLogin(login1);
   1.162 +        break;
   1.163 +
   1.164 +      /* signons.rememberSignons pref tests... */
   1.165 +
   1.166 +      case 6:
   1.167 +        // Same subtest, make sure we're getting the popup again.
   1.168 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.169 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.170 +        popup = getPopup(popupNotifications, "password-save");
   1.171 +        ok(popup, "got notification popup");
   1.172 +        popup.remove();
   1.173 +        // Change prefs to no longer remember signons
   1.174 +        prefs.setBoolPref("rememberSignons", false);
   1.175 +        break;
   1.176 +
   1.177 +      case 7:
   1.178 +        // Same subtest, make sure we're not prompting.
   1.179 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.180 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.181 +        popup = getPopup(popupNotifications, "password-save");
   1.182 +        ok(!popup, "checking for no notification popup");
   1.183 +        // Change prefs to remember signons again
   1.184 +        prefs.setBoolPref("rememberSignons", true);
   1.185 +        break;
   1.186 +
   1.187 +      case 8:
   1.188 +        // Same subtest, make sure we're getting the popup again.
   1.189 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.190 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.191 +        popup = getPopup(popupNotifications, "password-save");
   1.192 +        ok(popup, "got notification popup");
   1.193 +        popup.remove();
   1.194 +        break;
   1.195 +
   1.196 +      /* autocomplete=off tests... */
   1.197 +
   1.198 +      case 9:
   1.199 +        // Check for notification popup when autocomplete=off present
   1.200 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.201 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.202 +        popup = getPopup(popupNotifications, "password-save");
   1.203 +        ok(popup, "checking for notification popup");
   1.204 +        popup.remove();
   1.205 +        break;
   1.206 +
   1.207 +      case 10:
   1.208 +        // Check for notification popup when autocomplete=off present
   1.209 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.210 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.211 +        popup = getPopup(popupNotifications, "password-save");
   1.212 +        ok(popup, "checking for notification popup");
   1.213 +        popup.remove();
   1.214 +        break;
   1.215 +
   1.216 +      case 11:
   1.217 +        // Check for notification popup when autocomplete=off present
   1.218 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.219 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.220 +        popup = getPopup(popupNotifications, "password-save");
   1.221 +        ok(popup, "checking for notification popup");
   1.222 +        popup.remove();
   1.223 +        break;
   1.224 +
   1.225 +      /* no password field test... */
   1.226 +
   1.227 +      case 12:
   1.228 +        // Check for no notification popup when no password field present
   1.229 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.230 +        is(gotPass, "null",     "Checking submitted password");
   1.231 +        popup = getPopup(popupNotifications, "password-save");
   1.232 +        ok(!popup, "checking for no notification popup");
   1.233 +
   1.234 +        // Add login for the next test.
   1.235 +        pwmgr.addLogin(login2);
   1.236 +        break;
   1.237 +
   1.238 +      case 13:
   1.239 +        // Check for no notification popup when existing pw-only login matches form.
   1.240 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.241 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.242 +        popup = getPopup(popupNotifications, "password-save");
   1.243 +        ok(!popup, "checking for no notification popup");
   1.244 +        pwmgr.removeLogin(login2);
   1.245 +
   1.246 +        // Add login for the next test
   1.247 +        pwmgr.addLogin(login1);
   1.248 +        break;
   1.249 +
   1.250 +      case 14:
   1.251 +        // Check for no notification popup when pw-only form matches existing login.
   1.252 +        is(gotUser, "null",     "Checking submitted username");
   1.253 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.254 +        popup = getPopup(popupNotifications, "password-save");
   1.255 +        ok(!popup, "checking for no notification popup");
   1.256 +        pwmgr.removeLogin(login1);
   1.257 +
   1.258 +        // Add login for the next test
   1.259 +        pwmgr.addLogin(login2B);
   1.260 +        break;
   1.261 +
   1.262 +      case 15:
   1.263 +        // Check for notification popup when existing pw-only login doesn't match form.
   1.264 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.265 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.266 +        popup = getPopup(popupNotifications, "password-save");
   1.267 +        ok(popup, "got notification popup");
   1.268 +        pwmgr.removeLogin(login2B);
   1.269 +        popup.remove();
   1.270 +
   1.271 +        // Add login for the next test
   1.272 +        pwmgr.addLogin(login1B);
   1.273 +        break;
   1.274 +
   1.275 +      case 16:
   1.276 +        // Check for notification popup when pw-only form doesn't match existing login.
   1.277 +        is(gotUser, "null",     "Checking submitted username");
   1.278 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.279 +        popup = getPopup(popupNotifications, "password-save");
   1.280 +        ok(popup, "got notification popup");
   1.281 +        pwmgr.removeLogin(login1B);
   1.282 +        popup.remove();
   1.283 +
   1.284 +        // Add login for the next tests
   1.285 +        pwmgr.addLogin(login1);
   1.286 +        break;
   1.287 +
   1.288 +      case 17:
   1.289 +        // Check for change-password popup, u+p login on u+p form. (not changed)
   1.290 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.291 +        is(gotPass, "pass2",    "Checking submitted password");
   1.292 +        popup = getPopup(popupNotifications, "password-change");
   1.293 +        ok(popup, "got notification popup");
   1.294 +        clickPopupButton(popup, kDontChangeButton);
   1.295 +        break;
   1.296 +
   1.297 +      case 18:
   1.298 +        // Check for change-password popup, u+p login on u+p form.
   1.299 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.300 +        is(gotPass, "pass2",    "Checking submitted password");
   1.301 +        popup = getPopup(popupNotifications, "password-change");
   1.302 +        ok(popup, "got notification popup");
   1.303 +        clickPopupButton(popup, kChangeButton);
   1.304 +
   1.305 +        // Check to make sure we updated the timestamps and use count for
   1.306 +        // the login being changed with this form.
   1.307 +        var logins = pwmgr.getAllLogins();
   1.308 +        is(logins.length, 1, "Should only have 1 login");
   1.309 +        ok(SpecialPowers.call_Instanceof(logins[0], Ci.nsILoginMetaInfo), "metainfo QI");
   1.310 +        is(logins[0].timesUsed, 2, "check .timesUsed incremented on change");
   1.311 +        ok(logins[0].timeCreated < logins[0].timeLastUsed, "timeLastUsed bumped");
   1.312 +        ok(logins[0].timeLastUsed == logins[0].timePasswordChanged, "timeUsed == timeChanged");
   1.313 +
   1.314 +        // cleanup
   1.315 +        login1.password = "pass2";
   1.316 +        pwmgr.removeLogin(login1);
   1.317 +        login1.password = "notifyp1";
   1.318 +
   1.319 +        // Add login for the next test
   1.320 +        pwmgr.addLogin(login2);
   1.321 +        break;
   1.322 +
   1.323 +      // ...can't change a u+p login on a p-only form...
   1.324 +
   1.325 +      case 19:
   1.326 +        // Check for change-password popup, p-only login on u+p form.
   1.327 +        // (needed a different subtest for this because the login created in
   1.328 +        // test_0init was interfering)
   1.329 +        is(gotUser, "",         "Checking submitted username");
   1.330 +        is(gotPass, "pass2",    "Checking submitted password");
   1.331 +        popup = getPopup(popupNotifications, "password-change");
   1.332 +        ok(popup, "got notification popup");
   1.333 +        clickPopupButton(popup, kChangeButton);
   1.334 +        break;
   1.335 +
   1.336 +      case 20:
   1.337 +        // Check for change-password popup, p-only login on p-only form.
   1.338 +        is(gotUser, "null",     "Checking submitted username");
   1.339 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.340 +        popup = getPopup(popupNotifications, "password-change");
   1.341 +        ok(popup, "got notification popup");
   1.342 +        clickPopupButton(popup, kChangeButton);
   1.343 +
   1.344 +        pwmgr.removeLogin(login2);
   1.345 +        break;
   1.346 +
   1.347 +      case 21:
   1.348 +        // Check text on a user+pass notification popup
   1.349 +        is(gotUser, "notifyu1", "Checking submitted username");
   1.350 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.351 +        popup = getPopup(popupNotifications, "password-save");
   1.352 +        ok(popup, "got notification popup");
   1.353 +        // Check the text, which comes from the localized saveLoginText string.
   1.354 +        notificationText = popup.message;
   1.355 +        expectedText = /^Would you like to remember the password for \"notifyu1\" on example.org\?$/;
   1.356 +        ok(expectedText.test(notificationText), "Checking text: " + notificationText);
   1.357 +        popup.remove();
   1.358 +        break;
   1.359 +
   1.360 +      case 22:
   1.361 +        // Check text on a user+pass notification popup, username is really long
   1.362 +        is(gotUser, "nowisthetimeforallgoodmentocometotheaidoftheircountry", "Checking submitted username");
   1.363 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.364 +        popup = getPopup(popupNotifications, "password-save");
   1.365 +        ok(popup, "got notification popup");
   1.366 +        // Check the text, which comes from the localized saveLoginText string.
   1.367 +        notificationText = popup.message;
   1.368 +        expectedText = /^Would you like to remember the password for \"nowisthetimeforallgoodmentocom[^e]\" on example.org\?$/;
   1.369 +        ok(expectedText.test(notificationText), "Checking text: " + notificationText);
   1.370 +        popup.remove();
   1.371 +        break;
   1.372 +
   1.373 +      case 23:
   1.374 +        // Check text on a pass-only notification popup
   1.375 +        is(gotUser, "null",     "Checking submitted username");
   1.376 +        is(gotPass, "notifyp1", "Checking submitted password");
   1.377 +        popup = getPopup(popupNotifications, "password-save");
   1.378 +        ok(popup, "got notification popup");
   1.379 +        // Check the text, which comes from the localized saveLoginTextNoUser string.
   1.380 +        notificationText = popup.message;
   1.381 +        expectedText = /^Would you like to remember the password on example.org\?$/;
   1.382 +        ok(expectedText.test(notificationText), "Checking text: " + notificationText);
   1.383 +        popup.remove();
   1.384 +        break;
   1.385 +
   1.386 +      default:
   1.387 +        ok(false, "Unexpected call to checkTest for test #" + testNum);
   1.388 +
   1.389 +    }
   1.390 +
   1.391 +    // TODO:
   1.392 +    // * existing login test, form has different password --> change password, no save prompt
   1.393 +}
   1.394 +
   1.395 +const Ci = SpecialPowers.Ci;
   1.396 +const Cc = SpecialPowers.Cc;
   1.397 +ok(Ci != null, "Access Ci");
   1.398 +ok(Cc != null, "Access Cc");
   1.399 +
   1.400 +var pwmgr = Cc["@mozilla.org/login-manager;1"].
   1.401 +            getService(Ci.nsILoginManager);
   1.402 +ok(pwmgr != null, "Access pwmgr");
   1.403 +
   1.404 +pwmgr.removeAllLogins();
   1.405 +
   1.406 +var prefs = Cc["@mozilla.org/preferences-service;1"].
   1.407 +            getService(Ci.nsIPrefService);
   1.408 +ok(prefs != null, "Access prefs");
   1.409 +prefs = prefs.getBranch("signon.");
   1.410 +ok(prefs != null, "Access pref branch");
   1.411 +
   1.412 +var nsLoginInfo = new SpecialPowers.wrap(SpecialPowers.Components).Constructor("@mozilla.org/login-manager/loginInfo;1",
   1.413 +                                             Ci.nsILoginInfo, "init");
   1.414 +var login1 = new nsLoginInfo("http://mochi.test:8888", "http://mochi.test:8888", null,
   1.415 +                             "notifyu1", "notifyp1", "user", "pass");
   1.416 +var login2 = new nsLoginInfo("http://mochi.test:8888", "http://mochi.test:8888", null,
   1.417 +                             "", "notifyp1", "", "pass");
   1.418 +var login1B = new nsLoginInfo("http://mochi.test:8888", "http://mochi.test:8888", null,
   1.419 +                              "notifyu1B", "notifyp1B", "user", "pass");
   1.420 +var login2B = new nsLoginInfo("http://mochi.test:8888", "http://mochi.test:8888", null,
   1.421 +                              "", "notifyp1B", "", "pass");
   1.422 +
   1.423 +var iframe = document.getElementById("iframe");
   1.424 +iframe.onload = handleLoad;
   1.425 +
   1.426 +// popupNotifications (not *popup*) is a constant, per-tab container. So, we
   1.427 +// only need to fetch it once.
   1.428 +var popupNotifications = getPopupNotifications(window.top);
   1.429 +ok(popupNotifications, "Got popupNotifications");
   1.430 +
   1.431 +var testNum = 1;
   1.432 +ok(true, "Starting test #" + testNum);
   1.433 +iframe.src = subtests[testNum-1];
   1.434 +
   1.435 +SimpleTest.waitForExplicitFinish();
   1.436 +</script>
   1.437 +</pre>
   1.438 +</body>
   1.439 +</html>

mercurial