Skip to content

Commit ed16251

Browse files
committed
tests
1 parent 2e88f54 commit ed16251

File tree

7 files changed

+77
-6
lines changed

7 files changed

+77
-6
lines changed

packages/o-spreadsheet-engine/src/helpers/text_helper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ export function sliceTextToFitWidth(
438438
return slicedText ? slicedText + ellipsis : "";
439439
}
440440

441+
/**
442+
* Return the position to draw text on a rotated canvas to ensure that the rotated text alignment correspond
443+
* with to original's text vertical and horizontal alignment.
444+
*/
441445
export function computeRotationPosition(rect: BoxTextContent, style: Style): PixelPosition {
442446
if (!style.rotation) return rect;
443447
let { x, y } = rect;

tests/figures/chart/gauge/gauge_rendering.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ describe("Gauge rendering config", () => {
5252
expect(config.width).toEqual(testChartRect.width);
5353
});
5454

55-
/* In the following test, textPosition.y is expected to be NaN as the vertical position of the
55+
/* In the following test, textPosition.y is expected to be 23 as the vertical position of the
5656
title is computed according to the title height, and fontBoundingBoxAscent and
57-
fontBoundingBoxDescent are not implemented by js-dom so not available here.
57+
fontBoundingBoxDescent are not implemented by js-dom so not available here, they are mocked to set values.
5858
*/
5959
test("Chart title", () => {
6060
expect(getRenderingConfig(testRuntime).title).toEqual({
6161
label: testRuntime.title.text,
6262
fontSize: CHART_TITLE_FONT_SIZE,
6363
textPosition: {
6464
x: CHART_PADDING,
65-
y: NaN,
65+
y: 23,
6666
},
6767
color: chartMutedFontColor(testRuntime.background),
6868
});

tests/formats/formatting_plugin.test.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import { functionRegistry } from "@odoo/o-spreadsheet-engine/functions/function_
1515
import { toScalar } from "@odoo/o-spreadsheet-engine/functions/helper_matrices";
1616
import { toString } from "@odoo/o-spreadsheet-engine/functions/helpers";
1717
import { Model } from "@odoo/o-spreadsheet-engine/model";
18-
import { fontSizeInPixels, getCellContentHeight, toCartesian } from "../../src/helpers";
18+
import {
19+
fontSizeInPixels,
20+
getCellContentHeight,
21+
positionToZone,
22+
toCartesian,
23+
} from "../../src/helpers";
1924
import { CommandResult, Format, SetDecimalStep, UID } from "../../src/types";
2025
import {
2126
createSheet,
@@ -824,4 +829,58 @@ describe("Autoresize", () => {
824829
expect(getCellContent(model, "A1")).toEqual("-7");
825830
expect(model.getters.getRowSize(sheetId, 0)).toEqual(initialSize);
826831
});
832+
833+
test.each([
834+
0,
835+
-Math.PI / 2,
836+
-Math.PI / 3,
837+
-Math.PI / 4,
838+
0,
839+
Math.PI / 4,
840+
Math.PI / 3,
841+
Math.PI / 2,
842+
])("Autoresize work with rotated text %s", (rotation) => {
843+
model.dispatch("SET_FORMATTING", {
844+
sheetId,
845+
target: [positionToZone(toCartesian("A2"))],
846+
style: { rotation, fontSize: 20 },
847+
});
848+
849+
const cos = Math.abs(Math.cos(rotation));
850+
const sin = Math.abs(Math.sin(rotation));
851+
let width, height;
852+
853+
setCellContent(model, "A1", "ABC");
854+
({ width, height } = model.getters.getMultilineTextSize(["ABC"], { rotation, fontSize: 20 }));
855+
model.dispatch("AUTORESIZE_COLUMNS", { sheetId, cols: [0] });
856+
expect(model.getters.getColSize(sheetId, 0)).toEqual(
857+
cos * width + sin * height + 2 * PADDING_AUTORESIZE_HORIZONTAL
858+
);
859+
model.dispatch("AUTORESIZE_ROWS", { sheetId, rows: [0] });
860+
expect(model.getters.getRowSize(sheetId, 0)).toEqual(
861+
sin * width + cos * height + 2 * PADDING_AUTORESIZE_VERTICAL
862+
);
863+
864+
setCellContent(model, "A1", "ABC\n123");
865+
({ width, height } = model.getters.getMultilineTextSize(["ABC", "123"], { rotation }));
866+
model.dispatch("AUTORESIZE_COLUMNS", { sheetId, cols: [0] });
867+
expect(model.getters.getColSize(sheetId, 0)).toEqual(
868+
cos * width + sin * height + 2 * PADDING_AUTORESIZE_HORIZONTAL
869+
);
870+
model.dispatch("AUTORESIZE_ROWS", { sheetId, rows: [0] });
871+
expect(model.getters.getRowSize(sheetId, 0)).toEqual(
872+
sin * width + cos * height + 2 * PADDING_AUTORESIZE_VERTICAL
873+
);
874+
875+
setCellContent(model, "A1", "ABC-123");
876+
({ width, height } = model.getters.getMultilineTextSize(["ABC-123"], { rotation }));
877+
model.dispatch("AUTORESIZE_COLUMNS", { sheetId, cols: [0] });
878+
expect(model.getters.getColSize(sheetId, 0)).toEqual(
879+
cos * width + sin * height + 2 * PADDING_AUTORESIZE_HORIZONTAL
880+
);
881+
model.dispatch("AUTORESIZE_ROWS", { sheetId, rows: [0] });
882+
expect(model.getters.getRowSize(sheetId, 0)).toEqual(
883+
sin * width + cos * height + 2 * PADDING_AUTORESIZE_VERTICAL
884+
);
885+
});
827886
});

tests/renderer/cell_animations.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ beforeEach(() => {
6868
rendererStore = container.get(RendererStore);
6969
jest
7070
.spyOn(MockCanvasRenderingContext2D.prototype, "measureText")
71-
.mockImplementation((text: string) => ({ width: text.length }));
71+
.mockImplementation((text: string) => ({
72+
width: text.length,
73+
fontBoundingBoxAscent: 1,
74+
fontBoundingBoxDescent: 1,
75+
}));
7276

7377
const ctx = new MockGridRenderingContext(model, 1000, 1000, {});
7478
drawGrid = () => rendererStore.draw(ctx);

tests/renderer/renderer_store.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import { makeStoreWithModel } from "../test_helpers/stores";
5656
MockCanvasRenderingContext2D.prototype.measureText = function (text: string) {
5757
return {
5858
width: text.length,
59+
fontBoundingBoxAscent: 1,
60+
fontBoundingBoxDescent: 1,
5961
};
6062
};
6163

tests/setup/canvas.mock.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export class MockCanvasRenderingContext2D {
2424
const fontSize = getContextFontSize(this.font);
2525
return {
2626
width: fontSize * text.length || 0,
27+
fontBoundingBoxAscent: fontSize / 2,
28+
fontBoundingBoxDescent: fontSize / 2,
2729
};
2830
}
2931
drawImage() {}

tests/test_helpers/renderer_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GridRenderingContext, Viewport, Zone } from "../../src/types";
44
import { MockCanvasRenderingContext2D } from "../setup/canvas.mock";
55

66
MockCanvasRenderingContext2D.prototype.measureText = function () {
7-
return { width: 100 };
7+
return { width: 100, fontBoundingBoxAscent: 1, fontBoundingBoxDescent: 1 };
88
};
99

1010
interface ContextObserver {

0 commit comments

Comments
 (0)