Skip to content

Commit b92f764

Browse files
authored
Minor refactoring in the e2e infra (#2123)
1 parent 9326caf commit b92f764

File tree

9 files changed

+131
-110
lines changed

9 files changed

+131
-110
lines changed

example/src/Tests/deserialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const parseProp = (value: any, assets: Assets) => {
6666
return Skia.RuntimeEffect.Make(value.source);
6767
} else if (value.__typename__ === "SVG") {
6868
return Skia.SVG.MakeFromString(value.source);
69-
} else if (value.__typename__ === "Paragraph") {
69+
} else if (value.__typename__ === "SkiaObject") {
7070
// eslint-disable-next-line no-eval
7171
return eval(
7272
`(function Main(){return (${value.source})(this.Skia, this.ctx); })`

fabricexample/src/Tests/deserialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const parseProp = (value: any, assets: Assets) => {
6666
return Skia.RuntimeEffect.Make(value.source);
6767
} else if (value.__typename__ === "SVG") {
6868
return Skia.SVG.MakeFromString(value.source);
69-
} else if (value.__typename__ === "Paragraph") {
69+
} else if (value.__typename__ === "SkiaObject") {
7070
// eslint-disable-next-line no-eval
7171
return eval(
7272
`(function Main(){return (${value.source})(this.Skia, this.ctx); })`

package/src/renderer/__tests__/e2e/ParagraphPaint.spec.tsx

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from "react";
22

3-
import type { EvalContext } from "../setup";
43
import { importSkia, resolveFile, surface } from "../setup";
54
import { checkImage, docPath } from "../../../__tests__/setup";
65
import {
@@ -12,75 +11,13 @@ import {
1211
Paint,
1312
Paragraph,
1413
} from "../../components";
15-
import type {
16-
Skia,
17-
SkCanvas,
18-
SkParagraph,
19-
SkRectWithDirection,
20-
} from "../../../skia/types";
14+
15+
import { ParagraphAsset } from "./setup";
2116

2217
const RobotoMedium = Array.from(
2318
resolveFile("skia/__tests__/assets/Roboto-Medium.ttf")
2419
);
2520

26-
class ParagraphAsset<Ctx extends EvalContext> implements SkParagraph {
27-
private code: string;
28-
private paragraph: SkParagraph;
29-
constructor(
30-
Skia: Skia,
31-
fn: (Skia: Skia, ctx: Ctx) => SkParagraph,
32-
public context: Ctx = {} as Ctx
33-
) {
34-
this.code = `(Skia, ctx) => {
35-
return (${fn.toString()})(Skia, ctx);
36-
}`;
37-
this.paragraph = fn(Skia, context);
38-
}
39-
40-
layout(width: number) {
41-
this.paragraph.layout(width);
42-
}
43-
paint(canvas: SkCanvas, x: number, y: number) {
44-
this.paragraph.paint(canvas, x, y);
45-
}
46-
getHeight() {
47-
return this.paragraph.getHeight();
48-
}
49-
getMaxWidth() {
50-
return this.paragraph.getMaxWidth();
51-
}
52-
getMinIntrinsicWidth() {
53-
return this.paragraph.getMinIntrinsicWidth();
54-
}
55-
getMaxIntrinsicWidth() {
56-
return this.paragraph.getMaxIntrinsicWidth();
57-
}
58-
getLongestLine() {
59-
return this.paragraph.getLongestLine();
60-
}
61-
getGlyphPositionAtCoordinate(x: number, y: number) {
62-
return this.paragraph.getGlyphPositionAtCoordinate(x, y);
63-
}
64-
getRectsForRange(start: number, end: number) {
65-
return this.paragraph.getRectsForRange(start, end);
66-
}
67-
getLineMetrics() {
68-
return this.paragraph.getLineMetrics();
69-
}
70-
getRectsForPlaceholders(): SkRectWithDirection[] {
71-
return this.paragraph.getRectsForPlaceholders();
72-
}
73-
__typename__: "Paragraph" = "Paragraph" as const;
74-
75-
dispose(): void {
76-
this.paragraph.dispose();
77-
}
78-
79-
source() {
80-
return this.code;
81-
}
82-
}
83-
8421
describe("Paragraphs", () => {
8522
it("Should use shader for the foreground and background", async () => {
8623
const img = await surface.drawOffscreen(

package/src/renderer/__tests__/e2e/SVG.spec.tsx

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,8 @@ import {
1414
Paint,
1515
fitbox,
1616
} from "../../components";
17-
import type { SkSVG } from "../../../skia/types";
1817

19-
// Because SkSVG doesn't exist on web,
20-
// this instance is just to send the svg over the wire
21-
class SVGAsset implements SkSVG {
22-
__typename__ = "SVG" as const;
23-
constructor(
24-
private _source: string,
25-
private _width: number,
26-
private _height: number
27-
) {}
28-
29-
dispose() {}
30-
31-
source() {
32-
return this._source;
33-
}
34-
35-
width() {
36-
return this._width;
37-
}
38-
39-
height() {
40-
return this._height;
41-
}
42-
}
18+
import { SVGAsset } from "./setup";
4319

4420
const circle = new SVGAsset(
4521
`<svg viewBox='0 0 20 20' width="20" height="20" xmlns='http://www.w3.org/2000/svg'>
@@ -55,21 +31,13 @@ const tiger = new SVGAsset(
5531
800
5632
);
5733

58-
const svgWithoutSize = {
59-
__typename__: "SVG" as const,
60-
width() {
61-
return -1;
62-
},
63-
height() {
64-
return -1;
65-
},
66-
dispose() {},
67-
source() {
68-
return `<svg viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'>
69-
<circle cx='10' cy='10' r='10' fill='#00FFFF'/>
70-
</svg>`;
71-
},
72-
};
34+
const svgWithoutSize = new SVGAsset(
35+
`<svg viewBox='0 0 20 20' xmlns='http://www.w3.org/2000/svg'>
36+
<circle cx='10' cy='10' r='10' fill='#00FFFF'/>
37+
</svg>`,
38+
-1,
39+
-1
40+
);
7341

7442
describe("Displays SVGs", () => {
7543
itRunsE2eOnly(
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { SkParagraph, Skia, SkCanvas } from "../../../../skia/types";
2+
import type { EvalContext } from "../../setup";
3+
4+
import { SkiaObject } from "./SkiaObject";
5+
6+
export class ParagraphAsset<Ctx extends EvalContext>
7+
extends SkiaObject<Ctx, SkParagraph>
8+
implements SkParagraph
9+
{
10+
constructor(
11+
Skia: Skia,
12+
fn: (Skia: Skia, ctx: Ctx) => SkParagraph,
13+
context: Ctx = {} as Ctx
14+
) {
15+
super(Skia, fn, context);
16+
}
17+
18+
layout(width: number) {
19+
this.instance.layout(width);
20+
}
21+
paint(canvas: SkCanvas, x: number, y: number) {
22+
this.instance.paint(canvas, x, y);
23+
}
24+
getHeight() {
25+
return this.instance.getHeight();
26+
}
27+
getMaxWidth() {
28+
return this.instance.getMaxWidth();
29+
}
30+
getMinIntrinsicWidth() {
31+
return this.instance.getMinIntrinsicWidth();
32+
}
33+
getMaxIntrinsicWidth() {
34+
return this.instance.getMaxIntrinsicWidth();
35+
}
36+
getLongestLine() {
37+
return this.instance.getLongestLine();
38+
}
39+
getGlyphPositionAtCoordinate(x: number, y: number) {
40+
return this.instance.getGlyphPositionAtCoordinate(x, y);
41+
}
42+
getRectsForRange(start: number, end: number) {
43+
return this.instance.getRectsForRange(start, end);
44+
}
45+
getLineMetrics() {
46+
return this.instance.getLineMetrics();
47+
}
48+
getRectsForPlaceholders() {
49+
return this.instance.getRectsForPlaceholders();
50+
}
51+
__typename__ = "Paragraph" as const;
52+
53+
dispose(): void {
54+
this.instance.dispose();
55+
}
56+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Because SkSVG doesn't exist on web,
2+
3+
import type { SkSVG } from "../../../../skia/types";
4+
5+
// this instance is just to send the svg over the wire
6+
export class SVGAsset implements SkSVG {
7+
__typename__ = "SVG" as const;
8+
constructor(
9+
private _source: string,
10+
private _width: number,
11+
private _height: number
12+
) {}
13+
14+
dispose() {}
15+
16+
source() {
17+
return this._source;
18+
}
19+
20+
width() {
21+
return this._width;
22+
}
23+
24+
height() {
25+
return this._height;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { SkJSIInstance, Skia } from "../../../../skia/types";
2+
import type { EvalContext } from "../../setup";
3+
4+
export abstract class SkiaObject<
5+
Ctx extends EvalContext,
6+
R extends SkJSIInstance<string>
7+
> {
8+
protected _source: string;
9+
protected instance: R;
10+
11+
constructor(
12+
Skia: Skia,
13+
fn: (Skia: Skia, ctx: Ctx) => R,
14+
public _context: Ctx
15+
) {
16+
this._source = fn.toString();
17+
this.instance = fn(Skia, _context);
18+
}
19+
20+
get source() {
21+
return this._source;
22+
}
23+
24+
get context() {
25+
return this._context;
26+
}
27+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./Paragraph";
2+
export * from "./SVG";
3+
export * from "./SkiaObject";

package/src/renderer/__tests__/setup.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { SkiaRoot } from "../Reconciler";
1818
import { JsiDrawingContext } from "../../dom/types/DrawingContext";
1919
import { LoadSkiaWeb } from "../../web/LoadSkiaWeb";
2020

21+
import type { SkiaObject } from "./e2e/setup";
22+
2123
jest.setTimeout(180 * 1000);
2224

2325
type TestOS = "ios" | "android" | "web" | "node";
@@ -285,10 +287,11 @@ const serializeSkOjects = (obj: any): any => {
285287
source: obj.source(),
286288
};
287289
} else if (obj.__typename__ === "Paragraph") {
290+
const skObj: SkiaObject<EvalContext, any> = obj;
288291
return {
289-
__typename__: "Paragraph",
290-
source: obj.source(),
291-
context: obj.context,
292+
__typename__: "SkiaObject",
293+
source: skObj.source,
294+
context: skObj.context,
292295
};
293296
}
294297
}

0 commit comments

Comments
 (0)