Skip to content

Commit

Permalink
refactor(unitTests): use sinon to mock itowns.Fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
ftoromanoff committed Dec 20, 2023
1 parent 2c9936f commit 849eaa5
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 302 deletions.
1 change: 0 additions & 1 deletion src/Process/3dTilesProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ export function process3dTilesNode(cullingTest = $3dTilesCulling, subdivisionTes
return undefined;
}


// do proper culling
const isVisible = cullingTest ? (!cullingTest(layer, context.camera, node, node.matrixWorld)) : true;
node.visible = isVisible;
Expand Down
Binary file added test/data/unitTest/dem/dem3_3_8.bil
Binary file not shown.
74 changes: 54 additions & 20 deletions test/unit/3dtileslayerprocess.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,68 @@
import assert from 'assert';
import fs from 'fs';
import C3DTilesLayer from 'Layer/C3DTilesLayer';
import C3DTilesSource from 'Source/C3DTilesSource';
import View from 'Core/View';
import GlobeView from 'Core/Prefab/GlobeView';
import { HttpsProxyAgent } from 'https-proxy-agent';
import Coordinates from 'Core/Geographic/Coordinates';
import sinon from 'sinon';
import Fetcher from 'Provider/Fetcher';
import Renderer from './bootstrap';

import tilesetDiscretLOD from '../data/unitTest/3dTiles/tilesetDiscretLOD.json';

const urlDragonLow = 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/1.0/TilesetWithDiscreteLOD/dragon_low.b3dm';
const urlDragonMedium = 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/1.0/TilesetWithDiscreteLOD/dragon_medium.b3dm';

const dragonLow = fs.readFileSync('./test/data/unitTest/3dTiles/dragon_low.b3dm');
const dragonMedium = fs.readFileSync('./test/data/unitTest/3dTiles/dragon_medium.b3dm');

const resources = new Map([
[urlDragonLow, dragonLow.buffer],
[urlDragonMedium, dragonMedium.buffer],
]);

