2022-03-23 17:08:56 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-23 17:08:56 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-03-23 17:08:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-02-13 11:39:16 +00:00
|
|
|
import { Mocked } from "jest-mock";
|
2022-03-23 17:08:56 +00:00
|
|
|
|
|
|
|
import {
|
2023-02-13 11:39:16 +00:00
|
|
|
GenericPosition,
|
2022-03-23 17:08:56 +00:00
|
|
|
GeolocationError,
|
|
|
|
getGeoUri,
|
|
|
|
mapGeolocationError,
|
|
|
|
mapGeolocationPositionToTimedGeo,
|
|
|
|
watchPosition,
|
|
|
|
} from "../../../src/utils/beacon";
|
2022-03-25 11:30:50 +00:00
|
|
|
import { getCurrentPosition } from "../../../src/utils/beacon/geolocation";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { makeGeolocationPosition, mockGeolocation, getMockGeolocationPositionError } from "../../test-utils";
|
2022-03-23 17:08:56 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("geolocation utilities", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
let geolocation: Mocked<Geolocation>;
|
2022-03-23 17:08:56 +00:00
|
|
|
const defaultPosition = makeGeolocationPosition({});
|
|
|
|
|
2022-04-29 09:12:28 +00:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
|
2022-03-23 17:08:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
geolocation = mockGeolocation();
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(Date, "now").mockReturnValue(now);
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(Date, "now").mockRestore();
|
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("getGeoUri", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
it("Renders a URI with only lat and lon", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const pos: GenericPosition = {
|
2022-03-23 17:08:56 +00:00
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
altitude: undefined,
|
|
|
|
accuracy: undefined,
|
|
|
|
|
|
|
|
timestamp: 12334,
|
|
|
|
};
|
|
|
|
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Nulls in location are not shown in URI", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const pos: GenericPosition = {
|
2022-03-23 17:08:56 +00:00
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
|
|
|
|
timestamp: 12334,
|
|
|
|
};
|
|
|
|
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Renders a URI with 3 coords", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const pos: GenericPosition = {
|
2022-03-23 17:08:56 +00:00
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
altitude: 332.54,
|
|
|
|
accuracy: undefined,
|
|
|
|
timestamp: 12334,
|
|
|
|
};
|
|
|
|
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4,332.54");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Renders a URI with accuracy", () => {
|
2023-02-13 11:39:16 +00:00
|
|
|
const pos: GenericPosition = {
|
2022-03-23 17:08:56 +00:00
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
altitude: undefined,
|
|
|
|
accuracy: 21,
|
|
|
|
timestamp: 12334,
|
|
|
|
};
|
|
|
|
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4;u=21");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Renders a URI with accuracy and altitude", () => {
|
|
|
|
const pos = {
|
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
altitude: 12.3,
|
|
|
|
accuracy: 21,
|
|
|
|
timestamp: 12334,
|
|
|
|
};
|
|
|
|
expect(getGeoUri(pos)).toEqual("geo:43.2,12.4,12.3;u=21");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("mapGeolocationError", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
// suppress expected errors from test log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns default for other error", () => {
|
|
|
|
const error = new Error("oh no..");
|
2022-03-23 17:08:56 +00:00
|
|
|
expect(mapGeolocationError(error)).toEqual(GeolocationError.Default);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns unavailable for unavailable error", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
const error = new Error(GeolocationError.Unavailable);
|
|
|
|
expect(mapGeolocationError(error)).toEqual(GeolocationError.Unavailable);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("maps geo error permissiondenied correctly", () => {
|
|
|
|
const error = getMockGeolocationPositionError(1, "message");
|
2022-03-23 17:08:56 +00:00
|
|
|
expect(mapGeolocationError(error)).toEqual(GeolocationError.PermissionDenied);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("maps geo position unavailable error correctly", () => {
|
|
|
|
const error = getMockGeolocationPositionError(2, "message");
|
2022-03-23 17:08:56 +00:00
|
|
|
expect(mapGeolocationError(error)).toEqual(GeolocationError.PositionUnavailable);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("maps geo timeout error correctly", () => {
|
|
|
|
const error = getMockGeolocationPositionError(3, "message");
|
2022-03-23 17:08:56 +00:00
|
|
|
expect(mapGeolocationError(error)).toEqual(GeolocationError.Timeout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("mapGeolocationPositionToTimedGeo()", () => {
|
|
|
|
it("maps geolocation position correctly", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
expect(mapGeolocationPositionToTimedGeo(defaultPosition)).toEqual({
|
2022-12-12 11:24:14 +00:00
|
|
|
timestamp: now,
|
|
|
|
geoUri: "geo:54.001927,-8.253491;u=1",
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("watchPosition()", () => {
|
|
|
|
it("throws with unavailable error when geolocation is not available", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
// suppress expected errors from test log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-23 17:08:56 +00:00
|
|
|
|
|
|
|
// remove the mock we added
|
|
|
|
// @ts-ignore illegal assignment to readonly property
|
|
|
|
navigator.geolocation = undefined;
|
|
|
|
|
|
|
|
const positionHandler = jest.fn();
|
|
|
|
const errorHandler = jest.fn();
|
|
|
|
|
|
|
|
expect(() => watchPosition(positionHandler, errorHandler)).toThrow(GeolocationError.Unavailable);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets up position handler with correct options", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
const positionHandler = jest.fn();
|
|
|
|
const errorHandler = jest.fn();
|
|
|
|
watchPosition(positionHandler, errorHandler);
|
|
|
|
|
|
|
|
const [, , options] = geolocation.watchPosition.mock.calls[0];
|
|
|
|
expect(options).toEqual({
|
2022-04-28 14:51:47 +00:00
|
|
|
maximumAge: 60000,
|
|
|
|
timeout: 10000,
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("returns clearWatch function", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
const watchId = 1;
|
|
|
|
geolocation.watchPosition.mockReturnValue(watchId);
|
|
|
|
const positionHandler = jest.fn();
|
|
|
|
const errorHandler = jest.fn();
|
|
|
|
const clearWatch = watchPosition(positionHandler, errorHandler);
|
|
|
|
|
|
|
|
clearWatch();
|
|
|
|
|
|
|
|
expect(geolocation.clearWatch).toHaveBeenCalledWith(watchId);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("calls position handler with position", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
const positionHandler = jest.fn();
|
|
|
|
const errorHandler = jest.fn();
|
|
|
|
watchPosition(positionHandler, errorHandler);
|
|
|
|
|
|
|
|
expect(positionHandler).toHaveBeenCalledWith(defaultPosition);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("maps geolocation position error and calls error handler", () => {
|
2022-03-23 17:08:56 +00:00
|
|
|
// suppress expected errors from test log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2023-02-13 11:39:16 +00:00
|
|
|
geolocation.watchPosition.mockImplementation((_callback, error) => {
|
2023-02-16 09:38:44 +00:00
|
|
|
error!(getMockGeolocationPositionError(1, "message"));
|
2023-02-13 11:39:16 +00:00
|
|
|
return -1;
|
|
|
|
});
|
2022-03-23 17:08:56 +00:00
|
|
|
const positionHandler = jest.fn();
|
|
|
|
const errorHandler = jest.fn();
|
|
|
|
watchPosition(positionHandler, errorHandler);
|
|
|
|
|
|
|
|
expect(errorHandler).toHaveBeenCalledWith(GeolocationError.PermissionDenied);
|
|
|
|
});
|
|
|
|
});
|
2022-03-25 11:30:50 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("getCurrentPosition()", () => {
|
|
|
|
it("throws with unavailable error when geolocation is not available", async () => {
|
2022-03-25 11:30:50 +00:00
|
|
|
// suppress expected errors from test log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-25 11:30:50 +00:00
|
|
|
|
|
|
|
// remove the mock we added
|
|
|
|
// @ts-ignore illegal assignment to readonly property
|
|
|
|
navigator.geolocation = undefined;
|
|
|
|
|
|
|
|
await expect(() => getCurrentPosition()).rejects.toThrow(GeolocationError.Unavailable);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("throws with geolocation error when geolocation.getCurrentPosition fails", async () => {
|
2022-03-25 11:30:50 +00:00
|
|
|
// suppress expected errors from test log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-25 11:30:50 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const timeoutError = getMockGeolocationPositionError(3, "message");
|
2023-02-16 09:38:44 +00:00
|
|
|
geolocation.getCurrentPosition.mockImplementation((callback, error) => error!(timeoutError));
|
2022-03-25 11:30:50 +00:00
|
|
|
|
|
|
|
await expect(() => getCurrentPosition()).rejects.toThrow(GeolocationError.Timeout);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("resolves with current location", async () => {
|
2022-03-25 11:30:50 +00:00
|
|
|
geolocation.getCurrentPosition.mockImplementation((callback, error) => callback(defaultPosition));
|
|
|
|
|
|
|
|
const result = await getCurrentPosition();
|
|
|
|
expect(result).toEqual(defaultPosition);
|
|
|
|
});
|
|
|
|
});
|
2022-03-23 17:08:56 +00:00
|
|
|
});
|