From 33e32da2ddb29e9e7c09e3bc08ebd9204c323ed8 Mon Sep 17 00:00:00 2001 From: Gabby Getz Date: Thu, 26 Jan 2023 16:33:52 -0500 Subject: [PATCH] Cleanup specs --- .../Source/Core/VRTheWorldTerrainProvider.js | 12 +- ...ArcGISTiledElevationTerrainProviderSpec.js | 274 +++---- .../Specs/Core/CesiumTerrainProviderSpec.js | 669 ++++++------------ .../Core/GoogleEarthEnterpriseMetadataSpec.js | 64 +- ...oogleEarthEnterpriseTerrainProviderSpec.js | 202 +++--- .../Core/VRTheWorldTerrainProviderSpec.js | 163 ++--- 6 files changed, 529 insertions(+), 855 deletions(-) diff --git a/packages/engine/Source/Core/VRTheWorldTerrainProvider.js b/packages/engine/Source/Core/VRTheWorldTerrainProvider.js index cf5c59be0f51..b8801ac0f185 100644 --- a/packages/engine/Source/Core/VRTheWorldTerrainProvider.js +++ b/packages/engine/Source/Core/VRTheWorldTerrainProvider.js @@ -110,10 +110,12 @@ function metadataSuccess(terrainProviderBuilder, xml) { } function metadataFailure(resource, error, provider) { - const message = defaultValue( - defined(error) ? error.message : undefined, - `An error occurred while accessing ${resource.url}.` - ); + let message = `An error occurred while accessing ${resource.url}`; + + if (defined(error) && defined(error.message)) { + message = `${message}: ${error.message}`; + } + TileProviderError.reportError( undefined, provider, @@ -121,7 +123,7 @@ function metadataFailure(resource, error, provider) { message ); - throw error; + throw new RuntimeError(message); } async function requestMetadata(terrainProviderBuilder, resource, provider) { diff --git a/packages/engine/Specs/Core/ArcGISTiledElevationTerrainProviderSpec.js b/packages/engine/Specs/Core/ArcGISTiledElevationTerrainProviderSpec.js index 5dd53d1f84f7..6b22bcf79655 100644 --- a/packages/engine/Specs/Core/ArcGISTiledElevationTerrainProviderSpec.js +++ b/packages/engine/Specs/Core/ArcGISTiledElevationTerrainProviderSpec.js @@ -11,8 +11,6 @@ import { Math as CesiumMath, } from "../../index.js"; -import pollToPromise from "../../../../Specs/pollToPromise.js"; - describe("Core/ArcGISTiledElevationTerrainProvider", function () { const lercTileUrl = "Data/Images/Red16x16.png"; let availability; @@ -270,68 +268,52 @@ describe("Core/ArcGISTiledElevationTerrainProvider", function () { }); }); - it("has error event", function () { - const provider = new ArcGISTiledElevationTerrainProvider({ - url: "made/up/url", - }); + it("has error event", async function () { + const provider = await ArcGISTiledElevationTerrainProvider.fromUrl( + "made/up/url" + ); expect(provider.errorEvent).toBeDefined(); expect(provider.errorEvent).toBe(provider.errorEvent); - - return provider.readyPromise; }); - it("returns reasonable geometric error for various levels", function () { - const provider = new ArcGISTiledElevationTerrainProvider({ - url: "made/up/url", - }); - - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); - expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(1) * 2.0, - CesiumMath.EPSILON10 - ); - expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(2) * 2.0, - CesiumMath.EPSILON10 - ); - }); + it("returns reasonable geometric error for various levels", async function () { + const provider = await ArcGISTiledElevationTerrainProvider.fromUrl( + "made/up/url" + ); + expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); + expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(1) * 2.0, + CesiumMath.EPSILON10 + ); + expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(2) * 2.0, + CesiumMath.EPSILON10 + ); }); - it("logo is undefined if credit is not provided", function () { + it("logo is undefined if credit is not provided", async function () { delete metadata.copyrightText; - const provider = new ArcGISTiledElevationTerrainProvider({ - url: "made/up/url", - }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.credit).toBeUndefined(); - }); + const provider = await ArcGISTiledElevationTerrainProvider.fromUrl( + "made/up/url" + ); + expect(provider.credit).toBeUndefined(); }); - it("logo is defined if credit is provided", function () { - const provider = new ArcGISTiledElevationTerrainProvider({ - url: "made/up/url", - credit: "thanks to our awesome made up contributors!", - }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.credit).toBeDefined(); - }); + it("logo is defined if credit is provided", async function () { + const provider = await ArcGISTiledElevationTerrainProvider.fromUrl( + "made/up/url", + { + credit: "thanks to our awesome made up contributors!", + } + ); + expect(provider.credit).toBeDefined(); }); - it("does not have a water mask", function () { - const provider = new ArcGISTiledElevationTerrainProvider({ - url: "made/up/url", - }); + it("does not have a water mask", async function () { + const provider = await ArcGISTiledElevationTerrainProvider.fromUrl( + "made/up/url" + ); expect(provider.hasWaterMask).toBe(false); - return provider.readyPromise.catch(function (error) { - expect(error).toBeInstanceOf(RuntimeError); - }); }); it("is not ready immediately", function () { @@ -344,126 +326,88 @@ describe("Core/ArcGISTiledElevationTerrainProvider", function () { }); }); - it("detects WebMercator tiling scheme", function () { + it("detects WebMercator tiling scheme", async function () { const baseUrl = "made/up/url"; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.tilingScheme).toBeInstanceOf( - WebMercatorTilingScheme - ); - }); + expect(terrainProvider.tilingScheme).toBeInstanceOf( + WebMercatorTilingScheme + ); }); - it("detects Geographic tiling scheme", function () { + it("detects Geographic tiling scheme", async function () { const baseUrl = "made/up/url"; - metadata.spatialReference.latestWkid = 4326; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.tilingScheme).toBeInstanceOf( - GeographicTilingScheme - ); - }); + expect(terrainProvider.tilingScheme).toBeInstanceOf(GeographicTilingScheme); }); - it("raises an error if the SRS is not supported", function () { + it("fromUrl throws if the SRS is not supported", async function () { const baseUrl = "made/up/url"; - metadata.spatialReference.latestWkid = 1234; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); - - return terrainProvider.readyPromise.then(fail).catch(function (error) { - expect(error).toBeInstanceOf(RuntimeError); - }); + await expectAsync( + ArcGISTiledElevationTerrainProvider.fromUrl(baseUrl) + ).toBeRejectedWithError(RuntimeError, "Invalid spatial reference"); }); - it("raises an error if tileInfo missing", function () { + it("fromUrl throws if tileInfo missing", async function () { const baseUrl = "made/up/url"; - delete metadata.tileInfo; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); - - return terrainProvider.readyPromise.then(fail).catch(function (error) { - expect(error).toBeInstanceOf(RuntimeError); - }); + await expectAsync( + ArcGISTiledElevationTerrainProvider.fromUrl(baseUrl) + ).toBeRejectedWithError(RuntimeError, "tileInfo is required"); }); - it("checks availability if TileMap capability exists", function () { + it("checks availability if TileMap capability exists", async function () { const baseUrl = "made/up/url"; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider._hasAvailability).toBe(true); - expect(terrainProvider._tilesAvailable).toBeDefined(); - expect(terrainProvider._tilesAvailabilityLoaded).toBeDefined(); - }); + expect(terrainProvider._hasAvailability).toBe(true); + expect(terrainProvider._tilesAvailable).toBeDefined(); + expect(terrainProvider._tilesAvailabilityLoaded).toBeDefined(); }); - it("does not check availability if TileMap capability is missing", function () { + it("does not check availability if TileMap capability is missing", async function () { const baseUrl = "made/up/url"; - metadata.capabilities = "Image,Mensuration"; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider._hasAvailability).toBe(false); - expect(terrainProvider._tilesAvailable).toBeUndefined(); - expect(terrainProvider._tilesAvailablityLoaded).toBeUndefined(); - }); + expect(terrainProvider._hasAvailability).toBe(false); + expect(terrainProvider._tilesAvailable).toBeUndefined(); + expect(terrainProvider._tilesAvailablityLoaded).toBeUndefined(); }); describe("requestTileGeometry", function () { - it("provides HeightmapTerrainData", function () { + it("provides HeightmapTerrainData", async function () { const baseUrl = "made/up/url"; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); - - return pollToPromise(function () { - return terrainProvider.ready; - }) - .then(function () { - const promise = terrainProvider.requestTileGeometry(0, 0, 0); - RequestScheduler.update(); - return promise; - }) - .then(function (loadedData) { - expect(loadedData).toBeInstanceOf(HeightmapTerrainData); - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); + + const promise = terrainProvider.requestTileGeometry(0, 0, 0); + RequestScheduler.update(); + const loadedData = await promise; + expect(loadedData).toBeInstanceOf(HeightmapTerrainData); }); - it("returns undefined if too many requests are already in progress", function () { + it("returns undefined if too many requests are already in progress", async function () { const baseUrl = "made/up/url"; - const deferreds = []; Resource._Implementations.createImage = function ( @@ -475,39 +419,35 @@ describe("Core/ArcGISTiledElevationTerrainProvider", function () { deferreds.push(deferred); }; - const terrainProvider = new ArcGISTiledElevationTerrainProvider({ - url: baseUrl, - }); - - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - let promise; - let i; - // Make one less request to account for the additional availability request. - for (i = 0; i < RequestScheduler.maximumRequestsPerServer - 1; ++i) { - const request = new Request({ - throttle: true, - throttleByServer: true, - }); - promise = terrainProvider.requestTileGeometry(0, 0, 0, request); - } - RequestScheduler.update(); - expect(promise).toBeDefined(); - - promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); - expect(promise).toBeUndefined(); - - for (i = 0; i < deferreds.length; ++i) { - deferreds[i].resolve(); - } - - return Promise.all( - deferreds.map(function (deferred) { - return deferred.promise; - }) - ); - }); + const terrainProvider = await ArcGISTiledElevationTerrainProvider.fromUrl( + baseUrl + ); + + let promise; + let i; + // Make one less request to account for the additional availability request. + for (i = 0; i < RequestScheduler.maximumRequestsPerServer - 1; ++i) { + const request = new Request({ + throttle: true, + throttleByServer: true, + }); + promise = terrainProvider.requestTileGeometry(0, 0, 0, request); + } + RequestScheduler.update(); + expect(promise).toBeDefined(); + + promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); + expect(promise).toBeUndefined(); + + for (i = 0; i < deferreds.length; ++i) { + deferreds[i].resolve(); + } + + await Promise.all( + deferreds.map(function (deferred) { + return deferred.promise; + }) + ); }); }); }); diff --git a/packages/engine/Specs/Core/CesiumTerrainProviderSpec.js b/packages/engine/Specs/Core/CesiumTerrainProviderSpec.js index f7559617a8fe..28a533b5412b 100644 --- a/packages/engine/Specs/Core/CesiumTerrainProviderSpec.js +++ b/packages/engine/Specs/Core/CesiumTerrainProviderSpec.js @@ -10,9 +10,9 @@ import { Request, RequestScheduler, Resource, + RuntimeError, TerrainProvider, } from "../../index.js"; -import pollToPromise from "../../../../Specs/pollToPromise.js"; describe("Core/CesiumTerrainProvider", function () { beforeEach(function () { @@ -238,348 +238,196 @@ describe("Core/CesiumTerrainProvider", function () { }); }); - it("uses geographic tiling scheme by default", function () { + it("uses geographic tiling scheme by default", async function () { returnHeightmapTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.tilingScheme).toBeInstanceOf(GeographicTilingScheme); - }); + expect(provider.tilingScheme).toBeInstanceOf(GeographicTilingScheme); }); - it("can use a custom ellipsoid", function () { + it("can use a custom ellipsoid", async function () { returnHeightmapTileJson(); const ellipsoid = new Ellipsoid(1, 2, 3); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", + const provider = await CesiumTerrainProvider.fromUrl("made/up/url", { ellipsoid: ellipsoid, }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.tilingScheme.ellipsoid).toEqual(ellipsoid); - }); + expect(provider.tilingScheme.ellipsoid).toEqual(ellipsoid); }); - it("has error event", function () { - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.errorEvent).toBeDefined(); - expect(provider.errorEvent).toBe(provider.errorEvent); - }); + it("has error event", async function () { + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); + expect(provider.errorEvent).toBeDefined(); + expect(provider.errorEvent).toBe(provider.errorEvent); }); - it("returns reasonable geometric error for various levels", function () { + it("returns reasonable geometric error for various levels", async function () { returnQuantizedMeshTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return provider.readyPromise.then(function () { - expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); - expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(1) * 2.0, - CesiumMath.EPSILON10 - ); - expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(2) * 2.0, - CesiumMath.EPSILON10 - ); - }); + expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); + expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(1) * 2.0, + CesiumMath.EPSILON10 + ); + expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(2) * 2.0, + CesiumMath.EPSILON10 + ); }); - it("credit is undefined if credit option is not provided", function () { + it("credit is undefined if credit option is not provided", async function () { returnHeightmapTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.credit).toBeUndefined(); - }); + expect(provider.credit).toBeUndefined(); }); - it("credit is defined if credit option is provided", function () { + it("credit is defined if credit option is provided", async function () { returnHeightmapTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", + const provider = await CesiumTerrainProvider.fromUrl("made/up/url", { credit: "thanks to our awesome made up contributors!", }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.credit).toBeDefined(); - }); + expect(provider.credit).toBeDefined(); }); - it("has a water mask", function () { + it("has a water mask", async function () { returnHeightmapTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.hasWaterMask).toBe(true); - }); + expect(provider.hasWaterMask).toBe(true); }); - it("has vertex normals", function () { + it("has vertex normals", async function () { returnOctVertexNormalTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", + const provider = await CesiumTerrainProvider.fromUrl("made/up/url", { requestVertexNormals: true, }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.requestVertexNormals).toBe(true); - expect(provider.hasVertexNormals).toBe(true); - }); + expect(provider.requestVertexNormals).toBe(true); + expect(provider.hasVertexNormals).toBe(true); }); - it("does not request vertex normals", function () { + it("does not request vertex normals", async function () { returnOctVertexNormalTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", + const provider = await CesiumTerrainProvider.fromUrl("made/up/url", { requestVertexNormals: false, }); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider.requestVertexNormals).toBe(false); - expect(provider.hasVertexNormals).toBe(false); - }); + expect(provider.requestVertexNormals).toBe(false); + expect(provider.hasVertexNormals).toBe(false); }); - it("requests parent layer.json", function () { + it("requests parent layer.json", async function () { returnParentUrlTileJson(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", + const provider = await CesiumTerrainProvider.fromUrl("made/up/url", { requestVertexNormals: true, requestWaterMask: true, }); - return provider.readyPromise.then(function () { - expect(provider._tileCredits[0].html).toBe( - "This is a child tileset! This amazing data is courtesy The Amazing Data Source!" - ); - expect(provider.requestVertexNormals).toBe(true); - expect(provider.requestWaterMask).toBe(true); - expect(provider.hasVertexNormals).toBe(false); // Neither tileset has them - expect(provider.hasWaterMask).toBe(true); // The child tileset has them - expect(provider.availability.isTileAvailable(1, 2, 1)).toBe(true); // Both have this - expect(provider.availability.isTileAvailable(1, 3, 1)).toBe(true); // Parent has this, but child doesn't - expect(provider.availability.isTileAvailable(2, 0, 0)).toBe(false); // Neither has this - - const layers = provider._layers; - expect(layers.length).toBe(2); - expect(layers[0].hasVertexNormals).toBe(false); - expect(layers[0].hasWaterMask).toBe(true); - expect(layers[0].availability.isTileAvailable(1, 2, 1)).toBe(true); - expect(layers[0].availability.isTileAvailable(1, 3, 1)).toBe(false); - expect(layers[0].availability.isTileAvailable(2, 0, 0)).toBe(false); - expect(layers[1].hasVertexNormals).toBe(false); - expect(layers[1].hasWaterMask).toBe(false); - expect(layers[1].availability.isTileAvailable(1, 2, 1)).toBe(true); - expect(layers[1].availability.isTileAvailable(1, 3, 1)).toBe(true); - expect(layers[1].availability.isTileAvailable(2, 0, 0)).toBe(false); - }); - }); - - it("raises an error if layer.json does not specify a format", function () { - returnTileJson("Data/CesiumTerrainTileJson/NoFormat.tile.json"); - - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - let errorListenerCalled = false; - const errorMatcher = function (event) { - expect(event.message).toContain("format is not specified"); - errorListenerCalled = true; - provider.errorEvent.removeEventListener(errorMatcher); - }; - - provider.errorEvent.addEventListener(errorMatcher); - - return provider.readyPromise.then(fail).catch((e) => { - expect(errorListenerCalled).toBe(true); - expect(e.message).toContain( - "The tile format is not specified in the layer.json file." - ); - }); + expect(provider._tileCredits[0].html).toBe( + "This is a child tileset! This amazing data is courtesy The Amazing Data Source!" + ); + expect(provider.requestVertexNormals).toBe(true); + expect(provider.requestWaterMask).toBe(true); + expect(provider.hasVertexNormals).toBe(false); // Neither tileset has them + expect(provider.hasWaterMask).toBe(true); // The child tileset has them + expect(provider.availability.isTileAvailable(1, 2, 1)).toBe(true); // Both have this + expect(provider.availability.isTileAvailable(1, 3, 1)).toBe(true); // Parent has this, but child doesn't + expect(provider.availability.isTileAvailable(2, 0, 0)).toBe(false); // Neither has this + + const layers = provider._layers; + expect(layers.length).toBe(2); + expect(layers[0].hasVertexNormals).toBe(false); + expect(layers[0].hasWaterMask).toBe(true); + expect(layers[0].availability.isTileAvailable(1, 2, 1)).toBe(true); + expect(layers[0].availability.isTileAvailable(1, 3, 1)).toBe(false); + expect(layers[0].availability.isTileAvailable(2, 0, 0)).toBe(false); + expect(layers[1].hasVertexNormals).toBe(false); + expect(layers[1].hasWaterMask).toBe(false); + expect(layers[1].availability.isTileAvailable(1, 2, 1)).toBe(true); + expect(layers[1].availability.isTileAvailable(1, 3, 1)).toBe(true); + expect(layers[1].availability.isTileAvailable(2, 0, 0)).toBe(false); }); - it("raises an error if layer.json specifies an unknown format", function () { + it("fromUrl throws if layer.json specifies an unknown format", async function () { returnTileJson("Data/CesiumTerrainTileJson/InvalidFormat.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - let errorListenerCalled = false; - const errorMatcher = function (event) { - expect(event.message).toContain("invalid or not supported"); - errorListenerCalled = true; - provider.errorEvent.removeEventListener(errorMatcher); - }; - - provider.errorEvent.addEventListener(errorMatcher); - - return provider.readyPromise.then(fail).catch((e) => { - expect(errorListenerCalled).toBe(true); - expect(e.message).toContain( - 'The tile format "awesometron-9000.0" is invalid or not supported.' - ); - }); + await expectAsync( + CesiumTerrainProvider.fromUrl("made/up/url") + ).toBeRejectedWithError( + RuntimeError, + 'The tile format "awesometron-9000.0" is invalid or not supported.' + ); }); - it("raises an error if layer.json does not specify quantized-mesh 1.x format", function () { + it("fromUrl throws if layer.json does not specify quantized-mesh 1.x format", async function () { returnTileJson("Data/CesiumTerrainTileJson/QuantizedMesh2.0.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - let errorListenerCalled = false; - const errorMatcher = function (event) { - expect(event.message).toContain("invalid or not supported"); - errorListenerCalled = true; - provider.errorEvent.removeEventListener(errorMatcher); - }; - - provider.errorEvent.addEventListener(errorMatcher); - - return provider.readyPromise.then(fail).catch((e) => { - expect(errorListenerCalled).toBe(true); - expect(e.message).toContain( - 'The tile format "quantized-mesh-2.0" is invalid or not supported.' - ); - }); + await expectAsync( + CesiumTerrainProvider.fromUrl("made/up/url") + ).toBeRejectedWithError( + RuntimeError, + 'The tile format "quantized-mesh-2.0" is invalid or not supported.' + ); }); - it("supports quantized-mesh1.x minor versions", function () { + it("fromUrl supports quantized-mesh1.x minor versions", async function () { returnTileJson("Data/CesiumTerrainTileJson/QuantizedMesh1.1.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - const errorListener = jasmine.createSpy("error"); - provider.errorEvent.addEventListener(errorListener); - - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(errorListener).not.toHaveBeenCalled(); - }); + await expectAsync( + CesiumTerrainProvider.fromUrl("made/up/url") + ).toBeResolved(); }); - it("raises an error if layer.json does not specify a tiles property", function () { + it("fromUrl throws if layer.json does not specify a tiles property", async function () { returnTileJson("Data/CesiumTerrainTileJson/NoTiles.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - let errorListenerCalled = false; - const errorMatcher = function (event) { - expect(event.message).toContain( - "does not specify any tile URL templates" - ); - errorListenerCalled = true; - provider.errorEvent.removeEventListener(errorMatcher); - }; - - provider.errorEvent.addEventListener(errorMatcher); - - return provider.readyPromise.then(fail).catch((e) => { - expect(errorListenerCalled).toBe(true); - expect(e.message).toContain( - "The layer.json file does not specify any tile URL templates." - ); - }); + await expectAsync( + CesiumTerrainProvider.fromUrl("made/up/url") + ).toBeRejectedWithError( + RuntimeError, + "The layer.json file does not specify any tile URL templates." + ); }); - it("raises an error if layer.json tiles property is an empty array", function () { + it("fromUrl throws if layer.json tiles property is an empty array", async function () { returnTileJson("Data/CesiumTerrainTileJson/EmptyTilesArray.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); - - let errorListenerCalled = false; - const errorMatcher = function (event) { - expect(event.message).toContain( - "does not specify any tile URL templates" - ); - errorListenerCalled = true; - provider.errorEvent.removeEventListener(errorMatcher); - }; - - provider.errorEvent.addEventListener(errorMatcher); - - return provider.readyPromise.then(fail).catch((e) => { - expect(errorListenerCalled).toBe(true); - expect(e.message).toContain( - "The layer.json file does not specify any tile URL templates." - ); - }); + await expectAsync( + CesiumTerrainProvider.fromUrl("made/up/url") + ).toBeRejectedWithError( + RuntimeError, + "The layer.json file does not specify any tile URL templates." + ); }); - it("uses attribution specified in layer.json", function () { + it("fromUrl uses attribution specified in layer.json", async function () { returnTileJson("Data/CesiumTerrainTileJson/WithAttribution.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider._tileCredits[0].html).toBe( - "This amazing data is courtesy The Amazing Data Source!" - ); - }); + expect(provider._tileCredits[0].html).toBe( + "This amazing data is courtesy The Amazing Data Source!" + ); }); - it("do not add blank attribution if layer.json does not have one", function () { + it("formUrl does not add blank attribution if layer.json does not have one", async function () { returnTileJson("Data/CesiumTerrainTileJson/WaterMask.tile.json"); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); - return pollToPromise(function () { - return provider.ready; - }).then(function () { - expect(provider._tileCredit).toBeUndefined(); - }); + expect(provider._tileCredit).toBeUndefined(); }); it("The undefined availability tile is returned at level 0", function () { @@ -651,82 +499,80 @@ describe("Core/CesiumTerrainProvider", function () { }); describe("requestTileGeometry", function () { - it("uses multiple urls specified in layer.json", function () { + it("uses multiple urls specified in layer.json", async function () { returnTileJson("Data/CesiumTerrainTileJson/MultipleUrls.tile.json"); + const provider = await CesiumTerrainProvider.fromUrl("made/up/url"); + spyOn(Resource._Implementations, "loadWithXhr").and.callThrough(); - const provider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + try { + await provider.requestTileGeometry(0, 0, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo0.com"); + } - spyOn(Resource._Implementations, "loadWithXhr").and.callThrough(); + try { + await provider.requestTileGeometry(1, 0, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo1.com"); + } - return provider.readyPromise - .then(function () { - return provider.requestTileGeometry(0, 0, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo0.com"); - return provider.requestTileGeometry(1, 0, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo1.com"); - return provider.requestTileGeometry(1, -1, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo2.com"); - return provider.requestTileGeometry(1, 0, 1); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo3.com"); - }); + try { + await provider.requestTileGeometry(1, -1, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo2.com"); + } + + try { + await provider.requestTileGeometry(1, 0, 1); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo3.com"); + } }); - it("supports scheme-less template URLs in layer.json resolved with absolute URL", function () { + it("supports scheme-less template URLs in layer.json resolved with absolute URL", async function () { returnTileJson("Data/CesiumTerrainTileJson/MultipleUrls.tile.json"); - const url = getAbsoluteUri("Data/CesiumTerrainTileJson"); - - const provider = new CesiumTerrainProvider({ - url: url, - }); + const provider = await CesiumTerrainProvider.fromUrl(url); spyOn(Resource._Implementations, "loadWithXhr").and.callThrough(); - return provider.readyPromise - .then(function () { - return provider.requestTileGeometry(0, 0, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo0.com"); - return provider.requestTileGeometry(1, 0, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo1.com"); - return provider.requestTileGeometry(1, -1, 0); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo2.com"); - return provider.requestTileGeometry(1, 0, 1); - }) - .catch(function () { - expect( - Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] - ).toContain("foo3.com"); - }); + try { + await provider.requestTileGeometry(0, 0, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo0.com"); + } + try { + await provider.requestTileGeometry(1, 0, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo1.com"); + } + + try { + await provider.requestTileGeometry(1, -1, 0); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo2.com"); + } + try { + await provider.requestTileGeometry(1, 0, 1); + } catch (e) { + expect( + Resource._Implementations.loadWithXhr.calls.mostRecent().args[0] + ).toContain("foo3.com"); + } }); it("provides HeightmapTerrainData", function () { @@ -1008,7 +854,7 @@ describe("Core/CesiumTerrainProvider", function () { }); }); - it("provides QuantizedMeshTerrainData with Metadata availability", function () { + it("provides QuantizedMeshTerrainData with Metadata availability", async function () { Resource._Implementations.loadWithXhr = function ( url, responseType, @@ -1030,37 +876,22 @@ describe("Core/CesiumTerrainProvider", function () { returnMetadataAvailabilityTileJson(); - const terrainProvider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const terrainProvider = await CesiumTerrainProvider.fromUrl( + "made/up/url" + ); - return pollToPromise(function () { - return terrainProvider.ready; - }) - .then(function () { - expect(terrainProvider.hasMetadata).toBe(true); - expect(terrainProvider._layers[0].availabilityLevels).toBe(10); - expect(terrainProvider.availability.isTileAvailable(0, 0, 0)).toBe( - true - ); - expect(terrainProvider.availability.isTileAvailable(0, 1, 0)).toBe( - true - ); - expect(terrainProvider.availability.isTileAvailable(1, 0, 0)).toBe( - false - ); - - return terrainProvider.requestTileGeometry(0, 0, 0); - }) - .then(function (loadedData) { - expect(loadedData).toBeInstanceOf(QuantizedMeshTerrainData); - expect(terrainProvider.availability.isTileAvailable(1, 0, 0)).toBe( - true - ); - }); + expect(terrainProvider.hasMetadata).toBe(true); + expect(terrainProvider._layers[0].availabilityLevels).toBe(10); + expect(terrainProvider.availability.isTileAvailable(0, 0, 0)).toBe(true); + expect(terrainProvider.availability.isTileAvailable(0, 1, 0)).toBe(true); + expect(terrainProvider.availability.isTileAvailable(1, 0, 0)).toBe(false); + + const loadedData = await terrainProvider.requestTileGeometry(0, 0, 0); + expect(loadedData).toBeInstanceOf(QuantizedMeshTerrainData); + expect(terrainProvider.availability.isTileAvailable(1, 0, 0)).toBe(true); }); - it("returns undefined if too many requests are already in progress", function () { + it("returns undefined if too many requests are already in progress", async function () { const baseUrl = "made/up/url"; const deferreds = []; @@ -1080,40 +911,33 @@ describe("Core/CesiumTerrainProvider", function () { returnHeightmapTileJson(); - const terrainProvider = new CesiumTerrainProvider({ - url: baseUrl, - }); - - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - let promise; - let i; - for (i = 0; i < RequestScheduler.maximumRequestsPerServer; ++i) { - const request = new Request({ - throttle: true, - throttleByServer: true, + const terrainProvider = await CesiumTerrainProvider.fromUrl(baseUrl); + let promise; + let i; + for (i = 0; i < RequestScheduler.maximumRequestsPerServer; ++i) { + const request = new Request({ + throttle: true, + throttleByServer: true, + }); + promise = terrainProvider + .requestTileGeometry(0, 0, 0, request) + .then(fail) + .catch((e) => { + expect(e.message).toContain("Mesh buffer doesn't exist."); }); - promise = terrainProvider - .requestTileGeometry(0, 0, 0, request) - .then(fail) - .catch((e) => { - expect(e.message).toContain("Mesh buffer doesn't exist."); - }); - } - RequestScheduler.update(); - expect(promise).toBeDefined(); + } + RequestScheduler.update(); + expect(promise).toBeDefined(); - promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); - expect(promise).toBeUndefined(); + promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); + expect(promise).toBeUndefined(); - for (i = 0; i < deferreds.length; ++i) { - deferreds[i].resolve(); - } - }); + for (i = 0; i < deferreds.length; ++i) { + deferreds[i].resolve(); + } }); - it("supports getTileDataAvailable()", function () { + it("supports getTileDataAvailable()", async function () { const baseUrl = "made/up/url"; Resource._Implementations.loadWithXhr = function ( @@ -1137,36 +961,24 @@ describe("Core/CesiumTerrainProvider", function () { returnQuantizedMeshTileJson(); - const terrainProvider = new CesiumTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await CesiumTerrainProvider.fromUrl(baseUrl); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); - expect(terrainProvider.getTileDataAvailable(0, 0, 2)).toBe(false); - }); + expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); + expect(terrainProvider.getTileDataAvailable(0, 0, 2)).toBe(false); }); - it("getTileDataAvailable() converts xyz to tms", function () { + it("getTileDataAvailable() converts xyz to tms", async function () { const baseUrl = "made/up/url"; returnPartialAvailabilityTileJson(); - const terrainProvider = new CesiumTerrainProvider({ - url: baseUrl, - }); + const terrainProvider = await CesiumTerrainProvider.fromUrl(baseUrl); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.getTileDataAvailable(1, 3, 2)).toBe(true); - expect(terrainProvider.getTileDataAvailable(1, 0, 2)).toBe(false); - }); + expect(terrainProvider.getTileDataAvailable(1, 3, 2)).toBe(true); + expect(terrainProvider.getTileDataAvailable(1, 0, 2)).toBe(false); }); - it("getTileDataAvailable() with Metadata availability", function () { + it("getTileDataAvailable() with Metadata availability", async function () { Resource._Implementations.loadWithXhr = function ( url, responseType, @@ -1188,22 +1000,15 @@ describe("Core/CesiumTerrainProvider", function () { returnMetadataAvailabilityTileJson(); - const terrainProvider = new CesiumTerrainProvider({ - url: "made/up/url", - }); + const terrainProvider = await CesiumTerrainProvider.fromUrl( + "made/up/url" + ); - return pollToPromise(function () { - return terrainProvider.ready; - }) - .then(function () { - expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); - expect(terrainProvider.getTileDataAvailable(0, 0, 1)).toBeUndefined(); - - return terrainProvider.requestTileGeometry(0, 0, 0); - }) - .then(function () { - expect(terrainProvider.getTileDataAvailable(0, 0, 1)).toBe(true); - }); + expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); + expect(terrainProvider.getTileDataAvailable(0, 0, 1)).toBeUndefined(); + + await terrainProvider.requestTileGeometry(0, 0, 0); + expect(terrainProvider.getTileDataAvailable(0, 0, 1)).toBe(true); }); it("supports a query string in the base URL", function () { @@ -1234,26 +1039,24 @@ describe("Core/CesiumTerrainProvider", function () { }); }); - it("Uses query parameter extensions for ion resource", function () { - const terrainProvider = new CesiumTerrainProvider({ - url: IonResource.fromAssetId(1), - requestVertexNormals: true, - requestWaterMask: true, - }); + it("Uses query parameter extensions for ion resource", async function () { + const terrainProvider = await CesiumTerrainProvider.fromUrl( + IonResource.fromAssetId(1), + { + requestVertexNormals: true, + requestWaterMask: true, + } + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - const getDerivedResource = spyOn( - IonResource.prototype, - "getDerivedResource" - ).and.callThrough(); - terrainProvider.requestTileGeometry(0, 0, 0); - const options = getDerivedResource.calls.argsFor(0)[0]; - expect(options.queryParameters.extensions).toEqual( - "octvertexnormals-watermask-metadata" - ); - }); + const getDerivedResource = spyOn( + IonResource.prototype, + "getDerivedResource" + ).and.callThrough(); + await terrainProvider.requestTileGeometry(0, 0, 0); + const options = getDerivedResource.calls.argsFor(0)[0]; + expect(options.queryParameters.extensions).toEqual( + "octvertexnormals-watermask-metadata" + ); }); }); }); diff --git a/packages/engine/Specs/Core/GoogleEarthEnterpriseMetadataSpec.js b/packages/engine/Specs/Core/GoogleEarthEnterpriseMetadataSpec.js index f1e723b7ce46..2af772c023ba 100644 --- a/packages/engine/Specs/Core/GoogleEarthEnterpriseMetadataSpec.js +++ b/packages/engine/Specs/Core/GoogleEarthEnterpriseMetadataSpec.js @@ -108,7 +108,7 @@ describe("Core/GoogleEarthEnterpriseMetadata", function () { }).toThrowError(RuntimeError); }); - it("populateSubtree", function () { + it("populateSubtree", async function () { const quad = "0123"; let index = 0; spyOn( @@ -127,45 +127,35 @@ describe("Core/GoogleEarthEnterpriseMetadata", function () { return Promise.resolve(); }); - const metadata = new GoogleEarthEnterpriseMetadata({ - url: "http://test.server", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl( + "http://test.server" + ); const request = new Request({ throttle: true, }); - return metadata.readyPromise - .then(function () { - const tileXY = GoogleEarthEnterpriseMetadata.quadKeyToTileXY(quad); - return metadata.populateSubtree( - tileXY.x, - tileXY.y, - tileXY.level, - request - ); - }) - .then(function () { - expect( - GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket.calls.count() - ).toEqual(4); - expect( - GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket - ).toHaveBeenCalledWith("", 1); - expect( - GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket - ).toHaveBeenCalledWith("0", 1, request); - expect( - GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket - ).toHaveBeenCalledWith("01", 1, request); - expect( - GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket - ).toHaveBeenCalledWith("012", 1, request); - - const tileInfo = metadata._tileInfo; - expect(tileInfo["0"]).toBeDefined(); - expect(tileInfo["01"]).toBeDefined(); - expect(tileInfo["012"]).toBeDefined(); - expect(tileInfo["0123"]).toBeDefined(); - }); + const tileXY = GoogleEarthEnterpriseMetadata.quadKeyToTileXY(quad); + await metadata.populateSubtree(tileXY.x, tileXY.y, tileXY.level, request); + expect( + GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket.calls.count() + ).toEqual(4); + expect( + GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket + ).toHaveBeenCalledWith("", 1); + expect( + GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket + ).toHaveBeenCalledWith("0", 1, request); + expect( + GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket + ).toHaveBeenCalledWith("01", 1, request); + expect( + GoogleEarthEnterpriseMetadata.prototype.getQuadTreePacket + ).toHaveBeenCalledWith("012", 1, request); + + const tileInfo = metadata._tileInfo; + expect(tileInfo["0"]).toBeDefined(); + expect(tileInfo["01"]).toBeDefined(); + expect(tileInfo["012"]).toBeDefined(); + expect(tileInfo["0123"]).toBeDefined(); }); it("resolves readyPromise", function () { diff --git a/packages/engine/Specs/Core/GoogleEarthEnterpriseTerrainProviderSpec.js b/packages/engine/Specs/Core/GoogleEarthEnterpriseTerrainProviderSpec.js index 3b2efb4ebd6e..3f54ac70eebb 100644 --- a/packages/engine/Specs/Core/GoogleEarthEnterpriseTerrainProviderSpec.js +++ b/packages/engine/Specs/Core/GoogleEarthEnterpriseTerrainProviderSpec.js @@ -45,16 +45,13 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { let terrainProvider; - function waitForTile(level, x, y, f) { - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); - + async function waitForTile(level, x, y, f) { + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); return pollToPromise(function () { - return ( - terrainProvider.ready && - terrainProvider.getTileDataAvailable(x, y, level) - ); + return terrainProvider.getTileDataAvailable(x, y, level); }).then(function () { const promise = terrainProvider.requestTileGeometry(level, x, y); @@ -136,56 +133,50 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { }); }); - it("uses geographic tiling scheme by default", function () { + it("uses geographic tiling scheme by default", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.tilingScheme).toBeInstanceOf( - GeographicTilingScheme - ); - }); + expect(terrainProvider.tilingScheme).toBeInstanceOf(GeographicTilingScheme); }); - it("can use a custom ellipsoid", function () { + it("can use a custom ellipsoid", async function () { installMockGetQuadTreePacket(); const ellipsoid = new Ellipsoid(1, 2, 3); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - ellipsoid: ellipsoid, - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata, + { + ellipsoid: ellipsoid, + } + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.tilingScheme.ellipsoid).toEqual(ellipsoid); - }); + expect(terrainProvider.tilingScheme.ellipsoid).toEqual(ellipsoid); }); - it("has error event", function () { - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + it("has error event", async function () { + installMockGetQuadTreePacket(); + + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); expect(terrainProvider.errorEvent).toBeDefined(); expect(terrainProvider.errorEvent).toBe(terrainProvider.errorEvent); - - return terrainProvider.readyPromise.catch(function (e) { - expect(terrainProvider.ready).toBe(false); - }); }); - it("returns reasonable geometric error for various levels", function () { + it("returns reasonable geometric error for various levels", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); expect(terrainProvider.getLevelMaximumGeometricError(0)).toBeGreaterThan( 0.0 @@ -198,8 +189,6 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { terrainProvider.getLevelMaximumGeometricError(2) * 2.0, CesiumMath.EPSILON10 ); - - return terrainProvider.readyPromise; }); it("readyPromise rejects if there isn't terrain", function () { @@ -224,61 +213,51 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { }); }); - it("logo is undefined if credit is not provided", function () { + it("credit is undefined if credit is not provided", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.credit).toBeUndefined(); - }); + expect(terrainProvider.credit).toBeUndefined(); }); - it("logo is defined if credit is provided", function () { + it("logo is defined if credit is provided", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - credit: "thanks to our awesome made up contributors!", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata, + { + credit: "thanks to our awesome made up contributors!", + } + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.credit).toBeDefined(); - }); + expect(terrainProvider.credit).toBeDefined(); }); - it("has a water mask is false", function () { + it("has a water mask is false", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.hasWaterMask).toBe(false); - }); + expect(terrainProvider.hasWaterMask).toBe(false); }); - it("has vertex normals is false", function () { + it("has vertex normals is false", async function () { installMockGetQuadTreePacket(); - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: "made/up/url", - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl("made/up/url"); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - expect(terrainProvider.hasVertexNormals).toBe(false); - }); + expect(terrainProvider.hasVertexNormals).toBe(false); }); describe("requestTileGeometry", function () { @@ -345,7 +324,7 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { expect(loadedData).toBeInstanceOf(GoogleEarthEnterpriseTerrainData); }); - it("returns undefined if too many requests are already in progress", function () { + it("returns undefined if too many requests are already in progress", async function () { installMockGetQuadTreePacket(); const baseUrl = "made/up/url"; @@ -379,23 +358,19 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { deferreds.push(deferred); }; - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: baseUrl, - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl(baseUrl); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); const promises = []; return pollToPromise(function () { - return terrainProvider.ready; + let b = true; + for (let i = 0; i < 10; ++i) { + b = b && terrainProvider.getTileDataAvailable(i, i, i); + } + return b && terrainProvider.getTileDataAvailable(1, 2, 3); }) - .then(function () { - return pollToPromise(function () { - let b = true; - for (let i = 0; i < 10; ++i) { - b = b && terrainProvider.getTileDataAvailable(i, i, i); - } - return b && terrainProvider.getTileDataAvailable(1, 2, 3); - }); - }) .then(function () { let promise; for (let i = 0; i < RequestScheduler.maximumRequestsPerServer; ++i) { @@ -428,7 +403,7 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { }); }); - it("supports getTileDataAvailable()", function () { + it("supports getTileDataAvailable()", async function () { installMockGetQuadTreePacket(); const baseUrl = "made/up/url"; @@ -451,26 +426,23 @@ describe("Core/GoogleEarthEnterpriseTerrainProvider", function () { ); }; - terrainProvider = new GoogleEarthEnterpriseTerrainProvider({ - url: baseUrl, - }); + const metadata = await GoogleEarthEnterpriseMetadata.fromUrl(baseUrl); + terrainProvider = GoogleEarthEnterpriseTerrainProvider.fromMetadata( + metadata + ); - return pollToPromise(function () { - return terrainProvider.ready; - }).then(function () { - const tileInfo = terrainProvider._metadata._tileInfo; - const info = - tileInfo[GoogleEarthEnterpriseMetadata.tileXYToQuadKey(0, 1, 0)]; - info._bits = 0x7f; // Remove terrain bit from 0,1,0 tile - info.terrainState = 1; // NONE - info.ancestorHasTerrain = true; - - expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); - expect(terrainProvider.getTileDataAvailable(0, 1, 0)).toBe(false); - expect(terrainProvider.getTileDataAvailable(1, 0, 0)).toBe(true); - expect(terrainProvider.getTileDataAvailable(1, 1, 0)).toBe(true); - expect(terrainProvider.getTileDataAvailable(0, 0, 2)).toBe(false); - }); + const tileInfo = terrainProvider._metadata._tileInfo; + const info = + tileInfo[GoogleEarthEnterpriseMetadata.tileXYToQuadKey(0, 1, 0)]; + info._bits = 0x7f; // Remove terrain bit from 0,1,0 tile + info.terrainState = 1; // NONE + info.ancestorHasTerrain = true; + + expect(terrainProvider.getTileDataAvailable(0, 0, 0)).toBe(true); + expect(terrainProvider.getTileDataAvailable(0, 1, 0)).toBe(false); + expect(terrainProvider.getTileDataAvailable(1, 0, 0)).toBe(true); + expect(terrainProvider.getTileDataAvailable(1, 1, 0)).toBe(true); + expect(terrainProvider.getTileDataAvailable(0, 0, 2)).toBe(false); }); }); }); diff --git a/packages/engine/Specs/Core/VRTheWorldTerrainProviderSpec.js b/packages/engine/Specs/Core/VRTheWorldTerrainProviderSpec.js index 90f849cc4091..e9b4b1dc2618 100644 --- a/packages/engine/Specs/Core/VRTheWorldTerrainProviderSpec.js +++ b/packages/engine/Specs/Core/VRTheWorldTerrainProviderSpec.js @@ -5,6 +5,7 @@ import { Request, RequestScheduler, Resource, + RuntimeError, TerrainProvider, VRTheWorldTerrainProvider, } from "../../index.js"; @@ -141,63 +142,48 @@ describe("Core/VRTheWorldTerrainProvider", function () { }); }); - it("has error event", function () { + it("has error event", async function () { patchXHRLoad(); - const provider = new VRTheWorldTerrainProvider({ - url: "made/up/url", - }); + const provider = await VRTheWorldTerrainProvider.fromUrl("made/up/url"); expect(provider.errorEvent).toBeDefined(); expect(provider.errorEvent).toBe(provider.errorEvent); - return provider.readyPromise; }); - it("returns reasonable geometric error for various levels", function () { + it("returns reasonable geometric error for various levels", async function () { patchXHRLoad(); - const provider = new VRTheWorldTerrainProvider({ - url: "made/up/url", - }); + const provider = await VRTheWorldTerrainProvider.fromUrl("made/up/url"); - return provider.readyPromise.then(function () { - expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); - expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(1) * 2.0, - CesiumMath.EPSILON10 - ); - expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( - provider.getLevelMaximumGeometricError(2) * 2.0, - CesiumMath.EPSILON10 - ); - }); + expect(provider.getLevelMaximumGeometricError(0)).toBeGreaterThan(0.0); + expect(provider.getLevelMaximumGeometricError(0)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(1) * 2.0, + CesiumMath.EPSILON10 + ); + expect(provider.getLevelMaximumGeometricError(1)).toEqualEpsilon( + provider.getLevelMaximumGeometricError(2) * 2.0, + CesiumMath.EPSILON10 + ); }); - it("credit is undefined if credit option is not provided", function () { + it("credit is undefined if credit option is not provided", async function () { patchXHRLoad(); - const provider = new VRTheWorldTerrainProvider({ - url: "made/up/url", - }); + const provider = await VRTheWorldTerrainProvider.fromUrl("made/up/url"); expect(provider.credit).toBeUndefined(); - return provider.readyPromise; }); - it("credit is defined if credit optoin is provided", function () { + it("credit is defined if credit option is provided", async function () { patchXHRLoad(); - const provider = new VRTheWorldTerrainProvider({ - url: "made/up/url", + const provider = await VRTheWorldTerrainProvider.fromUrl("made/up/url", { credit: "thanks to our awesome made up contributors!", }); expect(provider.credit).toBeDefined(); - return provider.readyPromise; }); - it("does not have a water mask", function () { + it("does not have a water mask", async function () { patchXHRLoad(); - const provider = new VRTheWorldTerrainProvider({ - url: "made/up/url", - }); + const provider = await VRTheWorldTerrainProvider.fromUrl("made/up/url"); expect(provider.hasWaterMask).toBe(false); - return provider.readyPromise; }); it("is not ready immediately", function () { @@ -209,7 +195,7 @@ describe("Core/VRTheWorldTerrainProvider", function () { return provider.readyPromise; }); - it("raises an error if the SRS is not supported", function () { + it("fromUrl throws if the SRS is not supported", async function () { patchXHRLoad(); Resource._Implementations.loadWithXhr = function ( url, @@ -247,24 +233,16 @@ describe("Core/VRTheWorldTerrainProvider", function () { }, 1); }; - const terrainProvider = new VRTheWorldTerrainProvider({ - url: "made/up/url", - }); - - let called = false; - const errorFunction = function () { - called = true; - }; - - terrainProvider.errorEvent.addEventListener(errorFunction); - - return terrainProvider.readyPromise.then(fail).catch(() => { - expect(called).toBe(true); - }); + await expectAsync( + VRTheWorldTerrainProvider.fromUrl("made/up/url") + ).toBeRejectedWithError( + RuntimeError, + "An error occurred while accessing made/up/url: SRS EPSG:foo is not supported" + ); }); describe("requestTileGeometry", function () { - it("provides HeightmapTerrainData", function () { + it("provides HeightmapTerrainData", async function () { patchXHRLoad(); const baseUrl = "made/up/url"; @@ -286,23 +264,16 @@ describe("Core/VRTheWorldTerrainProvider", function () { ); }; - const terrainProvider = new VRTheWorldTerrainProvider({ - url: baseUrl, - }); - - return terrainProvider.readyPromise - .then(function () { - expect(terrainProvider.tilingScheme).toBeInstanceOf( - GeographicTilingScheme - ); - return terrainProvider.requestTileGeometry(0, 0, 0); - }) - .then(function (loadedData) { - expect(loadedData).toBeInstanceOf(HeightmapTerrainData); - }); + const terrainProvider = await VRTheWorldTerrainProvider.fromUrl(baseUrl); + + expect(terrainProvider.tilingScheme).toBeInstanceOf( + GeographicTilingScheme + ); + const loadedData = await terrainProvider.requestTileGeometry(0, 0, 0); + expect(loadedData).toBeInstanceOf(HeightmapTerrainData); }); - it("returns undefined if too many requests are already in progress", function () { + it("returns undefined if too many requests are already in progress", async function () { patchXHRLoad(); const baseUrl = "made/up/url"; @@ -317,39 +288,35 @@ describe("Core/VRTheWorldTerrainProvider", function () { deferreds.push(deferred); }; - const terrainProvider = new VRTheWorldTerrainProvider({ - url: baseUrl, - }); - - return terrainProvider.readyPromise.then(function () { - const promises = []; - let promise; - let i; - for (i = 0; i < RequestScheduler.maximumRequestsPerServer; ++i) { - const request = new Request({ - throttle: true, - throttleByServer: true, - }); - promise = terrainProvider.requestTileGeometry(0, 0, 0, request); - promises.push(promise); - } - RequestScheduler.update(); - expect(promise).toBeDefined(); - - promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); - expect(promise).toBeUndefined(); - - for (i = 0; i < deferreds.length; ++i) { - const deferred = deferreds[i]; - Resource._Implementations.loadImageElement( - "Data/Images/Red16x16.png", - false, - deferred - ); - } - - return Promise.all(promises); - }); + const terrainProvider = await VRTheWorldTerrainProvider.fromUrl(baseUrl); + + const promises = []; + let promise; + let i; + for (i = 0; i < RequestScheduler.maximumRequestsPerServer; ++i) { + const request = new Request({ + throttle: true, + throttleByServer: true, + }); + promise = terrainProvider.requestTileGeometry(0, 0, 0, request); + promises.push(promise); + } + RequestScheduler.update(); + expect(promise).toBeDefined(); + + promise = terrainProvider.requestTileGeometry(0, 0, 0, createRequest()); + expect(promise).toBeUndefined(); + + for (i = 0; i < deferreds.length; ++i) { + const deferred = deferreds[i]; + Resource._Implementations.loadImageElement( + "Data/Images/Red16x16.png", + false, + deferred + ); + } + + return Promise.all(promises); }); }); });