Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>Test for Login Manager</title>
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
6 <script type="text/javascript" src="pwmgr_common.js"></script>
7 <script type="text/javascript" src="prompt_common.js"></script>
8 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
9 </head>
10 <body>
11 Login Manager test: XML prompt
12 <p id="display"></p>
14 <div id="content" style="display: none">
15 <iframe id="iframe"></iframe>
16 </div>
18 <pre id="test">
19 <script class="testbody" type="text/javascript">
21 /** Test for Login Manager: XML prompts. **/
22 var pwmgr, login1, login2;
24 function initLogins() {
25 pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]
26 .getService(Ci.nsILoginManager);
28 login1 = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]
29 .createInstance(Ci.nsILoginInfo);
30 login2 = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]
31 .createInstance(Ci.nsILoginInfo);
33 login1.init("http://mochi.test:8888", null, "xml",
34 "xmluser1", "xmlpass1", "", "");
35 login2.init("http://mochi.test:8888", null, "xml2",
36 "xmluser2", "xmlpass2", "", "");
38 pwmgr.addLogin(login1);
39 pwmgr.addLogin(login2);
40 }
42 function finishTest() {
43 ok(true, "finishTest removing testing logins...");
44 pwmgr.removeLogin(login1);
45 pwmgr.removeLogin(login2);
47 SimpleTest.finish();
48 }
50 function handleDialog(doc, testNum) {
51 ok(true, "handleDialog running for test " + testNum);
53 var clickOK = true;
54 var userfield = doc.getElementById("loginTextbox");
55 var passfield = doc.getElementById("password1Textbox");
56 var username = userfield.getAttribute("value");
57 var password = passfield.getAttribute("value");
58 var dialog = doc.getElementById("commonDialog");
60 switch(testNum) {
61 case 1:
62 is(username, "xmluser1", "Checking provided username");
63 is(password, "xmlpass1", "Checking provided password");
64 break;
66 case 2:
67 is(username, "xmluser2", "Checking provided username");
68 is(password, "xmlpass2", "Checking provided password");
70 // Check that the dialog has the correct parent
71 ok(doc.defaultView.opener, "dialog has opener");
72 // Not using is() because its "expected" text doesn't deal
73 // with window objects very well
74 // Disabled due to Bug 718543.
75 // ok(doc.defaultView.opener == window, "dialog's opener is correct");
77 break;
79 default:
80 ok(false, "Uhh, unhandled switch for testNum #" + testNum);
81 break;
82 }
84 // Explicitly cancel the dialog and report a fail in this failure
85 // case, rather than letting the dialog get stuck due to an auth
86 // failure and having the test timeout.
87 if (!username && !password) {
88 ok(false, "No values prefilled");
89 clickOK = false;
90 }
92 if (clickOK)
93 dialog.acceptDialog();
94 else
95 dialog.cancelDialog();
97 ok(true, "handleDialog done");
98 didDialog = true;
99 }
101 var newWin;
102 function xmlLoad(responseDoc) {
103 ok(true, "xmlLoad running for test " + testNum);
105 // The server echos back the user/pass it received.
106 var username = responseDoc.getElementById("user").textContent;
107 var password = responseDoc.getElementById("pass").textContent;
108 var authok = responseDoc.getElementById("ok").textContent;
110 switch(testNum) {
111 case 1:
112 is(username, "xmluser1", "Checking provided username");
113 is(password, "xmlpass1", "Checking provided password");
114 break;
116 case 2:
117 is(username, "xmluser2", "Checking provided username");
118 is(password, "xmlpass2", "Checking provided password");
120 newWin.close();
121 break;
123 default:
124 ok(false, "Uhh, unhandled switch for testNum #" + testNum);
125 break;
126 }
128 doTest();
129 }
131 function doTest() {
132 switch(++testNum) {
133 case 1:
134 startCallbackTimer();
135 makeRequest("authenticate.sjs?user=xmluser1&pass=xmlpass1&realm=xml");
136 break;
138 case 2:
139 // Test correct parenting, by opening another window and
140 // making sure the prompt's opener is correct
141 newWin = window.open();
142 newWin.focus();
143 startCallbackTimer();
144 makeRequest("authenticate.sjs?user=xmluser2&pass=xmlpass2&realm=xml2");
145 break;
147 default:
148 finishTest();
149 }
150 }
152 function makeRequest(uri) {
153 var xmlDoc = document.implementation.createDocument("", "test", null);
155 function documentLoaded(e) {
156 xmlLoad(xmlDoc);
157 }
158 xmlDoc.addEventListener("load", documentLoaded, false);
159 xmlDoc.load(uri);
160 }
163 initLogins();
165 // clear plain HTTP auth sessions before the test, to allow
166 // running them more than once.
167 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1']
168 .getService(SpecialPowers.Ci.nsIHttpAuthManager);
169 authMgr.clearAll();
171 // start the tests
172 testNum = 0;
173 doTest();
175 SimpleTest.waitForExplicitFinish();
176 </script>
177 </pre>
178 </body>
179 </html>