2017-10-10 20:48:50 +00:00
|
|
|
module TK.SpaceTac.UI.Specs {
|
2017-10-26 21:47:13 +00:00
|
|
|
testing("UIBuilder", test => {
|
2017-10-29 21:08:55 +00:00
|
|
|
let testgame = setupEmptyView(test);
|
2017-10-10 20:48:50 +00:00
|
|
|
|
|
|
|
function get(path: (number | string)[]): [string, any] {
|
|
|
|
let spath = `[${path.join(" -> ")}]`;
|
2018-05-15 14:57:45 +00:00
|
|
|
let component: Phaser.GameObjects.GameObject | Phaser.Scene | null = testgame.view;
|
2017-10-10 20:48:50 +00:00
|
|
|
path.forEach(idx => {
|
2018-05-15 14:57:45 +00:00
|
|
|
if (component instanceof Phaser.Scene) {
|
|
|
|
component = (typeof idx == "number") ? component.children.list[idx] : component.children.getByName(idx);
|
|
|
|
} else if (component instanceof Phaser.GameObjects.Container) {
|
|
|
|
component = (typeof idx == "number") ? component.list[idx] : component.getByName(idx);
|
|
|
|
} else {
|
|
|
|
component = null;
|
|
|
|
}
|
2017-10-10 20:48:50 +00:00
|
|
|
if (!component) {
|
2018-05-15 14:57:45 +00:00
|
|
|
throw new Error(`Path not found: ${spath} (${idx} part)`);
|
2017-10-10 20:48:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return [spath, component];
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
function checkcomp<T extends Phaser.GameObjects.GameObject>(path: (number | string)[], ctype?: { new(...args: any[]): T }, name?: string, attrs?: Partial<T>): T {
|
2017-10-10 20:48:50 +00:00
|
|
|
let [spath, component] = get(path);
|
|
|
|
|
|
|
|
if (typeof ctype != "undefined") {
|
2017-10-26 21:47:13 +00:00
|
|
|
test.check.same(component instanceof ctype, true, `${spath} is not of good type`);
|
2017-10-10 20:48:50 +00:00
|
|
|
}
|
|
|
|
if (typeof name != "undefined") {
|
2017-10-26 21:47:13 +00:00
|
|
|
test.check.equals(component.name, name, spath);
|
2017-10-10 20:48:50 +00:00
|
|
|
}
|
|
|
|
if (typeof attrs != "undefined") {
|
2018-05-15 14:57:45 +00:00
|
|
|
iteritems(<any>attrs, (key, value) => {
|
2017-10-26 21:47:13 +00:00
|
|
|
test.check.equals(component[key], value, spath);
|
2017-10-10 20:48:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return component;
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
function checktext(path: (number | string)[], attrs?: Partial<UIText>, style?: Partial<Phaser.GameObjects.Text.TextStyle>): UIText {
|
|
|
|
let text = checkcomp(path, UIText, "", attrs);
|
|
|
|
|
|
|
|
if (typeof style != "undefined") {
|
|
|
|
iteritems(<any>style, (key, value) => {
|
|
|
|
test.check.equals((<any>text.style)[key], value, `text style ${key}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("can work on view layers", check => {
|
2017-10-11 20:58:08 +00:00
|
|
|
let builder = new UIBuilder(testgame.view, "tl1");
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("tg1");
|
|
|
|
checkcomp(["View layers", "tl1", 0], UIContainer, "tg1");
|
2017-10-11 20:58:08 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view, "tl2");
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("tg2");
|
|
|
|
checkcomp(["View layers", "tl2", 0], UIContainer, "tg2");
|
2017-10-11 20:58:08 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view, "tl1");
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("tg3");
|
|
|
|
checkcomp(["View layers", "tl1", 0], UIContainer, "tg1");
|
|
|
|
checkcomp(["View layers", "tl1", 1], UIContainer, "tg3");
|
2017-10-11 20:58:08 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view);
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("tg4");
|
|
|
|
checkcomp(["View layers", "base", 0], UIContainer, "tg4");
|
2017-10-11 20:58:08 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view);
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("tg5");
|
|
|
|
checkcomp(["View layers", "base", 0], UIContainer, "tg4");
|
|
|
|
checkcomp(["View layers", "base", 1], UIContainer, "tg5");
|
2017-10-11 20:58:08 +00:00
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
check.equals(testgame.view.layers.list.map((child: any) => child.name), ["tl1", "tl2", "base"]);
|
2017-10-11 20:58:08 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("creates component inside the parent container", check => {
|
2017-10-11 20:58:08 +00:00
|
|
|
let builder = new UIBuilder(testgame.view, testgame.view.getLayer("testlayer"));
|
2018-05-15 14:57:45 +00:00
|
|
|
let group = builder.container("test1");
|
|
|
|
checkcomp(["View layers", "testlayer", 0], UIContainer, "test1");
|
2017-10-10 20:48:50 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view, group);
|
|
|
|
builder.text("test2");
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "testlayer", 0, 0], UIText, "", { text: "test2", parentContainer: group });
|
2017-10-10 20:48:50 +00:00
|
|
|
|
|
|
|
builder = new UIBuilder(testgame.view, "anothertestlayer");
|
|
|
|
builder.text("test3");
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "anothertestlayer", 0], UIText, "", { text: "test3" });
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("can clear a container", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("group1", 50, 30);
|
2017-10-10 20:48:50 +00:00
|
|
|
builder.text("text1");
|
|
|
|
let [spath, container] = get(["View layers", "base"]);
|
2018-05-15 14:57:45 +00:00
|
|
|
if (check.instance(container, UIContainer, "is a container")) {
|
|
|
|
check.equals(container.list.length, 2);
|
|
|
|
builder.clear();
|
|
|
|
check.equals(container.list.length, 0);
|
|
|
|
}
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
test.case("can create containers", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
2018-05-15 14:57:45 +00:00
|
|
|
builder.container("group1", 50, 30);
|
|
|
|
checkcomp(["View layers", "base", 0], UIContainer, "group1", { x: 50, y: 30 });
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("can create texts", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
2017-10-10 22:32:46 +00:00
|
|
|
builder.text("Test content", 12, 41);
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], { text: "Test content", x: 12, y: 41 });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
|
|
|
builder.text("", 0, 0, { size: 61 });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, { fontFamily: "16pt SpaceTac" });
|
|
|
|
checktext(["View layers", "base", 1], undefined, { fontFamily: "61pt SpaceTac" });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
|
|
|
builder.text("", 0, 0, { color: "#252627" });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, { color: "#ffffff" });
|
|
|
|
checktext(["View layers", "base", 1], undefined, { color: "#252627" });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
|
|
|
builder.text("", 0, 0, { shadow: true });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, { shadowColor: "#000", shadowFill: false, shadowStroke: false });
|
|
|
|
checktext(["View layers", "base", 1], undefined, { shadowColor: "rgba(0,0,0,0.6)", shadowFill: true, shadowOffsetX: 3, shadowOffsetY: 4, shadowBlur: 3, shadowStroke: true });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
2017-10-11 21:49:56 +00:00
|
|
|
builder.text("", 0, 0, { stroke_width: 2, stroke_color: "#ff0000" });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, { stroke: "#fff", strokeThickness: 0 });
|
|
|
|
checktext(["View layers", "base", 1], undefined, { stroke: "#ff0000", strokeThickness: 2 });
|
2017-10-11 21:49:56 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
2017-10-10 22:32:46 +00:00
|
|
|
builder.text("", 0, 0, { bold: true });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, { fontFamily: "16pt SpaceTac" });
|
|
|
|
checktext(["View layers", "base", 1], undefined, { fontFamily: "bold 16pt SpaceTac" });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, {});
|
|
|
|
builder.text("", 0, 0, { center: false });
|
|
|
|
builder.text("", 0, 0, { vcenter: false });
|
|
|
|
builder.text("", 0, 0, { center: false, vcenter: false });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], { originX: 0.5, originY: 0.5 }, { align: "center" });
|
|
|
|
checktext(["View layers", "base", 1], { originX: 0, originY: 0.5 }, { align: "left" });
|
|
|
|
checktext(["View layers", "base", 2], { originX: 0.5, originY: 0 }, { align: "center" });
|
|
|
|
checktext(["View layers", "base", 3], { originX: 0, originY: 0 }, { align: "left" });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
|
|
|
builder.clear();
|
|
|
|
builder.text("", 0, 0, { width: 0 });
|
|
|
|
builder.text("", 0, 0, { width: 1100 });
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], undefined, <any>{ wordWrapWidth: null });
|
|
|
|
checktext(["View layers", "base", 1], undefined, <any>{ wordWrapWidth: 1100 });
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("can create images", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
|
|
|
builder.image("test-image", 100, 50);
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 0], UIImage, "test-image", { x: 100, y: 50 });
|
2017-10-10 22:32:46 +00:00
|
|
|
|
2017-10-29 21:08:55 +00:00
|
|
|
check.patch(testgame.view, "getFirstImage", (...images: string[]) => images[1]);
|
2017-10-10 22:32:46 +00:00
|
|
|
builder.image(["test-image1", "test-image2", "test-image3"]);
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 1], UIImage, "test-image2");
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("can create buttons", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
|
|
|
let a = 1;
|
|
|
|
let button1 = builder.button("test-image1", 100, 50, () => a += 1);
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 0], UIButton, "test-image1", { x: 100, y: 50 });
|
2017-10-10 20:48:50 +00:00
|
|
|
let button2 = builder.button("test-image2", 20, 10);
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 1], UIButton, "test-image2", { x: 20, y: 10 });
|
2017-10-10 20:48:50 +00:00
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
check.equals(a, 1);
|
2017-10-10 20:48:50 +00:00
|
|
|
testClick(button1);
|
2017-10-26 21:47:13 +00:00
|
|
|
check.equals(a, 2);
|
2017-10-10 20:48:50 +00:00
|
|
|
testClick(button2);
|
2017-10-26 21:47:13 +00:00
|
|
|
check.equals(a, 2);
|
2017-10-10 20:48:50 +00:00
|
|
|
testClick(button1);
|
2017-10-26 21:47:13 +00:00
|
|
|
check.equals(a, 3);
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("creates sub-builders, preserving text style", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let base_style = new UITextStyle();
|
|
|
|
base_style.width = 123;
|
|
|
|
let builder = new UIBuilder(testgame.view, undefined, base_style);
|
|
|
|
builder.text("Test 1");
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
let group = builder.container("testgroup");
|
2017-10-10 20:48:50 +00:00
|
|
|
let subbuilder = builder.in(group);
|
|
|
|
subbuilder.text("Test 2");
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], { text: "Test 1" }, <any>{ wordWrapWidth: 123 });
|
|
|
|
checktext(["View layers", "base", 1, 0], { text: "Test 2" }, <any>{ wordWrapWidth: 123 });
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2017-10-26 21:47:13 +00:00
|
|
|
test.case("allows to alter text style", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
|
|
|
builder.text("t1");
|
|
|
|
builder.styled({ bold: true }).text("t2");
|
|
|
|
builder.text("t3");
|
|
|
|
builder.text("t4", undefined, undefined, { bold: true });
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
checktext(["View layers", "base", 0], { text: "t1" }, { fontFamily: "16pt SpaceTac" });
|
|
|
|
checktext(["View layers", "base", 1], { text: "t2" }, { fontFamily: "bold 16pt SpaceTac" });
|
|
|
|
checktext(["View layers", "base", 2], { text: "t3" }, { fontFamily: "16pt SpaceTac" });
|
|
|
|
checktext(["View layers", "base", 3], { text: "t4" }, { fontFamily: "bold 16pt SpaceTac" });
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
test.case("allows to change text or image content", check => {
|
2017-10-10 20:48:50 +00:00
|
|
|
let builder = new UIBuilder(testgame.view);
|
|
|
|
let text = builder.text("test-text");
|
|
|
|
let image = builder.image("test-image");
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 0], UIText, "", { text: "test-text" });
|
|
|
|
checkcomp(["View layers", "base", 1], UIImage, "test-image");
|
2017-10-10 20:48:50 +00:00
|
|
|
|
|
|
|
builder.change(text, "test-mod-text");
|
|
|
|
builder.change(image, "test-mod-image");
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
checkcomp(["View layers", "base", 0], UIText, "", { text: "test-mod-text" });
|
|
|
|
checkcomp(["View layers", "base", 1], UIImage, "test-mod-image");
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
2018-02-08 15:16:03 +00:00
|
|
|
|
|
|
|
test.case("distributes children along an axis", check => {
|
|
|
|
let builder = new UIBuilder(testgame.view);
|
2018-05-15 14:57:45 +00:00
|
|
|
builder = builder.in(builder.container("test"));
|
2018-02-08 15:16:03 +00:00
|
|
|
|
|
|
|
let c1 = builder.text("");
|
|
|
|
let c2 = builder.button("test");
|
2018-05-15 14:57:45 +00:00
|
|
|
let c3 = builder.container("test");
|
2018-02-08 15:16:03 +00:00
|
|
|
|
|
|
|
check.equals(c1.x, 0);
|
|
|
|
check.equals(c1.y, 0);
|
|
|
|
check.equals(c2.x, 0);
|
|
|
|
check.equals(c2.y, 0);
|
|
|
|
check.equals(c3.x, 0);
|
|
|
|
check.equals(c3.y, 0);
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
check.patch(UITools, "getBounds", (obj: any) => {
|
2018-02-08 15:16:03 +00:00
|
|
|
if (obj === c1) {
|
2018-03-29 22:57:53 +00:00
|
|
|
return { x: 0, y: 0, width: 100, height: 51 };
|
2018-02-08 15:16:03 +00:00
|
|
|
} else if (obj === c2) {
|
2018-03-29 22:57:53 +00:00
|
|
|
return { x: 0, y: 0, width: 20, height: 7 };
|
2018-02-08 15:16:03 +00:00
|
|
|
} else if (obj === c3) {
|
2018-03-29 22:57:53 +00:00
|
|
|
return { x: 0, y: 0, width: 60, height: 11 };
|
2018-02-08 15:16:03 +00:00
|
|
|
} else {
|
2018-03-29 22:57:53 +00:00
|
|
|
return { x: 0, y: 0, width: 0, height: 0 };
|
2018-02-08 15:16:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.distribute("x", 100, 400);
|
|
|
|
|
|
|
|
check.equals(c1.x, 130);
|
|
|
|
check.equals(c1.y, 0);
|
|
|
|
check.equals(c2.x, 260);
|
|
|
|
check.equals(c2.y, 0);
|
|
|
|
check.equals(c3.x, 310);
|
|
|
|
check.equals(c3.y, 0);
|
|
|
|
|
|
|
|
builder.distribute("y", 60, 180);
|
|
|
|
|
|
|
|
check.equals(c1.x, 130);
|
|
|
|
check.equals(c1.y, 73);
|
|
|
|
check.equals(c2.x, 260);
|
|
|
|
check.equals(c2.y, 137);
|
|
|
|
check.equals(c3.x, 310);
|
|
|
|
check.equals(c3.y, 156);
|
|
|
|
})
|
2017-10-10 20:48:50 +00:00
|
|
|
})
|
|
|
|
}
|