toolkit/components/passwordmgr/test/test_xml_load.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/passwordmgr/test/test_xml_load.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,179 @@
     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="prompt_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: XML prompt
    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 +/** Test for Login Manager: XML prompts. **/
    1.25 +var pwmgr, login1, login2;
    1.26 +
    1.27 +function initLogins() {
    1.28 +  pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
    1.29 +                       .getService(Ci.nsILoginManager);
    1.30 +
    1.31 +  login1 = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]
    1.32 +                        .createInstance(Ci.nsILoginInfo);
    1.33 +  login2 = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]
    1.34 +                        .createInstance(Ci.nsILoginInfo);
    1.35 +
    1.36 +  login1.init("http://mochi.test:8888", null, "xml",
    1.37 +               "xmluser1", "xmlpass1", "", "");
    1.38 +  login2.init("http://mochi.test:8888", null, "xml2",
    1.39 +               "xmluser2", "xmlpass2", "", "");
    1.40 +
    1.41 +  pwmgr.addLogin(login1);
    1.42 +  pwmgr.addLogin(login2);
    1.43 +}
    1.44 +
    1.45 +function finishTest() {
    1.46 +  ok(true, "finishTest removing testing logins...");
    1.47 +  pwmgr.removeLogin(login1);
    1.48 +  pwmgr.removeLogin(login2);
    1.49 +
    1.50 +  SimpleTest.finish();
    1.51 +}
    1.52 +
    1.53 +function handleDialog(doc, testNum) {
    1.54 +  ok(true, "handleDialog running for test " + testNum);
    1.55 +
    1.56 +  var clickOK = true;
    1.57 +  var userfield = doc.getElementById("loginTextbox");
    1.58 +  var passfield = doc.getElementById("password1Textbox");
    1.59 +  var username = userfield.getAttribute("value");
    1.60 +  var password = passfield.getAttribute("value");
    1.61 +  var dialog    = doc.getElementById("commonDialog");
    1.62 +
    1.63 +  switch(testNum) {
    1.64 +    case 1:
    1.65 +        is(username, "xmluser1", "Checking provided username");
    1.66 +        is(password, "xmlpass1", "Checking provided password");
    1.67 +        break;
    1.68 +
    1.69 +    case 2:
    1.70 +        is(username, "xmluser2", "Checking provided username");
    1.71 +        is(password, "xmlpass2", "Checking provided password");
    1.72 +
    1.73 +        // Check that the dialog has the correct parent
    1.74 +        ok(doc.defaultView.opener, "dialog has opener");
    1.75 +        // Not using is() because its "expected" text doesn't deal
    1.76 +        // with window objects very well 
    1.77 +        // Disabled due to Bug 718543.
    1.78 +        // ok(doc.defaultView.opener == window, "dialog's opener is correct");
    1.79 +
    1.80 +        break;
    1.81 +
    1.82 +    default:
    1.83 +        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
    1.84 +        break;
    1.85 +  }
    1.86 +
    1.87 +  // Explicitly cancel the dialog and report a fail in this failure
    1.88 +  // case, rather than letting the dialog get stuck due to an auth
    1.89 +  // failure and having the test timeout.
    1.90 +  if (!username && !password) {
    1.91 +      ok(false, "No values prefilled");
    1.92 +      clickOK = false;
    1.93 +  }
    1.94 +
    1.95 +  if (clickOK)
    1.96 +    dialog.acceptDialog();
    1.97 +  else
    1.98 +    dialog.cancelDialog();
    1.99 +
   1.100 +  ok(true, "handleDialog done");
   1.101 +  didDialog = true;
   1.102 +}
   1.103 +
   1.104 +var newWin;
   1.105 +function xmlLoad(responseDoc) {
   1.106 +  ok(true, "xmlLoad running for test " + testNum);
   1.107 +
   1.108 +  // The server echos back the user/pass it received.
   1.109 +  var username = responseDoc.getElementById("user").textContent;
   1.110 +  var password = responseDoc.getElementById("pass").textContent;
   1.111 +  var authok = responseDoc.getElementById("ok").textContent;
   1.112 +
   1.113 +  switch(testNum) {
   1.114 +    case 1:
   1.115 +        is(username, "xmluser1", "Checking provided username");
   1.116 +        is(password, "xmlpass1", "Checking provided password");
   1.117 +        break;
   1.118 +
   1.119 +    case 2:
   1.120 +        is(username, "xmluser2", "Checking provided username");
   1.121 +        is(password, "xmlpass2", "Checking provided password");
   1.122 +
   1.123 +        newWin.close();
   1.124 +        break;
   1.125 +
   1.126 +    default:
   1.127 +        ok(false, "Uhh, unhandled switch for testNum #" + testNum);
   1.128 +        break;
   1.129 +  }
   1.130 +  
   1.131 +  doTest();
   1.132 +}
   1.133 +
   1.134 +function doTest() {
   1.135 +  switch(++testNum) {
   1.136 +    case 1:
   1.137 +        startCallbackTimer();
   1.138 +        makeRequest("authenticate.sjs?user=xmluser1&pass=xmlpass1&realm=xml");
   1.139 +        break;
   1.140 +
   1.141 +    case 2:
   1.142 +        // Test correct parenting, by opening another window and
   1.143 +        // making sure the prompt's opener is correct
   1.144 +        newWin = window.open();
   1.145 +        newWin.focus();
   1.146 +        startCallbackTimer();
   1.147 +        makeRequest("authenticate.sjs?user=xmluser2&pass=xmlpass2&realm=xml2");
   1.148 +        break;
   1.149 +
   1.150 +    default:
   1.151 +        finishTest();
   1.152 +  }
   1.153 +}
   1.154 +
   1.155 +function makeRequest(uri) {
   1.156 +  var xmlDoc = document.implementation.createDocument("", "test", null);
   1.157 +
   1.158 +  function documentLoaded(e) {
   1.159 +      xmlLoad(xmlDoc);
   1.160 +  }
   1.161 +  xmlDoc.addEventListener("load", documentLoaded, false);
   1.162 +  xmlDoc.load(uri);
   1.163 +}
   1.164 +
   1.165 +
   1.166 +initLogins();
   1.167 +
   1.168 +// clear plain HTTP auth sessions before the test, to allow
   1.169 +// running them more than once.
   1.170 +var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
   1.171 +                           .getService(SpecialPowers.Ci.nsIHttpAuthManager);
   1.172 +authMgr.clearAll();
   1.173 +
   1.174 +// start the tests
   1.175 +testNum = 0;
   1.176 +doTest();
   1.177 +
   1.178 +SimpleTest.waitForExplicitFinish();
   1.179 +</script>
   1.180 +</pre>
   1.181 +</body>
   1.182 +</html>

mercurial