1 (function(){ 2 // earthTest and earthAsyncTest 3 // ============================ 4 // 5 // Use earthTest rather than QUnit::test for testing functionality that 6 // requires an instance of the Google Earth Plugin and/or the 7 // earth-api-utility-library. 8 // 9 // Test functions will be called with two arguments, the plugin instance and 10 // the utility library. Here is an example: 11 // 12 // earthTest('Test grid', 1, function(ge, gex){ 13 // equals(ge.getOptions().getGridVisibility(), true, 'Grid should be visible.'); 14 // }); 15 // 16 // For asynchronous tests, use earthAsyncTest as a replacement for 17 // QUnit::testAsync: 18 // 19 // earthAsyncTest('Test parse kml', 1, function(ge, gex){ 20 // $.get('/path/to/kml', function(data){ 21 // var doc = ge.parseKml(data); 22 // equals(doc.getName(), 'My Name'); 23 // start(); 24 // }); 25 // }); 26 // 27 // When testing using asynchronous calls and the Earth Plugin it is 28 // important to fill in the `expected` argument. Otherwise, it can be 29 // difficult to pin down which test caused an error. 30 // 31 // See the following for more information on using QUnit: 32 // http://docs.jquery.com/QUnit 33 window.earthTest = function(name, expected, callback, async){ 34 if ( arguments.length === 2 ) { 35 callback = expected; 36 expected = 0; 37 } 38 39 var new_function = function(){ 40 if(ge && gex){ 41 if(!async){ 42 start(); 43 } 44 callback(ge, gex); 45 }else{ 46 initializePlugin(function(){ 47 if(!async){ 48 start(); 49 } 50 callback(ge, gex); 51 }); 52 } 53 } 54 asyncTest(name, expected, new_function); 55 } 56 57 window.earthAsyncTest = function(name, expected, callback){ 58 if ( arguments.length === 2 ) { 59 callback = expected; 60 expected = 0; 61 } 62 earthTest(name, expected, callback, true); 63 } 64 65 66 var ge; 67 var gex; 68 69 function initializePlugin(callback){ 70 var map = $(document.body).append('<div id="map3d"></div>'); 71 $('#map3d').css({width:'400', height: '200', 'position': 'absolute', 'left': '-400px'}); 72 google.earth.createInstance('map3d', function(plugin){ 73 ge = plugin; 74 gex = new GEarthExtensions(ge); 75 callback(); 76 }, googleEarthFailureCallback); 77 } 78 79 module('Custom Earth Test Cases'); 80 81 var reference_to_first_google_earth_instance; 82 83 earthAsyncTest('earthAsyncTest works', 2, function(ge, gex){ 84 ok(typeof ge === 'object', 'Google Earth Plugin initialized'); 85 reference_to_first_google_earth_instance = ge; 86 setTimeout(function(){ 87 start(); 88 ok(true === true, 'Additional asynchronous events can be run'); 89 }, 1000); 90 }); 91 92 earthTest('only loads once', 1, function(ge, gex){ 93 ok(ge === reference_to_first_google_earth_instance, 94 'Google Earth Plugin should initialize only once.'); 95 }); 96 97 function googleEarthFailureCallback(){ 98 alert('failed to load google earth plugin.'); 99 } 100 101 var DEFAULT_USERNAME = 'integration_tester'; 102 var DEFAULT_PASSWORD = 'password'; 103 var LOGIN = '/accounts/login/'; 104 var LOGOUT = '/accounts/logout/'; 105 106 function login(username, password, callback){ 107 $.post(LOGIN, {username: username, password: password}, function(data, textStatus){ 108 if(textStatus === "success"){ 109 if(data.match('label for="id_username"')){ 110 callback('username and password not valid.'); 111 }else{ 112 callback(false); 113 } 114 }else{ 115 callback(textStatus); 116 } 117 }, "html"); 118 } 119 120 121 // testLoggedIn, testLoggedInAsync, testLoggedOut, and testLoggedOutAsync 122 // ====================================================================== 123 // 124 // These methods expose a way to develop tests which require first that a 125 // default user or specified user is logged in or out before the starting the 126 // test. 127 // 128 // Examples: 129 // 130 // // optional `expected` argument specified. 131 // testLoggedIn('my test', 1, function(){ 132 // ok([some condition], 'My user can do ...'); 133 // }); 134 // 135 // // specify a different username and password 136 // testLoggedIn('my test', 'myusername', 'pword', function(){ 137 // ok([some condition], 'myusername can do ...'); 138 // }); 139 // 140 // // asynchronous tests can also be run 141 // testLoggedInAsync('my test', 2, function(){ 142 // ok([some condition], 'My user can do ...'); 143 // setTimeout(function(){ 144 // ok(true, 'asynchronous testing works.'); 145 // start(); 146 // }, 100); 147 // }); 148 // 149 // // If you have a test that must be run with an unauthenticated user: 150 // testLoggedOut('my test', 1, function(){ 151 // equals(can_do, false, 'user must be logged in to do x.'); 152 // }); 153 // 154 window.testLoggedIn = function(name, expected, username, pword, callback){ 155 var options; 156 if(arguments.length !== 1){ 157 options = parseArguments(arguments); 158 }else{ 159 options = name; 160 } 161 var new_function = function(){ 162 login(options['username'], options['pword'], function(error){ 163 if(error){ 164 ok(false, 'Could not login '+options['username'] + '. '+error); 165 start(); 166 }else{ 167 options['callback'](options['username']); 168 if(!options['async']){ 169 start(); 170 } 171 } 172 }); 173 } 174 asyncTest(options['name'], options['expected'], new_function); 175 } 176 177 window.testLoggedInAsync = function(name, expected, username, pword, callback){ 178 var options = parseArguments(arguments); 179 options['async'] = true; 180 window.testLoggedIn(options); 181 } 182 183 184 function logout(callback){ 185 $.get(LOGOUT, function(data, textStatus){ 186 if(textStatus === "success"){ 187 callback(false); 188 }else{ 189 callback(textStatus); 190 } 191 }); 192 } 193 194 window.testLoggedOut = function(name, expected, callback, async){ 195 if ( arguments.length === 2 ) { 196 callback = expected; 197 expected = 0; 198 } 199 var new_function = function(){ 200 logout(function(error){ 201 if(error){ 202 ok(false, 'Could not logout'); 203 start(); 204 }else{ 205 callback(); 206 if(!async){ 207 start(); 208 } 209 } 210 }); 211 } 212 asyncTest(name, expected, new_function); 213 } 214 215 window.testLoggedOutAsync = function(name, expected, callback){ 216 if ( arguments.length === 2 ) { 217 callback = expected; 218 expected = 0; 219 } 220 window.testLoggedOut(name, expected, callback, true); 221 } 222 223 function parseArguments(args){ 224 var options = { 225 username: DEFAULT_USERNAME, 226 pword: DEFAULT_PASSWORD, 227 expected: 0, 228 async: false, 229 name: args[0] 230 }; 231 // if (arguments.length === 1 && typeof name === 'object'){ 232 // callback = name['callback']; 233 // pword = name['pword'] || 'password'; 234 // username = name['username'] || 'integration_tester'; 235 // expected = name['expected'] || 0; 236 // async = name['async']; 237 // name = name['name']; 238 // }else{ 239 if ( args.length === 2 ) { 240 options['callback'] = args[1]; 241 } 242 if ( args.length === 3 ) { 243 options['callback'] = args[2]; 244 options['expected'] = args[1]; 245 } 246 if (args.length === 4) { 247 options['callback'] = args[3]; 248 options['pword'] = args[2]; 249 options['username'] = args[1]; 250 } 251 if (args.length === 5) { 252 options['callback'] = args[4]; 253 options['pword'] = args[3]; 254 options['username'] = args[2]; 255 options['expected'] = args[1]; 256 } 257 return options; 258 } 259 260 $(document).ready(function(){ 261 if(typeof window.skipAuthTests === 'undefined' || window.skipAuthTests === false){ 262 module('Custom Login-Related Test Cases'); 263 264 test('options parsed correctly', function(){ 265 var callback = function(){}; 266 // with username and password 267 // no expected 268 var options = parseArguments(['mytest', 'myusername', 'mypassword', callback]); 269 equals(options['name'], 'mytest', '`name` correctly set.'); 270 equals(options['expected'], 0, '`expected` loaded from defaults'); 271 equals(options['username'], 'myusername', '`username` correctly set.'); 272 equals(options['pword'], 'mypassword', '`pword` correctly set.'); 273 equals(options['callback'], callback, '`callback` correctly set.'); 274 equals(options['async'], false, '`async` correctly set.'); 275 276 // with expected 277 var options = parseArguments(['mytest', 8, 'myusername', 'mypassword', callback]); 278 equals(options['name'], 'mytest', '`name` correctly set.'); 279 equals(options['expected'], 8, '`expected` correctly set.'); 280 equals(options['username'], 'myusername', '`username` correctly set.'); 281 equals(options['pword'], 'mypassword', '`pword` correctly set.'); 282 equals(options['callback'], callback, '`callback` correctly set.'); 283 284 // without username and password 285 // no expected 286 var options = parseArguments(['mytest', callback]); 287 equals(options['name'], 'mytest', '`name` correctly set.'); 288 equals(options['expected'], 0, '`expected` loaded from defaults'); 289 equals(options['username'], DEFAULT_USERNAME, '`username` loaded from default.'); 290 equals(options['pword'], DEFAULT_PASSWORD, '`pword` loaded from default.'); 291 equals(options['callback'], callback, '`callback` correctly set.'); 292 equals(options['async'], false, '`async` correctly set.'); 293 294 // with expected 295 var options = parseArguments(['mytest', 4, callback]); 296 equals(options['name'], 'mytest', '`name` correctly set.'); 297 equals(options['expected'], 4, '`expected` set correctly.'); 298 equals(options['username'], DEFAULT_USERNAME, '`username` loaded from default.'); 299 equals(options['pword'], DEFAULT_PASSWORD, '`pword` loaded from default.'); 300 equals(options['callback'], callback, '`callback` correctly set.'); 301 equals(options['async'], false, '`async` correctly set.'); 302 303 }); 304 305 testLoggedIn('testLoggedIn - username and password, expected specified', 1, DEFAULT_USERNAME, DEFAULT_PASSWORD, function(username){ 306 equals(username, DEFAULT_USERNAME); 307 }); 308 309 testLoggedInAsync('testLoggedInAsync - username and password, expected specified', 2, DEFAULT_USERNAME, DEFAULT_PASSWORD, function(username){ 310 equals(username, DEFAULT_USERNAME); 311 setTimeout(function(){ 312 ok(true, 'asynchronous testing works.'); 313 start(); 314 }, 100); 315 }); 316 317 testLoggedIn('testLoggedIn - username and password, no expected argument', DEFAULT_USERNAME, DEFAULT_PASSWORD, function(username){ 318 equals(username, DEFAULT_USERNAME); 319 }); 320 321 testLoggedInAsync('testLoggedInAsync - username and password, no expected argument', DEFAULT_USERNAME, DEFAULT_PASSWORD, function(username){ 322 equals(username, DEFAULT_USERNAME); 323 setTimeout(function(){ 324 ok(true, 'asynchronous testing works.'); 325 start(); 326 }, 100); 327 }); 328 329 testLoggedIn('testLoggedIn - default user, expected specified', 1, DEFAULT_USERNAME, DEFAULT_PASSWORD, function(username){ 330 equals(username, DEFAULT_USERNAME); 331 }); 332 333 testLoggedInAsync('testLoggedInAsync - default user, expected specified', 2, function(username){ 334 equals(username, DEFAULT_USERNAME); 335 setTimeout(function(){ 336 ok(true, 'asynchronous testing works.'); 337 start(); 338 }, 100); 339 }); 340 341 testLoggedIn('testLoggedIn - default user, no expected argument', function(username){ 342 equals(username, DEFAULT_USERNAME); 343 }); 344 345 testLoggedInAsync('testLoggedInAsync - default user, no expected argument', function(username){ 346 equals(username, DEFAULT_USERNAME); 347 setTimeout(function(){ 348 ok(true, 'asynchronous testing works.'); 349 start(); 350 }, 100); 351 }); 352 353 testLoggedOut('testLoggedOut - expected argument', 1, function(){ 354 ok(true, 'testLoggedOut works with expected argument.'); 355 }); 356 357 testLoggedOutAsync('testLoggedOutAsync - expected argument', 2, function(){ 358 ok(true, 'testLoggedOut works with expected argument.'); 359 setTimeout(function(){ 360 ok(true, 'asynchronous testing works.'); 361 start(); 362 }, 100); 363 }); 364 365 testLoggedOut('testLoggedOut - expected argument', function(){ 366 ok(true, 'testLoggedOut works without expected.'); 367 }); 368 369 testLoggedOutAsync('testLoggedOutAsync - expected argument', function(){ 370 ok(true, 'testLoggedOut works without expected.'); 371 setTimeout(function(){ 372 ok(true, 'asynchronous testing works.'); 373 start(); 374 }, 100); 375 }); 376 // testing 377 // with username and password 378 // no expected 379 // with expected 380 // without username and password 381 // with expected 382 // without expected 383 } 384 }); 385 })();