1
0
Fork 0
spacetac/src/ui/TestGame.ts

182 lines
5.7 KiB
TypeScript
Raw Normal View History

2017-10-26 21:47:13 +00:00
/// <reference path="../common/Testing.ts" />
2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI.Specs {
let test_ui: MainUI;
2017-02-21 22:38:31 +00:00
/**
* Class to hold references to test objects (used as singleton in "describe" blocks)
*
* Attributes should only be accessed from inside corresponding "it" blocks (they are initialized by the setup).
*/
2017-10-09 21:13:56 +00:00
export class TestGame<T extends Phaser.State> {
2017-02-21 22:38:31 +00:00
ui: MainUI;
2017-10-09 21:13:56 +00:00
view: T;
multistorage: Multi.FakeRemoteStorage;
state: string;
2017-10-26 21:47:13 +00:00
clock: FakeClock;
2017-02-21 22:38:31 +00:00
}
/**
* Setup a headless test UI, with a single view started.
*/
2017-10-29 21:08:55 +00:00
export function setupSingleView<T extends Phaser.State>(test: TestSuite, buildView: () => [T, any[]]) {
2017-10-09 21:13:56 +00:00
let testgame = new TestGame<T>();
2017-02-21 22:38:31 +00:00
2017-10-29 21:08:55 +00:00
test.asetup(() => new Promise((resolve, reject) => {
let check = new TestContext(); // TODO Should be taken from test suite
check.patch(console, "log", null);
check.patch(console, "warn", null);
if (!test_ui) {
test_ui = new MainUI(true);
2015-01-08 00:00:00 +00:00
if (test_ui.load) {
2017-10-29 21:08:55 +00:00
check.patch(test_ui.load, 'image', null);
check.patch(test_ui.load, 'audio', null);
}
2017-02-16 22:59:41 +00:00
}
2017-02-10 00:08:28 +00:00
testgame.ui = test_ui;
testgame.ui.resetSession();
2017-10-09 21:13:56 +00:00
let [state, stateargs] = buildView();
2015-01-08 00:00:00 +00:00
if (state instanceof BaseView) {
testgame.multistorage = new Multi.FakeRemoteStorage();
let connection = new Multi.Connection(RandomGenerator.global.id(12), testgame.multistorage);
2017-10-29 21:08:55 +00:00
check.patch(state, "getConnection", () => connection);
}
2017-02-21 22:38:31 +00:00
let orig_create = bound(state, "create");
2017-10-29 21:08:55 +00:00
check.patch(state, "create", () => {
2017-02-21 22:38:31 +00:00
orig_create();
2017-10-29 21:08:55 +00:00
resolve();
2017-02-21 22:38:31 +00:00
});
testgame.ui.state.add("test", state);
testgame.ui.state.start("test", true, false, ...stateargs);
testgame.state = "test_initial";
2017-10-29 21:08:55 +00:00
check.patch(testgame.ui.state, "start", (name: string) => {
testgame.state = name;
});
if (!testgame.ui.isBooted) {
testgame.ui.device.canvas = true;
testgame.ui.boot();
}
2017-10-09 21:13:56 +00:00
testgame.view = state;
2017-10-29 21:08:55 +00:00
}));
2017-02-21 22:38:31 +00:00
return testgame;
2015-01-08 00:00:00 +00:00
}
2017-02-21 22:38:31 +00:00
/**
* Test setup of an empty BaseView
*/
2017-10-29 21:08:55 +00:00
export function setupEmptyView(test: TestSuite): TestGame<BaseView> {
return setupSingleView(test, () => {
2017-10-09 21:13:56 +00:00
return [new BaseView(), []];
2017-02-27 23:36:12 +00:00
});
}
2017-01-30 00:40:33 +00:00
2017-02-21 22:38:31 +00:00
/**
* Test setup of a battleview bound to a battle, to be called inside a "describe" block.
*/
2017-10-29 21:08:55 +00:00
export function setupBattleview(test: TestSuite): TestGame<BattleView> {
return setupSingleView(test, () => {
2017-10-09 21:13:56 +00:00
let view = new BattleView();
view.splash = false;
2017-02-21 22:38:31 +00:00
let battle = Battle.newQuickRandom();
2017-03-09 17:11:00 +00:00
let player = battle.playing_ship ? battle.playing_ship.getPlayer() : new Player();
2017-02-21 22:38:31 +00:00
2017-10-09 21:13:56 +00:00
return [view, [player, battle]];
2017-02-21 22:38:31 +00:00
});
}
/**
* Test setup of a mapview bound to a universe, to be called inside a "describe" block.
*/
2017-10-29 21:08:55 +00:00
export function setupMapview(test: TestSuite): TestGame<UniverseMapView> {
return setupSingleView(test, () => {
2017-02-21 22:38:31 +00:00
let mapview = new UniverseMapView();
let session = new GameSession();
session.startNewGame();
2017-10-09 21:13:56 +00:00
return [mapview, [session.universe, session.player]];
2017-02-21 22:38:31 +00:00
});
2017-01-30 00:40:33 +00:00
}
2017-05-16 23:12:05 +00:00
/**
* Crawn through the children of a node
*/
export function crawlChildren(node: UIContainer, recursive: boolean, callback: (child: any) => void): void {
node.children.forEach(child => {
callback(child);
if (recursive && (child instanceof Phaser.Group || child instanceof Phaser.Image)) {
crawlChildren(child, true, callback);
}
});
}
/**
* Collect all image codes in a node
*/
export function collectImages(node: UIContainer, recursive = true): (string | null)[] {
let result: (string | null)[] = [];
crawlChildren(node, recursive, child => {
if (child instanceof Phaser.Image) {
result.push(child.name || null);
}
});
return result;
}
/**
* Collect all texts in a node
*/
export function collectTexts(node: UIContainer, recursive = true): (string | null)[] {
let result: (string | null)[] = [];
crawlChildren(node, recursive, child => {
if (child instanceof Phaser.Text) {
result.push(child.text || null);
}
});
return result;
}
2017-05-16 23:12:05 +00:00
/**
* Check a given text node
*/
2017-10-26 21:47:13 +00:00
export function checkText(check: TestContext, node: any, content: string): void {
check.equals(node instanceof Phaser.Text, true);
2017-05-16 23:12:05 +00:00
let tnode = <Phaser.Text>node;
2017-10-26 21:47:13 +00:00
check.equals(tnode.text, content);
2017-05-16 23:12:05 +00:00
}
2017-06-08 17:32:57 +00:00
/**
* Check that a layer contains the given component at a given index
*/
2017-10-26 21:47:13 +00:00
export function checkComponentInLayer(check: TestContext, layer: Phaser.Group, index: number, component: UIComponent) {
2017-06-08 17:32:57 +00:00
if (index >= layer.children.length) {
2017-10-26 21:47:13 +00:00
check.fail(`Not enough children in group ${layer.name} for ${component} at index ${index}`);
2017-06-08 17:32:57 +00:00
} else {
let child = layer.children[index];
if (child !== (<any>component).container) {
2017-10-26 21:47:13 +00:00
check.fail(`${component} is not at index ${index} in ${layer.name}`);
2017-06-08 17:32:57 +00:00
}
}
}
/**
* Simulate a click on a button
*/
export function testClick(button: Phaser.Button): void {
button.onInputDown.dispatch();
button.onInputUp.dispatch();
}
2015-01-08 00:00:00 +00:00
}