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

108 lines
3.3 KiB
TypeScript
Raw Normal View History

/// <reference path="battle/BattleView.ts"/>
2017-01-30 00:40:33 +00:00
/// <reference path="map/UniverseMapView.ts"/>
2015-01-08 00:00:00 +00:00
2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI.Specs {
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).
*/
export class TestGame {
ui: MainUI;
2017-02-27 23:36:12 +00:00
baseview: BaseView;
2017-02-21 22:38:31 +00:00
battleview: BattleView;
mapview: UniverseMapView;
multistorage: Multi.FakeRemoteStorage;
2017-02-21 22:38:31 +00:00
}
/**
* Setup a headless test UI, with a single view started.
*/
export function setupSingleView(buildView: (testgame: TestGame) => [Phaser.State, any[]]) {
let testgame = new TestGame();
beforeEach(function (done) {
spyOn(console, "log").and.stub();
spyOn(console, "warn").and.stub();
2017-04-12 23:22:34 +00:00
jasmine.clock().install();
2017-02-21 22:38:31 +00:00
testgame.ui = new MainUI(true);
2015-01-08 00:00:00 +00:00
2017-02-21 22:38:31 +00:00
if (testgame.ui.load) {
spyOn(testgame.ui.load, 'image').and.stub();
spyOn(testgame.ui.load, 'audio').and.stub();
2017-02-16 22:59:41 +00:00
}
2017-02-10 00:08:28 +00:00
2017-02-21 22:38:31 +00:00
let [state, stateargs] = buildView(testgame);
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);
2015-01-08 00:00:00 +00:00
});
2017-02-21 22:38:31 +00:00
afterEach(function () {
let ui = testgame.ui;
2017-03-09 17:11:00 +00:00
window.requestAnimationFrame(() => {
if (ui) {
ui.destroy();
}
});
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
*/
export function setupEmptyView(): TestGame {
2017-02-27 23:36:12 +00:00
return setupSingleView(testgame => {
testgame.baseview = new BaseView();
return [testgame.baseview, []];
});
}
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.
*/
export function setupBattleview(): TestGame {
return setupSingleView(testgame => {
testgame.battleview = new BattleView();
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
return [testgame.battleview, [player, battle]];
});
}
/**
* Test setup of a mapview bound to a universe, to be called inside a "describe" block.
*/
export function setupMapview(): TestGame {
return setupSingleView(testgame => {
testgame.mapview = new UniverseMapView();
let mapview = new UniverseMapView();
let session = new GameSession();
session.startNewGame();
return [testgame.mapview, [session.universe, session.player]];
});
2017-01-30 00:40:33 +00:00
}
2015-01-08 00:00:00 +00:00
}