-
Notifications
You must be signed in to change notification settings - Fork 4
Achievements System Testing
The GameExtension class makes the job of testing very easy by mocking most of the libgdx and game-engine classes.
@ExtendWith(GameExtension.class)
public class AchievementStatsComponentTest {
-
Java Lang Reflect: Used to access private methods and private fields of a class from the testing suite and make them accessible for testing purposes. It finds its applications when
private static
,private
, andprivate static final
variables and methods need to be altered for ease of testing. -
JUnit: Used for assertions (mostly value equality) and class
extendWitih
decorator plugins. -
Mockito: Mocking static classes and simulating return values of static methods in order to control certain variables while performing the test in order to reduce complexity.
Test achievement unlocking logic, i.e, the logic which decides that an achievement popup should be rendered on the screen.
The most sensitive logic of the achievements system is to check if - given the in game time, item count and health, a certain achievement is valid or not. An event must be dispatched if the achievement is valid given the time, item count and health constraints. An event called updateAchievement
can only be dispatched if the particular achievement satisfies the constraints, in which case it is unlocked and perfectly valid.
private Method getIsValidMethod() throws NoSuchMethodException {
Method method = AchievementsStatsComponent.class
.getDeclaredMethod("isValid",
BaseAchievementConfig.class);
method.setAccessible(true);
return method;
}
The veteran achievement gets unlocked if the user survives for 10, 15 or 20 minutes. It is assumed that the JSON achievement details configuration is valid.
@Test
void shouldCorrectlyValidateVeteranAchievements() throws
IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Method method = getIsValidMethod();
AchievementsStatsComponent component = generateEntity()
.getComponent(AchievementsStatsComponent.class);
for (BaseAchievementConfig a : getList()) {
component.setTime(999999999);
component.setHealth(-1);
if (a.name.equals("Veteran")) {
assertEquals(method.invoke(component, a), true);
} else {
assertEquals(method.invoke(component, a), false);
}
}
}
- Veteran
- Game Breaker
- Tool Master
Test GameRecords
and GameInfo
persistence logic. More information about this flow can be found here
This setup was necessary in order to make sure that the test suite's FileLoader
does not communicate with the filesystem outside the project folder. So all the JSON operations were redirected to test files inside source/assets/test/files
directory and thats where fully fledged test read and write operations took place. This was necessary to test the GameInfo
which kept a tab of the number of games played. The incremement logic was tested as well which happens as soon as a player dies.
/**
* Change the value of a private static final variable
*
* @param field the private static final variable field
* @param newValue the value to replace
*/
static void setFinalStatic(Field field, Object newValue) {
try {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
} catch (Exception ignored) {
}
}
@BeforeEach
void beforeEach() throws Exception {
// Changing the JSON file path so that the original game data in the EXTERNAL location is not affected
setFinalStatic(GameInfo.class.getDeclaredField("path"),
"test/files/gameInfoTest.json");
setFinalStatic(GameInfo.class.getDeclaredField("location"), FileLoader.Location.LOCAL);
}
For reference, these are the actual variables in the GameInfo.java
's class:
private static final String path = ROOT_DIR + File.separator + GAME_INFO_FILE;
private static final FileLoader.Location location = FileLoader.Location.EXTERNAL;
MockedStatic<GameRecords> gameInfoMockedStatic =
Mockito.mockStatic(GameRecords.class);
The game needs to be played in order to unlock gold achievements. Gold achievements are needed to unlock chapters. Since this is not feasible, the GameRecords
class needs to be mocked in order to return unrealistic records, suitable for testing.
private void setGoldAchievementsToBeReturned(int count) {
gameInfoMockedStatic.when(GameRecords::getGoldAchievementsCount)
.thenReturn(count);
assertEquals(GameRecords.getGoldAchievementsCount(), count);
}
@AfterEach
void afterEach() {
// IMPORTANT! Each static mock can only be registered once.
// So it needs to be deregistered after every test run
gameInfoMockedStatic.close();
}
- Master
- Healer
- Stranger
The tests can be found in AchievementStatsComponentTest.java.
This is what a typical test looks like for the Stranger achievement which involves surviving for a particular amount of time without picking up any items.
@Test
void shouldCorrectlyValidateStrangerAchievements() throws
IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
Method method = getIsValidMethod();
AchievementsStatsComponent component = generateEntity()
.getComponent(AchievementsStatsComponent.class);
component.setTime(999999999);
component.setItemCountByVal(0);
BaseAchievementConfig achievement = genAchievement("Stranger", 10, -1, 0, -1, -1);
assertEquals(method.invoke(component, achievement), true);
achievement.unlocked = false;
component.setTime(999999999);
component.setItemCountByVal(0);
achievement = genAchievement("Stranger", 15, -1, 0, -1, -1);
assertEquals(method.invoke(component, achievement), true);
achievement.unlocked = false;
component.setTime(999999999);
component.setItemCountByVal(0);
achievement = genAchievement("Stranger", 20, -1, 0, -1, -1);
assertEquals(method.invoke(component, achievement), true);
achievement.unlocked = false;
component.setItemCountByVal(-1);
assertEquals(method.invoke(component, achievement), false);
Master achievements are simpler and involve crossing certain values of the in game score.
@Test
void shouldCorrectlyValidateMasterAchievements() throws
NoSuchMethodException,
InvocationTargetException, IllegalAccessException {
Method method = getIsValidMethod();
AchievementsStatsComponent component = generateEntity()
.getComponent(AchievementsStatsComponent.class);
for (BaseAchievementConfig a : getList()) {
component.setScore(999999999);
if (a.name.equals("Master")) {
assertEquals(method.invoke(component, a), true);
} else {
assertEquals(method.invoke(component, a), false);
}
}
}
- Testing the asynchronous event queue (
AsyncTaskQueue.java
) which is used to display each popup for a fixed time interval - Refactoring
AchievementsStatsComponentTest
file to look cleaner - Test
GameRecords.java
achievement and score storage and retrieval logic - Test
AchivementsDisplay.java
logic for conditional rendering of certain widgets on the screen (based on locked and unlocked status of chapters and achievements as well as data availability.)
Camera Angle and The Player's Perspective
Achievements Trophies and Cards
πΎ Obstacle/Enemy
βMonster Manual
βObstacles/Enemies
ββ- Alien Plants
ββ- Variation thorns
ββ- Falling Meteorites
ββ- FaceHugger
ββ- AlienMonkey
βSpaceship & Map Entry
βParticle effect
[code for debuff animations](code for debuff animations)
Main Character Movement, Interactions and Animations - Code Guidelines
ItemBar & Recycle system
πΎ Obstacle/Enemy
βObstacle/Enemy
βMonster Manual
βSpaceship Boss
βParticle effects
βOther Related Code
βUML & Sequence diagram of enemies/obstacles
Scoring System Implementation Explanation
Buff and Debuff Implementation
Infinite generating terrains Implementation Explanation
Game Over Screen and functions explaination
Buffer timer before game start
Rocks and woods layout optimization
Magma and nails code implementation
Guide: Adding Background music for a particular screen
History Scoreboard - Score Details
Listening for important events in the Achievements ecosystem
Hunger and Thirst icon code guidelines
Hunger and Thirst User Testing
Buff and Debuff Manual User Testing
The New Button User Test in Setting Page
The Main Menu Buttons User Testing
Infinite loop game and Terrain Testing
https://github.com/UQdeco2800/2021-ext-studio-2.wiki.git
πΎ Obstacle/Enemy
βObstacle testing
ββ- Alien Plants & Variation Thorns
ββ- Falling Meteorites
βEnemy testing
ββ- Alien Monkeys & Facehugger
ββ- Spaceship Boss
βMonster Manual
βParticle-effect
βPlayer attack testing
ββ- Player Attack
Sprint 1
Sprint 2
Sprint 3
Sprint 4
Changeable background & Buffer time testing
Game over screen test sprint 4
New terrain textures on bonus map test sprint 4
Achievements System, Game Records and Unlockable Chapters
Musics Implementation Testing plan