1159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockdescription("Tests that the PositionOptions.maximumAge parameter is correctly applied.");
2159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
3159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar mockLatitude = 51.478;
4159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar mockLongitude = -0.166;
5159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar mockAccuracy = 100.0;
6159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
7545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochvar mockCode = 2;
8159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar mockMessage = 'test';
9159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
10159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar position;
11159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockvar error;
12159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
13159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockfunction checkPosition(p) {
14159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    debug('');
15159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    position = p;
16159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    shouldBe('position.coords.latitude', 'mockLatitude');
17159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    shouldBe('position.coords.longitude', 'mockLongitude');
18159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    shouldBe('position.coords.accuracy', 'mockAccuracy');
19159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}
20159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
21159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockfunction checkError(e) {
22159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    debug('');
23159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    error = e;
24159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    shouldBe('error.code', 'mockCode');
25159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    shouldBe('error.message', 'mockMessage');
26159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}
27159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
28545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdochif (window.layoutTestController) {
29545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    layoutTestController.setGeolocationPermission(true);
30545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    layoutTestController.setMockGeolocationPosition(mockLatitude, mockLongitude, mockAccuracy);
31545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch} else
32545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    debug('This test can not be run without the LayoutTestController');
33159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
34159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block// Initialize the cached Position
35159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blocknavigator.geolocation.getCurrentPosition(function(p) {
36159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    checkPosition(p);
37159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    testZeroMaximumAge();
38159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}, function(e) {
39159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    testFailed('Error callback invoked unexpectedly');
40e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block    finishJSTest();
41159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block});
42159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
43159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockfunction testZeroMaximumAge() {
44159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    // Update the position provided by the mock service.
45545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    if (window.layoutTestController)
46545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        layoutTestController.setMockGeolocationPosition(++mockLatitude, ++mockLongitude, ++mockAccuracy);
47159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    // The default maximumAge is zero, so we expect the updated position from the service.
48159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    navigator.geolocation.getCurrentPosition(function(p) {
49159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        checkPosition(p);
50159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        testNonZeroMaximumAge();
51159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    }, function(e) {
52159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        testFailed('Error callback invoked unexpectedly');
53e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        finishJSTest();
54159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    });
55159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}
56159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
57159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockfunction testNonZeroMaximumAge() {
58159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    // Update the mock service to report an error.
59545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch    if (window.layoutTestController)
60545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch        layoutTestController.setMockGeolocationError(mockCode, mockMessage);
61159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    // The maximumAge is non-zero, so we expect the cached position, not the error from the service.
62159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    navigator.geolocation.getCurrentPosition(function(p) {
63159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        checkPosition(p);
64159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        testZeroMaximumAgeError();
65159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    }, function(e) {
66159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        testFailed('Error callback invoked unexpectedly');
67e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        finishJSTest();
68159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    }, {maximumAge: 1000});
69159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}
70159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
71159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Blockfunction testZeroMaximumAgeError() {
72159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    // The default maximumAge is zero, so we expect the error from the service.
73159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    navigator.geolocation.getCurrentPosition(function(p) {
74159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        testFailed('Success callback invoked unexpectedly');
75e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        finishJSTest();
76159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    }, function(e) {
77159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block        checkError(e);
78e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block        finishJSTest();
79159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block    });
80159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block}
81159531f4ce4c04455cd544a7c0f7c1644a50ccbeSteve Block
82e78cbe89e6f337f2f1fe40315be88f742b547151Steve Blockwindow.jsTestIsAsync = true;
83e78cbe89e6f337f2f1fe40315be88f742b547151Steve Blockwindow.successfullyParsed = true;
84