forked from r03ert0/thresholdmann
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.mjs
395 lines (350 loc) · 17.2 KB
/
test.mjs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/* eslint-disable max-lines */
import assert from 'assert';
import { text, buffer } from 'node:stream/consumers';
import { chromium, firefox, webkit, expect } from '@playwright/test';
describe('Test Thresholdmann', () => {
const browsers = [
chromium,
firefox,
webkit
];
browsers.forEach((browser) => {
describe(`With ${browser.name()} browser`, () => {
let page;
let sdim;
before(async function () {
// eslint-disable-next-line no-invalid-this
this.timeout(5000);
browser = await browser.launch({headless: true});
page = await browser.newPage();
// page.on('console', (msg) => {
// console.log(`PAGE ${msg.type().toUpperCase()}: ${msg.text()}`);
// });
await page.setViewportSize({width: 1200, height: 800});
await page.goto('http://127.0.0.1:8080');
});
after(async () => {
await browser.close();
});
describe('Unit tests', () => {
describe('Trivial test', () => {
it('should say hey', async () => {
const res = await page.evaluate(() => window.location);
assert.ok(res.origin);
});
});
});
describe('End to end tests', () => {
describe('Load a file', () => {
it('should display title', async () => {
const title = await page.evaluate(() => document.title);
assert.strictEqual(title, 'Thresholdmann');
});
it('should display "Choose..." message', async () => {
const msg = await page.evaluate(() => document.querySelector('.box_input').innerText);
assert.ok(msg.trim().startsWith('Choose a .nii.gz or a .nii file or drag it here.'));
});
it('init with test nifti file', async () => {
const pathString = './img/bear_uchar.nii.gz';
const fileChooserPromise = page.waitForEvent('filechooser');
await page.click('#loadNifti');
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(pathString);
await page.waitForSelector('.viewer');
sdim = await page.evaluate(() => window.globals.mv.mri.s2v.sdim);
assert.ok(Array.isArray(sdim));
assert.strictEqual(sdim.length, 3);
}).timeout(10000);
});
describe('can change slice', () => {
it('check initial slice', async () => {
const initialSlice = sdim[0] / 2 | 0;
const slice = await page.evaluate(() => (
window.globals.mv.views[0].slice
));
assert.strictEqual(slice, initialSlice);
});
it('can change to another slice', async () => {
const targetSlice = sdim[0] * 3 / 4 | 0;
await page.evaluate((newSlice) => {
const slider = document.querySelector('input.slice');
slider.value = newSlice;
slider.dispatchEvent(new Event('input', { bubbles: true }));
}, targetSlice);
const slice = await page.evaluate(() => (
window.globals.mv.views[0].slice
));
assert.strictEqual(slice, targetSlice);
});
});
describe('can switch planes', () => {
it('can switch to coronal plane', async () => {
await page.click('.cor-btn');
const plane = await page.evaluate(() => window.globals.mv.views[0].plane);
assert.strictEqual(plane, 'cor');
});
it('can switch to axial plane', async () => {
await page.click('.axi-btn');
const plane = await page.evaluate(() => window.globals.mv.views[0].plane);
assert.strictEqual(plane, 'axi');
});
it('can switch to sagittal plane', async () => {
await page.click('.sag-btn');
const plane = await page.evaluate(() => window.globals.mv.views[0].plane);
assert.strictEqual(plane, 'sag');
});
});
describe('can change settings', () => {
it('change threshold direction to down', async () => {
await page.click('#direction .mui[title="SelectDown"]');
const direction = await page.evaluate(() => window.globals.selectedDirection);
assert.strictEqual(direction, 'SelectDown');
});
it('change threshold direction to up', async () => {
await page.click('#direction .mui[title="SelectUp"]');
const direction = await page.evaluate(() => window.globals.selectedDirection);
assert.strictEqual(direction, 'SelectUp');
});
it('switch to threshold value view', async () => {
await page.click('#overlay .mui[title="Threshold Value"]');
const overlay = await page.evaluate(() => window.globals.selectedOverlay);
assert.strictEqual(overlay, 'Threshold Value');
});
it('switch to threshold mask view', async () => {
await page.click('#overlay .mui[title="Threshold Mask"]');
const overlay = await page.evaluate(() => window.globals.selectedOverlay);
assert.strictEqual(overlay, 'Threshold Mask');
});
});
describe('can manage points', () => {
let canvasBoundingBox;
let points;
it('check initial point', async () => {
canvasBoundingBox = await page.evaluate(() => {
const { x, y, width, height } = document.querySelector('.viewer').getBoundingClientRect();
return { x, y, width, height };
});
points = await page.evaluate(() => window.globals.points);
assert.strictEqual(points.length, 1);
// point is in the middle
assert.ok(points[0].every((v, i) => v === Math.floor(sdim[i] / 2)));
});
it('can add a point', async () => {
await page.click('#tools .mui[title="Add"]');
const clickX = canvasBoundingBox.x + canvasBoundingBox.width / 3;
const clickY = canvasBoundingBox.y + canvasBoundingBox.height / 3;
await page.mouse.click(clickX, clickY);
points = await page.evaluate(() => window.globals.points);
assert.strictEqual(points.length, 2);
assert.ok(points[0][0] === points[1][0]);
assert.ok(points[0][1] > points[1][1]);
assert.ok(points[0][2] < points[1][2]);
});
it('can move a point', async () => {
await page.click('#tools .mui[title="Move"]');
const startX = canvasBoundingBox.x + canvasBoundingBox.width / 3;
const startY = canvasBoundingBox.y + canvasBoundingBox.height / 3;
const endX = canvasBoundingBox.x + canvasBoundingBox.width * 2 / 3;
const endY = canvasBoundingBox.y + canvasBoundingBox.height * 2 / 3;
await page.mouse.move(startX, startY);
await page.mouse.down();
await page.mouse.move(endX, endY);
await page.mouse.up();
points = await page.evaluate(() => window.globals.points);
assert.strictEqual(points.length, 2);
assert.ok(points[0][0] === points[1][0]);
assert.ok(points[0][1] < points[1][1]);
assert.ok(points[0][2] > points[1][2]);
});
it('when selecting the first point, the first threshold slider is selected', async () => {
await page.click('#tools .mui[title="Select"]');
const clickX = canvasBoundingBox.x + canvasBoundingBox.width / 2;
const clickY = canvasBoundingBox.y + canvasBoundingBox.height / 2;
await page.mouse.click(clickX, clickY);
const firstSliderSelected = await page.$eval('#control-table tbody tr:nth-child(1)', (row) => row.classList.contains('selected'));
const secondSliderSelected = await page.$eval('#control-table tbody tr:nth-child(2)', (row) => row.classList.contains('selected'));
assert.strictEqual(firstSliderSelected, true);
assert.strictEqual(secondSliderSelected, false);
});
it('when selecting the second point, the second threshold slider is selected', async () => {
await page.click('#tools .mui[title="Select"]');
const clickX = canvasBoundingBox.x + canvasBoundingBox.width * 2 / 3;
const clickY = canvasBoundingBox.y + canvasBoundingBox.height * 2 / 3;
await page.mouse.click(clickX, clickY);
const firstSliderSelected = await page.$eval('#control-table tbody tr:nth-child(1)', (row) => row.classList.contains('selected'));
const secondSliderSelected = await page.$eval('#control-table tbody tr:nth-child(2)', (row) => row.classList.contains('selected'));
assert.strictEqual(firstSliderSelected, false);
assert.strictEqual(secondSliderSelected, true);
});
it('when selecting the first threshold slider, the first point slice is displayed', async () => {
await page.click('.axi-btn');
await page.click('#control-table tbody tr:nth-child(1) td:first-child');
const sliderValue = await page.$eval('input.slice', (slider) => slider.value);
const slice = await page.evaluate(() => (window.globals.mv.views[0].slice));
assert.strictEqual(parseInt(sliderValue, 10), points[0][2]);
assert.strictEqual(slice, points[0][2]);
});
it('when selecting the second threshold slider, the second point slice is displayed', async () => {
await page.click('#control-table tbody tr:nth-child(2) td:first-child');
const sliderValue = await page.$eval('input.slice', (slider) => slider.value);
const slice = await page.evaluate(() => (window.globals.mv.views[0].slice));
assert.strictEqual(parseInt(sliderValue, 10), points[1][2]);
assert.strictEqual(slice, points[1][2]);
});
it('can remove a point', async () => {
await page.click('.sag-btn');
await page.click('#tools .mui[title="Remove"]');
const clickX = canvasBoundingBox.x + canvasBoundingBox.width * 2 / 3;
const clickY = canvasBoundingBox.y + canvasBoundingBox.height * 2 / 3;
await page.mouse.click(clickX, clickY);
points = await page.evaluate(() => window.globals.points);
assert.strictEqual(points.length, 1);
});
});
describe('can adjust threshold', () => {
it('check initial threshold', async () => {
const initThreshold = await page.evaluate(() => (
window.globals.values[0]
));
assert.strictEqual(initThreshold, 127);
});
it('can adjust threshold with slider', async () => {
const targetThreshold = 75;
const sliderElement = await page.$('#control-table tbody tr:first-child td.slider-val input');
await page.evaluate(({slider, value}) => {
slider.value = value;
slider.dispatchEvent(new Event('input', { bubbles: true }));
}, {slider: sliderElement, value: targetThreshold.toString()});
const newThreshold = await page.evaluate(() => (
window.globals.values[0]
));
assert.strictEqual(newThreshold, targetThreshold);
});
it('can adjust threshold with input', async () => {
const targetThreshold = 125;
const inputElement = await page.$('#control-table tbody tr:first-child td.text-val input');
await inputElement.fill(String(targetThreshold));
await inputElement.dispatchEvent('change');
const newThreshold = await page.evaluate(() => (
window.globals.values[0]
));
assert.strictEqual(newThreshold, targetThreshold);
});
});
describe('can adjust opacity, contrast and brightness', () => {
it('check initial values', async () => {
const {opacity, contrast, brightness} = await page.evaluate(() => ({
opacity: window.globals.alpha,
contrast: window.globals.contrast,
brightness: window.globals.brightness
}));
assert.strictEqual(opacity, 0.5);
assert.strictEqual(contrast, 1);
assert.strictEqual(brightness, 1);
});
it('can adjust opacity', async () => {
const targetOpacity = 40;
await page.evaluate((opacity) => {
const alphaSlider = document.querySelector('#image-adjust input[type="range"][oninput="changeAlpha(event)"]');
alphaSlider.value = opacity;
alphaSlider.dispatchEvent(new Event('input', { bubbles: true }));
}, targetOpacity);
const newOpacity = await page.evaluate(() => (
window.globals.alpha
));
assert.strictEqual(newOpacity, targetOpacity / 100);
});
it('can adjust contrast', async () => {
const targetContrast = 80;
await page.evaluate((contrast) => {
const contrastSlider = document.querySelector('#image-adjust input[type="range"][oninput="changeContrast(event)"]');
contrastSlider.value = contrast;
contrastSlider.dispatchEvent(new Event('input', { bubbles: true }));
}, targetContrast);
const newContrast = await page.evaluate(() => (
window.globals.contrast
));
assert.strictEqual(newContrast, targetContrast / 100);
});
it('can adjust brightness', async () => {
const targetBrightness = 120;
await page.evaluate((brightness) => {
const brightnessSlider = document.querySelector('#image-adjust input[type="range"][oninput="changeBrightness(event)"]');
brightnessSlider.value = brightness;
brightnessSlider.dispatchEvent(new Event('input', { bubbles: true }));
}, targetBrightness);
const newBrightness = await page.evaluate(() => (
window.globals.brightness
));
assert.strictEqual(newBrightness, targetBrightness / 100);
});
});
describe('3D render', () => {
let newPage;
it('can open the 3D volume view window', async () => {
const newPagePromise = page.context().waitForEvent('page');
await page.click('#render3D');
newPage = await newPagePromise;
const newPageUrl = newPage.url();
assert.strictEqual(newPageUrl, 'http://127.0.0.1:8080/render3D/index.html');
}).timeout(5000);
// after(async () => {
// if (newPage) {
// await newPage.close();
// }
// });
});
describe('Saving', () => {
it('Save Control Points', async () => {
page.on('dialog', async (dialog) => {
if (dialog.type() === 'prompt') {
await dialog.accept();
}
});
const downloadPromise = page.waitForEvent('download');
await page.click('#saveControlPoints');
const download = await downloadPromise;
const readStream = await download.createReadStream();
const downloadedJson = await text(readStream);
const controlPoints = JSON.parse(downloadedJson);
assert.deepStrictEqual(controlPoints, { points: [sdim.map((element) => element / 2 | 0)], values: [125] });
});
it('Save Mask', async () => {
page.on('dialog', async (dialog) => {
if (dialog.type() === 'prompt') {
await dialog.accept();
}
});
const downloadPromise = page.waitForEvent('download');
await page.click('#saveMask');
const download = await downloadPromise;
const readStream = await download.createReadStream();
const niftiBuffer = await buffer(readStream);
// check that dowloaded nifti is a gzip file
assert.deepStrictEqual(niftiBuffer.slice(0, 2), Buffer.from([0x1F, 0x8B]));
}).timeout(10000);
});
describe('Loading', () => {
it('Load Control Points', async () => {
page.setDefaultTimeout(120000);
const fileChooserPromise = page.waitForEvent('filechooser');
await page.click('#loadControlPoints');
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles({
name: 'control-points.json',
mimeType: 'application/json',
buffer: Buffer.from(JSON.stringify({ points: [[50, 50, 50]], values: [100]}), 'utf-8')
});
// wait for the new points to be loaded
await expect(page.locator('#control-table tbody tr:first-child td.text-val input')).toHaveValue('100', { timeout: 15000 });
const points = await page.evaluate(() => window.globals.points);
const threshold = await page.evaluate(() => window.globals.values[0]);
assert.strictEqual(points.length, 1);
assert.deepStrictEqual(points, [[50, 50, 50]]);
assert.strictEqual(threshold, 100);
}).timeout(300000);
});
});
});
});
});