add testing facility to the project

This commit is contained in:
Eon S. Jeon 2019-04-03 23:57:45 +09:00
parent 1a3c95208a
commit 588ec48269
5 changed files with 293 additions and 40 deletions

10
.gitignore vendored
View File

@ -1,3 +1,9 @@
/pkg/
/node_modules/
/krohnkite.js
/package-lock.json
/package.json
*.kwinscript
script.js
pkg/
*.bak

View File

@ -1,68 +1,82 @@
PACKAGE_NAME = krohnkite
PACKAGE_VER = 0.6
PACKAGE_FILE = $(PACKAGE_NAME)-$(PACKAGE_VER).kwinscript
PROJECT_NAME = krohnkite
PROJECT_VER = 0.6
PROJECT_REV = $(shell git rev-parse HEAD | cut -b-7)
PACKAGE_DIR = pkg
KWINPKG_FILE = $(PROJECT_NAME)-$(PROJECT_VER).kwinscript
KWINPKG_DIR = pkg
FILE_SCRIPT = $(PACKAGE_DIR)/contents/code/script.js
FILE_META = $(PACKAGE_DIR)/metadata.desktop
FILE_QML = $(PACKAGE_DIR)/contents/ui/main.qml
FILE_CONFIG_XML = $(PACKAGE_DIR)/contents/config/main.xml
FILE_CONFIG_UI = $(PACKAGE_DIR)/contents/ui/config.ui
KWIN_SCRIPT = $(KWINPKG_DIR)/contents/code/script.js
KWIN_META = $(KWINPKG_DIR)/metadata.desktop
KWIN_QML = $(KWINPKG_DIR)/contents/ui/main.qml
KWIN_CONFIG_XML = $(KWINPKG_DIR)/contents/config/main.xml
KWIN_CONFIG_UI = $(KWINPKG_DIR)/contents/ui/config.ui
NODE_SCRIPT = krohnkite.js
NODE_META = package.json
NODE_FILES = $(NODE_SCRIPT) $(NODE_META) package-lock.json
SRC = $(shell find src -name "*.ts")
all: $(PACKAGE_DIR)
all: $(KWINPKG_DIR)
clean:
@rm -vf script.json
@rm -rvf $(PACKAGE_DIR)
@rm -rvf $(KWINPKG_DIR)
@rm -vf $(NODE_FILES)
install: package
plasmapkg2 -t kwinscript -s $(PACKAGE_NAME) \
&& plasmapkg2 -u $(PACKAGE_FILE) \
|| plasmapkg2 -i $(PACKAGE_FILE)
plasmapkg2 -t kwinscript -s $(PROJECT_NAME) \
&& plasmapkg2 -u $(KWINPKG_FILE) \
|| plasmapkg2 -i $(KWINPKG_FILE)
uninstall:
plasmapkg2 -t kwinscript -r $(PACKAGE_NAME)
plasmapkg2 -t kwinscript -r $(PROJECT_NAME)
package: $(PACKAGE_FILE)
package: $(KWINPKG_FILE)
run: $(PACKAGE_DIR)
bin/load-script.sh "$(FILE_QML)" "$(PACKAGE_NAME)-test"
@find "$(PACKAGE_DIR)" '(' -name "*.qmlc" -o -name "*.jsc" ')' -delete
test: $(NODE_SCRIPT) $(NODE_META)
npm test
run: $(KWINPKG_DIR)
bin/load-script.sh "$(KWIN_QML)" "$(PROJECT_NAME)-test"
@find "$(KWINPKG_DIR)" '(' -name "*.qmlc" -o -name "*.jsc" ')' -delete
stop:
bin/load-script.sh "unload" "$(PACKAGE_NAME)-test"
bin/load-script.sh "unload" "$(PROJECT_NAME)-test"
$(PACKAGE_FILE): $(PACKAGE_DIR)
@rm -f "$(PACKAGE_FILE)"
@7z a -tzip $(PACKAGE_FILE) ./$(PACKAGE_DIR)/*
$(KWINPKG_FILE): $(KWINPKG_DIR)
@rm -f "$(KWINPKG_FILE)"
@7z a -tzip $(KWINPKG_FILE) ./$(KWINPKG_DIR)/*
$(PACKAGE_DIR): $(FILE_SCRIPT) $(FILE_META) $(FILE_QML)
$(PACKAGE_DIR): $(FILE_CONFIG_XML) $(FILE_CONFIG_UI)
$(KWINPKG_DIR): $(KWIN_SCRIPT) $(KWIN_META) $(KWIN_QML)
$(KWINPKG_DIR): $(KWIN_CONFIG_XML) $(KWIN_CONFIG_UI)
@touch $@
$(FILE_SCRIPT): $(SRC)
@mkdir -vp `dirname $(FILE_SCRIPT)`
tsc --outFile $(FILE_SCRIPT)
$(KWIN_SCRIPT): $(NODE_SCRIPT)
@mkdir -vp `dirname $(KWIN_SCRIPT)`
cp -a $< $@
$(FILE_META): res/metadata.desktop
@mkdir -vp `dirname $(FILE_META)`
sed "s/\$$VER/$(PACKAGE_VER)/" $< \
| sed "s/\$$REV/`git rev-parse HEAD | cut -b-7`/" \
> $(FILE_META)
$(KWIN_META): res/metadata.desktop
@mkdir -vp `dirname $(KWIN_META)`
sed "s/\$$VER/$(PROJECT_VER)/" $< \
| sed "s/\$$REV/$(PROJECT_REV)/" \
> $(KWIN_META)
$(FILE_QML): res/main.qml
$(KWIN_QML): res/main.qml
@mkdir -vp `dirname $@`
@cp -v $< $@
$(FILE_CONFIG_XML): res/config.xml
$(KWIN_CONFIG_XML): res/config.xml
@mkdir -vp `dirname $@`
@cp -v $< $@
$(FILE_CONFIG_UI): res/config.ui
$(KWIN_CONFIG_UI): res/config.ui
@mkdir -vp `dirname $@`
@cp -v $< $@
.PHONY: all clean install package
$(NODE_SCRIPT): $(SRC)
tsc
$(NODE_META): res/package.json
sed "s/\$$VER/$(PROJECT_VER).0/" $< > $@
.PHONY: all clean install package test run stop

26
res/package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "krohnkite",
"version": "$VER",
"description": "A dynamic tiling extension for KWin",
"main": "krohnkite.js",
"directories": {
"test": "test"
},
"dependencies": {},
"devDependencies": {
"mocha": "^6.0.0"
},
"scripts": {
"test": "mocha 'test/*.spec.js'"
},
"repository": {
"type": "git",
"url": "git+https://github.com/esjeon/krohnkite.git"
},
"author": "Eon S. Jeon <esjeon@hyunmu.am>",
"license": "MIT",
"bugs": {
"url": "https://github.com/esjeon/krohnkite/issues"
},
"homepage": "https://github.com/esjeon/krohnkite#readme"
}

View File

@ -0,0 +1,137 @@
// Copyright (c) 2018-2019 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
class TestDriver implements IDriver {
public currentScreen: number;
public currentWindow: number;
public numScreen: number;
public screenSize: Rect;
public windows: Window[];
constructor() {
this.currentScreen = 0;
this.currentWindow = 0;
this.numScreen = 1;
this.screenSize = new Rect(0, 0, 10000, 10000);
this.windows = [];
}
public forEachScreen(func: (ctx: IDriverContext) => void) {
for (let screen = 0; screen < this.numScreen; screen ++)
func(new TestContext(screen));
}
public getCurrentContext(): IDriverContext {
const window = this.getCurrentWindow();
if (window)
return window.context;
return new TestContext(0);
}
public getCurrentWindow(): Window | null {
return (this.windows.length !== 0)
? this.windows[this.currentWindow]
: null;
}
public getWorkingArea(ctx: IDriverContext): Rect {
return this.screenSize;
}
public setCurrentWindow(window: Window) {
const idx = this.windows.indexOf(window);
if (idx !== -1)
this.currentWindow = idx;
}
}
class TestContext implements IDriverContext {
public readonly screen: number;
public get id(): string {
return String(this.screen);
}
public get ignore(): boolean {
// TODO: optionally ignore some context to test LayoutStore
return false;
}
constructor(screen: number) {
this.screen = screen;
}
}
class TestWindow implements IDriverWindow {
private static windowCount: number = 0;
public readonly id: string;
public readonly shouldFloat: boolean;
public readonly shouldIgnore: boolean;
public context: TestContext;
public fullScreen: boolean;
public geometry: Rect;
public keepBelow: boolean;
public noBorder: boolean;
constructor(ctx: TestContext, geometry?: Rect, ignore?: boolean, float?: boolean) {
this.id = String(TestWindow.windowCount);
TestWindow.windowCount += 1;
this.shouldFloat = (float !== undefined) ? float : false;
this.shouldIgnore = (ignore !== undefined) ? ignore : false;
this.context = ctx;
this.fullScreen = false;
this.geometry = geometry || new Rect(0, 0, 100, 100);
this.keepBelow = false;
this.noBorder = false;
}
public commit(geometry?: Rect, noBorder?: boolean, keepBelow?: boolean) {
if (geometry)
this.geometry = geometry;
if (noBorder !== undefined)
this.noBorder = noBorder;
if (keepBelow !== undefined)
this.keepBelow = keepBelow;
}
public visible(ctx: IDriverContext): boolean {
const tctx = ctx as TestContext;
return this.context.screen === tctx.screen;
}
}
function setTestConfig(name: string, value: any) {
if (!CONFIG)
CONFIG = {} as any;
(CONFIG as any)[name] = value;
}
try {
exports.TestContext = TestContext;
exports.TestDriver = TestDriver;
exports.TestWindow = TestWindow;
exports.setTestConfig = setTestConfig;
} catch (e) {
/* ignore */
}

