From dac1d488b7733b95c4f3af7307ce81f6eac0ba7d Mon Sep 17 00:00:00 2001 From: Peter Fajdiga Date: Fri, 20 Sep 2024 16:26:38 +0200 Subject: [PATCH] tests: introduce randomness in mocks --- src/tests/utils/mocks/MockKwinClient.ts | 58 ++++++++++++++++--------- src/tests/utils/random.ts | 26 +++++++++++ 2 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 src/tests/utils/random.ts diff --git a/src/tests/utils/mocks/MockKwinClient.ts b/src/tests/utils/mocks/MockKwinClient.ts index 5722a84..27c767f 100644 --- a/src/tests/utils/mocks/MockKwinClient.ts +++ b/src/tests/utils/mocks/MockKwinClient.ts @@ -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() { diff --git a/src/tests/utils/random.ts b/src/tests/utils/random.ts new file mode 100644 index 0000000..377dd4f --- /dev/null +++ b/src/tests/utils/random.ts @@ -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(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]]; + } +}