1 description("Tests that the PositionOptions.maximumAge parameter is correctly applied."); 2 3 var mockLatitude = 51.478; 4 var mockLongitude = -0.166; 5 var mockAccuracy = 100.0; 6 7 var mockCode = 2; 8 var mockMessage = 'test'; 9 10 var position; 11 var error; 12 13 function checkPosition(p) { 14 debug(''); 15 position = p; 16 shouldBe('position.coords.latitude', 'mockLatitude'); 17 shouldBe('position.coords.longitude', 'mockLongitude'); 18 shouldBe('position.coords.accuracy', 'mockAccuracy'); 19 } 20 21 function checkError(e) { 22 debug(''); 23 error = e; 24 shouldBe('error.code', 'mockCode'); 25 shouldBe('error.message', 'mockMessage'); 26 } 27 28 if (window.layoutTestController) { 29 layoutTestController.setGeolocationPermission(true); 30 layoutTestController.setMockGeolocationPosition(mockLatitude, mockLongitude, mockAccuracy); 31 } else 32 debug('This test can not be run without the LayoutTestController'); 33 34 // Initialize the cached Position 35 navigator.geolocation.getCurrentPosition(function(p) { 36 checkPosition(p); 37 testZeroMaximumAge(); 38 }, function(e) { 39 testFailed('Error callback invoked unexpectedly'); 40 finishJSTest(); 41 }); 42 43 function testZeroMaximumAge() { 44 // Update the position provided by the mock service. 45 if (window.layoutTestController) 46 layoutTestController.setMockGeolocationPosition(++mockLatitude, ++mockLongitude, ++mockAccuracy); 47 // The default maximumAge is zero, so we expect the updated position from the service. 48 navigator.geolocation.getCurrentPosition(function(p) { 49 checkPosition(p); 50 testNonZeroMaximumAge(); 51 }, function(e) { 52 testFailed('Error callback invoked unexpectedly'); 53 finishJSTest(); 54 }); 55 } 56 57 function testNonZeroMaximumAge() { 58 // Update the mock service to report an error. 59 if (window.layoutTestController) 60 layoutTestController.setMockGeolocationError(mockCode, mockMessage); 61 // The maximumAge is non-zero, so we expect the cached position, not the error from the service. 62 navigator.geolocation.getCurrentPosition(function(p) { 63 checkPosition(p); 64 testZeroMaximumAgeError(); 65 }, function(e) { 66 testFailed('Error callback invoked unexpectedly'); 67 finishJSTest(); 68 }, {maximumAge: 1000}); 69 } 70 71 function testZeroMaximumAgeError() { 72 // The default maximumAge is zero, so we expect the error from the service. 73 navigator.geolocation.getCurrentPosition(function(p) { 74 testFailed('Success callback invoked unexpectedly'); 75 finishJSTest(); 76 }, function(e) { 77 checkError(e); 78 finishJSTest(); 79 }); 80 } 81 82 window.jsTestIsAsync = true; 83 window.successfullyParsed = true; 84