1313import java .io .InputStreamReader ;
1414
1515public class SpriteAnimationTest {
16- private SpriteAtlas atlas ;
17- private JSONObject animationJSONData ;
18-
19- @ Before
20- public void loadAtlas () throws IOException , ParseException {
21- final FileInputStream atlasImageStream = new FileInputStream ("test_res/Atlas.png" );
22- FileInputStream atlasJSONStream = new FileInputStream ("test_res/Atlas.json" );
23- atlas = new SpriteAtlas (atlasImageStream , atlasJSONStream );
24-
25- atlasJSONStream = new FileInputStream ("test_res/Atlas.json" );
26- final JSONParser parser = new JSONParser ();
27- final JSONObject atlasData = (JSONObject ) parser .parse (new InputStreamReader (atlasJSONStream ));
28- final JSONArray sheetDataArray = (JSONArray ) atlasData .get ("Sheets" );
29- final JSONObject spriteJSONData = (JSONObject ) sheetDataArray .get (0 );
30- final JSONArray animDataArray = (JSONArray ) spriteJSONData .get ("Animations" );
31- animationJSONData = (JSONObject ) animDataArray .get (0 );
32- atlasJSONStream .close ();
33- }
16+ private final String pngFilePath = "test_res/Atlas.png" ;
17+ private final String jsonFilePath = "test_res/Atlas.json" ;
3418
3519 @ Test
36- public void testConstructor_withValidParams () {
37- final SpriteAnimation animation = new SpriteAnimation (atlas , animationJSONData );
20+ public void testConstructor_withValidParams () throws IOException , ParseException {
21+ final SpriteAtlas atlas = SpriteAtlas .createSpriteAtlas (pngFilePath , jsonFilePath );
22+ final SpriteSheet sheet = atlas .getSpriteSheet ("Player" );
23+ final SpriteAnimation animation = sheet .getAnimation ("Standing" );
3824 Assert .assertEquals ("Standing" , animation .getName ());
3925 Assert .assertEquals (3 , animation .getTotalFrames ());
4026 Assert .assertEquals (0 , animation .getCurFrame ());
4127 }
4228
43- @ Test (expected =NullPointerException .class )
44- public void testConstructor_withNullAtlas () {
45- new SpriteAnimation (null , animationJSONData );
46- }
47-
48- @ Test (expected =NullPointerException .class )
49- public void testConstructor_withNullAnimationData () {
50- new SpriteAnimation (atlas , null );
51- }
52-
5329 @ Test
54- public void testReset () {
55- final SpriteAnimation animation = new SpriteAnimation (atlas , animationJSONData );
30+ public void testReset () throws IOException , ParseException {
31+ final SpriteAtlas atlas = SpriteAtlas .createSpriteAtlas (pngFilePath , jsonFilePath );
32+ final SpriteSheet sheet = atlas .getSpriteSheet ("Player" );
33+ final SpriteAnimation animation = sheet .getAnimation ("Standing" );
5634 Assert .assertEquals (3 , animation .getTotalFrames ());
5735 Assert .assertEquals (0 , animation .getCurFrame ());
5836
@@ -64,8 +42,10 @@ public void testReset() {
6442 }
6543
6644 @ Test
67- public void testToNextFrame () {
68- final SpriteAnimation animation = new SpriteAnimation (atlas , animationJSONData );
45+ public void testToNextFrame () throws IOException , ParseException {
46+ final SpriteAtlas atlas = SpriteAtlas .createSpriteAtlas (pngFilePath , jsonFilePath );
47+ final SpriteSheet sheet = atlas .getSpriteSheet ("Player" );
48+ final SpriteAnimation animation = sheet .getAnimation ("Standing" );
6949 Assert .assertEquals (3 , animation .getTotalFrames ());
7050 Assert .assertEquals (0 , animation .getCurFrame ());
7151
@@ -80,8 +60,10 @@ public void testToNextFrame() {
8060 }
8161
8262 @ Test
83- public void testToPreviousFrame () {
84- final SpriteAnimation animation = new SpriteAnimation (atlas , animationJSONData );
63+ public void testToPreviousFrame () throws IOException , ParseException {
64+ final SpriteAtlas atlas = SpriteAtlas .createSpriteAtlas (pngFilePath , jsonFilePath );
65+ final SpriteSheet sheet = atlas .getSpriteSheet ("Player" );
66+ final SpriteAnimation animation = sheet .getAnimation ("Standing" );
8567 Assert .assertEquals (3 , animation .getTotalFrames ());
8668 Assert .assertEquals (0 , animation .getCurFrame ());
8769
@@ -96,8 +78,10 @@ public void testToPreviousFrame() {
9678 }
9779
9880 @ Test
99- public void testTotalFrames () {
100- final SpriteAnimation animation = new SpriteAnimation (atlas , animationJSONData );
81+ public void testTotalFrames () throws IOException , ParseException {
82+ final SpriteAtlas atlas = SpriteAtlas .createSpriteAtlas (pngFilePath , jsonFilePath );
83+ final SpriteSheet sheet = atlas .getSpriteSheet ("Player" );
84+ final SpriteAnimation animation = sheet .getAnimation ("Standing" );
10185 Assert .assertEquals (3 , animation .getTotalFrames ());
10286 }
10387}
0 commit comments