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

Commit

Permalink
MAPSJS-2983 Adding more tests for prepareRender
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Stichbury <[email protected]>
  • Loading branch information
nzjony committed Aug 23, 2021
1 parent 206e7f4 commit 7b43bd8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion @here/harp-mapview/test/PoiBatchRegistryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("PoiBatchRegistry", () => {
registry = new PoiBatchRegistry(renderer.capabilities);
});
describe("registerPoi", function () {
it.only("return undefined if poiInfo has no image item", () => {
it("return undefined if poiInfo has no image item", () => {
const poiInfo = new PoiInfoBuilder().build(textElement);
const buffer = registry.registerPoi(poiInfo, defaultPoiLayer);

Expand Down
92 changes: 91 additions & 1 deletion @here/harp-mapview/test/PoiRendererTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
import { Env } from "@here/harp-datasource-protocol";
import { Math2D } from "@here/harp-utils";
import { expect } from "chai";
import * as THREE from "three";

import { MapViewImageCache } from "../lib/image/MapViewImageCache";
import { MapView } from "../lib/MapView";
import { PoiManager } from "../lib/poi/PoiManager";
import { PoiBuffer, PoiRenderer } from "../lib/poi/PoiRenderer";
import { TextElement } from "../lib/text/TextElement";

import sinon = require("sinon");
import * as THREE from "three";

describe("PoiRenderer", function () {
describe("computeIconScreenBox", function () {
Expand Down Expand Up @@ -97,4 +103,88 @@ describe("PoiRenderer", function () {
expect(screenBox.h).to.equal(16);
});
});

describe("search for cached images", function () {
const webGLRenderer = {
capabilities: {
isWebGL2: false
}
} as THREE.WebGLRenderer;
const mapViewStub = sinon.createStubInstance(MapView);
const poiManager = new PoiManager((mapViewStub as any) as MapView);
const testImageName = "testImage";
const mapEnv = new Env();

const createPointLabel = (name: string) => {
return {
poiInfo: {
imageTextureName: name,
isValid: true,
buffer: undefined,
technique: {}
},
visible: true
} as TextElement;
};

let x = 1;
let y = 1;

const addRandomImageToCache = (
testCache: MapViewImageCache,
name: string,
load: boolean
) => {
// Note, the images must be unique, otherwise the test will fail, because the internal
// caching mechanism of the ImageCache will have already loaded the image.
return testCache.addImage(name, `https://picsum.photos/${x++}/${y++}`, load);
};

it("poi is valid when in cache and loading started", async function () {
this.timeout(5000);

const testCache = new MapViewImageCache();
const testImageItem = addRandomImageToCache(testCache, testImageName, true);
const caches = [testCache];
const poiRenderer = new PoiRenderer(webGLRenderer, poiManager, caches);
const pointLabel = createPointLabel(testImageName);
const waitingForLoad = poiRenderer.prepareRender(pointLabel, mapEnv);
// This is false because the image is still being loaded in the background, notice the
// true flag passed to the call to testCache.addImage.
expect(waitingForLoad).false;

// Promise.resolve used to convert the type `ImageItem | Promise<ImageItem>` to
// `Promise<ImageItem>`.
await Promise.resolve(testImageItem);

const imageLoaded = poiRenderer.prepareRender(pointLabel, mapEnv);
expect(imageLoaded).true;

// Check that a second attempt to prepareRender succeeds.
const imageLoadedAgain = poiRenderer.prepareRender(pointLabel, mapEnv);
expect(imageLoadedAgain).true;
});

it("poi is invalid when not in cache, adding to cache means it will load", async function () {
this.timeout(5000);

const testCache = new MapViewImageCache();
// Empty cache for now
const caches = [testCache];
const poiRenderer = new PoiRenderer(webGLRenderer, poiManager, caches);
const pointLabel = createPointLabel(testImageName);
const waitingForLoad = poiRenderer.prepareRender(pointLabel, mapEnv);
expect(waitingForLoad).false;

const testImageItem = addRandomImageToCache(testCache, testImageName, true);

// Promise.resolve used to convert the type `ImageItem | Promise<ImageItem>` to
// `Promise<ImageItem>`.
await Promise.resolve(testImageItem);

const imageLoaded = poiRenderer.prepareRender(pointLabel, mapEnv);
// Check that adding to the cache means it will now load.
expect(imageLoaded).true;
});
});
});
1 change: 1 addition & 0 deletions karma.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const options = function (isCoverage, isMapSdk, prefixDirectory) {
allowJs: true
},
coverageOptions: {
instrumentation: isCoverage ? true : false,
// This is needed otherwise the tests are included in the code coverage %.
exclude: [
/test\/+/,
Expand Down

0 comments on commit 7b43bd8

Please sign in to comment.