tests/utils/mocks: remove Mocks namespace

This commit is contained in:
Peter Fajdiga
2024-09-15 23:23:21 +02:00
parent d239ac24b3
commit 36bf942266
12 changed files with 233 additions and 253 deletions

View File

@@ -1,21 +1,21 @@
{
Qt = new Mocks.Qt();
const workspaceMock = new Mocks.Workspace();
Qt = new MockQt();
const workspaceMock = new MockWorkspace();
Workspace = workspaceMock;
const world = new World(getDefaultConfig());
const kwinClient = new Mocks.KwinClient(
const kwinClient = new MockKwinClient(
1,
"app1",
"Application 1",
new Mocks.QmlRect(0, 0, 200, 200),
new MockQmlRect(0, 0, 200, 200),
);
workspaceMock.createWindow(kwinClient);
kwinClient.fullScreen = true;
{
const frame = kwinClient.frameGeometry;
assert(frame.width === Mocks.screenWidth && frame.height === Mocks.screenHeight);
assert(frame.width === screenWidth && frame.height === screenHeight);
}
kwinClient.fullScreen = false;

View File

@@ -0,0 +1,2 @@
const screenWidth = 800;
const screenHeight = 600;

View File

@@ -1,113 +1,111 @@
namespace Mocks {
export class KwinClient {
private static readonly borderThickness = 10;
class MockKwinClient {
private static readonly borderThickness = 10;
public readonly shadeable: boolean = false;
public readonly minSize: Readonly<QmlSize> = new QmlSize(0, 0);
public readonly transient: boolean = false;
public readonly transientFor: KwinClient | null = null;
public readonly move: boolean = false;
public readonly resize: boolean = false;
public readonly moveable: boolean = false;
public readonly resizeable: boolean = false;
public readonly fullScreenable: boolean = false;
public readonly maximizable: boolean = false;
public readonly output: Output = false;
public readonly dock: boolean = false;
public readonly normalWindow: boolean = false;
public readonly managed: boolean = false;
public readonly popupWindow: boolean = false;
public readonly shadeable: boolean = false;
public readonly minSize: Readonly<QmlSize> = new MockQmlSize(0, 0);
public readonly transient: boolean = false;
public readonly transientFor: MockKwinClient | null = null;
public readonly move: boolean = false;
public readonly resize: boolean = false;
public readonly moveable: boolean = false;
public readonly resizeable: boolean = false;
public readonly fullScreenable: boolean = false;
public readonly maximizable: boolean = false;
public readonly output: Output = false;
public readonly dock: boolean = false;
public readonly normalWindow: boolean = false;
public readonly managed: boolean = false;
public readonly popupWindow: boolean = false;
private _fullScreen: boolean = false;
public activities: string[] = [];
public skipSwitcher: boolean = false;
public keepAbove: boolean = false;
public keepBelow: boolean = false;
public shade: boolean = false;
public minimized: boolean = false;
public desktops: KwinDesktop[] = [];
public tile: Tile = false;
public opacity: number = 1.0;
private _fullScreen: boolean = false;
public activities: string[] = [];
public skipSwitcher: boolean = false;
public keepAbove: boolean = false;
public keepBelow: boolean = false;
public shade: boolean = false;
public minimized: boolean = false;
public desktops: KwinDesktop[] = [];
public tile: Tile = false;
public opacity: number = 1.0;
public readonly fullScreenChanged: QSignal<[]> = new QSignal();
public readonly desktopsChanged: QSignal<[]> = new QSignal();
public readonly activitiesChanged: QSignal<[]> = new QSignal();
public readonly minimizedChanged: QSignal<[]> = new QSignal();
public readonly maximizedAboutToChange: QSignal<[MaximizedMode]> = new QSignal();
public readonly captionChanged: QSignal<[]> = new QSignal();
public readonly tileChanged: QSignal<[]> = new QSignal();
public readonly interactiveMoveResizeStarted: QSignal<[]> = new QSignal();
public readonly interactiveMoveResizeFinished: QSignal<[]> = new QSignal();
public readonly frameGeometryChanged: QSignal<[oldGeometry: QmlRect]> = new QSignal();
public readonly fullScreenChanged = new MockQSignal();
public readonly desktopsChanged = new MockQSignal();
public readonly activitiesChanged = new MockQSignal();
public readonly minimizedChanged = new MockQSignal();
public readonly maximizedAboutToChange = new MockQSignal<[MaximizedMode]>();
public readonly captionChanged = new MockQSignal();
public readonly tileChanged = new MockQSignal();
public readonly interactiveMoveResizeStarted = new MockQSignal();
public readonly interactiveMoveResizeFinished = new MockQSignal();
public readonly frameGeometryChanged = new MockQSignal<[oldGeometry: QmlRect]>();
private windowedFrameGeometry: QmlRect;
private windowed: boolean = false;
private windowedFrameGeometry: QmlRect;
private windowed: boolean = false;
constructor(
public readonly pid: number,
public readonly resourceClass: string,
public readonly caption: string,
private _frameGeometry: QmlRect,
) {
this.windowedFrameGeometry = _frameGeometry.clone();
constructor(
public readonly pid: number,
public readonly resourceClass: string,
public readonly caption: string,
private _frameGeometry: MockQmlRect,
) {
this.windowedFrameGeometry = _frameGeometry.clone();
}
setMaximize(vertically: boolean, horizontally: boolean) {
this.maximizedAboutToChange.fire(
vertically ? (
horizontally ? MaximizedMode.Maximized : MaximizedMode.Vertically
) : (
horizontally ? MaximizedMode.Horizontally : MaximizedMode.Unmaximized
)
);
}
public get clientGeometry() {
if (this._fullScreen) {
return this.frameGeometry;
}
setMaximize(vertically: boolean, horizontally: boolean) {
this.maximizedAboutToChange.fire(
vertically ? (
horizontally ? MaximizedMode.Maximized : MaximizedMode.Vertically
) : (
horizontally ? MaximizedMode.Horizontally : MaximizedMode.Unmaximized
)
);
}
return new MockQmlRect(
this.frameGeometry.x + MockKwinClient.borderThickness,
this.frameGeometry.y + MockKwinClient.borderThickness,
this.frameGeometry.width - 2 * MockKwinClient.borderThickness,
this.frameGeometry.height - 2 * MockKwinClient.borderThickness,
);
}
public get clientGeometry() {
if (this._fullScreen) {
return this.frameGeometry;
}
public get fullScreen() {
return this._fullScreen;
}
return new QmlRect(
this.frameGeometry.x + KwinClient.borderThickness,
this.frameGeometry.y + KwinClient.borderThickness,
this.frameGeometry.width - 2 * KwinClient.borderThickness,
this.frameGeometry.height - 2 * KwinClient.borderThickness,
);
}
public set fullScreen(fullScreen: boolean) {
this.windowed = !fullScreen;
this._fullScreen = fullScreen;
this.fullScreenChanged.fire();
public get fullScreen() {
return this._fullScreen;
}
public set fullScreen(fullScreen: boolean) {
this.windowed = !fullScreen;
this._fullScreen = fullScreen;
this.fullScreenChanged.fire();
if (fullScreen) {
this.frameGeometry = new QmlRect(0, 0, screenWidth, screenHeight);
} else {
this.frameGeometry = this.windowedFrameGeometry;
}
}
public get frameGeometry() {
return this._frameGeometry;
}
public set frameGeometry(frameGeometry: QmlRect) {
const oldFrameGeometry = this._frameGeometry;
this._frameGeometry = new QmlRect(
frameGeometry.x,
frameGeometry.y,
frameGeometry.width,
frameGeometry.height,
this.frameGeometryChanged.fire,
);
if (this.windowed) {
this.windowedFrameGeometry = this._frameGeometry.clone();
}
this.frameGeometryChanged.fire(oldFrameGeometry);
if (fullScreen) {
this.frameGeometry = new MockQmlRect(0, 0, screenWidth, screenHeight);
} else {
this.frameGeometry = this.windowedFrameGeometry;
}
}
public get frameGeometry() {
return this._frameGeometry;
}
public set frameGeometry(frameGeometry: QmlRect) {
const oldFrameGeometry = this._frameGeometry;
this._frameGeometry = new MockQmlRect(
frameGeometry.x,
frameGeometry.y,
frameGeometry.width,
frameGeometry.height,
this.frameGeometryChanged.fire,
);
if (this.windowed) {
this.windowedFrameGeometry = this._frameGeometry.clone();
}
this.frameGeometryChanged.fire(oldFrameGeometry);
}
}

View File

@@ -1,19 +1,17 @@
namespace Mocks {
export class QSignal<T extends unknown[]> {
private readonly handlers: Set<(...args: [...T]) => void> = new Set();
class MockQSignal<T extends unknown[]> {
private readonly handlers: Set<(...args: [...T]) => void> = new Set();
public connect(handler: (...args: [...T]) => void) {
this.handlers.add(handler);
};
public connect(handler: (...args: [...T]) => void) {
this.handlers.add(handler);
};
public disconnect(handler: (...args: [...T]) => void) {
this.handlers.delete(handler);
};
public disconnect(handler: (...args: [...T]) => void) {
this.handlers.delete(handler);
};
public fire(...args: [...T]) {
for (const handler of this.handlers) {
handler(...args);
}
public fire(...args: [...T]) {
for (const handler of this.handlers) {
handler(...args);
}
}
}

View File

@@ -1,8 +1,6 @@
namespace Mocks {
export class QmlPoint {
constructor(
public x: number,
public y: number,
) {}
}
class MockQmlPoint {
constructor(
public x: number,
public y: number,
) {}
}

View File

@@ -1,76 +1,74 @@
namespace Mocks {
export class QmlRect {
constructor(
private _x: number,
private _y: number,
private _width: number,
private _height: number,
private readonly onChanged: (oldRect: QmlRect) => void = () => {},
) {}
class MockQmlRect {
constructor(
private _x: number,
private _y: number,
private _width: number,
private _height: number,
private readonly onChanged: (oldRect: MockQmlRect) => void = () => {},
) {}
public get x() {
return this._x;
}
public get x() {
return this._x;
}
public set x(x: number) {
const oldRect = this.clone();
this._x = x;
this.onChanged(oldRect);
}
public set x(x: number) {
const oldRect = this.clone();
this._x = x;
this.onChanged(oldRect);
}
public get y() {
return this._y;
}
public get y() {
return this._y;
}
public set y(y: number) {
const oldRect = this.clone();
this._y = y;
this.onChanged(oldRect);
}
public set y(y: number) {
const oldRect = this.clone();
this._y = y;
this.onChanged(oldRect);
}
public get width() {
return this._width;
}
public get width() {
return this._width;
}
public set width(width: number) {
const oldRect = this.clone();
this._width = width;
this.onChanged(oldRect);
}
public set width(width: number) {
const oldRect = this.clone();
this._width = width;
this.onChanged(oldRect);
}
public get height() {
return this._height;
}
public get height() {
return this._height;
}
public set height(height: number) {
const oldRect = this.clone();
this._height = height;
this.onChanged(oldRect);
}
public set height(height: number) {
const oldRect = this.clone();
this._height = height;
this.onChanged(oldRect);
}
public get top() {
return this.y;
}
public get top() {
return this.y;
}
public get bottom() {
return this.y + this.height;
}
public get bottom() {
return this.y + this.height;
}
public get left() {
return this.x;
}
public get left() {
return this.x;
}
public get right() {
return this.x + this.width;
}
public get right() {
return this.x + this.width;
}
public clone() {
return new QmlRect(
this._x,
this._y,
this._width,
this._height,
);
}
public clone() {
return new MockQmlRect(
this._x,
this._y,
this._width,
this._height,
);
}
}

View File

@@ -1,8 +1,6 @@
namespace Mocks {
export class QmlSize {
constructor(
public width: number,
public height: number,
) {}
}
class MockQmlSize {
constructor(
public width: number,
public height: number,
) {}
}

View File

@@ -1,13 +1,11 @@
namespace Mocks {
export class QmlTimer {
public interval: number = 0;
public readonly triggered: QSignal<[]> = new QSignal();
class MockQmlTimer {
public interval = 0;
public readonly triggered = new MockQSignal();
public restart() {
// no need to wait in tests, just fire immediately
this.triggered.fire();
};
public restart() {
// no need to wait in tests, just fire immediately
this.triggered.fire();
};
public destroy() {}
}
public destroy() {}
}

View File

@@ -1,21 +1,19 @@
namespace Mocks {
export class Qt {
public point(x: number, y: number) {
return new Mocks.QmlPoint(x, y);
}
class MockQt {
public point(x: number, y: number) {
return new MockQmlPoint(x, y);
}
public rect(x: number, y: number, width: number, height: number) {
return new Mocks.QmlRect(x, y, width, height);
}
public rect(x: number, y: number, width: number, height: number) {
return new MockQmlRect(x, y, width, height);
}
public createQmlObject(qml: string, parent: QmlObject) {
if (qml.includes("Timer")) {
return new QmlTimer();
} else if (qml.includes("ShortcutHandler")) {
return new ShortcutHandler();
} else {
assert(false, "Unexpected qml string: " + qml);
}
public createQmlObject(qml: string, parent: QmlObject) {
if (qml.includes("Timer")) {
return new MockQmlTimer();
} else if (qml.includes("ShortcutHandler")) {
return new MockShortcutHandler();
} else {
assert(false, "Unexpected qml string: " + qml);
}
}
}

View File

@@ -1,7 +1,5 @@
namespace Mocks {
export class ShortcutHandler {
public readonly activated: QSignal<[]> = new QSignal();
class MockShortcutHandler {
public readonly activated: MockQSignal<[]> = new MockQSignal();
public destroy() {}
}
public destroy() {}
}

View File

@@ -1,31 +1,29 @@
namespace Mocks {
export class Workspace {
public activities = ["test-activity"];
public desktops = [{id: "desktop1"}, {id: "desktop2"}];
public currentDesktop = this.desktops[0];
public currentActivity = this.activities[0];
public activeScreen = {};
public windows = [];
public cursorPos = new QmlPoint(0, 0);
class MockWorkspace {
public activities = ["test-activity"];
public desktops = [{id: "desktop1"}, {id: "desktop2"}];
public currentDesktop = this.desktops[0];
public currentActivity = this.activities[0];
public activeScreen = {};
public windows = [];
public cursorPos = new MockQmlPoint(0, 0);
public activeWindow: any;
public activeWindow: any;
public readonly currentDesktopChanged = new Mocks.QSignal<[]>();
public readonly windowAdded = new Mocks.QSignal<[KwinClient]>();
public readonly windowRemoved = new Mocks.QSignal<[KwinClient]>();
public readonly windowActivated = new Mocks.QSignal<[KwinClient]>();
public readonly screensChanged = new Mocks.QSignal<[]>();
public readonly activitiesChanged = new Mocks.QSignal<[]>();
public readonly desktopsChanged = new Mocks.QSignal<[]>();
public readonly currentActivityChanged = new Mocks.QSignal<[]>();
public readonly virtualScreenSizeChanged = new Mocks.QSignal<[]>();
public readonly currentDesktopChanged = new MockQSignal<[]>();
public readonly windowAdded = new MockQSignal<[MockKwinClient]>();
public readonly windowRemoved = new MockQSignal<[MockKwinClient]>();
public readonly windowActivated = new MockQSignal<[MockKwinClient]>();
public readonly screensChanged = new MockQSignal<[]>();
public readonly activitiesChanged = new MockQSignal<[]>();
public readonly desktopsChanged = new MockQSignal<[]>();
public readonly currentActivityChanged = new MockQSignal<[]>();
public readonly virtualScreenSizeChanged = new MockQSignal<[]>();
public clientArea(option: ClientAreaOption, output: Output, kwinDesktop: KwinDesktop) {
return new QmlRect(0, 0, Mocks.screenWidth, Mocks.screenHeight);
}
public clientArea(option: ClientAreaOption, output: Output, kwinDesktop: KwinDesktop) {
return new MockQmlRect(0, 0, screenWidth, screenHeight);
}
public createWindow(kwinClient: KwinClient) {
this.windowActivated.fire(kwinClient);
}
public createWindow(kwinClient: MockKwinClient) {
this.windowActivated.fire(kwinClient);
}
}

View File

@@ -1,4 +0,0 @@
namespace Mocks {
export const screenWidth = 800;
export const screenHeight = 600;
}