Repository navigation
Expand file tree
/
Copy pathdesktop-layer.test.js
More file actions
77 lines (77 loc) · 2.34 KB
/
Copy pathdesktop-layer.test.js
File metadata and controls
77 lines (77 loc) · 2.34 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
const { test } = require("node:test");
const assert = require("node:assert/strict");
const vm = require("node:vm");
const fs = require("node:fs");
const path = require("node:path");
function fixture(refuseParent = false) {
let parent = null,
alive = true,
bounds = { x: 110, y: 220, width: 520, height: 740 };
const functions = {
FindWindowW: () => 100,
FindWindowExW: (_p, _a, cls) => (cls === "SHELLDLL_DefView" ? 200 : null),
SendMessageTimeoutW: () => 1,
SetParent: (_child, value) => {
if (!refuseParent) parent = value;
},
GetParent: () => parent,
SetWindowPos: () => 1,
GetWindowLongW: () => 0,
SetWindowLongW: () => 0,
IsWindow: (hwnd) => (hwnd === 100 ? alive : true),
GetWindowRect: (_h, rect) =>
Object.assign(rect, { left: 10, top: 20, right: 1930, bottom: 1100 }),
};
const context = vm.createContext({
module: { exports: {} },
require: () => ({
struct: () => ({}),
load: () => ({
func: (signature) =>
functions[signature.match(/(?:__stdcall )?(\w+)\(/)[1]],
}),
}),
console: { log: () => {} },
Date,
});
vm.runInContext(
fs.readFileSync(path.join(__dirname, "../desktop-layer.js"), "utf8"),
context,
);
const handle = Buffer.alloc(8);
handle.writeBigUInt64LE(500n);
const window = {
isDestroyed: () => false,
getNativeWindowHandle: () => handle,
getBounds: () => ({ ...bounds }),
setBounds: (value) => {
bounds = value;
},
};
return {
layer: context.module.exports,
window,
parent: () => parent,
bounds: () => bounds,
loseContainer: () => {
alive = false;
bounds = { ...bounds, x: 100, y: 200 };
},
};
}
test("lost desktop container restores the surviving window at its screen position", () => {
const f = fixture();
assert.equal(f.layer.attach(f.window), true);
f.loseContainer();
assert.equal(f.layer.ensureAttached(f.window), "container-lost");
assert.equal(f.layer.isAttached(), false);
assert.equal(f.parent(), null);
assert.equal(f.bounds().x, 110);
assert.equal(f.bounds().y, 220);
});
test("failed SetParent does not report a successful desktop attachment", () => {
const f = fixture(true);
assert.throws(() => f.layer.attach(f.window), /无法挂入/);
assert.equal(f.layer.isAttached(), false);
assert.equal(f.bounds().x, 110);
});