ddkilzer@apple.com | f930faf | 2008-10-24 20:10:40 +0000 | [diff] [blame] | 1 | if (window.layoutTestController) { |
| 2 | layoutTestController.dumpAsText(); |
| 3 | layoutTestController.waitUntilDone(); |
| 4 | } |
| 5 | var watchID = -1; |
| 6 | var passedPosition = null; |
| 7 | var console = document.createElement('div'); |
| 8 | document.body.appendChild(console); |
| 9 | |
| 10 | function reset() |
| 11 | { |
| 12 | if (window.layoutTestController) { |
| 13 | layoutTestController.notifyDone(); |
| 14 | watchID = -1; |
| 15 | passedPosition = null; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | function hanged() |
| 20 | { |
| 21 | consoleWrite("FAIL: timed out"); |
| 22 | reset(); |
| 23 | } |
| 24 | setTimeout(hanged, 10000); |
| 25 | |
| 26 | function testAndEnd(testFuncString) |
| 27 | { |
| 28 | test(testFuncString, true); |
| 29 | } |
| 30 | |
| 31 | function 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 | |
| 42 | function 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 | |
| 59 | function run(testFuncString) |
| 60 | { |
| 61 | consoleWrite("RUN(" + testFuncString + ")"); |
| 62 | try { |
| 63 | eval(testFuncString); |
| 64 | } catch (ex) { |
| 65 | consoleWrite(ex); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | function watchPositionAndEnd(funcString) |
| 70 | { |
| 71 | watchPosition(funcString, true) |
| 72 | } |
| 73 | |
| 74 | function 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 | |
| 94 | function watchPositionTestAndEnd(testFuncString) |
| 95 | { |
| 96 | watchPositionAndTest(testFuncString, true); |
| 97 | } |
| 98 | |
| 99 | function watchPositionAndFail() |
| 100 | { |
| 101 | watchPositionAndTest("false", true); |
| 102 | } |
| 103 | |
| 104 | function 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 | |
| 123 | function getCurrentPositionAndEnd(funcString) |
| 124 | { |
| 125 | getCurrentPosition(funcString, true) |
| 126 | } |
| 127 | |
| 128 | function 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 | |
| 146 | function getCurrentPositionTestAndEnd(testFuncString) |
| 147 | { |
| 148 | getCurrentPositionAndTest(testFuncString, true); |
| 149 | } |
| 150 | |
| 151 | function getCurrentPositionAndFail() |
| 152 | { |
| 153 | getCurrentPositionAndTest("false", true); |
| 154 | } |
| 155 | |
| 156 | function 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 | |
| 174 | function 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 | |
| 186 | var testEnded = false; |
| 187 | |
| 188 | function endTest() |
| 189 | { |
| 190 | consoleWrite("END OF TEST"); |
| 191 | testEnded = true; |
| 192 | reset(); |
| 193 | } |
| 194 | |
| 195 | function endTestLater() |
| 196 | { |
| 197 | setTimeout(endTest, 250); |
| 198 | } |
| 199 | |
| 200 | function failTestIn(ms) |
| 201 | { |
| 202 | setTimeout(function () { |
| 203 | consoleWrite("FAIL: did not end fast enough"); |
| 204 | endTest(); |
| 205 | }, ms); |
| 206 | } |
| 207 | |
| 208 | function consoleWrite(text) |
| 209 | { |
| 210 | if (testEnded) |
| 211 | return; |
| 212 | console.innerHTML += text + "<br>"; |
| 213 | } |
| 214 | |
| 215 | function relativeURL(url) |
| 216 | { |
| 217 | return url.substr(url.indexOf('/geolocation/')+13); |
| 218 | } |