|
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: XHR prompt |
|
12 <p id="display"></p> |
|
13 |
|
14 <div id="content" style="display: none"> |
|
15 <iframe id="iframe"></iframe> |
|
16 </div> |
|
17 |
|
18 <pre id="test"> |
|
19 <script class="testbody" type="text/javascript"> |
|
20 |
|
21 /** Test for Login Manager: XHR prompts. **/ |
|
22 var pwmgr, login1, login2; |
|
23 |
|
24 function initLogins() { |
|
25 pwmgr = Cc["@mozilla.org/login-manager;1"]. |
|
26 getService(Ci.nsILoginManager); |
|
27 |
|
28 login1 = Cc["@mozilla.org/login-manager/loginInfo;1"]. |
|
29 createInstance(Ci.nsILoginInfo); |
|
30 login2 = Cc["@mozilla.org/login-manager/loginInfo;1"]. |
|
31 createInstance(Ci.nsILoginInfo); |
|
32 |
|
33 login1.init("http://mochi.test:8888", null, "xhr", |
|
34 "xhruser1", "xhrpass1", "", ""); |
|
35 login2.init("http://mochi.test:8888", null, "xhr2", |
|
36 "xhruser2", "xhrpass2", "", ""); |
|
37 |
|
38 pwmgr.addLogin(login1); |
|
39 pwmgr.addLogin(login2); |
|
40 } |
|
41 |
|
42 function finishTest() { |
|
43 ok(true, "finishTest removing testing logins..."); |
|
44 pwmgr.removeLogin(login1); |
|
45 pwmgr.removeLogin(login2); |
|
46 |
|
47 SimpleTest.finish(); |
|
48 } |
|
49 |
|
50 function handleDialog(doc, testNum) { |
|
51 ok(true, "handleDialog running for test " + testNum); |
|
52 |
|
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"); |
|
59 |
|
60 switch(testNum) { |
|
61 case 1: |
|
62 is(username, "xhruser1", "Checking provided username"); |
|
63 is(password, "xhrpass1", "Checking provided password"); |
|
64 break; |
|
65 |
|
66 case 2: |
|
67 is(username, "xhruser2", "Checking provided username"); |
|
68 is(password, "xhrpass2", "Checking provided password"); |
|
69 |
|
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"); |
|
76 |
|
77 break; |
|
78 |
|
79 default: |
|
80 ok(false, "Uhh, unhandled switch for testNum #" + testNum); |
|
81 break; |
|
82 } |
|
83 |
|
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 } |
|
91 |
|
92 if (clickOK) |
|
93 dialog.acceptDialog(); |
|
94 else |
|
95 dialog.cancelDialog(); |
|
96 |
|
97 ok(true, "handleDialog done"); |
|
98 didDialog = true; |
|
99 } |
|
100 |
|
101 var newWin; |
|
102 function xhrLoad(xmlDoc) { |
|
103 ok(true, "xhrLoad running for test " + testNum); |
|
104 |
|
105 // The server echos back the user/pass it received. |
|
106 var username = xmlDoc.getElementById("user").textContent; |
|
107 var password = xmlDoc.getElementById("pass").textContent; |
|
108 var authok = xmlDoc.getElementById("ok").textContent; |
|
109 |
|
110 |
|
111 switch(testNum) { |
|
112 case 1: |
|
113 is(username, "xhruser1", "Checking provided username"); |
|
114 is(password, "xhrpass1", "Checking provided password"); |
|
115 break; |
|
116 |
|
117 case 2: |
|
118 is(username, "xhruser2", "Checking provided username"); |
|
119 is(password, "xhrpass2", "Checking provided password"); |
|
120 |
|
121 newWin.close(); |
|
122 break; |
|
123 |
|
124 default: |
|
125 ok(false, "Uhh, unhandled switch for testNum #" + testNum); |
|
126 break; |
|
127 } |
|
128 |
|
129 doTest(); |
|
130 } |
|
131 |
|
132 function doTest() { |
|
133 switch(++testNum) { |
|
134 case 1: |
|
135 startCallbackTimer(); |
|
136 makeRequest("authenticate.sjs?user=xhruser1&pass=xhrpass1&realm=xhr"); |
|
137 break; |
|
138 |
|
139 case 2: |
|
140 // Test correct parenting, by opening another window and |
|
141 // making sure the prompt's opener is correct |
|
142 newWin = window.open(); |
|
143 newWin.focus(); |
|
144 startCallbackTimer(); |
|
145 makeRequest("authenticate.sjs?user=xhruser2&pass=xhrpass2&realm=xhr2"); |
|
146 break; |
|
147 |
|
148 default: |
|
149 finishTest(); |
|
150 } |
|
151 } |
|
152 |
|
153 function makeRequest(uri) { |
|
154 var request = new XMLHttpRequest(); |
|
155 request.open("GET", uri, true); |
|
156 request.onreadystatechange = function () { |
|
157 if (request.readyState == 4) |
|
158 xhrLoad(request.responseXML); |
|
159 }; |
|
160 request.send(null); |
|
161 } |
|
162 |
|
163 |
|
164 initLogins(); |
|
165 |
|
166 // clear plain HTTP auth sessions before the test, to allow |
|
167 // running them more than once. |
|
168 var authMgr = SpecialPowers.Cc['@mozilla.org/network/http-auth-manager;1'] |
|
169 .getService(SpecialPowers.Ci.nsIHttpAuthManager); |
|
170 authMgr.clearAll(); |
|
171 |
|
172 // start the tests |
|
173 testNum = 0; |
|
174 doTest(); |
|
175 |
|
176 SimpleTest.waitForExplicitFinish(); |
|
177 </script> |
|
178 </pre> |
|
179 </body> |
|
180 </html> |