-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathImageData.spec.ts
More file actions
157 lines (155 loc) · 5.41 KB
/
Copy pathImageData.spec.ts
File metadata and controls
157 lines (155 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import fs from "fs";
import path from "path";
import {
checkImage,
client,
encodeImage,
decodeImage,
itSkipsOnWeb,
} from "./setup";
describe("Image Bitmap", () => {
it("createImageBitmap (1)", async () => {
const bitmap = await decodeImage(
path.join(__dirname, "./assets/Di-3d.png"),
);
const image = encodeImage(bitmap);
checkImage(image, "snapshots/ref.png");
});
it("createImageBitmap (2)", async () => {
const bitmap = await decodeImage(
path.join(__dirname, "./assets/Di-3d.png"),
);
const result = await client.eval(
({ bitmap: bmp }) => {
return bmp;
},
{ bitmap },
);
const image = encodeImage(result);
checkImage(image, "snapshots/ref.png");
});
// The following tests exercise the React Native ArrayBuffer/TypedArray
// overload of createImageBitmap, which is not part of the standard web API.
itSkipsOnWeb("createImageBitmap from ArrayBuffer", async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const expected = decodeImage(path.join(__dirname, "./assets/Di-3d.png"));
const result = await client.eval(
({ pngData }) => {
const bytes = new Uint8Array(pngData);
return createImageBitmap(bytes.buffer).then((bmp) => {
return { width: bmp.width, height: bmp.height };
});
},
{ pngData: pngBytes },
);
expect(result.width).toBe(expected.width);
expect(result.height).toBe(expected.height);
});
itSkipsOnWeb("createImageBitmap from Uint8Array", async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const expected = decodeImage(path.join(__dirname, "./assets/Di-3d.png"));
const result = await client.eval(
({ pngData }) => {
const bytes = new Uint8Array(pngData);
return createImageBitmap(bytes).then((bmp) => {
return { width: bmp.width, height: bmp.height };
});
},
{ pngData: pngBytes },
);
expect(result.width).toBe(expected.width);
expect(result.height).toBe(expected.height);
});
itSkipsOnWeb("ImageBitmap.close() zeroes width and height", async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const expected = decodeImage(path.join(__dirname, "./assets/Di-3d.png"));
const result = await client.eval(
({ pngData }) => {
const bytes = new Uint8Array(pngData);
return createImageBitmap(bytes.buffer).then((bmp) => {
const before = { width: bmp.width, height: bmp.height };
bmp.close();
const after = { width: bmp.width, height: bmp.height };
return { before, after };
});
},
{ pngData: pngBytes },
);
expect(result.before.width).toBe(expected.width);
expect(result.before.height).toBe(expected.height);
expect(result.after.width).toBe(0);
expect(result.after.height).toBe(0);
});
itSkipsOnWeb("ImageBitmap.close() is idempotent", async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const result = await client.eval(
({ pngData }) => {
const bytes = new Uint8Array(pngData);
return createImageBitmap(bytes.buffer).then((bmp) => {
bmp.close();
bmp.close();
return { width: bmp.width, height: bmp.height };
});
},
{ pngData: pngBytes },
);
expect(result.width).toBe(0);
expect(result.height).toBe(0);
});
itSkipsOnWeb("ImageBitmap exposes close as a function", async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const result = await client.eval(
({ pngData }) => {
const bytes = new Uint8Array(pngData);
return createImageBitmap(bytes.buffer).then((bmp) => {
return typeof bmp.close;
});
},
{ pngData: pngBytes },
);
expect(result).toBe("function");
});
itSkipsOnWeb(
"createImageBitmap from Uint8Array subarray (byteOffset/byteLength)",
async () => {
const pngBytes = Array.from(
fs.readFileSync(path.join(__dirname, "./assets/Di-3d.png")),
);
const expected = decodeImage(path.join(__dirname, "./assets/Di-3d.png"));
const result = await client.eval(
({ pngData }) => {
// Embed PNG bytes at an offset within a larger buffer
const padding = 128;
const totalLength = padding + pngData.length + padding;
const largeBuffer = new ArrayBuffer(totalLength);
const fullView = new Uint8Array(largeBuffer);
// Fill with garbage bytes
fullView.fill(0xff);
// Copy PNG bytes into the middle
const pngView = new Uint8Array(largeBuffer, padding, pngData.length);
for (let i = 0; i < pngData.length; i++) {
pngView[i] = pngData[i];
}
// createImageBitmap must respect byteOffset/byteLength of the view,
// not use the full underlying ArrayBuffer (which has garbage padding)
return createImageBitmap(pngView).then((bmp) => {
return { width: bmp.width, height: bmp.height };
});
},
{ pngData: pngBytes },
);
expect(result.width).toBe(expected.width);
expect(result.height).toBe(expected.height);
},
);
});