Improve text replay test
This commit is contained in:
@@ -2,49 +2,87 @@ import Feature from '../../../../../src/ol/Feature.js';
|
||||
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
|
||||
import Polygon from '../../../../../src/ol/geom/Polygon.js';
|
||||
import CanvasTextReplay from '../../../../../src/ol/render/canvas/TextBuilder.js';
|
||||
import InstructionExecutor from '../../../../../src/ol/render/canvas/InstructionsExecutor.js';
|
||||
import Text from '../../../../../src/ol/style/Text.js';
|
||||
import {create as createTransform} from '../../../../../src/ol/transform.js';
|
||||
|
||||
function createBuilder() {
|
||||
return new CanvasTextReplay(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, expectedReplayImageCalls) {
|
||||
const transform = createTransform();
|
||||
const context = createContext();
|
||||
const executor = new InstructionExecutor(1, [-180, -90, 180, 90], 0.02, 1, null);
|
||||
sinon.spy(executor, 'drawTextImageWithPointPlacement_');
|
||||
const replayImageStub = sinon.stub(executor, 'replayImage_');
|
||||
executor.replaceInstructions(builder.finish());
|
||||
executor.execute(context, transform);
|
||||
expect(executor.drawTextImageWithPointPlacement_.callCount).to.be(expectedDrawTextImageCalls);
|
||||
expect(replayImageStub.callCount).to.be(expectedReplayImageCalls);
|
||||
}
|
||||
|
||||
describe('ol.render.canvas.TextReplay', function() {
|
||||
|
||||
it('always build rendering instructions for polygon labels', function() {
|
||||
const replay = new CanvasTextReplay(1, [-180, -90, 180, 90], 0.02, 1, true);
|
||||
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);
|
||||
|
||||
replay.setTextStyle(new Text({
|
||||
builder.setTextStyle(new Text({
|
||||
text: 'This is a long text'
|
||||
}));
|
||||
replay.drawText(geometry, feature);
|
||||
expect(replay.instructions.length).to.be(3);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 0);
|
||||
|
||||
replay.instructions.length = 0;
|
||||
replay.setTextStyle(new Text({
|
||||
|
||||
builder = createBuilder();
|
||||
builder.setTextStyle(new Text({
|
||||
text: 'short'
|
||||
}));
|
||||
replay.drawText(geometry, feature);
|
||||
expect(replay.instructions.length).to.be(3);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 1);
|
||||
});
|
||||
|
||||
it('always build rendering instructinos for multipolygon labels', function() {
|
||||
const replay = new CanvasTextReplay(1, [-180, -90, 180, 90], 0.02, 1, true);
|
||||
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);
|
||||
|
||||
replay.setTextStyle(new Text({
|
||||
let builder = createBuilder();
|
||||
builder.setTextStyle(new Text({
|
||||
text: 'This is a long text'
|
||||
}));
|
||||
replay.drawText(geometry, feature);
|
||||
expect(replay.instructions.length).to.be(3);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 0);
|
||||
|
||||
replay.instructions.length = 0;
|
||||
replay.setTextStyle(new Text({
|
||||
builder = createBuilder();
|
||||
builder.setTextStyle(new Text({
|
||||
text: 'short'
|
||||
}));
|
||||
replay.drawText(geometry, feature);
|
||||
expect(replay.instructions.length).to.be(3);
|
||||
builder.drawText(geometry, feature);
|
||||
expect(builder.instructions.length).to.be(3);
|
||||
executeInstructions(builder, 1, 2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user