Home | History | Annotate | Download | only in resources
      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 = 1;
      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 window.layoutTestController.setGeolocationPermission(true);
     29 window.layoutTestController.setMockGeolocationPosition(mockLatitude, mockLongitude, mockAccuracy);
     30 
     31 // Initialize the cached Position
     32 navigator.geolocation.getCurrentPosition(function(p) {
     33     checkPosition(p);
     34     testZeroMaximumAge();
     35 }, function(e) {
     36     testFailed('Error callback invoked unexpectedly');
     37     window.layoutTestController.notifyDone();
     38 });
     39 
     40 function testZeroMaximumAge() {
     41     // Update the position provided by the mock service.
     42     window.layoutTestController.setMockGeolocationPosition(++mockLatitude, ++mockLongitude, ++mockAccuracy);
     43     // The default maximumAge is zero, so we expect the updated position from the service.
     44     navigator.geolocation.getCurrentPosition(function(p) {
     45         checkPosition(p);
     46         testNonZeroMaximumAge();
     47     }, function(e) {
     48         testFailed('Error callback invoked unexpectedly');
     49         window.layoutTestController.notifyDone();
     50     });
     51 }
     52 
     53 function testNonZeroMaximumAge() {
     54     // Update the mock service to report an error.
     55     window.layoutTestController.setMockGeolocationError(mockCode, mockMessage);
     56     // The maximumAge is non-zero, so we expect the cached position, not the error from the service.
     57     navigator.geolocation.getCurrentPosition(function(p) {
     58         checkPosition(p);
     59         testZeroMaximumAgeError();
     60     }, function(e) {
     61         testFailed('Error callback invoked unexpectedly');
     62         window.layoutTestController.notifyDone();
     63     }, {maximumAge: 1000});
     64 }
     65 
     66 function testZeroMaximumAgeError() {
     67     // The default maximumAge is zero, so we expect the error from the service.
     68     navigator.geolocation.getCurrentPosition(function(p) {
     69         testFailed('Success callback invoked unexpectedly');
     70         window.layoutTestController.notifyDone();
     71     }, function(e) {
     72         checkError(e);
     73         debug('<br /><span class="pass">TEST COMPLETE</span>');
     74         window.layoutTestController.notifyDone();
     75     });
     76 }
     77 
     78 window.layoutTestController.waitUntilDone();
     79 
     80 var isAsynchronous = true;
     81 var successfullyParsed = true;
     82