Some test coverage on ui
This commit is contained in:
parent
53bbe510d2
commit
3190ea4f8b
|
@ -6,14 +6,23 @@
|
|||
<title>SpaceTac - Unit tests</title>
|
||||
|
||||
<link rel="stylesheet" href="vendor/jasmine-core/lib/jasmine-core/jasmine.css">
|
||||
<style>
|
||||
canvas {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/jasmine.js"></script>
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/boot.js"></script>
|
||||
<script src="vendor/phaser/build/phaser.min.js"></script>
|
||||
<script src="build.js"></script>
|
||||
<div style="display: none; visibility: hidden; height: 0; overflow: hidden;">
|
||||
<div id="-space-tac" class="game"></div>
|
||||
</div>
|
||||
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/jasmine.js"></script>
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
|
||||
<script src="vendor/jasmine-core/lib/jasmine-core/boot.js"></script>
|
||||
<script src="vendor/phaser/build/phaser.min.js"></script>
|
||||
<script src="build.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
47
src/MainUI.spec.ts
Normal file
47
src/MainUI.spec.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
if (typeof window != "undefined") {
|
||||
(<any>window).describe = (<any>window).describe || function () { };
|
||||
}
|
||||
|
||||
module TS.SpaceTac.Specs {
|
||||
class FakeStorage {
|
||||
data = {}
|
||||
getItem(name: string) {
|
||||
return this.data[name];
|
||||
}
|
||||
setItem(name: string, value: string) {
|
||||
this.data[name] = value;
|
||||
}
|
||||
}
|
||||
|
||||
describe("MainUI", () => {
|
||||
beforeEach(function () {
|
||||
spyOn(console, "log").and.stub();
|
||||
spyOn(console, "warn").and.stub();
|
||||
spyOn(console, "error").and.stub();
|
||||
});
|
||||
|
||||
it("saves games in local browser storage", function () {
|
||||
let ui = new MainUI(true);
|
||||
ui.storage = <any>new FakeStorage();
|
||||
|
||||
let result = ui.loadGame("spacetac-test-save");
|
||||
expect(result).toBe(false);
|
||||
|
||||
ui.session.startNewGame();
|
||||
let systems = ui.session.universe.stars.length;
|
||||
let links = ui.session.universe.starlinks.length;
|
||||
|
||||
result = ui.saveGame("spacetac-test-save");
|
||||
expect(result).toBe(true);
|
||||
expect(ui.storage.getItem("spacetac-test-save")).toBeTruthy();
|
||||
|
||||
ui.session = new GameSession();
|
||||
expect(ui.session.universe.stars.length).not.toBe(systems);
|
||||
|
||||
result = ui.loadGame("spacetac-test-save");
|
||||
expect(result).toBe(true);
|
||||
expect(ui.session.universe.stars.length).toBe(systems);
|
||||
expect(ui.session.universe.starlinks.length).toBe(links);
|
||||
});
|
||||
});
|
||||
}
|
|
@ -10,46 +10,74 @@ module TS.SpaceTac {
|
|||
// Current game session
|
||||
session: GameSession;
|
||||
|
||||
// Current focused star system
|
||||
star: Star;
|
||||
|
||||
// Audio manager
|
||||
audio: UI.Audio;
|
||||
|
||||
// Storage used
|
||||
storage: Storage;
|
||||
|
||||
// Headless mode
|
||||
headless: boolean;
|
||||
|
||||
constructor(headless: boolean = false) {
|
||||
super(1920, 1080, headless ? Phaser.HEADLESS : Phaser.AUTO, '-space-tac');
|
||||
|
||||
this.headless = headless;
|
||||
|
||||
this.audio = new UI.Audio(this);
|
||||
|
||||
this.storage = localStorage;
|
||||
|
||||
this.session = new GameSession();
|
||||
this.star = null;
|
||||
|
||||
this.state.add('boot', UI.Boot);
|
||||
this.state.add('preload', UI.Preload);
|
||||
this.state.add('mainmenu', UI.MainMenu);
|
||||
this.state.add('router', UI.Router);
|
||||
this.state.add('battle', UI.BattleView);
|
||||
this.state.add('universe', UI.UniverseMapView);
|
||||
if (!headless) {
|
||||
this.state.add('boot', UI.Boot);
|
||||
this.state.add('preload', UI.Preload);
|
||||
this.state.add('mainmenu', UI.MainMenu);
|
||||
this.state.add('router', UI.Router);
|
||||
this.state.add('battle', UI.BattleView);
|
||||
this.state.add('universe', UI.UniverseMapView);
|
||||
|
||||
this.state.start('boot');
|
||||
this.state.start('boot');
|
||||
}
|
||||
}
|
||||
|
||||
// Save current game in local browser storage
|
||||
saveGame(): boolean {
|
||||
if (typeof (Storage) !== "undefined") {
|
||||
localStorage.setItem("spacetac-savegame", this.session.saveToString());
|
||||
(<UI.BaseView>this.state.getCurrentState()).messages.addMessage("Game saved");
|
||||
boot() {
|
||||
if (!this.headless) {
|
||||
super.boot();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a popup message in current view
|
||||
*/
|
||||
displayMessage(message: string) {
|
||||
let state = <UI.BaseView>this.state.getCurrentState();
|
||||
if (state) {
|
||||
state.messages.addMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save current game in local browser storage
|
||||
*/
|
||||
saveGame(name = "spacetac-savegame"): boolean {
|
||||
if (typeof this.storage != "undefined") {
|
||||
this.storage.setItem(name, this.session.saveToString());
|
||||
this.displayMessage("Game saved");
|
||||
return true;
|
||||
} else {
|
||||
(<UI.BaseView>this.state.getCurrentState()).messages.addMessage("Your browser does not support saving");
|
||||
this.displayMessage("Your browser does not support saving");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Load current game from local browser storage
|
||||
loadGame(): boolean {
|
||||
if (typeof (Storage) !== "undefined") {
|
||||
var loaded = localStorage.getItem("spacetac-savegame");
|
||||
/**
|
||||
* Load current game from local browser storage
|
||||
*/
|
||||
loadGame(name = "spacetac-savegame"): boolean {
|
||||
if (typeof this.storage != "undefined") {
|
||||
var loaded = this.storage.getItem(name);
|
||||
if (loaded) {
|
||||
this.session = GameSession.loadFromString(loaded);
|
||||
console.log("Game loaded");
|
||||
|
@ -63,14 +91,5 @@ module TS.SpaceTac {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Get the focuses star system
|
||||
getFocusedStar(): Star {
|
||||
if (this.star && this.star.universe === this.session.universe) {
|
||||
return this.star;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
23
src/ui/BaseView.spec.ts
Normal file
23
src/ui/BaseView.spec.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
/// <reference path="TestGame.ts" />
|
||||
/// <reference path="BaseView.ts" />
|
||||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
function inview_it(desc: string, func: (view: BaseView) => void) {
|
||||
var view = new BaseView();
|
||||
ingame_it(desc, (game: Phaser.Game, state: Phaser.State) => {
|
||||
func(view);
|
||||
}, view);
|
||||
}
|
||||
|
||||
describe("BaseView", () => {
|
||||
inview_it("initializes variables", function (view) {
|
||||
expect(view.messages instanceof Messages).toBe(true);
|
||||
expect(view.inputs instanceof InputManager).toBe(true);
|
||||
|
||||
expect(view.getWidth()).toEqual(1920);
|
||||
expect(view.getHeight()).toEqual(1080);
|
||||
expect(view.getMidWidth()).toEqual(960);
|
||||
expect(view.getMidHeight()).toEqual(540);
|
||||
});
|
||||
});
|
||||
}
|
18
src/ui/Boot.spec.ts
Normal file
18
src/ui/Boot.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/// <reference path="TestGame.ts" />
|
||||
/// <reference path="Boot.ts" />
|
||||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
function inview_it(desc: string, func: (view: Boot) => void) {
|
||||
var view = new Boot();
|
||||
ingame_it(desc, (game: Phaser.Game, state: Phaser.State) => {
|
||||
func(view);
|
||||
}, view);
|
||||
}
|
||||
|
||||
describe("Boot", () => {
|
||||
inview_it("places empty loading background", function (view) {
|
||||
expect(view.world.children.length).toBe(1);
|
||||
expect(view.world.children[0] instanceof Phaser.Image).toBe(true);
|
||||
});
|
||||
});
|
||||
}
|
26
src/ui/MainMenu.spec.ts
Normal file
26
src/ui/MainMenu.spec.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
/// <reference path="TestGame.ts" />
|
||||
/// <reference path="MainMenu.ts" />
|
||||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
function inview_it(desc: string, func: (view: MainMenu) => void) {
|
||||
var view = new MainMenu();
|
||||
ingame_it(desc, (game: Phaser.Game, state: Phaser.State) => {
|
||||
func(view);
|
||||
}, view);
|
||||
}
|
||||
|
||||
describe("MainMenu", () => {
|
||||
inview_it("adds moving stars, a title and three buttons", function (view) {
|
||||
expect(view.world.children.length).toBe(301);
|
||||
|
||||
let group = <Phaser.Group>view.world.children[300];
|
||||
expect(group instanceof Phaser.Group).toBe(true);
|
||||
|
||||
expect(group.children.length).toBe(4);
|
||||
expect(group.children[0] instanceof Phaser.Button).toBe(true);
|
||||
expect(group.children[1] instanceof Phaser.Button).toBe(true);
|
||||
expect(group.children[2] instanceof Phaser.Button).toBe(true);
|
||||
expect(group.children[3] instanceof Phaser.Image).toBe(true);
|
||||
});
|
||||
});
|
||||
}
|
18
src/ui/Preload.spec.ts
Normal file
18
src/ui/Preload.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/// <reference path="TestGame.ts" />
|
||||
/// <reference path="Preload.ts" />
|
||||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
function inview_it(desc: string, func: (view: Preload) => void) {
|
||||
var view = new Preload();
|
||||
ingame_it(desc, (game: Phaser.Game, state: Phaser.State) => {
|
||||
func(view);
|
||||
}, view);
|
||||
}
|
||||
|
||||
describe("Preload", () => {
|
||||
inview_it("loads correctly", function (view) {
|
||||
expect(view.game.state.current).toEqual("test");
|
||||
// TODO test asset loading
|
||||
});
|
||||
});
|
||||
}
|
|
@ -90,16 +90,16 @@ module TS.SpaceTac.UI {
|
|||
this.game.state.start("mainmenu");
|
||||
}
|
||||
|
||||
private loadShip(name: string) {
|
||||
loadShip(name: string) {
|
||||
this.loadImage("ship/" + name + "/sprite.png");
|
||||
this.loadImage("ship/" + name + "/portrait.png");
|
||||
}
|
||||
|
||||
private loadImage(path: string) {
|
||||
loadImage(path: string) {
|
||||
this.load.image(path.replace(/\//g, "-").replace(".png", "").replace(".jpg", ""), "assets/images/" + path);
|
||||
}
|
||||
|
||||
private loadSound(path: string) {
|
||||
loadSound(path: string) {
|
||||
var key = path.replace(/\//g, "-").replace(".wav", "").replace(".mp3", "");
|
||||
this.load.audio(key, "assets/sounds/" + path);
|
||||
}
|
||||
|
|
18
src/ui/Router.spec.ts
Normal file
18
src/ui/Router.spec.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
/// <reference path="TestGame.ts" />
|
||||
/// <reference path="Router.ts" />
|
||||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
function inview_it(desc: string, func: (view: Router) => void) {
|
||||
var view = new Router();
|
||||
ingame_it(desc, (game: Phaser.Game, state: Phaser.State) => {
|
||||
func(view);
|
||||
}, view);
|
||||
}
|
||||
|
||||
describe("Router", () => {
|
||||
inview_it("loads correctly", function (view) {
|
||||
expect(view.game.state.current).toEqual("test");
|
||||
// TODO test routing
|
||||
});
|
||||
});
|
||||
}
|
|
@ -11,9 +11,6 @@ module TS.SpaceTac.UI {
|
|||
} else if (session.getBattle()) {
|
||||
// A battle is raging, go to it
|
||||
this.game.state.start("battle", true, false, session.player, session.getBattle());
|
||||
} else if (ui.getFocusedStar()) {
|
||||
// Go to the focused star system
|
||||
this.game.state.start("starsystem", true, false, ui.star, session.player);
|
||||
} else if (session.hasUniverse()) {
|
||||
// Go to the universe map
|
||||
this.game.state.start("universe", true, false, session.universe, session.player);
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
module TS.SpaceTac.UI.Specs {
|
||||
// Test game wrapper (use instead of jasmine 'it')
|
||||
export function ingame_it(desc: string, func: (game: Phaser.Game, state: Phaser.State) => void,
|
||||
export function ingame_it(desc: string, func: (game: MainUI, state: Phaser.State) => void,
|
||||
state: Phaser.State = null, ...stateargs: any[]) {
|
||||
it(desc, (done: () => void) => {
|
||||
spyOn(console, "log").and.stub();
|
||||
|
@ -11,6 +11,9 @@ module TS.SpaceTac.UI.Specs {
|
|||
|
||||
var game = new MainUI(true);
|
||||
|
||||
spyOn(game.load, 'image').and.stub();
|
||||
spyOn(game.load, 'audio').and.stub();
|
||||
|
||||
if (!state) {
|
||||
state = new Phaser.State();
|
||||
}
|
||||
|
|
|
@ -2,18 +2,18 @@ module TS.SpaceTac.UI {
|
|||
// Utility functions for sounds
|
||||
export class Audio {
|
||||
|
||||
private game: Phaser.Game;
|
||||
private game: MainUI;
|
||||
|
||||
private music: Phaser.Sound;
|
||||
|
||||
constructor(game: Phaser.Game) {
|
||||
constructor(game: MainUI) {
|
||||
this.game = game;
|
||||
this.music = null;
|
||||
}
|
||||
|
||||
// Check if the sound system is up and running
|
||||
isActive(): boolean {
|
||||
return this.game.sound.context ? true : false;
|
||||
return !this.game.headless && this.game.sound.context;
|
||||
}
|
||||
|
||||
// Play a ponctual sound
|
||||
|
|
Loading…
Reference in a new issue