Organize tests

This commit is contained in:
Tim Schaub
2021-04-27 15:41:14 -07:00
parent 278e355795
commit 490cfabe91
599 changed files with 12374 additions and 1603 deletions
+118
View File
@@ -0,0 +1,118 @@
import ImageWrapper from '../../../../../src/ol/Image.js';
import ReprojImage from '../../../../../src/ol/reproj/Image.js';
import {get as getProjection} from '../../../../../src/ol/proj.js';
import {listen} from '../../../../../src/ol/events.js';
describe('ol.reproj.Image', function () {
function createImage(pixelRatio) {
return new ReprojImage(
getProjection('EPSG:3857'),
getProjection('EPSG:4326'),
[-180, -85, 180, 85],
10,
pixelRatio,
function (extent, resolution, pixelRatio) {
return new ImageWrapper(
extent,
resolution,
pixelRatio,
'data:image/gif;base64,' +
'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
null,
function (image, src) {
image.getImage().src = src;
}
);
}
);
}
function createTranslucentImage(pixelRatio) {
return new ReprojImage(
getProjection('EPSG:3857'),
getProjection('EPSG:4326'),
[-180, -85, 180, 85],
10,
pixelRatio,
function (extent, resolution, pixelRatio) {
return new ImageWrapper(
extent,
resolution,
pixelRatio,
'data:image/png;base64,' +
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8depePQAIiwMjFXlnJQAAAABJRU5ErkJggg==',
null,
function (image, src) {
image.getImage().src = src;
}
);
}
);
}
it('changes state as expected', function (done) {
const image = createImage(1);
expect(image.getState()).to.be(0); // IDLE
listen(image, 'change', function () {
if (image.getState() == 2) {
// LOADED
done();
}
});
image.load();
});
it('returns correct canvas size', function (done) {
const image = createImage(1);
listen(image, 'change', function () {
if (image.getState() == 2) {
// LOADED
const canvas = image.getImage();
expect(canvas.width).to.be(36);
expect(canvas.height).to.be(17);
done();
}
});
image.load();
});
it('respects pixelRatio', function (done) {
const image = createImage(2);
listen(image, 'change', function () {
if (image.getState() == 2) {
// LOADED
const canvas = image.getImage();
expect(canvas.width).to.be(72);
expect(canvas.height).to.be(34);
done();
}
});
image.load();
});
it('has uniform color', function (done) {
const image = createTranslucentImage(1);
listen(image, 'change', function () {
if (image.getState() == 2) {
// LOADED
const canvas = image.getImage();
expect(canvas.width).to.be(36);
expect(canvas.height).to.be(17);
const pixels = canvas
.getContext('2d')
.getImageData(0, 0, canvas.width, canvas.height).data;
for (let i = 0; i < canvas.width * canvas.height * 4; i += 4) {
expect(
Math.abs(pixels[i + 0] - pixels[0]) +
Math.abs(pixels[i + 1] - pixels[1]) +
Math.abs(pixels[i + 2] - pixels[2]) +
Math.abs(pixels[i + 3] - pixels[3])
).to.be.lessThan(5);
}
done();
}
});
image.load();
});
});
@@ -0,0 +1,64 @@
import {calculateSourceResolution} from '../../../../../src/ol/reproj.js';
import {get as getProjection, transform} from '../../../../../src/ol/proj.js';
describe('ol.reproj', function () {
describe('#calculateSourceResolution', function () {
const proj3857 = getProjection('EPSG:3857');
const proj4326 = getProjection('EPSG:4326');
const origin = [0, 0];
const point3857 = [50, 40];
const point4326 = transform(point3857, proj3857, proj4326);
it('is identity for identical projection', function () {
let result;
const resolution = 500;
result = calculateSourceResolution(
proj3857,
proj3857,
origin,
resolution
);
expect(result).to.be(resolution);
result = calculateSourceResolution(
proj3857,
proj3857,
point3857,
resolution
);
expect(result).to.be(resolution);
result = calculateSourceResolution(
proj4326,
proj4326,
point4326,
resolution
);
expect(result).to.be(resolution);
});
it('calculates correctly', function () {
const resolution4326 = 5;
const resolution3857 = calculateSourceResolution(
proj3857,
proj4326,
point4326,
resolution4326
);
expect(resolution3857).not.to.be(resolution4326);
expect(resolution3857).to.roughlyEqual(
5 * proj4326.getMetersPerUnit(),
1e-4
);
const result = calculateSourceResolution(
proj4326,
proj3857,
point3857,
resolution3857
);
expect(result).to.be(resolution4326);
});
});
});
+136
View File
@@ -0,0 +1,136 @@
import ImageTile from '../../../../../src/ol/ImageTile.js';
import ReprojTile from '../../../../../src/ol/reproj/Tile.js';
import {
addCommon,
clearAllProjections,
get as getProjection,
} from '../../../../../src/ol/proj.js';
import {createForProjection} from '../../../../../src/ol/tilegrid.js';
import {listen} from '../../../../../src/ol/events.js';
import {register} from '../../../../../src/ol/proj/proj4.js';
describe('ol.reproj.Tile', function () {
beforeEach(function () {
proj4.defs(
'EPSG:27700',
'+proj=tmerc +lat_0=49 +lon_0=-2 ' +
'+k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy ' +
'+towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 ' +
'+units=m +no_defs'
);
register(proj4);
const proj27700 = getProjection('EPSG:27700');
proj27700.setExtent([0, 0, 700000, 1300000]);
});
afterEach(function () {
delete proj4.defs['EPSG:27700'];
clearAllProjections();
addCommon();
});
function createTile(pixelRatio, opt_tileSize) {
const proj4326 = getProjection('EPSG:4326');
const proj3857 = getProjection('EPSG:3857');
return new ReprojTile(
proj3857,
createForProjection(proj3857),
proj4326,
createForProjection(proj4326, 3, opt_tileSize),
[3, 2, 1],
null,
pixelRatio,
0,
function (z, x, y, pixelRatio) {
return new ImageTile(
[z, x, y],
0, // IDLE
'data:image/gif;base64,' +
'R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
null,
function (tile, src) {
tile.getImage().src = src;
}
);
}
);
}
it('changes state as expected', function (done) {
const tile = createTile(1);
expect(tile.getState()).to.be(0); // IDLE
listen(tile, 'change', function () {
if (tile.getState() == 2) {
// LOADED
done();
}
});
tile.load();
});
it('is empty when outside target tile grid', function () {
const proj4326 = getProjection('EPSG:4326');
const proj3857 = getProjection('EPSG:3857');
const tile = new ReprojTile(
proj3857,
createForProjection(proj3857),
proj4326,
createForProjection(proj4326),
[0, -1, 0],
null,
1,
0,
function () {
expect().fail('No tiles should be required');
}
);
expect(tile.getState()).to.be(4); // EMPTY
});
it('is empty when outside source tile grid', function () {
const proj4326 = getProjection('EPSG:4326');
const proj27700 = getProjection('EPSG:27700');
const tile = new ReprojTile(
proj27700,
createForProjection(proj27700),
proj4326,
createForProjection(proj4326),
[3, 2, -2],
null,
1,
0,
function () {
expect().fail('No tiles should be required');
}
);
expect(tile.getState()).to.be(4); // EMPTY
});
it('respects tile size of target tile grid', function (done) {
const tile = createTile(1, [100, 40]);
listen(tile, 'change', function () {
if (tile.getState() == 2) {
// LOADED
const canvas = tile.getImage();
expect(canvas.width).to.be(100);
expect(canvas.height).to.be(40);
done();
}
});
tile.load();
});
it('respects pixelRatio', function (done) {
const tile = createTile(3, [60, 20]);
listen(tile, 'change', function () {
if (tile.getState() == 2) {
// LOADED
const canvas = tile.getImage();
expect(canvas.width).to.be(180);
expect(canvas.height).to.be(60);
done();
}
});
tile.load();
});
});
@@ -0,0 +1,67 @@
import Triangulation from '../../../../../src/ol/reproj/Triangulation.js';
import {
addCommon,
clearAllProjections,
get as getProjection,
} from '../../../../../src/ol/proj.js';
import {register} from '../../../../../src/ol/proj/proj4.js';
describe('ol.reproj.Triangulation', function () {
beforeEach(function () {
proj4.defs(
'EPSG:27700',
'+proj=tmerc +lat_0=49 +lon_0=-2 ' +
'+k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy ' +
'+towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 ' +
'+units=m +no_defs'
);
register(proj4);
const proj27700 = getProjection('EPSG:27700');
proj27700.setExtent([0, 0, 700000, 1300000]);
});
afterEach(function () {
delete proj4.defs['EPSG:27700'];
clearAllProjections();
addCommon();
});
describe('constructor', function () {
it('is trivial for identity', function () {
const proj4326 = getProjection('EPSG:4326');
const triangulation = new Triangulation(
proj4326,
proj4326,
[20, 20, 30, 30],
[-180, -90, 180, 90],
0
);
expect(triangulation.getTriangles().length).to.be(2);
});
it('is empty when outside source extent', function () {
const proj4326 = getProjection('EPSG:4326');
const proj27700 = getProjection('EPSG:27700');
const triangulation = new Triangulation(
proj27700,
proj4326,
[0, 0, 10, 10],
proj27700.getExtent(),
0
);
expect(triangulation.getTriangles().length).to.be(0);
});
it('can handle null source extent', function () {
const proj4326 = getProjection('EPSG:4326');
const triangulation = new Triangulation(
proj4326,
proj4326,
[20, 20, 30, 30],
null,
0
);
expect(triangulation.getTriangles().length).to.be(2);
});
});
});