Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / Geolocation / script-tests / maximum-age.js
blob4ce320e5c2d0b5a11af6105baa1aa4446335f153
1 description("Tests that the PositionOptions.maximumAge parameter is correctly applied.");
3 var mockLatitude = 51.478;
4 var mockLongitude = -0.166;
5 var mockAccuracy = 100.0;
7 var mockMessage = 'test';
9 var position;
10 var error;
12 function checkPosition(p) {
13 debug('');
14 position = p;
15 shouldBe('position.coords.latitude', 'mockLatitude');
16 shouldBe('position.coords.longitude', 'mockLongitude');
17 shouldBe('position.coords.accuracy', 'mockAccuracy');
20 function checkError(e) {
21 debug('');
22 error = e;
23 shouldBe('error.code', 'error.POSITION_UNAVAILABLE');
24 shouldBe('error.message', 'mockMessage');
27 if (!window.testRunner || !window.internals)
28 debug('This test can not run without testRunner or internals');
30 internals.setGeolocationClientMock(document);
31 internals.setGeolocationPermission(document, true);
32 internals.setGeolocationPosition(document, mockLatitude, mockLongitude, mockAccuracy);
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 });
43 function testZeroMaximumAge() {
44 // Update the position provided by the mock service.
45 internals.setGeolocationPosition(document, ++mockLatitude, ++mockLongitude, ++mockAccuracy);
46 // The default maximumAge is zero, so we expect the updated position from the service.
47 navigator.geolocation.getCurrentPosition(function(p) {
48 checkPosition(p);
49 testNonZeroMaximumAge();
50 }, function(e) {
51 testFailed('Error callback invoked unexpectedly');
52 finishJSTest();
53 });
56 function testNonZeroMaximumAge() {
57 // Update the mock service to report an error.
58 internals.setGeolocationPositionUnavailableError(document, mockMessage);
59 // The maximumAge is non-zero, so we expect the cached position, not the error from the service.
60 navigator.geolocation.getCurrentPosition(function(p) {
61 checkPosition(p);
62 testNegativeValueMaximumAge();
63 }, function(e) {
64 testFailed('Error callback invoked unexpectedly');
65 finishJSTest();
66 }, {maximumAge: 1000});
69 function testNegativeValueMaximumAge() {
70 // Update the position provided by the mock service.
71 internals.setGeolocationPosition(document, ++mockLatitude, ++mockLongitude, ++mockAccuracy);
72 // The maximumAge is same as zero, so we expect the updated position from the service.
73 navigator.geolocation.getCurrentPosition(function(p) {
74 checkPosition(p);
75 testOverSignedIntMaximumAge();
76 }, function(e) {
77 testFailed('Error callback invoked unexpectedly');
78 finishJSTest();
79 }, {maximumAge: -1000});
82 function testOverSignedIntMaximumAge() {
83 // Update the mock service to report an error.
84 internals.setGeolocationPositionUnavailableError(document, mockMessage);
85 // The maximumAge is non-zero, so we expect the cached position, not the error from the service.
86 navigator.geolocation.getCurrentPosition(function(p) {
87 checkPosition(p);
88 testOverUnsignedIntMaximumAge();
89 }, function(e) {
90 testFailed('Error callback invoked unexpectedly');
91 finishJSTest();
92 }, {maximumAge: 2147483648});
95 function testOverUnsignedIntMaximumAge() {
96 // Update the mock service to report an error.
97 internals.setGeolocationPositionUnavailableError(document, mockMessage);
98 // The maximumAge is max-value of unsigned, so we expect the cached position, not the error from the service.
99 navigator.geolocation.getCurrentPosition(function(p) {
100 checkPosition(p);
101 testZeroMaximumAgeError();
102 }, function(e) {
103 testFailed('Error callback invoked unexpectedly');
104 finishJSTest();
105 }, {maximumAge: 4294967296});
108 function testZeroMaximumAgeError() {
109 // The default maximumAge is zero, so we expect the error from the service.
110 navigator.geolocation.getCurrentPosition(function(p) {
111 testFailed('Success callback invoked unexpectedly');
112 finishJSTest();
113 }, function(e) {
114 checkError(e);
115 finishJSTest();
119 window.jsTestIsAsync = true;