Skip to content

Commit 3aafad6

Browse files
committed
Add tests in GameInstanceTest.java
1 parent f7fd328 commit 3aafad6

File tree

4 files changed

+272
-28
lines changed

4 files changed

+272
-28
lines changed

src/evhh/Testing/evhh/model/GameInstanceTest.java

Lines changed: 190 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -282,93 +282,257 @@ void loadGridFromSave()
282282
@Test
283283
void testLoadGridFromSave()
284284
{
285-
}
285+
try
286+
{
286287

287-
@Test
288-
void saveMainGrid()
289-
{
290-
}
288+
GameObject[] constObjs = new GameObject[Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight())];
289+
for (int i = 0; i <Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight()); i++)
290+
{
291+
constObjs[i] = getTestObject(game1.getMainGrid(), true, i, i);
292+
game1.addGameObject(constObjs[i],i,i);
293+
}
294+
game1.saveMainGrid(GRID_SAVE_PATH);
291295

292-
@Test
293-
void testSaveMainGrid()
294-
{
295-
}
296+
game1.loadGridFromSave(GRID_SAVE_PATH);
297+
assertNotEquals(mainGrid, game1.getMainGrid());
298+
assertEquals(constObjs.length ,game1.getMainGrid().getStaticObjects().size());
299+
assertEquals(constObjs.length ,game1.getGameObjects(TestComponent.class).length);
296300

297-
@Test
298-
void getGridSavePath()
299-
{
301+
}
302+
catch (Exception e)
303+
{
304+
e.printStackTrace();
305+
fail();
306+
}
300307
}
301308

309+
302310
@Test
303311
void addSavedGridPath()
304312
{
313+
try
314+
{
315+
316+
GameObject[] constObjs = new GameObject[Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight())];
317+
for (int i = 0; i <Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight()); i++)
318+
{
319+
constObjs[i] = getTestObject(game1.getMainGrid(), true, i, i);
320+
game1.addGameObject(constObjs[i],i,i);
321+
}
322+
game1.saveMainGrid(GRID_SAVE_PATH);
323+
324+
game1.addSavedGridPath(GRID_SAVE_PATH);
325+
game1.switchGrid(0);
326+
assertNotEquals(mainGrid, game1.getMainGrid());
327+
assertEquals(constObjs.length ,game1.getMainGrid().getStaticObjects().size());
328+
assertEquals(constObjs.length ,game1.getGameObjects(TestComponent.class).length);
329+
330+
}
331+
catch (Exception e)
332+
{
333+
e.printStackTrace();
334+
fail();
335+
}
305336
}
306337

307338
@Test
308339
void switchGrid()
309340
{
341+
try
342+
{
343+
344+
GameObject[] constObjs = new GameObject[Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight())];
345+
for (int i = 0; i <Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight()); i++)
346+
{
347+
constObjs[i] = getTestObject(game1.getMainGrid(), true, i, i);
348+
game1.addGameObject(constObjs[i],i,i);
349+
}
350+
game1.saveMainGrid(GRID_SAVE_PATH);
351+
352+
game1.addSavedGridPath(GRID_SAVE_PATH);
353+
game1.addSavedGridPath(GRID_SAVE_PATH);
354+
game1.addSavedGridPath(GRID_SAVE_PATH);
355+
game1.addSavedGridPath(GRID_SAVE_PATH);
356+
game1.addSavedGridPath(GRID_SAVE_PATH);
357+
for (int i = 0; i < 4; i++)
358+
{
359+
game1.switchGrid(i);
360+
361+
assertNotEquals(mainGrid, game1.getMainGrid());
362+
assertEquals(constObjs.length ,game1.getMainGrid().getStaticObjects().size());
363+
assertEquals(constObjs.length ,game1.getGameObjects(TestComponent.class).length);
364+
}
365+
assertThrows(AssertionError.class,()->game1.switchGrid(5));
366+
}
367+
catch (Exception e)
368+
{
369+
e.printStackTrace();
370+
fail();
371+
}
310372
}
311373

312374
@Test
313375
void reloadGrid()
314376
{
377+
try
378+
{
379+
380+
GameObject[] constObjs = new GameObject[Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight())];
381+
for (int i = 0; i <Math.min(mainGrid.getGridWidth(),mainGrid.getGridHeight()); i++)
382+
{
383+
constObjs[i] = getTestObject(game1.getMainGrid(), true, i, i);
384+
game1.addGameObject(constObjs[i],i,i);
385+
}
386+
game1.saveMainGrid(GRID_SAVE_PATH);
387+
388+
game1.addSavedGridPath(GRID_SAVE_PATH);
389+
game1.switchGrid(0);
390+
for (int i = 0; i < 100; i++)
391+
{
392+
Grid prevGrid = game1.getMainGrid();
393+
game1.reloadGrid();
394+
assertNotEquals(prevGrid, game1.getMainGrid());
395+
assertEquals(constObjs.length ,game1.getMainGrid().getStaticObjects().size());
396+
assertEquals(constObjs.length ,game1.getGameObjects(TestComponent.class).length);
397+
}
398+
assertThrows(AssertionError.class,()->game1.switchGrid(5));
399+
}
400+
catch (Exception e)
401+
{
402+
e.printStackTrace();
403+
fail();
404+
}
315405
}
316406

317407
@Test
318408
void refreshSpritesInRenderer()
319409
{
320-
}
410+
try
411+
{
412+
game1.loadTextureAssets(System.getProperty("user.dir")+"\\Assets\\Images");
413+
GameObject[] gameObjects = new GameObject[Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight())];
414+
for (int i = 0; i < Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight()); i++)
415+
{
416+
gameObjects[i] = getTestObject(game1.getMainGrid(),false,i,i);
417+
gameObjects[i].addComponent(new Sprite(gameObjects[i],game1.getTexture("img1"),"img1"));
418+
game1.addGameObject(gameObjects[i],i,i);
321419

322-
@Test
323-
void addSprite()
324-
{
325-
}
420+
}
421+
game1.refreshSpritesInRenderer();
422+
assertTrue(game1.getFrameRenderer().getGridRenderer().getSprites().stream().allMatch(sprite -> Arrays.stream(gameObjects).anyMatch(gameObject -> gameObject.getSprite().equals(sprite))));
326423

327-
@Test
328-
void addRendererTimer()
329-
{
424+
}
425+
catch (Exception e)
426+
{
427+
e.printStackTrace();
428+
fail();
429+
}
330430
}
331431

332432
@Test
333-
void testAddRendererTimer()
433+
void addSprite()
334434
{
435+
try
436+
{
437+
game1.loadTextureAssets(System.getProperty("user.dir")+"\\Assets\\Images");
438+
GameObject[] gameObjects = new GameObject[Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight())];
439+
for (int i = 0; i < Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight()); i++)
440+
{
441+
gameObjects[i] = getTestObject(game1.getMainGrid(),false,i,i);
442+
gameObjects[i].addComponent(new Sprite(gameObjects[i],game1.getTexture("img1"),"img1"));
443+
game1.addGameObject(gameObjects[i],i,i);
444+
game1.addSprite(gameObjects[i].getSprite());
445+
446+
}
447+
assertTrue(game1.getFrameRenderer().getGridRenderer().getSprites().stream().allMatch(sprite -> Arrays.stream(gameObjects).anyMatch(gameObject -> gameObject.getSprite().equals(sprite))));
448+
}
449+
catch (Exception e)
450+
{
451+
e.printStackTrace();
452+
fail();
453+
}
335454
}
336455

337456
@Test
338457
void refreshMappedUserInput()
339458
{
459+
try
460+
{
461+
GameObject[] gameObjects = new GameObject[Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight())];
462+
for (int i = 0; i < Math.min(game1.getMainGrid().getGridWidth(),game1.getMainGrid().getGridHeight()); i++)
463+
{
464+
gameObjects[i] = getTestObject(game1.getMainGrid(),false,i,i);
465+
gameObjects[i].addComponent(new TestControllableComponent(gameObjects[i],game1.getUserInputManager(),i));
466+
game1.addGameObject(gameObjects[i],i,i);
467+
468+
}
469+
refreshMappedUserInput();
470+
assertTrue(game1.getFrameRenderer().getGridRenderer().getSprites().stream().allMatch(sprite -> Arrays.stream(gameObjects).anyMatch(gameObject -> gameObject.getSprite().equals(sprite))));
471+
}
472+
catch (Exception e)
473+
{
474+
e.printStackTrace();
475+
fail();
476+
}
340477
}
341478

