mapillary-js/test/viewer/PanService.test.ts
Oscar Lorentzon 40a2d5e539 feat: add camera factory
Make it possible to register camera constructors.
Register default cameras.
Use camera factory when creating cameras.
2022-07-26 09:34:30 -07:00

172 lines
7.8 KiB
TypeScript

import { Subject } from "rxjs";
import { skip } from "rxjs/operators";
import { Image } from "../../src/graph/Image";
import { Transform } from "../../src/geo/Transform";
import { LngLatAlt } from "../../src/api/interfaces/LngLatAlt";
import { GraphService } from "../../src/graph/GraphService";
import { AnimationFrame } from "../../src/state/interfaces/AnimationFrame";
import { StateService } from "../../src/state/StateService";
import { PanService } from "../../src/viewer/PanService";
import { FrameHelper } from "../helper/FrameHelper";
import { GraphServiceMockCreator } from "../helper/GraphServiceMockCreator";
import { ImageHelper } from "../helper/ImageHelper";
import { StateServiceMockCreator } from "../helper/StateServiceMockCreator";
import { State } from "../../src/state/State";
import { ProjectionService } from "../../src/viewer/ProjectionService";
describe("PanService.ctor", () => {
it("should be defined when constructed", () => {
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const panService: PanService = new PanService(graphService, stateService, projectionService);
expect(panService).toBeDefined();
});
});
describe("PanService.panImages$", () => {
it("should emit empty initially", (done: Function) => {
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const cacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(cacheBoundingBoxSubject);
const panService: PanService = new PanService(graphService, stateService, projectionService);
panService.panImages$.subscribe(
(images: [Image, Transform, number][]): void => {
expect(images.length).toBe(0);
done();
});
(<Subject<AnimationFrame>>stateService.currentState$).next(new FrameHelper().createFrame());
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
});
it("should emit", (done: Function) => {
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const cacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(cacheBoundingBoxSubject);
const panService: PanService = new PanService(graphService, stateService, projectionService);
panService.start();
panService.panImages$.pipe(
skip(1))
.subscribe(
(images: [Image, Transform, number][]): void => {
expect(images.length).toBe(0);
done();
});
(<Subject<AnimationFrame>>stateService.currentState$).next(new FrameHelper().createFrame());
(<Subject<State>>stateService.state$).next(State.Traversing);
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
cacheBoundingBoxSubject.next([]);
});
it("should emit empty when not merged", (done: Function) => {
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const cacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(cacheBoundingBoxSubject);
const panService: PanService = new PanService(graphService, stateService, projectionService);
panService.start();
panService.panImages$.pipe(
skip(1))
.subscribe(
(images: [Image, Transform, number][]): void => {
expect(images.length).toBe(0);
done();
});
(<Subject<AnimationFrame>>stateService.currentState$).next(new FrameHelper().createFrame());
(<Subject<State>>stateService.state$).next(State.Traversing);
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createUnmergedImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
cacheBoundingBoxSubject.next([]);
});
it("should catch error and keep emitting", () => {
spyOn(console, "error").and.stub();
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const erroredCacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(erroredCacheBoundingBoxSubject);
const panService: PanService = new PanService(graphService, stateService, projectionService);
panService.start();
let emitCount: number = 0;
panService.panImages$.pipe(skip(1))
.subscribe(
(): void => {
emitCount++;
});
(<Subject<AnimationFrame>>stateService.currentState$).next(new FrameHelper().createFrame());
(<Subject<State>>stateService.state$).next(State.Traversing);
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
erroredCacheBoundingBoxSubject.error(new Error());
expect(emitCount).toBe(1);
const cacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(cacheBoundingBoxSubject);
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
cacheBoundingBoxSubject.next([]);
expect(emitCount).toBe(3);
});
it("should emit after being disabled", (done: Function) => {
const graphService: GraphService = new GraphServiceMockCreator().create();
const stateService: StateService = new StateServiceMockCreator().create();
const projectionService = new ProjectionService();
const cacheBoundingBoxSubject: Subject<Image[]> = new Subject<Image[]>();
(<jasmine.Spy>graphService.cacheBoundingBox$).and.returnValue(cacheBoundingBoxSubject);
const panService: PanService = new PanService(graphService, stateService, projectionService);
panService.start();
panService.disable();
panService.enable();
panService.panImages$.pipe(
skip(1))
.subscribe(
(images: [Image, Transform, number][]): void => {
expect(images.length).toBe(0);
done();
});
(<Subject<AnimationFrame>>stateService.currentState$).next(new FrameHelper().createFrame());
(<Subject<State>>stateService.state$).next(State.Traversing);
(<Subject<Image>>stateService.currentImage$).next(new ImageHelper().createImage());
(<Subject<LngLatAlt>>stateService.reference$).next({ alt: 0, lat: 0, lng: 0 });
cacheBoundingBoxSubject.next([]);
});
});