mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
SettingsTest added
This commit is contained in:
@@ -5,18 +5,16 @@ import org.toop.framework.audio.VolumeControl;
|
|||||||
import org.toop.framework.audio.events.AudioEvents;
|
import org.toop.framework.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.framework.resource.ResourceManager;
|
import org.toop.framework.resource.ResourceManager;
|
||||||
import org.toop.framework.resource.ResourceMeta;
|
|
||||||
import org.toop.framework.resource.resources.SettingsAsset;
|
import org.toop.framework.resource.resources.SettingsAsset;
|
||||||
import org.toop.framework.settings.Settings;
|
import org.toop.framework.settings.Settings;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class AppSettings {
|
public class AppSettings {
|
||||||
private static SettingsAsset settingsAsset;
|
private static SettingsAsset settingsAsset;
|
||||||
|
|
||||||
public static void applySettings() {
|
public static void applySettings() {
|
||||||
settingsAsset = getPath();
|
settingsAsset = getSettingsAsset();
|
||||||
if (!settingsAsset.isLoaded()) {
|
if (!settingsAsset.isLoaded()) {
|
||||||
settingsAsset.load();
|
settingsAsset.load();
|
||||||
}
|
}
|
||||||
@@ -45,26 +43,9 @@ public class AppSettings {
|
|||||||
.postEvent();
|
.postEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SettingsAsset getPath() {
|
public static SettingsAsset getSettingsAsset() {
|
||||||
if (settingsAsset == null) {
|
if (settingsAsset == null) {
|
||||||
String os = System.getProperty("os.name").toLowerCase();
|
settingsAsset = SettingsAsset.getPath();
|
||||||
String basePath;
|
|
||||||
|
|
||||||
if (os.contains("win")) {
|
|
||||||
basePath = System.getenv("APPDATA");
|
|
||||||
if (basePath == null) {
|
|
||||||
basePath = System.getProperty("user.home");
|
|
||||||
}
|
|
||||||
} else if (os.contains("mac")) {
|
|
||||||
basePath = System.getProperty("user.home") + "/Library/Application Support";
|
|
||||||
} else {
|
|
||||||
basePath = System.getProperty("user.home") + "/.config";
|
|
||||||
}
|
|
||||||
|
|
||||||
File settingsFile =
|
|
||||||
new File(basePath + File.separator + "ISY1" + File.separator + "settings.json");
|
|
||||||
// this.settingsAsset = new SettingsAsset(settingsFile);
|
|
||||||
ResourceManager.addAsset(new ResourceMeta<>("settings.json", new SettingsAsset(settingsFile)));
|
|
||||||
}
|
}
|
||||||
return ResourceManager.get("settings.json");
|
return ResourceManager.get("settings.json");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,9 @@ package org.toop.framework.resource.resources;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import org.toop.framework.resource.ResourceManager;
|
||||||
|
import org.toop.framework.resource.ResourceMeta;
|
||||||
import org.toop.framework.settings.Settings;
|
import org.toop.framework.settings.Settings;
|
||||||
|
|
||||||
public class SettingsAsset extends JsonAsset<Settings> {
|
public class SettingsAsset extends JsonAsset<Settings> {
|
||||||
@@ -108,4 +111,45 @@ public class SettingsAsset extends JsonAsset<Settings> {
|
|||||||
getContent().firstReversi = firstReversi;
|
getContent().firstReversi = firstReversi;
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setContent(Settings settings) {
|
||||||
|
setVolume(settings.volume);
|
||||||
|
setFxVolume(settings.fxVolume);
|
||||||
|
setMusicVolume(settings.musicVolume);
|
||||||
|
setLocale(settings.locale);
|
||||||
|
setFullscreen(settings.fullScreen);
|
||||||
|
setTheme(settings.theme);
|
||||||
|
setLayoutSize(settings.layoutSize);
|
||||||
|
setTutorialFlag(settings.showTutorials);
|
||||||
|
setFirstTTT(settings.firstTTT);
|
||||||
|
setFirstConnect4(settings.firstConnect4);
|
||||||
|
setFirstReversi(settings.firstReversi);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SettingsAsset getPath() {
|
||||||
|
SettingsAsset settingsAsset;
|
||||||
|
try {
|
||||||
|
settingsAsset = ResourceManager.get("settings.json");
|
||||||
|
} catch (Exception e) {
|
||||||
|
String os = System.getProperty("os.name").toLowerCase();
|
||||||
|
String basePath;
|
||||||
|
|
||||||
|
if (os.contains("win")) {
|
||||||
|
basePath = System.getenv("APPDATA");
|
||||||
|
if (basePath == null) {
|
||||||
|
basePath = System.getProperty("user.home");
|
||||||
|
}
|
||||||
|
} else if (os.contains("mac")) {
|
||||||
|
basePath = System.getProperty("user.home") + "/Library/Application Support";
|
||||||
|
} else {
|
||||||
|
basePath = System.getProperty("user.home") + "/.config";
|
||||||
|
}
|
||||||
|
|
||||||
|
File settingsFile =
|
||||||
|
new File(basePath + File.separator + "ISY1" + File.separator + "settings.json");
|
||||||
|
settingsAsset = new SettingsAsset(settingsFile);
|
||||||
|
ResourceManager.addAsset(new ResourceMeta<>("settings.json", settingsAsset));
|
||||||
|
}
|
||||||
|
return settingsAsset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,135 @@
|
|||||||
|
package org.toop.framework.settings;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.toop.framework.resource.resources.SettingsAsset;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class SettingsAssetTest {
|
||||||
|
|
||||||
|
private Settings copySettings;
|
||||||
|
private SettingsAsset settingsAsset;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
this.settingsAsset = SettingsAsset.getPath();
|
||||||
|
Settings original = settingsAsset.getContent();
|
||||||
|
this.copySettings = new Settings();
|
||||||
|
this.copySettings.volume = original.volume;
|
||||||
|
this.copySettings.fxVolume = original.fxVolume;
|
||||||
|
this.copySettings.musicVolume = original.musicVolume;
|
||||||
|
this.copySettings.locale = original.locale;
|
||||||
|
this.copySettings.fullScreen = original.fullScreen;
|
||||||
|
this.copySettings.theme = original.theme;
|
||||||
|
this.copySettings.layoutSize = original.layoutSize;
|
||||||
|
this.copySettings.showTutorials = original.showTutorials;
|
||||||
|
this.copySettings.firstTTT = original.firstTTT;
|
||||||
|
this.copySettings.firstConnect4 = original.firstConnect4;
|
||||||
|
this.copySettings.firstReversi = original.firstReversi;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void teardown() {
|
||||||
|
settingsAsset.setContent(copySettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testVolume() {
|
||||||
|
settingsAsset.setVolume(55);
|
||||||
|
assertEquals(55, settingsAsset.getVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFxVolume() {
|
||||||
|
settingsAsset.setFxVolume(33);
|
||||||
|
assertEquals(33, settingsAsset.getFxVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMusicVolume() {
|
||||||
|
settingsAsset.setMusicVolume(77);
|
||||||
|
assertEquals(77, settingsAsset.getMusicVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLocale() {
|
||||||
|
settingsAsset.setLocale("es-ES");
|
||||||
|
assertEquals(Locale.forLanguageTag("es-ES"), settingsAsset.getLocale());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testFullscreen() {
|
||||||
|
settingsAsset.setFullscreen(true);
|
||||||
|
assertTrue(settingsAsset.getFullscreen());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTheme() {
|
||||||
|
settingsAsset.setTheme("dark");
|
||||||
|
assertEquals("dark", settingsAsset.getTheme());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLayoutsize() {
|
||||||
|
settingsAsset.setLayoutSize("large");
|
||||||
|
assertEquals("large", settingsAsset.getLayoutSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTutorialflag() {
|
||||||
|
settingsAsset.setTutorialFlag(false);
|
||||||
|
assertFalse(settingsAsset.getTutorialFlag());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTTT() {
|
||||||
|
settingsAsset.setFirstTTT(true);
|
||||||
|
assertTrue(settingsAsset.getFirstTTT());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConnect4() {
|
||||||
|
settingsAsset.setFirstConnect4(false);
|
||||||
|
assertFalse(settingsAsset.getFirstConnect4());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReversi() {
|
||||||
|
settingsAsset.setFirstReversi(true);
|
||||||
|
assertTrue(settingsAsset.getFirstReversi());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReplaceFields() {
|
||||||
|
Settings newSettings = new Settings();
|
||||||
|
newSettings.volume = 10;
|
||||||
|
newSettings.fxVolume = 20;
|
||||||
|
newSettings.musicVolume = 30;
|
||||||
|
newSettings.locale = "fr-FR";
|
||||||
|
newSettings.fullScreen = true;
|
||||||
|
newSettings.theme = "light";
|
||||||
|
newSettings.layoutSize = "medium";
|
||||||
|
newSettings.showTutorials = false;
|
||||||
|
newSettings.firstTTT = false;
|
||||||
|
newSettings.firstConnect4 = true;
|
||||||
|
newSettings.firstReversi = false;
|
||||||
|
settingsAsset.setContent(newSettings);
|
||||||
|
assertEquals(10, settingsAsset.getVolume());
|
||||||
|
assertEquals(20, settingsAsset.getFxVolume());
|
||||||
|
assertEquals(30, settingsAsset.getMusicVolume());
|
||||||
|
assertEquals(Locale.forLanguageTag("fr-FR"), settingsAsset.getLocale());
|
||||||
|
assertTrue(settingsAsset.getFullscreen());
|
||||||
|
assertEquals("light", settingsAsset.getTheme());
|
||||||
|
assertEquals("medium", settingsAsset.getLayoutSize());
|
||||||
|
assertFalse(settingsAsset.getTutorialFlag());
|
||||||
|
assertFalse(settingsAsset.getFirstTTT());
|
||||||
|
assertTrue(settingsAsset.getFirstConnect4());
|
||||||
|
assertFalse(settingsAsset.getFirstReversi());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user