User Settings test - UQcsse3200/2024-studio-2 GitHub Wiki

Overview

The UserSettings class is responsible for managing user preferences such as fullscreen mode, FPS settings, and audio scales (for both music and sound). It interacts with various components like AudioManager and Gdx.graphics to apply these settings to the game. These tests ensure that the settings are applied correctly, that the audio behaves as expected (including muting/unmuting), and that the correct display mode is used.

Testing (JUnit) for UserSettings

Purpose of Tests

The tests are designed to validate that the UserSettings class correctly applies the user preferences to the game environment. This includes verifying fullscreen settings, FPS limits, audio volume, and mute/unmute functionality. Using Mockito, we mock necessary classes like AudioManager and Gdx.graphics to ensure the settings are applied in the correct order.


Test Descriptions

shouldApplySettings

  • Purpose: Validates that the user settings, such as fullscreen mode, FPS, and audio volume, are correctly applied when the applySettings() method is called.

  • What it does:

    • Mocks the Gdx.graphics and AudioManager to verify the application of the settings.
    • Ensures the setFullscreenMode() and setForegroundFPS() methods are called in the correct order.
    • Confirms that the music and sound volumes are set to the expected values (75% and 80%, respectively).
  • Why This Test: Ensures that critical game settings such as FPS and fullscreen mode are applied properly, and that audio settings are configured according to the user's preferences.


shouldApplyWindowedSettings

  • Purpose: Verifies that when the game is set to windowed mode, the correct settings (such as window size and FPS limit) are applied.

  • What it does:

    • Mocks the Gdx.graphics to ensure the windowed mode is correctly set with a resolution of 1280x800.
    • Verifies that audio settings for music (50% volume) and sound (60% volume) are applied.
  • Why This Test: Confirms that when the game is not in fullscreen mode, the correct window resolution and FPS limits are applied, and the audio scales are appropriately handled.


shouldMuteAudio

  • Purpose: Tests that muting the audio through UserSettings.muteAudio() correctly sets both music and sound volumes to zero.

  • What it does:

    • Mocks the static AudioManager class and verifies that both music and sound volumes are set to 0f when muting.
  • Why This Test: Ensures that the game's mute functionality works as intended, muting both music and sound simultaneously.


shouldUnmuteAudio

  • Purpose: Ensures that when unmuting the audio, the volumes are restored to their previous levels.

  • What it does:

    • Mocks the AudioManager, first mutes the audio, then calls UserSettings.unmuteAudio() to restore volumes.
    • Verifies that the volumes are reset to their original values before muting.
  • Why This Test: Confirms that the game's unmute functionality works as intended and restores the user's preferred audio settings.


shouldFindMatchingDisplayMode

  • Purpose: Validates that the correct display mode is found and applied when switching to fullscreen mode.

  • What it does:

    • Mocks Gdx.graphics and simulates various display modes.
    • Ensures that the correct display mode (e.g., 1920x1080 at 60Hz) is selected and applied when the game switches to fullscreen.
  • Why This Test: Ensures that the game selects and applies the appropriate display mode when transitioning to fullscreen, based on user settings.


Mocking Key Components

Gdx.graphics

  • The Graphics object in libGDX is responsible for managing the display properties of the game, such as resolution, fullscreen mode, and FPS. In these tests, Gdx.graphics is mocked to ensure that display settings (like resolution and FPS) are applied correctly without actually changing the game's display mode.

AudioManager

  • AudioManager is mocked in these tests to verify that audio settings are correctly applied. This includes setting the volume for both music and sound, as well as muting/unmuting functionality.

DisplayMode

  • The DisplayMode class is used to manage screen resolutions and refresh rates. In shouldFindMatchingDisplayMode(), various display modes are mocked to simulate different screen resolutions and verify that the correct one is selected for fullscreen mode.

Test Implementation Example

@Test
void shouldApplySettings() {
    Gdx.graphics = mock(Graphics.class);
    DisplayMode displayMode = mock(DisplayMode.class);
    when(Gdx.graphics.getDisplayMode()).thenReturn(displayMode);

    try (MockedStatic<AudioManager> audioManagerMock = mockStatic(AudioManager.class)) {
        UserSettings.Settings settings = new UserSettings.Settings();
        settings.fullscreen = true;
        settings.fps = 40;
        settings.audioScale = 75;
        settings.soundScale = 80;

        UserSettings.unmuteAudio();
        UserSettings.applySettings(settings);

        InOrder inOrder = inOrder(Gdx.graphics);
        inOrder.verify(Gdx.graphics).setForegroundFPS(settings.fps);
        inOrder.verify(Gdx.graphics).setFullscreenMode(displayMode);

        audioManagerMock.verify(() -> AudioManager.setMusicVolume(0.75f));
        audioManagerMock.verify(() -> AudioManager.setSoundVolume(0.80f));
    }
}