1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/passwordmgr/test/test_xhr.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,180 @@ 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: XHR 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: XHR prompts. **/ 1.25 +var pwmgr, login1, login2; 1.26 + 1.27 +function initLogins() { 1.28 + pwmgr = Cc["@mozilla.org/login-manager;1"]. 1.29 + getService(Ci.nsILoginManager); 1.30 + 1.31 + login1 = Cc["@mozilla.org/login-manager/loginInfo;1"]. 1.32 + createInstance(Ci.nsILoginInfo); 1.33 + login2 = Cc["@mozilla.org/login-manager/loginInfo;1"]. 1.34 + createInstance(Ci.nsILoginInfo); 1.35 + 1.36 + login1.init("http://mochi.test:8888", null, "xhr", 1.37 + "xhruser1", "xhrpass1", "", ""); 1.38 + login2.init("http://mochi.test:8888", null, "xhr2", 1.39 + "xhruser2", "xhrpass2", "", ""); 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, "xhruser1", "Checking provided username"); 1.66 + is(password, "xhrpass1", "Checking provided password"); 1.67 + break; 1.68 + 1.69 + case 2: 1.70 + is(username, "xhruser2", "Checking provided username"); 1.71 + is(password, "xhrpass2", "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 xhrLoad(xmlDoc) { 1.106 + ok(true, "xhrLoad running for test " + testNum); 1.107 + 1.108 + // The server echos back the user/pass it received. 1.109 + var username = xmlDoc.getElementById("user").textContent; 1.110 + var password = xmlDoc.getElementById("pass").textContent; 1.111 + var authok = xmlDoc.getElementById("ok").textContent; 1.112 + 1.113 + 1.114 + switch(testNum) { 1.115 + case 1: 1.116 + is(username, "xhruser1", "Checking provided username"); 1.117 + is(password, "xhrpass1", "Checking provided password"); 1.118 + break; 1.119 + 1.120 + case 2: 1.121 + is(username, "xhruser2", "Checking provided username"); 1.122 + is(password, "xhrpass2", "Checking provided password"); 1.123 + 1.124 + newWin.close(); 1.125 + break; 1.126 + 1.127 + default: 1.128 + ok(false, "Uhh, unhandled switch for testNum #" + testNum); 1.129 + break; 1.130 + } 1.131 + 1.132 + doTest(); 1.133 +} 1.134 + 1.135 +function doTest() { 1.136 + switch(++testNum) { 1.137 + case 1: 1.138 + startCallbackTimer(); 1.139 + makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr"); 1.140 + break; 1.141 + 1.142 + case 2: 1.143 + // Test correct parenting, by opening another window and 1.144 + // making sure the prompt's opener is correct 1.145 + newWin = window.open(); 1.146 + newWin.focus(); 1.147 + startCallbackTimer(); 1.148 + makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2"); 1.149 + break; 1.150 + 1.151 + default: 1.152 + finishTest(); 1.153 + } 1.154 +} 1.155 + 1.156 +function makeRequest(uri) { 1.157 + var request = new XMLHttpRequest(); 1.158 + request.open("GET", uri, true); 1.159 + request.onreadystatechange = function () { 1.160 + if (request.readyState == 4) 1.161 + xhrLoad(request.responseXML); 1.162 + }; 1.163 + request.send(null); 1.164 +} 1.165 + 1.166 + 1.167 +initLogins(); 1.168 + 1.169 +// clear plain HTTP auth sessions before the test, to allow 1.170 +// running them more than once. 1.171 +var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] 1.172 + .getService(SpecialPowers.Ci.nsIHttpAuthManager); 1.173 +authMgr.clearAll(); 1.174 + 1.175 +// start the tests 1.176 +testNum = 0; 1.177 +doTest(); 1.178 + 1.179 +SimpleTest.waitForExplicitFinish(); 1.180 +</script> 1.181 +</pre> 1.182 +</body> 1.183 +</html>