Home | History | Annotate | Download | only in script-tests
      1 description("Tests that watchPosition correctly reports position updates and errors from the Geolocation service.");
      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     position = p;
     15     shouldBe('position.coords.latitude', 'mockLatitude');
     16     shouldBe('position.coords.longitude', 'mockLongitude');
     17     shouldBe('position.coords.accuracy', 'mockAccuracy');
     18     debug('');
     19 }
     20 
     21 function checkError(e) {
     22     error = e;
     23     shouldBe('error.code', 'mockCode');
     24     shouldBe('error.message', 'mockMessage');
     25     debug('');
     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 var state = 0;
     35 navigator.geolocation.watchPosition(function(p) {
     36     switch (state++) {
     37         case 0:
     38             checkPosition(p);
     39             if (window.layoutTestController)
     40                 layoutTestController.setMockGeolocationPosition(++mockLatitude, ++mockLongitude, ++mockAccuracy);
     41             break;
     42         case 1:
     43             checkPosition(p);
     44             if (window.layoutTestController)
     45                 layoutTestController.setMockGeolocationError(mockCode, mockMessage);
     46             break;
     47         case 3:
     48             checkPosition(p);
     49             finishJSTest();
     50             break;
     51         default:
     52             testFailed('Success callback invoked unexpectedly');
     53             finishJSTest();
     54     }
     55 }, function(e) {
     56     switch (state++) {
     57         case 2:
     58             checkError(e);
     59             if (window.layoutTestController)
     60                 layoutTestController.setMockGeolocationPosition(++mockLatitude, ++mockLongitude, ++mockAccuracy);
     61             break;
     62         default:
     63             testFailed('Error callback invoked unexpectedly');
     64             finishJSTest();
     65     }
     66 });
     67 
     68 window.jsTestIsAsync = true;
     69 window.successfullyParsed = true;
     70