Organize tests
This commit is contained in:
68
test/browser/spec/ol/render/canvas/executorgroup.test.js
Normal file
68
test/browser/spec/ol/render/canvas/executorgroup.test.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import {getPixelIndexArray} from '../../../../../../src/ol/render/canvas/ExecutorGroup.js';
|
||||
|
||||
describe('ol.render.canvas.ExecutorGroup', function () {
|
||||
describe('#getPixelIndexArray', function () {
|
||||
it('creates an array with every index within distance', function () {
|
||||
const radius = 10;
|
||||
const size = radius * 2 + 1;
|
||||
const hitIndexes = getPixelIndexArray(radius);
|
||||
|
||||
const circleArray = new Array(size);
|
||||
for (let i = 0; i < size; i++) {
|
||||
circleArray[i] = new Array(size);
|
||||
}
|
||||
|
||||
hitIndexes.forEach(function (d) {
|
||||
const x = ((d - 3) / 4) % size;
|
||||
const y = ((d - 3) / 4 / size) | 0;
|
||||
circleArray[x][y] = true;
|
||||
});
|
||||
|
||||
const minRadiusSq = Math.pow(radius - Math.SQRT2, 2);
|
||||
const maxRadiusSq = Math.pow(radius + Math.SQRT2, 2);
|
||||
expect(circleArray.length).to.be(size);
|
||||
|
||||
for (let i = 0; i < size; i++) {
|
||||
expect(circleArray[i].length).to.be(size);
|
||||
for (let j = 0; j < size; j++) {
|
||||
const dx = Math.abs(radius - i);
|
||||
const dy = Math.abs(radius - j);
|
||||
const distanceSq = Math.pow(dx, 2) + Math.pow(dy, 2);
|
||||
if (circleArray[i][j] === true) {
|
||||
expect(distanceSq).to.be.within(0, maxRadiusSq);
|
||||
} else {
|
||||
expect(distanceSq).to.be.within(minRadiusSq, Infinity);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
it('orders the indexes correctly from closest to farthest away', function () {
|
||||
const radius = 10;
|
||||
const size = radius * 2 + 1;
|
||||
const hitIndexes = getPixelIndexArray(radius);
|
||||
|
||||
// Center first
|
||||
expect(hitIndexes[0]).to.be((size * radius + radius) * 4 + 3);
|
||||
|
||||
// 4 Pixels above/below/left/right of center next
|
||||
const begin = hitIndexes.slice(1, 5);
|
||||
expect(begin).to.contain((radius * size + radius + 1) * 4 + 3);
|
||||
expect(begin).to.contain(((radius + 1) * size + radius) * 4 + 3);
|
||||
expect(begin).to.contain(((radius - 1) * size + radius) * 4 + 3);
|
||||
expect(begin).to.contain((radius * size + radius - 1) * 4 + 3);
|
||||
|
||||
// 4 Pixels in the middle of each side in the last 12 elements (at radius 10)
|
||||
const last = hitIndexes.slice(hitIndexes.length - 12);
|
||||
expect(last).to.contain((0 * size + radius) * 4 + 3);
|
||||
expect(last).to.contain((radius * size + 0) * 4 + 3);
|
||||
expect(last).to.contain((radius * size + size - 1) * 4 + 3);
|
||||
expect(last).to.contain(((size - 1) * size + radius) * 4 + 3);
|
||||
});
|
||||
it('has no duplicate indexes', function () {
|
||||
const radius = 10;
|
||||
const hitIndexes = getPixelIndexArray(radius);
|
||||
|
||||
expect(new Set(hitIndexes).size).to.be(hitIndexes.length);
|
||||
});
|
||||
});
|
||||
});
|
||||
96
test/browser/spec/ol/render/canvas/hitdetect.test.js
Normal file
96
test/browser/spec/ol/render/canvas/hitdetect.test.js
Normal file
@@ -0,0 +1,96 @@
|
||||
import Circle from '../../../../../../src/ol/style/Circle.js';
|
||||
import Feature from '../../../../../../src/ol/Feature.js';
|
||||
import Point from '../../../../../../src/ol/geom/Point.js';
|
||||
import {Style} from '../../../../../../src/ol/style.js';
|
||||
import {create} from '../../../../../../src/ol/transform.js';
|
||||
import {createCanvasContext2D} from '../../../../../../src/ol/dom.js';
|
||||
import {
|
||||
createHitDetectionImageData,
|
||||
hitDetect,
|
||||
} from '../../../../../../src/ol/render/canvas/hitdetect.js';
|
||||
|
||||
describe('hitdetect', function () {
|
||||
let features, styleFunction;
|
||||
|
||||
beforeEach(function () {
|
||||
features = [
|
||||
new Feature(new Point([0, 75])),
|
||||
new Feature(new Point([0, 50])),
|
||||
new Feature(new Point([0, 25])),
|
||||
new Feature(new Point([0, 0])),
|
||||
];
|
||||
styleFunction = function () {
|
||||
return new Style({
|
||||
image: new Circle({
|
||||
radius: 5,
|
||||
}),
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
it('does not exceed the color range', function () {
|
||||
const imageData = createHitDetectionImageData(
|
||||
[2, 2],
|
||||
[create()],
|
||||
features,
|
||||
styleFunction,
|
||||
[0, 0, 0, 0],
|
||||
1,
|
||||
0
|
||||
);
|
||||
expect(Array.prototype.slice.call(imageData.data, 0, 3)).to.eql([
|
||||
255,
|
||||
255,
|
||||
252,
|
||||
]);
|
||||
});
|
||||
it('detects hit at the correct position', function () {
|
||||
const context = createCanvasContext2D(3, 3);
|
||||
context.fillStyle = '#ffffff';
|
||||
context.fillRect(1, 1, 1, 1);
|
||||
const features = [new Feature()];
|
||||
const imageData = context.getImageData(0, 0, 3, 3);
|
||||
expect(hitDetect([2, 2], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([2, 3], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([3, 2], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([3, 3], features, imageData)).to.have.length(1);
|
||||
|
||||
expect(hitDetect([1.5, 1.5], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([3.4, 3.4], features, imageData)).to.have.length(1);
|
||||
|
||||
expect(hitDetect([1.4, 1], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([1, 2.4], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([2.4, 1], features, imageData)).to.have.length(0);
|
||||
|
||||
expect(hitDetect([3.5, 4.5], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([5, 4], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([4.5, 5], features, imageData)).to.have.length(0);
|
||||
|
||||
expect(hitDetect([1.4, 3.5], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([1, 4.5], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([1.5, 5], features, imageData)).to.have.length(0);
|
||||
});
|
||||
it('correctly detects hit for pixel exceeding canvas dimension', function () {
|
||||
const features = [new Feature()];
|
||||
const context = createCanvasContext2D(2, 2);
|
||||
context.fillStyle = '#ffffff';
|
||||
|
||||
context.fillRect(1, 1, 1, 1);
|
||||
let imageData = context.getImageData(0, 0, 2, 2);
|
||||
expect(hitDetect([4, 2], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([2, 4], features, imageData)).to.have.length(1);
|
||||
|
||||
expect(hitDetect([-2, 4], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([4, -2], features, imageData)).to.have.length(0);
|
||||
|
||||
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
|
||||
|
||||
context.fillRect(0, 0, 1, 1);
|
||||
imageData = context.getImageData(0, 0, 2, 2);
|
||||
expect(hitDetect([-2, 0], features, imageData)).to.have.length(1);
|
||||
expect(hitDetect([0, -2], features, imageData)).to.have.length(1);
|
||||
|
||||
expect(hitDetect([-2, 4], features, imageData)).to.have.length(0);
|
||||
expect(hitDetect([4, -2], features, imageData)).to.have.length(0);
|
||||
});
|
||||
});
|
||||
359
test/browser/spec/ol/render/canvas/immediate.test.js
Normal file
359
test/browser/spec/ol/render/canvas/immediate.test.js
Normal file
@@ -0,0 +1,359 @@
|
||||
import CanvasImmediateRenderer from '../../../../../../src/ol/render/canvas/Immediate.js';
|
||||
import Circle from '../../../../../../src/ol/geom/Circle.js';
|
||||
import CircleStyle from '../../../../../../src/ol/style/Circle.js';
|
||||
import Fill from '../../../../../../src/ol/style/Fill.js';
|
||||
import GeometryCollection from '../../../../../../src/ol/geom/GeometryCollection.js';
|
||||
import LineString from '../../../../../../src/ol/geom/LineString.js';
|
||||
import MultiLineString from '../../../../../../src/ol/geom/MultiLineString.js';
|
||||
import MultiPoint from '../../../../../../src/ol/geom/MultiPoint.js';
|
||||
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js';
|
||||
import Point from '../../../../../../src/ol/geom/Point.js';
|
||||
import Polygon from '../../../../../../src/ol/geom/Polygon.js';
|
||||
import Stroke from '../../../../../../src/ol/style/Stroke.js';
|
||||
import Style from '../../../../../../src/ol/style/Style.js';
|
||||
import Text from '../../../../../../src/ol/style/Text.js';
|
||||
import VectorContext from '../../../../../../src/ol/render/VectorContext.js';
|
||||
|
||||
describe('ol.render.canvas.Immediate', function () {
|
||||
function getMockContext() {
|
||||
return {
|
||||
setLineDash: sinon.spy(),
|
||||
beginPath: sinon.spy(),
|
||||
closePath: sinon.spy(),
|
||||
stroke: sinon.spy(),
|
||||
lineTo: sinon.spy(),
|
||||
moveTo: sinon.spy(),
|
||||
};
|
||||
}
|
||||
|
||||
describe('constructor', function () {
|
||||
it('creates an instance', function () {
|
||||
const instance = new CanvasImmediateRenderer();
|
||||
expect(instance).to.be.a(CanvasImmediateRenderer);
|
||||
expect(instance).to.be.a(VectorContext);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#setStyle()', function () {
|
||||
it('calls the more specific methods with style parts', function () {
|
||||
const context = new CanvasImmediateRenderer();
|
||||
sinon.spy(context, 'setFillStrokeStyle');
|
||||
sinon.spy(context, 'setImageStyle');
|
||||
sinon.spy(context, 'setTextStyle');
|
||||
const fill = new Fill({});
|
||||
const stroke = new Stroke({});
|
||||
const text = new Text({});
|
||||
const image = new CircleStyle({});
|
||||
const style = new Style({
|
||||
fill: fill,
|
||||
stroke: stroke,
|
||||
image: image,
|
||||
text: text,
|
||||
});
|
||||
|
||||
context.setStyle(style);
|
||||
expect(context.setFillStrokeStyle.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.setFillStrokeStyle.firstCall.calledWithExactly(fill, stroke)
|
||||
).to.be(true);
|
||||
expect(context.setImageStyle.calledOnce).to.be(true);
|
||||
expect(context.setImageStyle.firstCall.calledWithExactly(image)).to.be(
|
||||
true
|
||||
);
|
||||
expect(context.setTextStyle.calledOnce).to.be(true);
|
||||
expect(context.setTextStyle.firstCall.calledWithExactly(text)).to.be(
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#drawGeometry()', function () {
|
||||
const extent = [-10, -10, 10, 10];
|
||||
|
||||
it('calls drawPoint() with a Point', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawPoint');
|
||||
|
||||
const geometry = new Point([1, 2]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawPoint.calledOnce).to.be(true);
|
||||
expect(context.drawPoint.firstCall.calledWithExactly(geometry)).to.be(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it('calls drawLineString() with a LineString', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawLineString');
|
||||
|
||||
const geometry = new LineString([
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawLineString.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.drawLineString.firstCall.calledWithExactly(geometry)
|
||||
).to.be(true);
|
||||
});
|
||||
|
||||
it('calls drawPolygon() with a Polygon', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawPolygon');
|
||||
|
||||
const geometry = new Polygon([
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6],
|
||||
[1, 2],
|
||||
],
|
||||
]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawPolygon.calledOnce).to.be(true);
|
||||
expect(context.drawPolygon.firstCall.calledWithExactly(geometry)).to.be(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it('calls drawMultiPoint() with a MultiPoint', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawMultiPoint');
|
||||
|
||||
const geometry = new MultiPoint([
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawMultiPoint.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.drawMultiPoint.firstCall.calledWithExactly(geometry)
|
||||
).to.be(true);
|
||||
});
|
||||
|
||||
it('calls drawMultiLineString() with a MultiLineString', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawMultiLineString');
|
||||
|
||||
const geometry = new MultiLineString([
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
],
|
||||
]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawMultiLineString.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.drawMultiLineString.firstCall.calledWithExactly(geometry)
|
||||
).to.be(true);
|
||||
});
|
||||
|
||||
it('calls drawMultiPolygon() with a MultiPolygon', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawMultiPolygon');
|
||||
|
||||
const geometry = new MultiPolygon([
|
||||
[
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6],
|
||||
[1, 2],
|
||||
],
|
||||
],
|
||||
]);
|
||||
context.drawGeometry(geometry);
|
||||
expect(context.drawMultiPolygon.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.drawMultiPolygon.firstCall.calledWithExactly(geometry)
|
||||
).to.be(true);
|
||||
});
|
||||
|
||||
it('calls drawGeometryCollection() with a GeometryCollection', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawGeometryCollection');
|
||||
sinon.spy(context, 'drawPoint');
|
||||
sinon.spy(context, 'drawLineString');
|
||||
sinon.spy(context, 'drawPolygon');
|
||||
|
||||
const point = new Point([1, 2]);
|
||||
const linestring = new LineString([
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
]);
|
||||
const polygon = new Polygon([
|
||||
[
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6],
|
||||
[1, 2],
|
||||
],
|
||||
]);
|
||||
|
||||
const geometry = new GeometryCollection([point, linestring, polygon]);
|
||||
context.drawGeometry(geometry);
|
||||
|
||||
expect(context.drawGeometryCollection.calledOnce).to.be(true);
|
||||
expect(context.drawPoint.calledOnce).to.be(true);
|
||||
expect(context.drawPoint.firstCall.calledWithExactly(point)).to.be(true);
|
||||
expect(context.drawLineString.calledOnce).to.be(true);
|
||||
expect(
|
||||
context.drawLineString.firstCall.calledWithExactly(linestring)
|
||||
).to.be(true);
|
||||
expect(context.drawPolygon.calledOnce).to.be(true);
|
||||
expect(context.drawPolygon.firstCall.calledWithExactly(polygon)).to.be(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it('calls drawCircle() with a Circle', function () {
|
||||
const context = new CanvasImmediateRenderer(getMockContext(), 1, extent);
|
||||
sinon.spy(context, 'drawCircle');
|
||||
|
||||
const geometry = new Circle([0, 0]);
|
||||
context.drawGeometry(geometry);
|
||||
|
||||
expect(context.drawCircle.calledOnce).to.be(true);
|
||||
expect(context.drawCircle.firstCall.calledWithExactly(geometry)).to.be(
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#drawMultiPolygon()', function () {
|
||||
it('creates the correct canvas instructions for 3D geometries', function () {
|
||||
const instructions = [];
|
||||
|
||||
function serialize(index, instruction) {
|
||||
if (!instruction) {
|
||||
return 'id: ' + index + ' NO INSTRUCTION';
|
||||
}
|
||||
const parts = ['id: ' + index, 'type: ' + instruction.type];
|
||||
if (instruction.args) {
|
||||
parts.push(
|
||||
'args: [' +
|
||||
instruction.args
|
||||
.map(function (arg) {
|
||||
if (typeof arg === 'number') {
|
||||
return arg.toFixed(9);
|
||||
} else {
|
||||
return arg;
|
||||
}
|
||||
})
|
||||
.join(', ') +
|
||||
']'
|
||||
);
|
||||
}
|
||||
return parts.join(', ');
|
||||
}
|
||||
|
||||
const context = {
|
||||
beginPath: function () {},
|
||||
moveTo: function (x, y) {
|
||||
instructions.push({
|
||||
type: 'moveTo',
|
||||
args: [x, y],
|
||||
});
|
||||
},
|
||||
lineTo: function (x, y) {
|
||||
instructions.push({
|
||||
type: 'lineTo',
|
||||
args: [x, y],
|
||||
});
|
||||
},
|
||||
closePath: function () {
|
||||
instructions.push({
|
||||
type: 'closePath',
|
||||
});
|
||||
},
|
||||
setLineDash: function () {},
|
||||
stroke: function () {},
|
||||
};
|
||||
|
||||
const transform = [
|
||||
0.0004088332670837288,
|
||||
0,
|
||||
0,
|
||||
-0.0004088332670837288,
|
||||
4480.991370439071,
|
||||
1529.5752568707105,
|
||||
];
|
||||
|
||||
const extent = [
|
||||
-10960437.252092224,
|
||||
2762924.0275091752,
|
||||
-7572748.158493212,
|
||||
3741317.9895594316,
|
||||
];
|
||||
|
||||
const canvas = new CanvasImmediateRenderer(context, 1, extent, transform);
|
||||
|
||||
canvas.strokeState_ = {
|
||||
lineCap: 'round',
|
||||
lineDash: [],
|
||||
lineJoin: 'round',
|
||||
lineWidth: 3,
|
||||
miterLimit: 10,
|
||||
strokeStyle: '#00FFFF',
|
||||
};
|
||||
|
||||
const multiPolygonGeometry = new MultiPolygon([
|
||||
[
|
||||
[
|
||||
// first polygon
|
||||
[-80.736061, 28.788576000000006, 0], // moveTo()
|
||||
[-80.763557, 28.821799999999996, 0], // lineTo()
|
||||
[-80.817406, 28.895123999999996, 0], // lineTo()
|
||||
[-80.891304, 29.013130000000004, 0], // lineTo()
|
||||
[-80.916512, 29.071560000000005, 0], // lineTo()
|
||||
[-80.899323, 29.061249000000004, 0], // lineTo()
|
||||
[-80.862663, 28.991361999999995, 0], // lineTo()
|
||||
[-80.736061, 28.788576000000006, 0], // closePath()
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
// second polygon
|
||||
[-82.102127, 26.585724, 0], // moveTo()
|
||||
[-82.067139, 26.497208, 0], // lineTo()
|
||||
[-82.097641, 26.493585999999993, 0], // lineTo()
|
||||
[-82.135895, 26.642279000000002, 0], // lineTo()
|
||||
[-82.183495, 26.683082999999996, 0], // lineTo()
|
||||
[-82.128838, 26.693342, 0], // lineTo()
|
||||
[-82.102127, 26.585724, 0], // closePath()
|
||||
],
|
||||
],
|
||||
]).transform('EPSG:4326', 'EPSG:3857');
|
||||
|
||||
canvas.drawMultiPolygon(multiPolygonGeometry, null);
|
||||
|
||||
const expected = [
|
||||
// first polygon
|
||||
{type: 'moveTo', args: [806.6035275946265, 160.48916296287916]},
|
||||
{type: 'lineTo', args: [805.3521540835154, 158.76358389011807]},
|
||||
{type: 'lineTo', args: [802.9014262612932, 154.95335187132082]},
|
||||
{type: 'lineTo', args: [799.5382461724039, 148.815592819916]},
|
||||
{type: 'lineTo', args: [798.3910020835165, 145.77392230456553]},
|
||||
{type: 'lineTo', args: [799.1732925724045, 146.31080369865776]},
|
||||
{type: 'lineTo', args: [800.8417299057378, 149.94832216046188]},
|
||||
{type: 'closePath'},
|
||||
// second polygon
|
||||
{type: 'moveTo', args: [744.4323460835158, 273.7179168205373]},
|
||||
{type: 'lineTo', args: [746.0246888390716, 278.22094795365365]},
|
||||
{type: 'lineTo', args: [744.6365089279602, 278.40513424671826]},
|
||||
{type: 'lineTo', args: [742.8955268835157, 270.83899948444764]},
|
||||
{type: 'lineTo', args: [740.7291979946272, 268.76099731369345]},
|
||||
{type: 'lineTo', args: [743.2166987946266, 268.23842607400616]},
|
||||
{type: 'closePath'},
|
||||
];
|
||||
|
||||
for (let i = 0, ii = instructions.length; i < ii; ++i) {
|
||||
const actualInstruction = serialize(i, instructions[i]);
|
||||
const expectedInstruction = serialize(i, expected[i]);
|
||||
expect(actualInstruction).to.equal(expectedInstruction);
|
||||
}
|
||||
|
||||
expect(instructions.length).to.equal(expected.length);
|
||||
});
|
||||
});
|
||||
});
|
||||
152
test/browser/spec/ol/render/canvas/index.test.js
Normal file
152
test/browser/spec/ol/render/canvas/index.test.js
Normal file
@@ -0,0 +1,152 @@
|
||||
import * as render from '../../../../../../src/ol/render/canvas.js';
|
||||
|
||||
describe('ol.render.canvas', function () {
|
||||
const font = document.createElement('link');
|
||||
font.href =
|
||||
'https://fonts.googleapis.com/css?family=Abel&text=wmytzilWMYTZIL%40%23%2F%26%3F%24%2510';
|
||||
font.rel = 'stylesheet';
|
||||
const head = document.getElementsByTagName('head')[0];
|
||||
|
||||
describe('ol.render.canvas.registerFont()', function () {
|
||||
beforeEach(function () {
|
||||
render.checkedFonts.values_ = {};
|
||||
render.measureTextHeight('12px sans-serif');
|
||||
});
|
||||
|
||||
const retries = 100;
|
||||
|
||||
it('does not trigger redraw and clear measurements for unavailable fonts', function (done) {
|
||||
this.timeout(4000);
|
||||
const spy = sinon.spy();
|
||||
render.checkedFonts.addEventListener('propertychange', spy);
|
||||
const interval = setInterval(function () {
|
||||
if (
|
||||
render.checkedFonts.get('normal\nnormal\nfoo') == retries &&
|
||||
render.checkedFonts.get('normal\nnormal\nsans-serif') == retries
|
||||
) {
|
||||
clearInterval(interval);
|
||||
render.checkedFonts.removeEventListener('propertychange', spy);
|
||||
expect(spy.callCount).to.be(0);
|
||||
expect(render.textHeights).to.not.eql({});
|
||||
done();
|
||||
}
|
||||
}, 32);
|
||||
render.registerFont('12px foo,sans-serif');
|
||||
});
|
||||
|
||||
it('does not trigger redraw and clear measurements for available fonts', function (done) {
|
||||
const spy = sinon.spy();
|
||||
render.checkedFonts.addEventListener('propertychange', spy);
|
||||
const interval = setInterval(function () {
|
||||
if (render.checkedFonts.get('normal\nnormal\nsans-serif') == retries) {
|
||||
clearInterval(interval);
|
||||
render.checkedFonts.removeEventListener('propertychange', spy);
|
||||
expect(spy.callCount).to.be(0);
|
||||
expect(render.textHeights).to.not.eql({});
|
||||
done();
|
||||
}
|
||||
}, 32);
|
||||
render.registerFont('12px sans-serif');
|
||||
});
|
||||
|
||||
it("does not trigger redraw and clear measurements for the 'monospace' font", function (done) {
|
||||
const spy = sinon.spy();
|
||||
render.checkedFonts.addEventListener('propertychange', spy);
|
||||
const interval = setInterval(function () {
|
||||
if (render.checkedFonts.get('normal\nnormal\nmonospace') == retries) {
|
||||
clearInterval(interval);
|
||||
render.checkedFonts.removeEventListener('propertychange', spy);
|
||||
expect(spy.callCount).to.be(0);
|
||||
expect(render.textHeights).to.not.eql({});
|
||||
done();
|
||||
}
|
||||
}, 32);
|
||||
render.registerFont('12px monospace');
|
||||
});
|
||||
|
||||
it('triggers redraw and clear measurements for fonts that become available', function (done) {
|
||||
head.appendChild(font);
|
||||
render.checkedFonts.addEventListener(
|
||||
'propertychange',
|
||||
function onPropertyChange(e) {
|
||||
render.checkedFonts.removeEventListener(
|
||||
'propertychange',
|
||||
onPropertyChange
|
||||
);
|
||||
expect(e.key).to.be('normal\nnormal\nAbel');
|
||||
expect(render.textHeights).to.eql({});
|
||||
done();
|
||||
}
|
||||
);
|
||||
render.registerFont('12px Abel');
|
||||
});
|
||||
});
|
||||
|
||||
describe('measureTextHeight', function () {
|
||||
it('respects line-height', function () {
|
||||
const height = render.measureTextHeight('12px/1.2 sans-serif');
|
||||
expect(render.measureTextHeight('12px/2.4 sans-serif')).to.be.greaterThan(
|
||||
height
|
||||
);
|
||||
expect(render.measureTextHeight('12px/0.1 sans-serif')).to.be.lessThan(
|
||||
height
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('rotateAtOffset', function () {
|
||||
it('rotates a canvas at an offset point', function () {
|
||||
const context = {
|
||||
translate: sinon.spy(),
|
||||
rotate: sinon.spy(),
|
||||
};
|
||||
render.rotateAtOffset(context, Math.PI, 10, 10);
|
||||
expect(context.translate.callCount).to.be(2);
|
||||
expect(context.translate.firstCall.args).to.eql([10, 10]);
|
||||
expect(context.translate.secondCall.args).to.eql([-10, -10]);
|
||||
expect(context.rotate.callCount).to.be(1);
|
||||
expect(context.rotate.firstCall.args).to.eql([Math.PI]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('drawImageOrLabel', function () {
|
||||
it('draws the image with correct parameters', function () {
|
||||
const layerContext = {
|
||||
save: sinon.spy(),
|
||||
setTransform: sinon.spy(),
|
||||
drawImage: sinon.spy(),
|
||||
restore: sinon.spy(),
|
||||
globalAlpha: 1,
|
||||
};
|
||||
const transform = [1, 0, 0, 1, 0, 0];
|
||||
const opacity = 0.5;
|
||||
const image = {};
|
||||
const x = 0;
|
||||
const y = 0;
|
||||
const w = 1;
|
||||
const h = 1;
|
||||
const scale = 1;
|
||||
|
||||
render.drawImageOrLabel(
|
||||
layerContext,
|
||||
transform.slice(),
|
||||
opacity,
|
||||
image,
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
x,
|
||||
y,
|
||||
scale
|
||||
);
|
||||
|
||||
expect(layerContext.save.callCount).to.be(1);
|
||||
expect(layerContext.setTransform.callCount).to.be(1);
|
||||
expect(layerContext.setTransform.firstCall.args).to.eql(transform);
|
||||
expect(layerContext.drawImage.callCount).to.be(1);
|
||||
expect(layerContext.globalAlpha).to.be(0.5);
|
||||
expect(layerContext.restore.callCount).to.be(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
369
test/browser/spec/ol/render/canvas/textbuilder.test.js
Normal file
369
test/browser/spec/ol/render/canvas/textbuilder.test.js
Normal file
@@ -0,0 +1,369 @@
|
||||
import Circle from '../../../../../../src/ol/geom/Circle.js';
|
||||
import Executor from '../../../../../../src/ol/render/canvas/Executor.js';
|
||||
import Feature from '../../../../../../src/ol/Feature.js';
|
||||
import LineString from '../../../../../../src/ol/geom/LineString.js';
|
||||
import MultiLineString from '../../../../../../src/ol/geom/MultiLineString.js';
|
||||
import MultiPoint from '../../../../../../src/ol/geom/MultiPoint.js';
|
||||
import MultiPolygon from '../../../../../../src/ol/geom/MultiPolygon.js';
|
||||
import Point from '../../../../../../src/ol/geom/Point.js';
|
||||
import Polygon from '../../../../../../src/ol/geom/Polygon.js';
|
||||
import Text from '../../../../../../src/ol/style/Text.js';
|
||||
import TextBuilder from '../../../../../../src/ol/render/canvas/TextBuilder.js';
|
||||
import {create as createTransform} from '../../../../../../src/ol/transform.js';
|
||||
|
||||
function createBuilder() {
|
||||
return new TextBuilder(1, [-180, -90, 180, 90], 0.02, 1, true);
|
||||
}
|
||||
|
||||
function createContext() {
|
||||
return {
|
||||
fill: function () {},
|
||||
stroke: function () {},
|
||||
beginPath: function () {},
|
||||
clip: function () {},
|
||||
moveTo: function () {},
|
||||
lineTo: function () {},
|
||||
closePath: function () {},
|
||||
setLineDash: function () {},
|
||||
save: function () {},
|
||||
restore: function () {},
|
||||
};
|
||||
}
|
||||
|
||||
function executeInstructions(
|
||||
builder,
|
||||
expectedDrawTextImageCalls,
|
||||
expectedBuilderImageCalls
|
||||
) {
|
||||
const transform = createTransform();
|
||||
const context = createContext();
|
||||
const executor = new Executor(0.02, 1, false, builder.finish());
|
||||
sinon.spy(executor, 'drawLabelWithPointPlacement_');
|
||||
const replayImageOrLabelStub = sinon.stub(executor, 'replayImageOrLabel_');
|
||||
executor.execute(context, 1, transform);
|
||||
expect(executor.drawLabelWithPointPlacement_.callCount).to.be(
|
||||
expectedDrawTextImageCalls
|
||||
);
|
||||
expect(replayImageOrLabelStub.callCount).to.be(expectedBuilderImageCalls);
|
||||
}
|
||||
|
||||
describe('ol.render.canvas.TextBuilder', function () {
|
||||
it('builds correct coordinates array with a stride of 2 for geometries with 2 dimensions', function () {
|
||||
const builder = createBuilder();
|
||||
const features = [
|
||||
new Feature(new Point([0, 0])),
|
||||
new Feature(new Point([1, 1])),
|
||||
new Feature(
|
||||
new MultiLineString([
|
||||
new LineString([
|
||||
[1, 1],
|
||||
[3, 3],
|
||||
]),
|
||||
new LineString([
|
||||
[2, 2],
|
||||
[4, 4],
|
||||
]),
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new LineString([
|
||||
[3, 3],
|
||||
[5, 5],
|
||||
])
|
||||
),
|
||||
new Feature(new Circle([5, 5, 7], 4)),
|
||||
new Feature(
|
||||
new MultiPoint([
|
||||
[6, 6],
|
||||
[7, 7],
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new Polygon([
|
||||
[
|
||||
[7, 7],
|
||||
[7, 9],
|
||||
[9, 9],
|
||||
[9, 7],
|
||||
[7, 7],
|
||||
],
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new MultiPolygon([
|
||||
new Polygon([
|
||||
[
|
||||
[8, 8],
|
||||
[8, 10],
|
||||
[10, 10],
|
||||
[10, 8],
|
||||
[8, 8],
|
||||
],
|
||||
]),
|
||||
new Polygon([
|
||||
[
|
||||
[9, 9],
|
||||
[9, 11],
|
||||
[11, 11],
|
||||
[11, 9],
|
||||
[9, 9],
|
||||
],
|
||||
]),
|
||||
])
|
||||
),
|
||||
];
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'Text',
|
||||
})
|
||||
);
|
||||
features.forEach(function (feature) {
|
||||
builder.drawText(feature.getGeometry(), feature);
|
||||
});
|
||||
expect(builder.coordinates).to.eql([
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
3,
|
||||
3,
|
||||
4,
|
||||
4,
|
||||
5,
|
||||
5,
|
||||
6,
|
||||
6,
|
||||
7,
|
||||
7,
|
||||
8,
|
||||
8,
|
||||
9,
|
||||
9,
|
||||
10,
|
||||
10,
|
||||
]);
|
||||
});
|
||||
|
||||
it('builds correct coordinates array with a stride of 2 for geometries with 3 dimensions', function () {
|
||||
const builder = createBuilder();
|
||||
const features = [
|
||||
new Feature(new Point([0, 0, 1])),
|
||||
new Feature(new Point([1, 1, 2])),
|
||||
new Feature(
|
||||
new MultiLineString([
|
||||
new LineString([
|
||||
[1, 1, 1],
|
||||
[3, 3, 2],
|
||||
]),
|
||||
new LineString([
|
||||
[2, 2, 3],
|
||||
[4, 4, 4],
|
||||
]),
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new LineString([
|
||||
[3, 3, 5],
|
||||
[5, 5, 6],
|
||||
])
|
||||
),
|
||||
new Feature(new Circle([5, 5, 7], 4)),
|
||||
new Feature(
|
||||
new MultiPoint([
|
||||
[6, 6, 8],
|
||||
[7, 7, 9],
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new Polygon([
|
||||
[
|
||||
[7, 7, 1],
|
||||
[7, 9, 2],
|
||||
[9, 9, 3],
|
||||
[9, 7, 4],
|
||||
[7, 7, 5],
|
||||
],
|
||||
])
|
||||
),
|
||||
new Feature(
|
||||
new MultiPolygon([
|
||||
new Polygon([
|
||||
[
|
||||
[8, 8, 1],
|
||||
[8, 10, 2],
|
||||
[10, 10, 3],
|
||||
[10, 8, 4],
|
||||
[8, 8, 1],
|
||||
],
|
||||
]),
|
||||
new Polygon([
|
||||
[
|
||||
[9, 9, 5],
|
||||
[9, 11, 6],
|
||||
[11, 11, 7],
|
||||
[11, 9, 8],
|
||||
[9, 9, 5],
|
||||
],
|
||||
]),
|
||||
])
|
||||
),
|
||||
];
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'Text',
|
||||
})
|
||||
);
|
||||
features.forEach(function (feature) {
|
||||
builder.drawText(feature.getGeometry(), feature);
|
||||
});
|
||||
expect(builder.coordinates).to.eql([
|
||||
0,
|
||||
0,
|
||||
1,
|
||||
1,
|
||||
2,
|
||||
2,
|
||||
3,
|
||||
3,
|
||||
4,
|
||||
4,
|
||||
5,
|
||||
5,
|
||||
6,
|
||||
6,
|
||||
7,
|
||||
7,
|
||||
8,
|
||||
8,
|
||||
9,
|
||||
9,
|
||||
10,
|
||||
10,
|
||||
]);
|
||||
});
|
||||
|
||||
it('renders polygon labels only when they fit', function () {
|
||||
let builder = createBuilder();
|
||||
const geometry = new Polygon([
|
||||
[
|
||||
[0, 0],
|
||||
[0, 1],
|
||||
[1, 1],
|
||||
[1, 0],
|
||||
[0, 0],
|
||||
],
|
||||
]);
|
||||
const feature = new Feature(geometry);
|
||||
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'This is a long text',
|
||||
})
|
||||
);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 0);
|
||||
|
||||
builder = createBuilder();
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'short',
|
||||
})
|
||||
);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 1);
|
||||
});
|
||||
|
||||
it('renders multipolygon labels only when they fit', function () {
|
||||
const geometry = new MultiPolygon([
|
||||
[
|
||||
[
|
||||
[0, 0],
|
||||
[0, 1],
|
||||
[1, 1],
|
||||
[1, 0],
|
||||
[0, 0],
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
[1, 1],
|
||||
[1, 2],
|
||||
[2, 2],
|
||||
[2, 1],
|
||||
[1, 1],
|
||||
],
|
||||
],
|
||||
]);
|
||||
const feature = new Feature(geometry);
|
||||
|
||||
let builder = createBuilder();
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'This is a long text',
|
||||
})
|
||||
);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 0);
|
||||
|
||||
builder = createBuilder();
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'short',
|
||||
})
|
||||
);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 2);
|
||||
});
|
||||
|
||||
it('generates a matching geometry widths array for multipolygons', function () {
|
||||
const feature = new Feature(
|
||||
new MultiPolygon([
|
||||
[
|
||||
[
|
||||
[-180, -90],
|
||||
[-180, 90],
|
||||
[-50, 90],
|
||||
[-50, -90],
|
||||
[-180, -90],
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
[-50, -90],
|
||||
[-50, 90],
|
||||
[70, 90],
|
||||
[70, -90],
|
||||
[-50, -90],
|
||||
],
|
||||
],
|
||||
[
|
||||
[
|
||||
[70, -90],
|
||||
[70, 90],
|
||||
[180, 90],
|
||||
[180, -90],
|
||||
[70, -90],
|
||||
],
|
||||
],
|
||||
])
|
||||
);
|
||||
const builder = new TextBuilder(1, [-50, -90, 70, 90], 1, 1);
|
||||
builder.setTextStyle(
|
||||
new Text({
|
||||
text: 'text',
|
||||
})
|
||||
);
|
||||
builder.drawText(feature.getGeometry(), feature);
|
||||
expect(builder.coordinates).to.have.length(2);
|
||||
expect(builder.instructions).to.have.length(3);
|
||||
const geometryWidths = builder.instructions[1][24];
|
||||
expect(geometryWidths).to.have.length(1);
|
||||
expect(geometryWidths[0]).to.be(120);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user