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

131 lines
4 KiB
TypeScript
Raw Normal View History

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;
2017-02-21 22:38:31 +00:00
}
/**
* Setup a headless test UI, with a single view started.
*/
2017-10-09 21:13:56 +00:00
export function setupSingleView<T extends Phaser.State>(buildView: () => [T, any[]]) {
let testgame = new TestGame<T>();
2017-02-21 22:38:31 +00:00
beforeEach(function (done) {
spyOn(console, "log").and.stub();
spyOn(console, "warn").and.stub();
2017-04-12 23:22:34 +00:00
jasmine.clock().install();
if (!test_ui) {
test_ui = new MainUI(true);
2015-01-08 00:00:00 +00:00
if (test_ui.load) {
spyOn(test_ui.load, 'image').and.stub();
spyOn(test_ui.load, 'audio').and.stub();
}
2017-02-16 22:59:41 +00:00
}
2017-02-10 00:08:28 +00:00
testgame.ui = test_ui;
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);
spyOn(state, "getConnection").and.returnValue(connection);
}
2017-02-21 22:38:31 +00:00
let orig_create = bound(state, "create");
spyOn(state, "create").and.callFake(() => {
orig_create();
2015-01-08 00:00:00 +00:00
done();
2017-02-21 22:38:31 +00:00
});
testgame.ui.state.add("test", state);
testgame.ui.state.start("test", true, false, ...stateargs);
if (!testgame.ui.isBooted) {
testgame.ui.device.canvas = true;
testgame.ui.boot();
}
2017-10-09 21:13:56 +00:00
testgame.view = state;
2015-01-08 00:00:00 +00:00
});
2017-02-21 22:38:31 +00:00
afterEach(function () {
2017-04-12 23:22:34 +00:00
jasmine.clock().uninstall();
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-09 21:13:56 +00:00
export function setupEmptyView(): TestGame<BaseView> {
return setupSingleView(() => {
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-09 21:13:56 +00:00
export function setupBattleview(): TestGame<BattleView> {
return setupSingleView(() => {
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-09 21:13:56 +00:00
export function setupMapview(): TestGame<UniverseMapView> {
return setupSingleView(() => {
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
/**
* Check a given text node
*/
export function checkText(node: any, content: string): void {
expect(node instanceof Phaser.Text).toBe(true);
let tnode = <Phaser.Text>node;
expect(tnode.text).toEqual(content);
}
2017-06-08 17:32:57 +00:00
/**
* Check that a layer contains the given component at a given index
*/
export function checkComponentInLayer(layer: Phaser.Group, index: number, component: UIComponent) {
if (index >= layer.children.length) {
fail(`Not enough children in group ${layer.name} for ${component} at index ${index}`);
} else {
let child = layer.children[index];
if (child !== (<any>component).container) {
fail(`${component} is not at index ${index} in ${layer.name}`);
}
}
}
2015-01-08 00:00:00 +00:00
}