Skip to content

Commit e8d3669

Browse files
author
Stanimir Karoserov
committed
updated location and location tests
1 parent 0242773 commit e8d3669

File tree

7 files changed

+163
-16
lines changed

7 files changed

+163
-16
lines changed

BCL.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@
205205
<TypeScriptCompile Include="localsettings\local_settings_common.ts">
206206
<DependentUpon>local_settings.d.ts</DependentUpon>
207207
</TypeScriptCompile>
208+
<TypeScriptCompile Include="location\location_common.ts">
209+
<DependentUpon>location.d.ts</DependentUpon>
210+
</TypeScriptCompile>
208211
<Content Include="_references.ts" />
209212
</ItemGroup>
210213
<ItemGroup>

Tests/location_tests.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import TKUnit = require("Tests/TKUnit");
22
import locationModule = require("location/location");
3+
import types = require("location/location_types");
34

45
var LocationManager = locationModule.LocationManager;
56
var Location = locationModule.Location;
@@ -16,17 +17,19 @@ export var testLocation = function () {
1617
locationManager.startLocationMonitoring(function(location) {
1718
locationReceived = true;
1819
}, function(error) {
19-
console.log('Location error received: ' + error);
20+
//console.log('Location error received: ' + error);
2021
locationReceived = error;
2122
}
22-
);
23+
);
2324

2425
var isReady = function () {
2526
return locationReceived;
2627
}
2728

28-
TKUnit.waitUntilReady(isReady, 3);
29+
TKUnit.waitUntilReady(isReady, 10);
30+
2931
locationManager.stopLocationMonitoring();
32+
3033
TKUnit.assert(true === locationReceived, locationReceived);
3134
};
3235

@@ -46,4 +49,44 @@ export var testLastKnownLocation = function () {
4649
var lastKnownLocation = locationManager.lastKnownLocation;
4750
TKUnit.assert((lastKnownLocation != null), "There is no last known location");
4851
};
49-
52+
53+
function doOnce(options: locationModule.Options) {
54+
var locationReceived;
55+
locationModule.getLocation(options).then(function (location) {
56+
locationReceived = true;
57+
}).fail(function (error) {
58+
//console.log('Location error received: ' + error);
59+
locationReceived = error;
60+
});
61+
62+
var isReady = function () {
63+
return locationReceived;
64+
}
65+
66+
TKUnit.waitUntilReady(isReady, 10);
67+
68+
TKUnit.assert(true === locationReceived, locationReceived);
69+
}
70+
71+
export var testLocationOnce = function () {
72+
doOnce(undefined);
73+
};
74+
75+
export var testLocationOnceTimeout0 = function () {
76+
doOnce({timeout: 0});
77+
};
78+
79+
export var testLocationOnceMaximumAge = function () {
80+
TKUnit.waitUntilReady(function () { return false; }, 2);
81+
doOnce({ maximumAge: 3000, timeout: 0 }); // this should pass
82+
try {
83+
doOnce({ maximumAge: 1000, timeout: 0 });
84+
TKUnit.assert(false, "maximumAge check failed");
85+
}
86+
catch (e) {
87+
}
88+
};
89+
90+
export var testLocationOnceTimeout1000 = function () {
91+
doOnce({ timeout: 1000 });
92+
};

location/location.android.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import types = require("location/location_types");
22
import appModule = require("application/application");
3+
import common = require("location/location_common");
4+
import merger = require("utils/module_merge");
35

46
// merge the exports of the types module with the exports of this file
57
declare var exports;
6-
require("utils/module_merge").merge(types, exports);
8+
merger.merge(types, exports);
9+
merger.merge(common, exports);
710