70
test/tilelayout.spec.js Normal file
View File

@ -0,0 +1,70 @@
// Copyright (c) 2018-2019 Eon S. Jeon <esjeon@hyunmu.am>
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
let assert = require('assert');
let K = require('../krohnkite');
describe('TileLayout', function() {
describe('#apply', function() {
let layout = new K.TileLayout();
let area = new K.Rect(0, 0, 1000, 1000);
let ctx = new K.TestContext(0);
let gap;
K.setTestConfig('tileLayoutGap', gap = 0);
it('tiles sole master to full screen', function() {
let win = new K.Window(new K.TestWindow(ctx));
layout.apply([win], area);
assert(win.geometry.equals(area));
});
it('corretly applies master ratio', function() {
let master = new K.Window(new K.TestWindow(ctx));
let stack = new K.Window(new K.TestWindow(ctx));
let ratio = layout.masterRatio;
let masterWidth = Math.floor(area.width * ratio);
layout.apply([master, stack], area);
assert.equal(master.geometry.width, masterWidth);
assert.equal(stack.geometry.width, area.width - masterWidth);
});
it('supports non-origin screen', function() {
const base = 30;
const size = 1000;
const area = new K.Rect(base, base, size, size);
let tiles = [];
for (let i = 0; i < 5; i++) {
tiles.push(new K.Window(new K.TestWindow(ctx)));
layout.apply(tiles, area);
for (let j = 0; j <= i; j++) {
assert(tiles[i].geometry.x >= base);
assert(tiles[i].geometry.y >= base);
assert(tiles[i].geometry.x + tiles[i].geometry.width <= base + size);
assert(tiles[i].geometry.y + tiles[i].geometry.height <= base + size);
}
}
});
});
});