Skip to content

Achievements System Testing

ThatTechedGuy edited this page Oct 18, 2021 · 17 revisions

Testing Ecosystem

Note: Sprint 4 updates included.

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, and private 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.

Team 7 Test Plan - Sprint 1

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.

Accessing private methods

private Method getIsValidMethod() throws NoSuchMethodException {
        Method method = AchievementsStatsComponent.class
                .getDeclaredMethod("isValid",
                        BaseAchievementConfig.class);
        method.setAccessible(true);
        return method;
}

Sample test where the Veteran achievement should be valid

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);
            }
        }
    }

Achievements already tested

  • Veteran
  • Game Breaker
  • Tool Master

Team 7 Test Plan - Sprint 2

Test GameRecords and GameInfo persistence logic. More information about this flow can be found here

GameInfo

Accessing private static final variables to safely test files/GameInfo.java!

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;

Unlockable Chapters

Mocking static classes

    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();
    }

More achievements tested

Achievements tested in Sprint 2

  • 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);
            }
        }
    } 

Team 7 Test Plan - Sprint 3

Achievements tested in this sprint

  • Alphamineron (Boss achievement)
  • Survival Expert
  • Collector
  • Game Fanatic

The boss achievement was tested in this Sprint. The name of this achievement is Alphamineron and this achievement gets triggered when the main character has successfully avoided the boss spaceship. The idea is to simulate the condition where the success flag of avoiding the spaceship is set to true, and then check if the achievement has unlocked or not.

    /**
     * Tests if Alphamineron achievement are un-lockable
     *
     * @throws NoSuchMethodException     thrown if corresponding method doesn't exist
     * @throws InvocationTargetException wraps exception thrown by constructor invoked
     * @throws IllegalAccessException    thrown if access is not allowed for a method
     */
    @Test
    void shouldCorrectlyValidateAlphamineronAchievement() throws
            IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {

        Method method = getIsValidMethod();

        // Get component from mocked entity
        AchievementsStatsComponent component = generateEntity()
                .getComponent(AchievementsStatsComponent.class);

        for (BaseAchievementConfig a : getList()) {
            component.setSpaceshipAvoidSuccess();
            // Only the Alphamineron achievement should be valid if
            // the spaceship has been avoided
            if (a.name.equals("Alphamineron")) {
                assertEquals(method.invoke(component, a), true);
            } else {
                // Other achievements should be invalid in this condition
                assertEquals(method.invoke(component, a), false);
            }
        }
    }

Another achievement of note is the Game Fanatic achievement. This achievement gets unlocked when the user picks up a particular number of first aids while having full health. The health and first aid pick ups are then simulated to check the correctness of the Game Fanatic achievement logic.

        // Invalid number of first aid count values
        component.setFirstAidByVal(-1);
        assertEquals(method.invoke(component, achievement), false);
        component.setFirstAidByVal(-3);
        assertEquals(method.invoke(component, achievement), false);
        component.setFirstAidByVal(-99);
        assertEquals(method.invoke(component, achievement), false);

        // Simulate 3 first aids picked up, which is a valid.
        component.setFirstAidByVal(3);
        // When the health is boosted, the achievement should still be valid
        component.setHealth(110);
        assertEquals(method.invoke(component, achievement), true);
        // Lock the achievement again
        achievement.unlocked = false;
        // The health value is high, but not full. The achievement should be invalid.
        component.setHealth(99);
        assertEquals(method.invoke(component, achievement), false);
        // Moderate health, achievement should not be unlocked
        component.setHealth(40);
        assertEquals(method.invoke(component, achievement), false);
        // Locked in low health conditions
        component.setHealth(0);
        assertEquals(method.invoke(component, achievement), false);
        component.setHealth(1);
        assertEquals(method.invoke(component, achievement), false);
        // Locked when the health value is invalid
        component.setHealth(-1);
        assertEquals(method.invoke(component, achievement), false);

Team 7 Test Plan - Sprint 4

In this sprint,GameRecordUtils utility methods were tested. The file can be found here.

Tested:

  • Testing getHighestScores utility method which fetches score records from persisted gameRecords.json and returns the list of score objects
  • Testing getBestAchievementsByGame utility method which fetches the highest tier achievements of a particular game from persisted gameRecords.json

The general flow involved mocking the GameRecords methods, API reference for which can be found here.

MockedStatic<GameRecords> gameRecordsMockedStatic =
            Mockito.mockStatic(GameRecords.class);
// Mocking method which interfaces with FileLoader (which does not work in test suite)
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(() -> GameRecords.getAchievementsByGame(Mockito.anyInt()))
                .thenReturn(achievementList);
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(GameRecords::getAllScores).thenReturn(scoreList);

Notice how parameterized static methods are mocked:

// Mocking method which interfaces with FileLoader (which does not work in test suite)
// Mock the static method and return the generated list
gameRecordsMockedStatic.when(() -> GameRecords.getAchievementsByGame(Mockito.anyInt()))
                .thenReturn(achievementList);

