aboutsummaryrefslogtreecommitdiffstats
path: root/window-switcher/test_expose.js
blob: e8ef4592d74bfbb68e6b8d5e8e5eab93afe12c16 (plain)
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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Run: node window-switcher/test_expose.js
// Expose.js is a QML library, so its .pragma line is stripped before node
// evaluates it, as test_perf.js does for Perf.js.

const fs = require("fs");
const assert = require("assert");

const src = fs.readFileSync(__dirname + "/Expose.js", "utf8")
    .replace(/^\.pragma library$/m, "");
const { layout, step } = new Function(src + "\nreturn { layout, step };")();

const W = 2560, H = 1080;
const wide = { w: 2556, h: 1076 }, tall = { w: 1076, h: 1916 };

function apart(a, b) {
    return a.x + a.w <= b.x || b.x + b.w <= a.x || a.y + a.h <= b.y || b.y + b.h <= a.y;
}
function check(out) {
    for (const o of out) {
        assert.ok(o.x >= 0 && o.y >= 0 && o.x + o.w <= W + 1e-6 && o.y + o.h <= H + 1e-6, "inside the screen");
    }
    for (let i = 0; i < out.length; i++)
        for (let j = i + 1; j < out.length; j++)
            assert.ok(apart(out[i], out[j]), `windows ${i} and ${j} overlap`);
}

assert.deepStrictEqual(layout([], W, H), []);

// Seven identical full-screen windows, the normal case here: no overlap.
const seven = Array(7).fill(wide);
check(layout(seven, W, H));

// A busy mixed desk still separates within the pass limit.
check(layout(Array.from({ length: 14 }, (_, i) => i % 3 ? wide : tall), W, H));

// Deterministic: the same input gives the same scatter.
assert.deepStrictEqual(layout(seven, W, H), layout(seven, W, H));

// A lone window is capped at half size.
const one = layout([wide], W, H)[0];
assert.ok(Math.abs(one.w - wide.w * 0.5) < 1e-6);

// One scale for all: sizes stay relative.
const [a, b] = layout([wide, tall], W, H);
assert.ok(Math.abs(a.w / b.w - wide.w / tall.w) < 1e-9);
assert.ok(Math.abs(a.h / b.h - wide.h / tall.h) < 1e-9);

// Arrow selection: a 2x2 grid, plus one far off to the upper right.
const g = [{ x: 0, y: 0, w: 10, h: 10 }, { x: 20, y: 0, w: 10, h: 10 },
           { x: 0, y: 20, w: 10, h: 10 }, { x: 20, y: 20, w: 10, h: 10 },
           { x: 100, y: -60, w: 10, h: 10 }];
assert.strictEqual(step(g, -1, 1, 0), 0);  // top-left first
assert.strictEqual(step(g, 0, 1, 0), 1);   // level neighbour beats the far one
assert.strictEqual(step(g, 0, 0, 1), 2);
assert.strictEqual(step(g, 3, -1, 0), 2);
assert.strictEqual(step(g, 2, -1, 0), 2);  // nothing to the left: stays
assert.strictEqual(step([], -1, 1, 0), -1);

console.log("test_expose: ok");