Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework test functional #2179

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/layers/JSONLayers/OPENSM.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"crs": "EPSG:3857",
"isInverted": true,
"format": "image/png",
"url": "http://osm.oslandia.io/styles/klokantech-basic/${z}/${x}/${y}.png",
"url": "https://maps.pole-emploi.fr/styles/klokantech-basic/${z}/${x}/${y}.png",
"attribution": {
"name":"OpenStreetMap",
"url": "http://www.openstreetmap.org/"
Expand Down
70 changes: 46 additions & 24 deletions test/hooks_functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ import events from 'events';

events.EventEmitter.prototype._maxListeners = 100;

const TIMEOUT = 50000;

let itownsServer;
let itownsPort;
let browser;
// We could run 'npm start' to serve itowns for the tests,
// but it's slow to start (so tests might fail on timeouts).
// Since the 'test-examples' target depends on the 'run' target,
// we instead run the simplest http server.

function startStaticFileServer() {
return new Promise((resolve) => {
const ext2mime = new Map();
Expand Down Expand Up @@ -79,10 +82,14 @@ function waitServerReady(port) {
});
}

const layersAreInitialized = async () => {
await page.waitForFunction(() => view.mainLoop.scheduler.commandsWaitingExecutionCount() === 0
&& view.mainLoop.renderingState === 0
&& view.getLayers().every(layer => layer.ready), { timeout: 60000 });
const initializeLayers = async (timeout = TIMEOUT) => {
await page.waitForFunction(
() => (view.mainLoop.scheduler.commandsWaitingExecutionCount() === 0
&& view.mainLoop.renderingState === 0
&& view.getLayers().every(layer => layer.ready)),
{ timeout },
timeout,
);
};

const waitNextRender = async page => page.evaluate(() => new Promise((resolve) => {
Expand Down Expand Up @@ -113,34 +120,60 @@ const loadExample = async (url, screenshotName) => {
const pageErrors = [];
page.on('pageerror', (e) => { pageErrors.push(e); });

await page.goto(url);
try {
await page.goto(url);
} catch (e) {
throw new Error(`page ${url} couldn't load`, { cause: e });
}

pageErrors.forEach((e) => { throw e; });
if (pageErrors.length > 0) {
const err = new Error(`page ${url} encoutered ${pageErrors.length} error(s). [${pageErrors.map(e => e.message)}]`, { errors: pageErrors });
throw err;
}

await page.waitForFunction(() => typeof (view) === 'object');
await page.waitForFunction(() => typeof view === 'object' && view instanceof itowns.View);

await page.evaluate(() => {
itowns.CameraUtils.defaultStopPlaceOnGroundAtEnd = true;
});

try {
await layersAreInitialized();
await initializeLayers();
} catch (e) {
if (e instanceof Error && e.name === 'TimeoutError') {
console.warn(' *** Warning: initializeLayers timed out -> Camera motion had been stopped ***');
await page.evaluate(() => {
itowns.CameraUtils.stop(view, view.camera3D);
});
await layersAreInitialized();
await initializeLayers();
} else {
console.warn(e);
throw e;
}
}

await waitNextRender(page);

await saveScreenshot(page, screenshotName);

// store initial position for restoration after each tests
await saveInitialPosition();

return true;
};

async function saveInitialPosition() {
global.initialPosition = await page.evaluate(() => {
if (view.isGlobeView && view.controls) {
return Promise.resolve(itowns.CameraUtils.getTransformCameraLookingAtTarget(view, view.controls.camera));
} else if (view.isPlanarView) {
// TODO: make the controls accessible from PlanarView before doing
// anything more here
return Promise.resolve();
}
});
}

// Use waitUntilItownsIsIdle to wait until itowns has finished all its work (= layer updates)
const waitUntilItownsIsIdle = async (screenshotName) => {
const result = await page.evaluate(() => new Promise((resolve) => {
Expand Down Expand Up @@ -206,9 +239,9 @@ export const mochaHooks = {
});

// the page all tests will be tested in
return browser.newPage().then((p) => { global.page = p; });
global.page = await browser.newPage();
},
// store initial position for restoration after the test

afterAll(done) {
browser.close();
if (itownsServer) {
Expand All @@ -218,21 +251,10 @@ export const mochaHooks = {
done();
}
},
beforeEach: async () => {
global.initialPosition = await page.evaluate(() => {
if (view.isGlobeView && view.controls) {
return Promise.resolve(itowns.CameraUtils.getTransformCameraLookingAtTarget(view, view.controls.camera));
} else if (view.isPlanarView) {
// TODO: make the controls accessible from PlanarView before doing
// anything more here
return Promise.resolve();
}
});
},
// reset browser state instead of closing it
afterEach: async () => {
await page.evaluate((init) => {
if (view.isGlobeView && view.controls) {
if (view?.isGlobeView && view.controls) {
// eslint-disable-next-line no-param-reassign
init.coord = new itowns.Coordinates(
init.coord.crs,
Expand All @@ -242,7 +264,7 @@ export const mochaHooks = {
);
view.controls.lookAtCoordinate(init, false);
view.notifyChange();
} else if (view.isPlanarView) {
} else if (view?.isPlanarView) {
// TODO: make the controls accessible from PlanarView before doing
// anything more here
}
Expand Down
Loading