package com.gebauz.watergame.desktop.tests;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Vector;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.bauzoid2.game.Engine;
import com.gebauz.bauzoid2.graphics.sprite.SpriteSheet;
import com.gebauz.bauzoid2.graphics.sprite.SpriteSheetDefinition;
import com.gebauz.bauzoid2.math.MathUtil;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith
(MockitoJUnitRunner.
class)
public class SpriteSheetTest
{
public static final float EPSILON = 0.00001f
;
@Mock
AssetManager mockAssets
;
@Mock
Texture mockTexture
;
@Mock
SpriteSheetDefinition mockSheetDefinition
;
@Mock
Vector<SpriteSheetDefinition.
FrameInfo> mockFrameInfoVector
;
public SpriteSheet createTestSpriteSheet
(SpriteSheetDefinition.
FrameInfo[] frames,
int texWidth,
int texHeight
)
{
Engine.
assets = mockAssets
;
Mockito.
when(mockAssets.
get("texture.png")).
thenReturn(mockTexture
);
SpriteSheetDefinition def =
new SpriteSheetDefinition
();
if (frames ==
null)
{
Mockito.
when(mockFrameInfoVector.
size()).
thenReturn(0);
}
else
{
for (int i =
0; i
< frames.
length; i++
)
{
Mockito.
when(mockFrameInfoVector.
get(i
)).
thenReturn(frames
[i
]);
}
Mockito.
when(mockFrameInfoVector.
size()).
thenReturn(frames.
length);
}
def.
frames = mockFrameInfoVector
;
Mockito.
when(mockAssets.
get("atlas.sheet")).
thenReturn(def
);
Mockito.
when(mockTexture.
getWidth()).
thenReturn(texWidth
);
Mockito.
when(mockTexture.
getHeight()).
thenReturn(texHeight
);
SpriteSheet spriteSheet =
new SpriteSheet
("texture.png",
"atlas.sheet");
spriteSheet.
initAsync();
spriteSheet.
init();
return spriteSheet
;
}
@Test
public void testSpriteSheetCreationWithTexture
()
{
Engine.
assets = mockAssets
;
Mockito.
when(mockAssets.
get("texture.png")).
thenReturn(mockTexture
);
SpriteSheet spriteSheet =
new SpriteSheet
("texture.png");
spriteSheet.
initAsync();
spriteSheet.
init();
assertNotNull
("sprite texture is null!", spriteSheet.
getTexture());
assertSame
("sprite texture is not the same!", spriteSheet.
getTexture(), mockTexture
);
}
@Test
public void testSpriteSheetCreationWithEmptyDefinition
()
{
SpriteSheet spriteSheet = createTestSpriteSheet
(null,
150,
100);
assertNotNull
("sprite texture is null!", spriteSheet.
getTexture());
assertSame
("sprite texture is not the same!", spriteSheet.
getTexture(), mockTexture
);
}
@Test
public void testSpriteSheetCreationWithFilledDefinition
()
{
SpriteSheetDefinition.
FrameInfo[] frames =
new SpriteSheetDefinition.
FrameInfo[3];
frames
[0] =
new SpriteSheetDefinition.
FrameInfo(15,
15,
30,
30);
frames
[1] =
new SpriteSheetDefinition.
FrameInfo(55,
25,
10,
39);
frames
[2] =
new SpriteSheetDefinition.
FrameInfo(348,
180,
400,
2);
int texWidth =
150;
int texHeight =
100;
SpriteSheet spriteSheet = createTestSpriteSheet
(frames, texWidth, texHeight
);
assertNotNull
("sprite texture is null!", spriteSheet.
getTexture());
assertSame
("sprite texture is not the same!", spriteSheet.
getTexture(), mockTexture
);
assertNotNull
("sprite region is not the same!", spriteSheet.
getRegion(0));
for (int i =
0; i
< frames.
length; i++
)
{
/*assertEquals("Value wrong [" + i + "]: left", frames[i].x, spriteSheet.getRegion(i).left, EPSILON);
assertEquals("Value wrong [" + i + "]: top", frames[i].y, spriteSheet.getRegion(i).top, EPSILON);
assertEquals("Value wrong [" + i + "]: right", frames[i].x + frames[i].w, spriteSheet.getRegion(i).right, EPSILON);
assertEquals("Value wrong [" + i + "]: bottom", frames[i].y + frames[i].h, spriteSheet.getRegion(i).bottom, EPSILON);*/
float left = MathUtil.
clamp(frames
[i
].
x,
0, texWidth-
1);
float right = MathUtil.
clamp(frames
[i
].
x + frames
[i
].
w,
0, texWidth-
1);
float top = MathUtil.
clamp(frames
[i
].
y,
0, texHeight-
1);
float bottom = MathUtil.
clamp(frames
[i
].
y + frames
[i
].
h,
0, texHeight-
1);
assertEquals
("Value wrong [" + i +
"]: left",
Math.
min(left, right
), spriteSheet.
getRegion(i
).
left, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: top",
Math.
min(top, bottom
), spriteSheet.
getRegion(i
).
top, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: right",
Math.
max(left, right
), spriteSheet.
getRegion(i
).
right, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: bottom",
Math.
max(top, bottom
), spriteSheet.
getRegion(i
).
bottom, EPSILON
);
}
}
@Test
public void testSpriteSheetCreationWithFilledDefinitionNegativeValues
()
{
SpriteSheetDefinition.
FrameInfo[] frames =
new SpriteSheetDefinition.
FrameInfo[3];
frames
[0] =
new SpriteSheetDefinition.
FrameInfo(15,
15, -
30,
30);
frames
[1] =
new SpriteSheetDefinition.
FrameInfo(55, -
25,
10, -
39);
frames
[2] =
new SpriteSheetDefinition.
FrameInfo(-
348, -
180, -
400, -
2);
int texWidth =
150;
int texHeight =
100;
SpriteSheet spriteSheet = createTestSpriteSheet
(frames, texWidth, texHeight
);
assertNotNull
("sprite texture is null!", spriteSheet.
getTexture());
assertSame
("sprite texture is not the same!", spriteSheet.
getTexture(), mockTexture
);
assertNotNull
("sprite region is not the same!", spriteSheet.
getRegion(0));
for (int i =
0; i
< frames.
length; i++
)
{
float left = MathUtil.
clamp(frames
[i
].
x,
0, texWidth-
1);
float right = MathUtil.
clamp(frames
[i
].
x + frames
[i
].
w,
0, texWidth-
1);
float top = MathUtil.
clamp(frames
[i
].
y,
0, texHeight-
1);
float bottom = MathUtil.
clamp(frames
[i
].
y + frames
[i
].
h,
0, texHeight-
1);
assertEquals
("Value wrong [" + i +
"]: left",
Math.
min(left, right
), spriteSheet.
getRegion(i
).
left, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: top",
Math.
min(top, bottom
), spriteSheet.
getRegion(i
).
top, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: right",
Math.
max(left, right
), spriteSheet.
getRegion(i
).
right, EPSILON
);
assertEquals
("Value wrong [" + i +
"]: bottom",
Math.
max(top, bottom
), spriteSheet.
getRegion(i
).
bottom, EPSILON
);
assertSame
("Spritesheet not same", spriteSheet, spriteSheet.
getRegion(i
).
getParent());
}
}
}