Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
HARP-5144: Unit tests for picking (#733)
Browse files Browse the repository at this point in the history
MapView.dispose()/MapView.update() fix
    MapViewTest fix

Signed-off-by: Stepan Krovspei <11714928+pit-rpg@users.noreply.github.com>
  • Loading branch information
pit-rpg committed Sep 13, 2019
1 parent 1ae2a7c commit ccf277f
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 8 deletions.
6 changes: 4 additions & 2 deletions @here/harp-mapview-decoder/lib/GeoJsonTiler.ts
Expand Up @@ -8,7 +8,9 @@ import { GeoJson, ITiler } from "@here/harp-datasource-protocol";
import { TileKey } from "@here/harp-geoutils";

// tslint:disable-next-line:no-var-requires
const geojsonvt = require("geojson-vt");
const geojsonvtExport = require("geojson-vt");
// to be able to run tests on nodejs
const geojsonvt = geojsonvtExport.default || geojsonvtExport;

interface GeoJsonVtIndex {
getTile(level: number, column: number, row: number): any;
Expand Down Expand Up @@ -51,7 +53,7 @@ export class GeoJsonTiler implements ITiler {

this.indexes.set(
indexId,
geojsonvt.default(input, {
geojsonvt(input, {
maxZoom: 20, // max zoom to preserve detail on
indexMaxZoom: 5, // max zoom in the tile index
indexMaxPoints: 100000, // max number of points per tile in the tile index
Expand Down
14 changes: 13 additions & 1 deletion @here/harp-mapview/lib/MapView.ts
Expand Up @@ -621,6 +621,7 @@ export class MapView extends THREE.EventDispatcher {
*/
readonly mapRenderingManager: IMapRenderingManager;

private m_movementFinishedUpdateTimerId?: any;
private m_postEffects?: PostEffects;

private m_skyBackground?: SkyBackground;
Expand Down Expand Up @@ -999,6 +1000,11 @@ export class MapView extends THREE.EventDispatcher {
* cleanup, you must ensure that all references to this `MapView` are removed.
*/
dispose() {
if (this.m_movementFinishedUpdateTimerId) {
clearTimeout(this.m_movementFinishedUpdateTimerId);
this.m_movementFinishedUpdateTimerId = undefined;
}

if (this.m_animationFrameHandle !== undefined) {
cancelAnimationFrame(this.m_animationFrameHandle);
this.m_animationFrameHandle = undefined;
Expand Down Expand Up @@ -2891,7 +2897,13 @@ export class MapView extends THREE.EventDispatcher {

// render at the next possible time.
if (!this.animating) {
setTimeout(() => this.update(), 0);
if (this.m_movementFinishedUpdateTimerId !== undefined) {
clearTimeout(this.m_movementFinishedUpdateTimerId);
}
this.m_movementFinishedUpdateTimerId = setTimeout(() => {
this.m_movementFinishedUpdateTimerId = undefined;
this.update();
}, 0);
}
}

Expand Down
24 changes: 19 additions & 5 deletions @here/harp-mapview/test/MapViewTest.ts
Expand Up @@ -41,7 +41,7 @@ describe("MapView", function() {
let addEventListenerSpy: sinon.SinonStub;
let removeEventListenerSpy: sinon.SinonStub;
let canvas: HTMLCanvasElement;
let mapView: MapView;
let mapView: MapView | undefined;

beforeEach(function() {
sandbox = sinon.createSandbox();
Expand All @@ -64,10 +64,13 @@ describe("MapView", function() {
addEventListener: addEventListenerSpy,
removeEventListener: removeEventListenerSpy
} as unknown) as HTMLCanvasElement;
mapView = new MapView({ canvas });
});

afterEach(function() {
if (mapView !== undefined) {
mapView.dispose();
mapView = undefined;
}
sandbox.restore();
if (inNodeContext) {
delete global.window;
Expand All @@ -85,6 +88,7 @@ describe("MapView", function() {
rotationSpy = sinon.spy(MapViewUtils, "setRotation");
zoomSpy = sinon.spy(MapViewUtils, "zoomOnTargetPosition");

mapView = new MapView({ canvas });
mapView.setCameraGeolocationAndZoom(coords, 18, 10, 20);

expect(zoomSpy.calledOnce).to.be.true;
Expand All @@ -96,6 +100,7 @@ describe("MapView", function() {
});

it("Correctly sets event listeners and handlers webgl context restored", function() {
mapView = new MapView({ canvas });
const updateSpy = sinon.spy(mapView, "update");

expect(addEventListenerSpy.callCount).to.be.equal(2);
Expand Down Expand Up @@ -139,6 +144,8 @@ describe("MapView", function() {
});

it("Correctly sets and removes event listeners by API", function() {
mapView = new MapView({ canvas });

const restoreStub = sinon.stub();
const lostStub = sinon.stub();

Expand All @@ -162,6 +169,7 @@ describe("MapView", function() {
it("supports #dispose", async function() {
const dataSource = new FakeOmvDataSource();
const dataSourceDisposeStub = sinon.stub(dataSource, "dispose");
mapView = new MapView({ canvas });
mapView.addDataSource(dataSource);
await dataSource.connect();

Expand Down Expand Up @@ -256,6 +264,7 @@ describe("MapView", function() {
});

it("returns the fog through #fog getter", function() {
mapView = new MapView({ canvas });
expect(mapView.fog instanceof MapViewFog).to.equal(true);
});

Expand All @@ -272,6 +281,7 @@ describe("MapView", function() {
for (let y = -100; y <= 100; y += 100) {
mapView = new MapView({ canvas: (customCanvas as any) as HTMLCanvasElement });
const resultA = mapView.getScreenPosition(mapView.getGeoCoordinatesAt(x, y)!);
mapView.dispose();

customCanvas.pixelRatio = 2;
mapView = new MapView({ canvas: (customCanvas as any) as HTMLCanvasElement });
Expand All @@ -288,14 +298,18 @@ describe("MapView", function() {
it("updates background storage level offset", async function() {
if (inNodeContext) {
global.requestAnimationFrame = (callback: FrameRequestCallback) => {
setTimeout(() => {
return setTimeout(() => {
// avoid camera movement events, needed setup is not done.
mapView.cameraMovementDetector.clear(mapView);
if (mapView !== undefined) {
mapView.cameraMovementDetector.clear(mapView);
}
callback(0);
return 0;
}, 0);
};
global.cancelAnimationFrame = () => {};
global.cancelAnimationFrame = (id: number) => {
clearTimeout(id);
};
}
mapView = new MapView({ canvas });
mapView.theme = {};
Expand Down
1 change: 1 addition & 0 deletions @here/harp-omv-datasource/package.json
Expand Up @@ -43,6 +43,7 @@
"protobufjs": "^6.8.4"
},
"devDependencies": {
"@here/harp-geojson-datasource": "^0.10.0",
"@here/harp-fetch": "^0.3.6",
"@here/harp-test-utils": "^0.10.0",
"@types/chai": "^4.1.2",
Expand Down

0 comments on commit ccf277f

Please sign in to comment.