Improve text replay test

This commit is contained in:
Guillaume Beraudo
2018-11-15 20:02:34 +01:00
parent eaed37da96
commit da92b2ab3f
+56 -18
View File
@@ -2,49 +2,87 @@ import Feature from '../../../../../src/ol/Feature.js';
import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js'; import MultiPolygon from '../../../../../src/ol/geom/MultiPolygon.js';
import Polygon from '../../../../../src/ol/geom/Polygon.js'; import Polygon from '../../../../../src/ol/geom/Polygon.js';
import CanvasTextReplay from '../../../../../src/ol/render/canvas/TextBuilder.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 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() { describe('ol.render.canvas.TextReplay', function() {
it('always build rendering instructions for polygon labels', function() { it('renders polygon labels only when they fit', function() {
const replay = new CanvasTextReplay(1, [-180, -90, 180, 90], 0.02, 1, true); let builder = createBuilder();
const geometry = new Polygon([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]); const geometry = new Polygon([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]);
const feature = new Feature(geometry); const feature = new Feature(geometry);
replay.setTextStyle(new Text({ builder.setTextStyle(new Text({
text: 'This is a long text' text: 'This is a long text'
})); }));
replay.drawText(geometry, feature); builder.drawText(geometry, feature);
expect(replay.instructions.length).to.be(3); 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' text: 'short'
})); }));
replay.drawText(geometry, feature); builder.drawText(geometry, feature);
expect(replay.instructions.length).to.be(3); expect(builder.instructions.length).to.be(3);
executeInstructions(builder, 1, 1);
}); });
it('always build rendering instructinos for multipolygon labels', function() { it('renders multipolygon labels only when they fit', function() {
const replay = new CanvasTextReplay(1, [-180, -90, 180, 90], 0.02, 1, true);
const geometry = new MultiPolygon([ const geometry = new MultiPolygon([
[[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]], [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
[[[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]] [[[1, 1], [1, 2], [2, 2], [2, 1], [1, 1]]]
]); ]);
const feature = new Feature(geometry); const feature = new Feature(geometry);
replay.setTextStyle(new Text({ let builder = createBuilder();
builder.setTextStyle(new Text({
text: 'This is a long text' text: 'This is a long text'
})); }));
replay.drawText(geometry, feature); builder.drawText(geometry, feature);
expect(replay.instructions.length).to.be(3); expect(builder.instructions.length).to.be(3);
executeInstructions(builder, 1, 0);
replay.instructions.length = 0; builder = createBuilder();
replay.setTextStyle(new Text({ builder.setTextStyle(new Text({
text: 'short' text: 'short'
})); }));
replay.drawText(geometry, feature); builder.drawText(geometry, feature);
expect(replay.instructions.length).to.be(3); expect(builder.instructions.length).to.be(3);
executeInstructions(builder, 1, 2);
}); });
}); });