tests: introduce randomness in mocks

This commit is contained in:
Peter Fajdiga
2024-09-20 16:26:38 +02:00
parent df587fc37b
commit dac1d488b7
2 changed files with 64 additions and 20 deletions

View File

@@ -90,28 +90,46 @@ class MockKwinClient {
public set fullScreen(fullScreen: boolean) {
const oldFullScreen = this._fullScreen;
this.hasBorder = !fullScreen;
this._fullScreen = fullScreen;
this.fullScreenChanged.fire();
if (oldFullScreen && !fullScreen) {
// when switching from full-screen to windowed, Kwin sometimes first adds the frame before changing the frameGeometry to the final value
this.frameGeometry = new MockQmlRect(
-MockKwinClient.borderThickness,
-MockKwinClient.borderThickness,
screenWidth + 2 * MockKwinClient.borderThickness,
screenHeight + 2 * MockKwinClient.borderThickness,
);
}
this.windowed = !fullScreen;
if (fullScreen) {
this.frameGeometry = new MockQmlRect(0, 0, screenWidth, screenHeight);
} else {
this.frameGeometry = this.windowedFrameGeometry;
}
runReorder(
() => {
this._fullScreen = fullScreen;
this.fullScreenChanged.fire();
},
() => {
if (oldFullScreen && !fullScreen) {
// when switching from full-screen to windowed, Kwin sometimes first adds the frame before changing the frameGeometry to the final value
runOneOf(
() => {
this.frameGeometry = new MockQmlRect(
0,
0,
screenWidth + 2 * MockKwinClient.borderThickness,
screenHeight + 2 * MockKwinClient.borderThickness,
);
},
() => {
this.frameGeometry = new MockQmlRect(
-MockKwinClient.borderThickness,
-MockKwinClient.borderThickness,
screenWidth + 2 * MockKwinClient.borderThickness,
screenHeight + 2 * MockKwinClient.borderThickness,
);
},
() => {},
);
}
},
() => {
this.windowed = !fullScreen;
if (fullScreen) {
this.frameGeometry = new MockQmlRect(0, 0, screenWidth, screenHeight);
} else {
this.frameGeometry = this.windowedFrameGeometry;
}
},
);
}
public get frameGeometry() {

26
src/tests/utils/random.ts Normal file
View File

@@ -0,0 +1,26 @@
function runOneOf(...fs: (() => void)[]) {
randomItem(fs)();
}
function runReorder(...fs: (() => void)[]) {
shuffle(fs);
for (const f of fs) {
f();
}
}
function randomInt(n: number) {
return Math.floor(Math.random() * n);
}
function randomItem<T>(items: T[]): T {
return items[randomInt(items.length)];
}
function shuffle(items: any[]) {
for (let n = items.length; n > 1; n--) {
const i = n-1;
const j = randomInt(n);
[items[i], items[j]] = [items[j], items[i]];
}
}