After mocking setup, logic is tested:

List<BaseAchievementConfig> bestAchievements = GameRecordUtils.getBestAchievementsByGame(99);
// Based on mocked data, should return only 5 achievements (only the highest tier ones)
assertEquals(bestAchievements.size(), 5);
// Setup mocks
mockScoreList();

List<GameRecords.Score> highestScores = GameRecordUtils.getHighestScores();

int[] highestScoresExpected = {99, 51, 23, 23, 1};

for (int i = 0; i < highestScoresExpected.length; i++) {
   assertEquals(highestScoresExpected[i], highestScores.get(i).score);
}

After each test case, close down the mocked instance to reuse it:

@AfterEach
void afterEach() {
    gameRecordsMockedStatic.close();
}

Future testing plan (Team 7)

  • 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, which will also involve making the logic of AchievementStatsComponent more modular and concise, and remove a lot of duplicate patterns.
  • UI Testing of AchievementsDisplay.java, ScoreDetailsDialog.java,ChapterDisplay.java and BackgroundSelectionComponent.java
  • Testing BackgroundMusic.java using PowerMockito in order to mock static method invocations within other static methods
  • Testing GameRecords.java using PowerMockito in order to mock static method invocations within other static methods

Gameplay

Home

Main Character

πŸ‘Ύ Obstacle/Enemy

Food & Water

Pickable Items

Game achievements

Game Design

Emotional Goals

Game Story

Influences

Style

Pixel Grid Resolution

Camera Angle and The Player's Perspective

Features Design

Achievements Screen

Achievements Trophies and Cards

Music Selection GUI

πŸ‘Ύ Obstacle/Enemy

 Monster Manual
 Obstacles/Enemies
  - Alien Plants
  - Variation thorns
  - Falling Meteorites
  - FaceHugger
  - AlienMonkey
 Spaceship & Map Entry
 Particle effect

Buffs

Debuffs

Buffs and Debuffs manual

Game Instruction

[code for debuff animations](code for debuff animations)

Infinite loop game system

Main Menu Screen

New Setting Screen

Hunger and Thirst

Goals and Objectives

HUD User Interface

Inventory System

Item Bar System

Scoring System

Props store

BGM design

Sound Effect design

Main game interface

Invisible ceiling

New terrain sprint 4

New game over screen sprint 4

Code Guidelines

Main Character Movement, Interactions and Animations - Code Guidelines

Item Pickup

ItemBar & Recycle system

Main Menu Button Code

Game Instructions Code

πŸ‘Ύ Obstacle/Enemy

 Obstacle/Enemy
 Monster Manual
 Spaceship Boss
 Particle effects
 Other Related Code
 UML & Sequence diagram of enemies/obstacles

Scoring System Implementation Explanation

Music Implementation

Buff and Debuff Implementation

Score History Display

code improvement explanation

Infinite generating terrains Implementation Explanation

Game Over Screen and functions explaination

Buffer timer before game start

Scrolling background

Multiple Maps

Invisible ceiling

Rocks and woods layout optimization

Magma and nails code implementation

Background Music Selection

Chooser GUI Implementation

Chooser GUI Logic Persistence

Guide: Adding Background music for a particular screen

Achievements Ecosystem - Code Guidelines

Achievements System

Achievements Screen

Adding Achievements (Guide)

Game Records

DateTimeUtils

History Scoreboard - Score Details

Listening for important events in the Achievements ecosystem

Food and Water System

Food System Water System

Hunger and Thirst icon code guidelines

Asset Creation

In Game Background Music

User Testing

Hunger and Thirst User Testing

Main Character Testing

Buffs and Debuffs Testing

Buff and Debuff Manual User Testing

Game Instruction User Testing

The Main Menu User Test

The New Button User Test in Setting Page

The Main Menu Buttons User Testing

Hunger and Thirst User Test

Infinite loop game and Terrain Testing

Item Bar System Testing

Randomised Item Drops

Recycle System Testing

Scoring System Testing

Music User test

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

Inventory system UI layout

Props store user testing

Achievements User Testing

Sprint 1

Sprint 2

Sprint 3

Sprint 4

Items testing

Player Status testing

Changeable background & Buffer time testing

Main game interface test

Invisible ceiling test

Game over screen test sprint 4

New terrain textures on bonus map test sprint 4

Buying Props User Testing

Testing

Hunger and Thirst Testing

Main Character Player

Achievements System, Game Records and Unlockable Chapters

DateTimeUtils Testing

Scoring System Testing Plan

Distance Display Testing Plan

Musics Implementation Testing plan

History Board Testing plan

Rocks and woods testing plan

Sprint 4 terrain tests

Items

Item Bar System Testing Plan

Recycle System Testing Plan

Game Engine

Game Engine Help

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally