1- package evhh .model ;
21
32import evhh .controller .InputManager .UserInputManager ;
3+ import evhh .model .GameInstance ;
4+ import evhh .model .GameObject ;
5+ import evhh .model .Grid ;
6+ import evhh .model .gamecomponents .Sprite ;
47import evhh .view .renderers .FrameRenderer ;
58import evhh .view .renderers .GameFrame ;
69import functional .testingcomponents .TestComponent ;
710import org .junit .jupiter .api .AfterEach ;
811import org .junit .jupiter .api .BeforeEach ;
912import org .junit .jupiter .api .Test ;
1013
14+ import javax .imageio .ImageIO ;
15+ import java .awt .image .BufferedImage ;
16+ import java .io .File ;
17+ import java .io .IOException ;
18+ import java .util .Arrays ;
19+ import java .util .HashMap ;
20+
1121import static org .junit .jupiter .api .Assertions .*;
1222
1323/***********************************************************************************************************************
2131class GameInstanceTest
2232{
2333
24- private int DEFAULT_GRID_WIDTH = 16 ;
25- private int DEFAULT_GRID_HEIGHT = 16 ;
26- private int DEFAULT_CELL_SIZE = 32 ;
34+ private final int DEFAULT_GRID_WIDTH = 16 ;
35+ private final int DEFAULT_GRID_HEIGHT = 16 ;
36+ private final int DEFAULT_CELL_SIZE = 32 ;
37+ private final String GRID_SAVE_PATH = System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets/TestGridSave.ser" ;
2738 private GameInstance game1 ;
2839 private FrameRenderer frameRenderer ;
2940 private Grid mainGrid ;
3041 private UserInputManager userInputManager ;
3142 private GameObject testObject ;
3243 private TestComponent testComponent ;
44+ private BufferedImage img1 ,img2 ,img3 ;
3345 @ BeforeEach
3446 void setUp ()
3547 {
@@ -48,12 +60,26 @@ void setUp()
4860 game1 .setMainGrid (mainGrid );
4961 game1 .setUserInputManager (userInputManager );
5062 game1 .addRendererTimer (100 );
51- game1 .loadTextureAssets (System .getProperty ("user.dir" )+"\\ Assets\\ Images" );
63+ // game1.loadTextureAssets(System.getProperty("user.dir")+"\\Assets\\Images");
5264 game1 .setUpdateTimer (100 );
5365
5466 testObject = new GameObject (game1 .getMainGrid (),false ,0 ,0 );
5567 testComponent = new TestComponent (testObject );
5668 testObject .addComponent (testComponent );
69+ // /src/evhh/Testing/TestAssets/img1.png
70+
71+
72+ try
73+ {
74+ img1 = ImageIO .read (new File (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets/img1.png" ));
75+ img2 = ImageIO .read (new File (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets/img2.png" ));
76+ img3 = ImageIO .read (new File (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets/img3.png" ));
77+
78+ } catch (IOException e )
79+ {
80+ e .printStackTrace ();
81+ fail ();
82+ }
5783
5884 }
5985
@@ -73,11 +99,12 @@ void setUpdateTimer()
7399
74100 try
75101 {
76- game1 .setUpdateTimer (1 );
102+ game1 .addGameObject (testObject ,1 ,1 );
103+ game1 .setUpdateTimer (10 );
77104 game1 .start ();
78105 Thread .sleep (100 );
79106 game1 .exit ();
80- assertEquals ( 100 ,testComponent .numUpdates );
107+ assertNotEquals ( 0 ,testComponent .numUpdates );
81108 assertTrue (testComponent .ranStart );
82109 assertTrue (testComponent .ranExit );
83110
@@ -97,7 +124,9 @@ void setAllowedTextureFileExtension()
97124 void loadTextureAssets ()
98125 {
99126 try {
100- game1 .loadTextureAssets (System .getProperty ("user.dir" ) + "/Assets/Images" );
127+ game1 .loadTextureAssets (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets" );
128+ assertNotNull (game1 .getTextures ());
129+ assertNotEquals (0 ,game1 .getTextures ().size ());
101130
102131 }catch (Exception e )
103132 {
@@ -106,54 +135,148 @@ void loadTextureAssets()
106135 }
107136 }
108137
109- @ Test
110- void testLoadTextureAssets ()
111- {
112- }
113-
114138 @ Test
115139 void getTextures ()
116140 {
141+ try {
142+ game1 .loadTextureAssets (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets" );
143+ HashMap <String , BufferedImage > textures = game1 .getTextures ();
144+ assertNotNull (textures );
145+ assertNotEquals (0 , textures .size ());
146+ assertEquals (3 ,textures .size ());
147+ assertArrayEquals (textures .keySet ().toArray (), new String []{"img3" ,"img2" ,"img1" });
148+
149+ }catch (Exception e )
150+ {
151+ e .printStackTrace ();
152+ fail ();
153+ }
117154 }
118155
119156 @ Test
120157 void getTexture ()
121158 {
159+ try {
160+ game1 .loadTextureAssets (System .getProperty ("user.dir" ) + "/src/evhh/Testing/TestAssets" );
161+ assertNotNull (game1 .getTexture ("img1" ));
162+ assertTrue (compareImages (img1 ,game1 .getTexture ("img1" )));
163+
164+ }catch (Exception e )
165+ {
166+ e .printStackTrace ();
167+ fail ();
168+ }
122169 }
123170
124171 @ Test
125172 void setMainGrid ()
126173 {
174+ try {
175+ Grid prevGrid = game1 .getMainGrid ();
176+ prevGrid .addGameObject (testObject ,1 ,1 );
177+
178+ game1 .setMainGrid (new Grid (prevGrid .getGridWidth (),prevGrid .getGridHeight ()));
179+ assertNotEquals (prevGrid ,game1 .getMainGrid ());
180+ assertTrue (game1 .getMainGrid ().isEmpty (1 ,1 ));
181+ assertEquals (0 , game1 .getMainGrid ().getDynamicObjects ().size ());
182+
183+ game1 .setMainGrid (new Grid (prevGrid .getGridWidth ()*2 ,prevGrid .getGridHeight ()*2 ));
184+ assertNotEquals (prevGrid ,game1 .getMainGrid ());
185+ assertTrue (game1 .getMainGrid ().isEmpty (1 ,1 ));
186+ assertEquals (0 , game1 .getMainGrid ().getDynamicObjects ().size ());
187+ game1 .setMainGrid (new Grid (prevGrid .getGridWidth ()*2 ,prevGrid .getGridHeight ()*2 ));
188+
189+ assertThrows (AssertionError .class ,()->{game1 .setMainGrid (null );});
190+
191+ }catch (Exception e )
192+ {
193+ e .printStackTrace ();
194+ fail ();
195+ }
196+
127197 }
128198
129199 @ Test
130200 void addGameObject ()
131201 {
202+ try
203+ {
204+ game1 .addGameObject (testObject ,1 ,1 );
205+ assertThrows (IndexOutOfBoundsException .class ,()->game1 .addGameObject (testObject ,-1 ,-1 ));
206+ assertEquals (testObject ,game1 .getGameObject (1 ,1 ));
207+ assertEquals (testObject ,mainGrid .get (1 ,1 ));
208+ assertTrue (game1 .getMainGrid ().getDynamicObjects ().contains (testObject ));
209+ }
210+ catch (Exception e )
211+ {
212+ e .printStackTrace ();
213+ fail ();
214+ }
132215 }
133216
134- @ Test
135- void testAddGameObject ()
136- {
137- }
138217
139218 @ Test
140219 void getGameObject ()
141220 {
142- }
143-
144- @ Test
145- void testGetGameObject ()
146- {
221+ try
222+ {
223+ game1 .addGameObject (testObject ,1 ,1 );
224+ assertEquals (testObject ,game1 .getGameObject (1 ,1 ));
225+ assertThrows (AssertionError .class ,()->game1 .getGameObject (-1 ,-1 ));
226+ assertNotEquals (testObject ,mainGrid .get (2 ,2 ));
227+ }
228+ catch (Exception e )
229+ {
230+ e .printStackTrace ();
231+ fail ();
232+ }
147233 }
148234
149235 @ Test
150236 void getGameObjects ()
151237 {
238+ try
239+ {
240+
241+ GameObject [] constObjs = new GameObject [Math .min (mainGrid .getGridWidth (),mainGrid .getGridHeight ())];
242+ for (int i = 0 ; i <Math .min (mainGrid .getGridWidth (),mainGrid .getGridHeight ()); i ++)
243+ {
244+ constObjs [i ] = getTestObject (game1 .getMainGrid (), true , i , i );
245+ game1 .addGameObject (constObjs [i ],i ,i );
246+ }
247+ GameObject [] retObjects = game1 .getGameObjects (TestComponent .class );
248+ assertNotNull (retObjects );
249+ assertEquals (constObjs .length ,retObjects .length );
250+ assertTrue (Arrays .stream (retObjects ).allMatch (o1 -> Arrays .asList (constObjs ).contains (o1 )));
251+
252+ assertEquals (0 ,game1 .getGameObjects (Sprite .class ).length );
253+ }
254+ catch (Exception e )
255+ {
256+ e .printStackTrace ();
257+ fail ();
258+ }
152259 }
153260
154261 @ Test
155262 void loadGridFromSave ()
156263 {
264+ try
265+ {
266+
267+ GameObject [] constObjs = new GameObject [Math .min (mainGrid .getGridWidth (),mainGrid .getGridHeight ())];
268+ for (int i = 0 ; i <Math .min (mainGrid .getGridWidth (),mainGrid .getGridHeight ()); i ++)
269+ {
270+ constObjs [i ] = getTestObject (game1 .getMainGrid (), true , i , i );
271+ game1 .addGameObject (constObjs [i ],i ,i );
272+ }
273+ game1 .saveMainGrid (GRID_SAVE_PATH );
274+ }
275+ catch (Exception e )
276+ {
277+ e .printStackTrace ();
278+ fail ();
279+ }
157280 }
158281
159282 @ Test
@@ -245,4 +368,39 @@ void startPeriodicEventChecking()
245368 void stopPeriodicEventChecking ()
246369 {
247370 }
371+
372+ /**
373+ * Compares two images pixel by pixel.
374+ * All credit goes to https://stackoverflow.com/users/1762224/mr-polywhirl
375+ * @param imgA the first image.
376+ * @param imgB the second image.
377+ * @return whether the images are both the same or not.
378+ */
379+ private static boolean compareImages (BufferedImage imgA , BufferedImage imgB ) {
380+ // The images must be the same size.
381+ if (imgA .getWidth () != imgB .getWidth () || imgA .getHeight () != imgB .getHeight ()) {
382+ return false ;
383+ }
384+
385+ int width = imgA .getWidth ();
386+ int height = imgA .getHeight ();
387+
388+ // Loop over every pixel.
389+ for (int y = 0 ; y < height ; y ++) {
390+ for (int x = 0 ; x < width ; x ++) {
391+ // Compare the pixels for equality.
392+ if (imgA .getRGB (x , y ) != imgB .getRGB (x , y )) {
393+ return false ;
394+ }
395+ }
396+ }
397+
398+ return true ;
399+ }
400+ private static GameObject getTestObject (Grid grid ,boolean isStatic ,int x , int y )
401+ {
402+ GameObject testObject = new GameObject (grid ,isStatic ,x ,y );
403+ testObject .addComponent (new TestComponent (testObject ));
404+ return testObject ;
405+ }
248406}
0 commit comments