811
export class LocationManager {
912
// in meters

location/location.d.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export declare enum Accuracy {
1+

2+
import promises = require("promises/promises");
3+
4+
export declare enum Accuracy {
25
// in meters
36
ANY,
47
HIGH,
@@ -31,21 +34,31 @@ export declare class Location {
3134
public ios: any; // iOS CLLocation
3235
}
3336

34-
export declare class Options {
37+
export interface Options {
3538
/**
3639
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
3740
*/
38-
desiredAccuracy: number;
41+
desiredAccuracy?: number;
3942

4043
/**
4144
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
4245
*/
43-
updateDistance: number;
46+
updateDistance?: number;
4447

4548
/**
46-
* Minimum time interval between location updates, in milliseconds (android only)
49+
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
4750
*/
48-
minimumUpdateTime: number;
51+
minimumUpdateTime?: number;
52+
53+
/**
54+
* how old locations to receive in ms.
55+
*/
56+
maximumAge?: number;
57+
58+
/**
59+
* how long to wait for a location in ms.
60+
*/
61+
timeout?: number;
4962
}
5063

5164
export declare class LocationManager {
@@ -98,3 +111,5 @@ export declare class LocationManager {
98111
*/
99112
lastKnownLocation: Location;
100113
}
114+
115+
export declare var getLocation: (options?: Options) => promises.Promise<Location>;

location/location.ios.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import types = require("location/location_types");
2+
import common = require("location/location_common");
3+
import merger = require("utils/module_merge");
24

35
// merge the exports of the types module with the exports of this file
46
declare var exports;
5-
require("utils/module_merge").merge(types, exports);
7+
merger.merge(types, exports);
8+
merger.merge(common, exports);
69

710
export class LocationManager {
811

location/location_common.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+

2+
import types = require("location/location_types");
3+
import promises = require("promises/promises");
4+
import locationModule = require("location/location");
5+
import timer = require("timer/timer");
6+
7+
export var getLocation = function (options?: types.Options) : promises.Promise<types.Location> {
8+
var d = promises.defer<types.Location>();
9+
10+
var timerId;
11+
var locationManager = new locationModule.LocationManager();
12+
13+
if (options && (0 === options.timeout)) {
14+
var location = locationManager.lastKnownLocation;
15+
if (location) {
16+
if (options && ("number" === typeof options.maximumAge)) {
17+
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
18+
d.resolve(location);
19+
}
20+
else {
21+
d.reject(new Error("timeout is 0 and last known location is older than maximumAge"));
22+
}
23+
}
24+
else {
25+
d.resolve(location);
26+
}
27+
}
28+
else {
29+
d.reject(new Error("timeout is 0 and no known location found"));
30+
}
31+
return d.promise();
32+
}
33+
34+
locationManager.startLocationMonitoring(function (location: types.Location) {
35+
if (options && ("number" === typeof options.maximumAge)) {
36+
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
37+
locationManager.stopLocationMonitoring();
38+
if ("undefined" !== typeof timerId) {
39+
timer.clearTimeout(timerId);
40+
}
41+
d.resolve(location);
42+
}
43+
}
44+
else {
45+
locationManager.stopLocationMonitoring();
46+
if ("undefined" !== typeof timerId) {
47+
timer.clearTimeout(timerId);
48+
}
49+
d.resolve(location);
50+
}
51+
}, function (error: Error) {
52+
console.error('Location error received: ' + error);
53+
locationManager.stopLocationMonitoring();
54+
if ("undefined" !== typeof timerId) {
55+
timer.clearTimeout(timerId);
56+
}
57+
d.reject(error);
58+
},
59+
options
60+
);
61+
62+
if (options && ("number" === typeof options.timeout)) {
63+
timerId = timer.setTimeout(function () {
64+
locationManager.stopLocationMonitoring();
65+
d.reject(new Error("timeout searching for location"));
66+
}, options.timeout);
67+
}
68+
69+
return d.promise();
70+
}

location/location_types.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,31 @@ export class Location {
2323
public ios: any; // iOS native location
2424
}
2525

26-
export class Options {
26+
export interface Options {
2727
/**
2828
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
2929
*/
30-
public desiredAccuracy: number;
30+
desiredAccuracy?: number;
3131

3232
/**
3333
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
3434
*/
35-
public updateDistance: number;
35+
updateDistance?: number;
3636

3737
/**
3838
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
3939
*/
40-
public minimumUpdateTime: number;
40+
minimumUpdateTime?: number;
41+
42+
/**
43+
* how old locations to receive in ms.
44+
*/
45+
maximumAge?: number;
46+
47+
/**
48+
* how long to wait for a location in ms.
49+
*/
50+
timeout?: number;
4151
}
4252

4353
export class LocationRegion {

0 commit comments

Comments
 (0)