updated location and location tests

This commit is contained in:
Stanimir Karoserov
2014-05-14 19:30:10 +03:00
parent 0242773cae
commit e8d3669d98
7 changed files with 163 additions and 16 deletions

View File

@ -205,6 +205,9 @@
<TypeScriptCompile Include="localsettings\local_settings_common.ts"> <TypeScriptCompile Include="localsettings\local_settings_common.ts">
<DependentUpon>local_settings.d.ts</DependentUpon> <DependentUpon>local_settings.d.ts</DependentUpon>
</TypeScriptCompile> </TypeScriptCompile>
<TypeScriptCompile Include="location\location_common.ts">
<DependentUpon>location.d.ts</DependentUpon>
</TypeScriptCompile>
<Content Include="_references.ts" /> <Content Include="_references.ts" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,5 +1,6 @@
import TKUnit = require("Tests/TKUnit"); import TKUnit = require("Tests/TKUnit");
import locationModule = require("location/location"); import locationModule = require("location/location");
import types = require("location/location_types");
var LocationManager = locationModule.LocationManager; var LocationManager = locationModule.LocationManager;
var Location = locationModule.Location; var Location = locationModule.Location;
@ -16,17 +17,19 @@ export var testLocation = function () {
locationManager.startLocationMonitoring(function(location) { locationManager.startLocationMonitoring(function(location) {
locationReceived = true; locationReceived = true;
}, function(error) { }, function(error) {
console.log('Location error received: ' + error); //console.log('Location error received: ' + error);
locationReceived = error; locationReceived = error;
} }
); );
var isReady = function () { var isReady = function () {
return locationReceived; return locationReceived;
} }
TKUnit.waitUntilReady(isReady, 3); TKUnit.waitUntilReady(isReady, 10);
locationManager.stopLocationMonitoring(); locationManager.stopLocationMonitoring();
TKUnit.assert(true === locationReceived, locationReceived); TKUnit.assert(true === locationReceived, locationReceived);
}; };
@ -46,4 +49,44 @@ export var testLastKnownLocation = function () {
var lastKnownLocation = locationManager.lastKnownLocation; var lastKnownLocation = locationManager.lastKnownLocation;
TKUnit.assert((lastKnownLocation != null), "There is no last known location"); TKUnit.assert((lastKnownLocation != null), "There is no last known location");
}; };
function doOnce(options: locationModule.Options) {
var locationReceived;
locationModule.getLocation(options).then(function (location) {
locationReceived = true;
}).fail(function (error) {
//console.log('Location error received: ' + error);
locationReceived = error;
});
var isReady = function () {
return locationReceived;
}
TKUnit.waitUntilReady(isReady, 10);
TKUnit.assert(true === locationReceived, locationReceived);
}
export var testLocationOnce = function () {
doOnce(undefined);
};
export var testLocationOnceTimeout0 = function () {
doOnce({timeout: 0});
};
export var testLocationOnceMaximumAge = function () {
TKUnit.waitUntilReady(function () { return false; }, 2);
doOnce({ maximumAge: 3000, timeout: 0 }); // this should pass
try {
doOnce({ maximumAge: 1000, timeout: 0 });
TKUnit.assert(false, "maximumAge check failed");
}
catch (e) {
}
};
export var testLocationOnceTimeout1000 = function () {
doOnce({ timeout: 1000 });
};

View File

@ -1,9 +1,12 @@
import types = require("location/location_types"); import types = require("location/location_types");
import appModule = require("application/application"); import appModule = require("application/application");
import common = require("location/location_common");
import merger = require("utils/module_merge");
// merge the exports of the types module with the exports of this file // merge the exports of the types module with the exports of this file
declare var exports; declare var exports;
require("utils/module_merge").merge(types, exports); merger.merge(types, exports);
merger.merge(common, exports);
export class LocationManager { export class LocationManager {
// in meters // in meters

View File

@ -1,4 +1,7 @@
export declare enum Accuracy { 
import promises = require("promises/promises");
export declare enum Accuracy {
// in meters // in meters
ANY, ANY,
HIGH, HIGH,
@ -31,21 +34,31 @@ export declare class Location {
public ios: any; // iOS CLLocation public ios: any; // iOS CLLocation
} }
export declare class Options { export interface Options {
/** /**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH * Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/ */
desiredAccuracy: number; desiredAccuracy?: number;
/** /**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters * Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/ */
updateDistance: number; updateDistance?: number;
/** /**
* Minimum time interval between location updates, in milliseconds (android only) * Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/ */
minimumUpdateTime: number; minimumUpdateTime?: number;
/**
* how old locations to receive in ms.
*/
maximumAge?: number;
/**
* how long to wait for a location in ms.
*/
timeout?: number;
} }
export declare class LocationManager { export declare class LocationManager {
@ -98,3 +111,5 @@ export declare class LocationManager {
*/ */
lastKnownLocation: Location; lastKnownLocation: Location;
} }
export declare var getLocation: (options?: Options) => promises.Promise<Location>;

View File

@ -1,8 +1,11 @@
import types = require("location/location_types"); import types = require("location/location_types");
import common = require("location/location_common");
import merger = require("utils/module_merge");
// merge the exports of the types module with the exports of this file // merge the exports of the types module with the exports of this file
declare var exports; declare var exports;
require("utils/module_merge").merge(types, exports); merger.merge(types, exports);
merger.merge(common, exports);
export class LocationManager { export class LocationManager {

View File

@ -0,0 +1,70 @@

import types = require("location/location_types");
import promises = require("promises/promises");
import locationModule = require("location/location");
import timer = require("timer/timer");
export var getLocation = function (options?: types.Options) : promises.Promise<types.Location> {
var d = promises.defer<types.Location>();
var timerId;
var locationManager = new locationModule.LocationManager();
if (options && (0 === options.timeout)) {
var location = locationManager.lastKnownLocation;
if (location) {
if (options && ("number" === typeof options.maximumAge)) {
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
d.resolve(location);
}
else {
d.reject(new Error("timeout is 0 and last known location is older than maximumAge"));
}
}
else {
d.resolve(location);
}
}
else {
d.reject(new Error("timeout is 0 and no known location found"));
}
return d.promise();
}
locationManager.startLocationMonitoring(function (location: types.Location) {
if (options && ("number" === typeof options.maximumAge)) {
if (location.timestamp.valueOf() + options.maximumAge > new Date().valueOf()) {
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.resolve(location);
}
}
else {
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.resolve(location);
}
}, function (error: Error) {
console.error('Location error received: ' + error);
locationManager.stopLocationMonitoring();
if ("undefined" !== typeof timerId) {
timer.clearTimeout(timerId);
}
d.reject(error);
},
options
);
if (options && ("number" === typeof options.timeout)) {
timerId = timer.setTimeout(function () {
locationManager.stopLocationMonitoring();
d.reject(new Error("timeout searching for location"));
}, options.timeout);
}
return d.promise();
}

View File

@ -23,21 +23,31 @@ export class Location {
public ios: any; // iOS native location public ios: any; // iOS native location
} }
export class Options { export interface Options {
/** /**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH * Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/ */
public desiredAccuracy: number; desiredAccuracy?: number;
/** /**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters * Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/ */
public updateDistance: number; updateDistance?: number;
/** /**
* Minimum time interval between location updates, in milliseconds (ignored on iOS) * Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/ */
public minimumUpdateTime: number; minimumUpdateTime?: number;
/**
* how old locations to receive in ms.
*/
maximumAge?: number;
/**
* how long to wait for a location in ms.
*/
timeout?: number;
} }
export class LocationRegion { export class LocationRegion {