blob: 09c152dba530dc488986263be43edf74455badd5 [file] [log] [blame]
ddkilzer@apple.comf930faf2008-10-24 20:10:40 +00001if (window.layoutTestController) {
2 layoutTestController.dumpAsText();
3 layoutTestController.waitUntilDone();
4}
5var watchID = -1;
6var passedPosition = null;
7var console = document.createElement('div');
8document.body.appendChild(console);
9
10function reset()
11{
12 if (window.layoutTestController) {
13 layoutTestController.notifyDone();
14 watchID = -1;
15 passedPosition = null;
16 }
17}
18
19function hanged()
20{
21 consoleWrite("FAIL: timed out");
22 reset();
23}
24setTimeout(hanged, 10000);
25
26function testAndEnd(testFuncString)
27{
28 test(testFuncString, true);
29}
30
31function test(testFuncString, endit)
32{
33 if (eval(testFuncString))
34 consoleWrite("TEST(" + testFuncString + ") <span style='color:green'>OK</span>");
35 else
36 consoleWrite("TEST(" + testFuncString + ") <span style='color:red'>FAIL</span>");
37
38 if (endit)
39 endTest();
40}
41
42function testExpected(testFuncString, expected)
43{
44 try {
45 var observed = eval(testFuncString);
46 } catch (ex) {
47 consoleWrite(ex);
48 return;
49 }
50
51 var msg = "expected " + testFuncString + "=='" + expected + "', observed '" + observed + "'";
52
53 if (observed == expected)
54 consoleWrite(msg + " - <span style='color:green'>OK</span>");
55 else
56 consoleWrite(msg + " - <span style='color:red'>FAIL</span>");
57}
58
59function run(testFuncString)
60{
61 consoleWrite("RUN(" + testFuncString + ")");
62 try {
63 eval(testFuncString);
64 } catch (ex) {
65 consoleWrite(ex);
66 }
67}
68
69function watchPositionAndEnd(funcString)
70{
71 watchPosition(funcString, true)
72}
73
74function watchPosition(func, endit)
75{
76 function _positionCallback(position)
77 {
78 passedPosition = position;
79 test("passedPosition == navigator.geolocation.lastPosition");
80
81 consoleWrite("POSITION(" + position + ")");
82
83 if (func)
84 func();
85
86 if (endit)
87 endTest();
88 }
89
90 watchID = navigator.geolocation.watchPosition(_positionCallback);
91 test("watchID > 0");
92}
93
94function watchPositionTestAndEnd(testFuncString)
95{
96 watchPositionAndTest(testFuncString, true);
97}
98
99function watchPositionAndFail()
100{
101 watchPositionAndTest("false", true);
102}
103
104function watchPositionAndTest(testFuncString, endit)
105{
106 function _positionCallback(position)
107 {
108 passedPosition = position;
109
110 if (eval(testFuncString))
111 consoleWrite("POSITION(" + position + ") TEST(" + testFuncString + ") <span style='color:green'>OK</span>");
112 else
113 consoleWrite("POSITION(" + position + ") TEST(" + testFuncString + ") <span style='color:red'>FAIL</span>");
114
115 if (endit)
116 endTest();
117 }
118
119 watchID = navigator.geolocation.watchPosition(_positionCallback);
120 test("watchID > 0");
121}
122
123function getCurrentPositionAndEnd(funcString)
124{
125 getCurrentPosition(funcString, true)
126}
127
128function getCurrentPosition(func, endit)
129{
130 function _positionCallback(position)
131 {
132 passedPosition = position;
133
134 consoleWrite("POSITION(" + position + ")");
135
136 if (func)
137 func();
138
139 if (endit)
140 endTest();
141 }
142
143 navigator.geolocation.getCurrentPosition(_positionCallback);
144}
145
146function getCurrentPositionTestAndEnd(testFuncString)
147{
148 getCurrentPositionAndTest(testFuncString, true);
149}
150
151function getCurrentPositionAndFail()
152{
153 getCurrentPositionAndTest("false", true);
154}
155
156function getCurrentPositionAndTest(testFuncString, endit)
157{
158 function _positionCallback(position)
159 {
160 passedPosition = position;
161
162 if (eval(testFuncString))
163 consoleWrite("POSITION(" + position + ") TEST(" + testFuncString + ") <span style='color:green'>OK</span>");
164 else
165 consoleWrite("POSITION(" + position + ") TEST(" + testFuncString + ") <span style='color:red'>FAIL</span>");
166
167 if (endit)
168 endTest();
169 }
170
171 navigator.geolocation.getCurrentPosition(_positionCallback);
172}
173
174function testException(testString, exceptionString)
175{
176 try {
177 eval(testString);
178 } catch (ex) {
179 if (ex.code == eval(exceptionString))
180 consoleWrite("TEST(" + testString + ") THROWS("+exceptionString+") <span style='color:green'>OK</span>");
181 else
182 consoleWrite("TEST(" + testString + ") THROWS("+exceptionString+") <span style='color:red'>FAIL</span>");
183 }
184}
185
186var testEnded = false;
187
188function endTest()
189{
190 consoleWrite("END OF TEST");
191 testEnded = true;
192 reset();
193}
194
195function endTestLater()
196{
197 setTimeout(endTest, 250);
198}
199
200function failTestIn(ms)
201{
202 setTimeout(function () {
203 consoleWrite("FAIL: did not end fast enough");
204 endTest();
205 }, ms);
206}
207
208function consoleWrite(text)
209{
210 if (testEnded)
211 return;
212 console.innerHTML += text + "<br>";
213}
214
215function relativeURL(url)
216{
217 return url.substr(url.indexOf('/geolocation/')+13);
218}