describe('3Dtiles layer', function () {
const url3dTilesDiscretLOD = 'test/data/unitTest/3dTiles/tilesetDiscretLOD.json';
let stubFetcherArrayBuf;
let context;
let viewer;
let threedTilesLayer;

before(function () {
const stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(tilesetDiscretLOD)));

stubFetcherArrayBuf = sinon.stub(Fetcher, 'arrayBuffer')
.callsFake(url => Promise.resolve(resources.get(url)));

const renderer = new Renderer();
const p = { coord: new Coordinates('EPSG:4326', -75.6114, 40.03428, 0), heading: 180, range: 4000, tilt: 22 };
const viewer = new GlobeView(renderer.domElement, p, { renderer, noControls: true });
const renderer = new Renderer();
const p = { coord: new Coordinates('EPSG:4326', -75.6114, 40.03428, 0), heading: 180, range: 4000, tilt: 22 };
viewer = new GlobeView(renderer.domElement, p, { renderer, noControls: true });

const source = new C3DTilesSource({
// url: 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/1.0/TilesetWithDiscreteLOD/tileset.json',
url: url3dTilesDiscretLOD,
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
const source = new C3DTilesSource({
url: 'https://raw.githubusercontent.com/CesiumGS/3d-tiles-samples/master/1.0/TilesetWithDiscreteLOD/tileset.json',
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
});
threedTilesLayer = new C3DTilesLayer('3d-tiles-discrete-lod', {
source,
sseThreshold: 0.05,
}, viewer);

context = {
camera: viewer.camera,
engine: viewer.mainLoop.gfxEngine,
scheduler: viewer.mainLoop.scheduler,
geometryLayer: threedTilesLayer,
view: viewer,
};

stubFetcherJson.restore();
});

after(() => {
stubFetcherArrayBuf.restore();
});
const threedTilesLayer = new C3DTilesLayer('3d-tiles-discrete-lod', {
source,
sseThreshold: 0.05,
}, viewer);

const context = {
camera: viewer.camera,
engine: viewer.mainLoop.gfxEngine,
scheduler: viewer.mainLoop.scheduler,
geometryLayer: threedTilesLayer,
view: viewer,
};

it('Add 3dtiles layer', function (done) {
View.prototype.addLayer.call(viewer, threedTilesLayer)
Expand All @@ -39,10 +71,12 @@ describe('3Dtiles layer', function () {
done();
}).catch(done);
});

it('preUpdate 3dtiles layer', function () {
const elements = threedTilesLayer.preUpdate(context, new Set([threedTilesLayer]));
assert.equal(elements.length, 1);
});

it('update 3dtiles layer', function () {
const node = threedTilesLayer.root;
viewer.camera3D.updateMatrixWorld();
Expand Down
72 changes: 47 additions & 25 deletions test/unit/3dtileslayerprocessbatchtable.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from 'assert';
import fs from 'fs';
import C3DTilesLayer from 'Layer/C3DTilesLayer';
import C3DTBatchTableHierarchyExtension from 'Core/3DTiles/C3DTBatchTableHierarchyExtension';
import C3DTilesSource from 'Source/C3DTilesSource';
Expand All @@ -8,37 +9,58 @@ import View from 'Core/View';
import GlobeView from 'Core/Prefab/GlobeView';
import { HttpsProxyAgent } from 'https-proxy-agent';
import Coordinates from 'Core/Geographic/Coordinates';
import Fetcher from 'Provider/Fetcher';
import sinon from 'sinon';
import Renderer from './bootstrap';

import tilesetBatchTable from '../data/unitTest/3dTiles/tilesetBatchTable.json';

const tileBatchTable = fs.readFileSync('./test/data/unitTest/3dTiles/tile.b3dm');

describe('3Dtiles batch table', function () {
const url3dTilesBatchTable = 'test/data/unitTest/3dTiles/tilesetBatchTable.json';

const renderer = new Renderer();

const p = {
coord: new Coordinates('EPSG:4326', -75.61349, 40.044259),
range: 200,
tilt: 10,
heading: -145,
};
const viewer = new GlobeView(renderer.domElement, p, { renderer, noControls: true });

// Map the extension name to its manager
const extensions = new C3DTExtensions();
extensions.registerExtension('3DTILES_batch_table_hierarchy',
{ [C3DTilesTypes.batchtable]:
let stubFetcherArrayBuf;
let stubFetcherJson;
let viewer;
let threedTilesLayerBTHierarchy;

before(function () {
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(tilesetBatchTable)));

stubFetcherArrayBuf = sinon.stub(Fetcher, 'arrayBuffer')
.callsFake(() => Promise.resolve(tileBatchTable.buffer));

const renderer = new Renderer();

const p = {
coord: new Coordinates('EPSG:4326', -75.61349, 40.044259),
range: 200,
tilt: 10,
heading: -145,
};
viewer = new GlobeView(renderer.domElement, p, { renderer, noControls: true });

// Map the extension name to its manager
const extensions = new C3DTExtensions();
extensions.registerExtension('3DTILES_batch_table_hierarchy',
{ [C3DTilesTypes.batchtable]:
C3DTBatchTableHierarchyExtension });

const source = new C3DTilesSource({
// url: 'https://raw.githubusercontent.com/AnalyticalGraphicsInc/cesium/master/Apps/SampleData/Cesium3DTiles/Hierarchy/BatchTableHierarchy/tileset.json',
url: url3dTilesBatchTable,
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
const source = new C3DTilesSource({
url: 'https://raw.githubusercontent.com/AnalyticalGraphicsInc/cesium/master/Apps/SampleData/Cesium3DTiles/Hierarchy/BatchTableHierarchy/tileset.json',
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
});
threedTilesLayerBTHierarchy = new C3DTilesLayer('3d-tiles-bt-hierarchy', {
name: 'BTHierarchy',
source,
registeredExtensions: extensions,
}, viewer);
});

after(() => {
stubFetcherJson.restore();
stubFetcherArrayBuf.restore();
});
const threedTilesLayerBTHierarchy = new C3DTilesLayer('3d-tiles-bt-hierarchy', {
name: 'BTHierarchy',
source,
registeredExtensions: extensions,
}, viewer);

it('Add 3dtiles layer with batch table', function (done) {
View.prototype.addLayer.call(viewer, threedTilesLayerBTHierarchy)
Expand Down
79 changes: 46 additions & 33 deletions test/unit/3dtileslayerstyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,68 @@ import PlanarView from 'Core/Prefab/PlanarView';
import C3DTBatchTable from 'Core/3DTiles/C3DTBatchTable';
import C3DTilesSource from 'Source/C3DTilesSource';
import C3DTilesLayer from 'Layer/C3DTilesLayer';
import sinon from 'sinon';
import Fetcher from 'Provider/Fetcher';
import Renderer from './bootstrap';

describe('3DTilesLayer Style', () => {
// Define crs
proj4.defs('EPSG:3946', '+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
import tileset from '../data/unitTest/3dTiles/tileset.json';

// Define geographic extent: CRS, min/max X, min/max Y
const extent = new Extent('EPSG:3946',
1840816.94334, 1843692.32501,
5175036.4587, 5177412.82698);
// Create a 'fake' tile content for this test purpose
function createTileContent(tileId) {
const geometry = new THREE.SphereGeometry(15, 32, 16);
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });

const renderer = new Renderer();
// Add _BATCHID geometry attributes
const array = [];
let currentBatchId = Math.round(Math.random() * 50);
for (let index = 0; index < geometry.attributes.position.count; index++) {
array.push(currentBatchId);

const view = new PlanarView(renderer.domElement, extent, { renderer, noControls: true });
// Change randomly batch id
if (Math.random() > 0.5) {
currentBatchId = Math.round(Math.random() * 50);
}
}
geometry.setAttribute('_BATCHID', new THREE.BufferAttribute(Int32Array.from(array), 1));

// Create a 'fake' tile content for this test purpose
const createTileContent = (tileId) => {
const geometry = new THREE.SphereGeometry(15, 32, 16);
const material = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const result = new THREE.Mesh(geometry, material);
result.batchTable = new C3DTBatchTable();
result.tileId = tileId;

// Add _BATCHID geometry attributes
const array = [];
let currentBatchId = Math.round(Math.random() * 50);
for (let index = 0; index < geometry.attributes.position.count; index++) {
array.push(currentBatchId);
return result;
}

// Change randomly batch id
if (Math.random() > 0.5) {
currentBatchId = Math.round(Math.random() * 50);
}
}
geometry.setAttribute('_BATCHID', new THREE.BufferAttribute(Int32Array.from(array), 1));
describe('3DTilesLayer Style', () => {
let view;
let $3dTilesLayer;
let source;
let stubFetcherJson;

const result = new THREE.Mesh(geometry, material);
before(function () {
stubFetcherJson = sinon.stub(Fetcher, 'json')
.callsFake(() => Promise.resolve(JSON.parse(tileset)));

result.batchTable = new C3DTBatchTable();
result.tileId = tileId;
// Define crs
proj4.defs('EPSG:3946', '+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');

return result;
};
// Define geographic extent: CRS, min/max X, min/max Y
const extent = new Extent('EPSG:3946',
1840816.94334, 1843692.32501,
5175036.4587, 5177412.82698);

let $3dTilesLayer;
let source;
const renderer = new Renderer();

const url = 'test/data/unitTest/3dTiles/tileset.json';
view = new PlanarView(renderer.domElement, extent, { renderer, noControls: true });
});

after(function () {
stubFetcherJson.restore();
});

it('should instance C3DTilesLayer', function (done) {
source = new C3DTilesSource({
url,
url: 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/' +
'3DTiles/lyon1_with_surface_type_2018/tileset.json',
networkOptions: process.env.HTTPS_PROXY ? { agent: new HttpsProxyAgent(process.env.HTTPS_PROXY) } : {},
});

Expand Down
Loading

0 comments on commit 849eaa5

Please sign in to comment.