342479
@Test
343480
void removeAllMappedUserInputFromFrame()
344481
{
482+
try
483+
{
484+
}
485+
catch (Exception e)
486+
{
487+
e.printStackTrace();
488+
fail();
489+
}
345490
}
346491

347492
@Test
348493
void removeEvent()
349494
{
495+
try
496+
{
497+
}
498+
catch (Exception e)
499+
{
500+
e.printStackTrace();
501+
fail();
502+
}
350503
}
351504

352505
@Test
353506
void addEvent()
354507
{
508+
try
509+
{
510+
}
511+
catch (Exception e)
512+
{
513+
e.printStackTrace();
514+
fail();
515+
}
355516
}
356517

357518
@Test
358519
void checkEvents()
359520
{
521+
try
522+
{
523+
}
524+
catch (Exception e)
525+
{
526+
e.printStackTrace();
527+
fail();
528+
}
360529
}
361530

362531
@Test
363532
void startPeriodicEventChecking()
364533
{
365534
}
366535

367-
@Test
368-
void stopPeriodicEventChecking()
369-
{
370-
}
371-
372536
/**
373537
* Compares two images pixel by pixel.
374538
* All credit goes to https://stackoverflow.com/users/1762224/mr-polywhirl
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import evhh.common.RunnableArg;
2+
import evhh.controller.InputManager.KeyboardInput;
3+
import evhh.controller.InputManager.UserInputManager;
4+
import evhh.model.ControllableComponent;
5+
import evhh.model.GameObject;
6+
7+
import java.awt.event.KeyEvent;
8+
import java.util.HashMap;
9+
10+
/***********************************************************************************************************************
11+
* @project: MainProject
12+
* @package: PACKAGE_NAME
13+
* ---------------------------------------------------------------------------------------------------------------------
14+
* @authors: Hamed Haghjo & Elias Vahlberg
15+
* @date: 2021-05-27
16+
* @time: 19:15
17+
**********************************************************************************************************************/
18+
public class TestControllableComponent extends ControllableComponent
19+
{
20+
private int keyCode;
21+
transient KeyboardInput keyboardInput;
22+
public boolean hasBeenPressed = false;
23+
public int numberOfTimesPressed = 0;
24+
25+
26+
/**
27+
* @param parent Parent GameObject passed to GameComponent default constructor
28+
* @param uIM The UserInputManager where KeyboardInput/MouseInput is stored
29+
*/
30+
public TestControllableComponent(GameObject parent, UserInputManager uIM, int keyCode)
31+
{
32+
super(parent, uIM);
33+
this.uIM = uIM;
34+
this.keyCode = keyCode;
35+
RunnableArg<KeyEvent> keyInputEvent = keyEvent -> pressed(keyEvent.getKeyCode());
36+
HashMap<Integer,Integer> map = new HashMap<>();
37+
38+
map.put(keyCode,KeyEvent.KEY_PRESSED);
39+
keyboardInput = new KeyboardInput(keyInputEvent,map);
40+
uIM.addKeyInput(keyboardInput);
41+
}
42+
43+
@Override
44+
public void onUIMRefresh(UserInputManager uIM)
45+
{
46+
this.uIM = uIM;
47+
RunnableArg<KeyEvent> keyInputEvent = keyEvent -> pressed(keyEvent.getKeyCode());
48+
HashMap<Integer,Integer> map = new HashMap<>();
49+
map.put(keyCode,KeyEvent.KEY_PRESSED);
50+
keyboardInput = new KeyboardInput(keyInputEvent,map);
51+
uIM.addKeyInput(keyboardInput);
52+
}
53+
public void pressed(int key)
54+
{
55+
hasBeenPressed = true;
56+
numberOfTimesPressed++;
57+
}
58+
59+
@Override
60+
public void onStart()
61+
{
62+
63+
}
64+
65+
@Override
66+
public void update()
67+
{
68+
69+
}
70+
71+
@Override
72+
public void onExit()
73+
{
74+
75+
}
76+
}

src/evhh/Testing/functional/testingcomponents/TestPlayerComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package functional.testingcomponents;
1+
22

33
import evhh.common.RunnableArg;
44
import evhh.controller.InputManager.KeyboardInput;

0 commit comments

Comments
 (0)