WebCore:
[WebKit-https.git] / LayoutTests / http / tests / security / resources / cross-frame-access.js
1 function log(s)
2 {
3     document.getElementById("console").appendChild(document.createTextNode(s + "\n"));
4 }
5
6 function shouldBe(a, b, shouldNotPrintValues)
7 {
8     var evalA, evalB;
9     try {
10         evalA = eval(a);
11         evalB = eval(b);
12     } catch(e) {
13         evalA = e;
14     }
15
16     var message;
17     if (evalA === evalB) {
18         message = "PASS";
19         if (!shouldNotPrintValues) {
20             message += ": " + a + " should be '" + evalB + "' and is.";
21         } else {
22             message += ": " + a + " matched the expected value.";
23         }
24     } else {
25        message = "*** FAIL: " + a + " should be '" + evalB + "' but instead is " + evalA + ". ***";
26     }
27
28     message = String(message).replace(/\n/g, "");
29     log(message);
30 }
31
32 function shouldBeTrue(a) 
33
34     shouldBe(a, "true"); 
35 }
36
37 function shouldBeFalse(b) 
38
39     shouldBe(b, "false"); 
40 }
41
42 function canGet(keyPath)
43 {
44     try {
45         return eval("window." + keyPath) !== undefined;
46     } catch(e) {
47         return false;
48     }
49 }
50
51 window.marker = { };
52
53 function canSet(keyPath, valuePath)
54 {
55     if (valuePath === undefined)
56         valuePath = "window.marker";
57
58     try {
59         eval("window." + keyPath + " = " + valuePath);
60         return eval("window." + keyPath) === eval("window." + valuePath);
61     } catch(e) {
62         return false;
63     }
64 }
65
66 function canCall(keyPath, argumentString)
67 {
68     try {
69         eval("window." + keyPath + "(" + (argumentString === undefined ? "" : "'" + argumentString + "'") + ")");
70         return true;
71     } catch(e) {
72         return false;
73     }
74 }
75
76 function toString(expression, valueForException)
77 {
78     if (valueForException === undefined)
79         valueForException = "[exception]";
80         
81     try {
82         var evalExpression = eval(expression);
83         if (evalExpression === undefined)
84             throw null;
85         return String(evalExpression);
86     } catch(e) {
87         return valueForException;
88     }
89 }
90
91 // Frame Access Tests
92
93 function canAccessFrame(iframeURL, iframeId, passMessage, failMessage) {
94     if (window.layoutTestController) {
95         layoutTestController.dumpAsText();
96         layoutTestController.dumpChildFramesAsText();
97         layoutTestController.waitUntilDone();
98     }
99
100     var targetWindow = frames[0];
101     if (!targetWindow.document.body)
102         log("FAIL: targetWindow started with no document, we won't know if the test passed or failed.");
103
104     var iframe = document.getElementById(iframeId);
105     iframe.src = iframeURL;
106
107     var testDone = false;
108
109     setTimeout(test, 1);
110
111     setTimeout(function() {
112         if (!testDone) {
113             log(failMessage);
114             if (window.layoutTestController)
115                 layoutTestController.notifyDone();
116         }
117     }, 2000);
118
119     function test() {
120         try {
121             if (targetWindow.document.body) {
122                 if (targetWindow.document.getElementById('accessMe')) {
123                     targetWindow.document.getElementById('accessMe').innerHTML = passMessage;
124                     log(passMessage);
125                     testDone = true;
126                     if (window.layoutTestController)
127                         layoutTestController.notifyDone();
128                     return;
129                 }
130             }
131         } catch (e) {
132             log("In catch");
133         }
134
135         setTimeout(test, 1);
136     }
137 }
138
139 function cannotAccessFrame(iframeURL, iframeId, passMessage, failMessage) {
140     if (window.layoutTestController) {
141         layoutTestController.dumpAsText();
142         layoutTestController.dumpChildFramesAsText();
143         layoutTestController.waitUntilDone();
144     }
145
146     var targetWindow = frames[0];
147     if (!targetWindow.document.body)
148         log("FAIL: targetWindow started with no document, we won't know if the test passed or failed.");
149
150     var iframe = document.getElementById(iframeId);
151     iframe.src = iframeURL;
152
153     var testDone = false;
154
155     setTimeout(test, 1);
156
157     setTimeout(function() {
158         if (!testDone) {
159             log(failMessage);
160             window.stop();
161             if (window.layoutTestController)
162                 layoutTestController.notifyDone();
163         }
164     }, 2000);
165
166     function test() {
167         try {
168             if (targetWindow.document.body) {
169                 if (targetWindow.document.getElementById('accessMe')) {
170                     targetWindow.document.getElementById('accessMe').innerHTML = failMessage;
171                     log(failMessage);
172                     testDone = true;
173                     window.stop();
174                     if (window.layoutTestController)
175                         layoutTestController.notifyDone();
176                     return;
177                 }
178
179                 setTimeout(test, 1);
180                 return;
181             }
182         } catch (e) {
183         }
184
185         log(passMessage);
186         testDone = true;
187         window.stop();
188         if (window.layoutTestController)
189             layoutTestController.notifyDone();
190     }
191 }
192
193 function closeWindowAndNotifyDone(win)
194 {
195     win.close();
196     setTimeout(doneHandler, 1);
197     function doneHandler() {
198         if (win.closed) {
199             if (window.layoutTestController)
200                 layoutTestController.notifyDone();
201             return;
202         }
203
204         setTimeout(doneHandler, 1);
205     }
206 }