Skip to content

Commit

Permalink
Fix UI tests :)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jun 7, 2024
1 parent 42771b6 commit 64de3fb
Show file tree
Hide file tree
Showing 18 changed files with 557 additions and 493 deletions.
4 changes: 2 additions & 2 deletions forge/src/test/java/fr/thesmyler/terramap/TerramapTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fr.thesmyler.terramap;

import fr.thesmyler.smylibgui.SmyLibGuiTest;
import fr.thesmyler.terramap.maps.raster.MapStylesLibrary;
import net.smyler.smylib.SmyLibTest;
import org.apache.logging.log4j.LogManager;
import org.junit.jupiter.api.BeforeEach;

public class TerramapTest extends SmyLibGuiTest {
public class TerramapTest extends SmyLibTest {

@BeforeEach
public void initTerramap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package fr.thesmyler.terramap.gui.widgets.map;

import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.game.TestGameClient;
import fr.thesmyler.terramap.MapContext;
import fr.thesmyler.terramap.TerramapTest;
import net.smyler.smylib.gui.screen.Screen;
import net.smyler.smylib.math.Vec2dMutable;
import net.smyler.terramap.util.geo.GeoPointImmutable;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -15,16 +16,19 @@ public class MapControllerTest extends TerramapTest {

public static GeoPointImmutable PARIS = new GeoPointImmutable(2.350987d, 48.856667d);

private TestingWidgetContainer screen;
private Screen screen;
private MapWidget map;
private MapController controller;

@BeforeEach
public void setupMap() {
this.screen = new TestingWidgetContainer(60, 1280f, 720f);
TestGameClient client = this.getTestGameClient();
client.setWindowDimensions(1280f, 720f);
client.setTargetFps(60);
this.screen = client.getCurrentScreen();
this.map = new MapWidget(-4f, -2f, 10, 141f, 83f, MapContext.MINIMAP, 2.0f);
this.screen.addWidget(this.map);
this.controller = map.getController();
this.controller = this.map.getController();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package fr.thesmyler.terramap.gui.widgets.map;

import com.google.gson.JsonPrimitive;
import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.game.TestGameClient;
import fr.thesmyler.terramap.TerramapTest;
import fr.thesmyler.terramap.gui.widgets.map.layer.OnlineRasterMapLayer;
import fr.thesmyler.terramap.gui.widgets.map.layer.RasterMapLayer;
import fr.thesmyler.terramap.maps.SavedLayerState;
import fr.thesmyler.terramap.maps.SavedMapState;
import fr.thesmyler.terramap.maps.raster.MapStylesLibrary;
import net.smyler.smylib.gui.screen.Screen;
import net.smyler.smylib.math.Vec2dImmutable;
import net.smyler.terramap.util.geo.GeoPointImmutable;
import org.junit.jupiter.api.Test;

import static fr.thesmyler.terramap.MapContext.FULLSCREEN;
import static fr.thesmyler.terramap.gui.widgets.map.MapLayerRegistry.RASTER_LAYER_ID;
import static net.smyler.terramap.util.geo.GeoPointImmutable.ORIGIN;
import static fr.thesmyler.terramap.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

class MapWidgetTest extends TerramapTest {
Expand All @@ -23,9 +25,11 @@ class MapWidgetTest extends TerramapTest {

@Test
void canSaveMapWidgetToSavedMapState() throws InterruptedException {
TestingWidgetContainer screen = new TestingWidgetContainer(60, 500f, 500f);
TestGameClient client = this.getTestGameClient();
client.setWindowDimensions(500f, 500f);
client.setTargetFps(60);
MapWidget map = new MapWidget(0f, 0f, 0, 500f, 500f, FULLSCREEN, 1f);
screen.addWidget(map);
client.getCurrentScreen().addWidget(map);
OnlineRasterMapLayer raster_osm = (OnlineRasterMapLayer) map.createLayer(RASTER_LAYER_ID);
OnlineRasterMapLayer osm_fr_hot = (OnlineRasterMapLayer) map.createLayer(RASTER_LAYER_ID);
raster_osm.setTiledMap(MapStylesLibrary.getBaseMaps().get("osm"));
Expand All @@ -39,10 +43,10 @@ void canSaveMapWidgetToSavedMapState() throws InterruptedException {

map.getController().setZoom(10, false);
map.getController().setRotation(45f, false);
screen.doTick();
client.doTick();

map.getController().moveLocationToCenter(PARIS, false);
screen.doTick();
client.doTick();

SavedMapState saved = map.save();

Expand Down Expand Up @@ -71,9 +75,13 @@ void canSaveMapWidgetToSavedMapState() throws InterruptedException {
@Test
public void canRestoreMapState() throws InterruptedException {

TestingWidgetContainer screen = new TestingWidgetContainer(60, 500f, 500f);
TestGameClient client = this.getTestGameClient();
client.setWindowDimensions(500f, 500f);
client.setTargetFps(60);
Screen screen = client.getCurrentScreen();

MapWidget map = new MapWidget(0f, 0f, 0, 500f, 500f, FULLSCREEN, 1f);
screen.moveMouse(750, 750, 1000);
client.moveMouse(750, 750, 1000);
screen.addWidget(map);

// Let's start in some random state
Expand All @@ -83,9 +91,9 @@ public void canRestoreMapState() throws InterruptedException {
map.setLayerZ(rasterLayer, -1);
map.getController().setZoomStaticLocation(PARIS);
map.getController().setZoom(18, true);
screen.runFor(1000);
client.runFor(1000);
map.getController().moveLocationToCenter(PARIS, true);
screen.runFor(1000);
client.runFor(1000);
map.getController().setRotationStaticLocation(PARIS);
map.getController().setRotation(45f, true);

Expand All @@ -101,7 +109,7 @@ public void canRestoreMapState() throws InterruptedException {

// And we try restoring...
map.restore(state);
screen.doTick();
client.doTick();

assertEquals(ORIGIN, map.getController().getCenterLocation().getImmutable());
assertEquals(0, map.getController().getZoom());
Expand All @@ -117,9 +125,14 @@ public void canRestoreMapState() throws InterruptedException {
@Test
public void canRestoreMapStateWithInvalidLayerId() throws InterruptedException {

TestingWidgetContainer screen = new TestingWidgetContainer(60, 500f, 500f);
TestGameClient client = this.getTestGameClient();
client.setWindowDimensions(500f, 500f);
client.setTargetFps(60);

Screen screen = client.getCurrentScreen();

MapWidget map = new MapWidget(0f, 0f, 0, 500f, 500f, FULLSCREEN, 1f);
screen.moveMouse(750, 750, 1000);
client.moveMouse(750, 750, 1000);
screen.addWidget(map);

SavedMapState state = new SavedMapState();
Expand All @@ -128,7 +141,7 @@ public void canRestoreMapStateWithInvalidLayerId() throws InterruptedException {
state.layers.add(layerState);

map.restore(state);
screen.doTick();
client.doTick();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class PopupScreen extends Screen {

private final Popup popup;

public PopupScreen(Popup popup) {
PopupScreen(Popup popup) {
super(BackgroundOption.NONE);
this.popup = popup;
this.addWidget(popup);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.smyler.smylib.gui.widgets;

import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.game.TestGameClient;
import net.smyler.smylib.SmyLibTest;
import net.smyler.smylib.game.Key;
import net.smyler.smylib.gui.widgets.ColorPickerWidget;
import net.smyler.smylib.gui.screen.Screen;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -48,68 +48,62 @@ void canDealWithCustomOnTextChangedCallback() {

@Test
void canInputColors() throws InterruptedException {
//TODO repair ColorPickerWidgetTest
/*
TestingWidgetContainer container = new TestingWidgetContainer(30, 500f, 500f);
TestGameClient client = this.getTestGameClient();
Screen container = client.getCurrentScreen();
ColorPickerWidget colorPicker = new ColorPickerWidget(0, 0, 0, BLACK, getGameClient().defaultFont());
container.addWidget(colorPicker);

// Focus the widget and move cursor to the right
container.moveMouse(10f, 10f, 500);
container.click(0);
client.moveMouse(10f, 10f, 500);
client.click(0);
for (int i = 0; i < 10; i++) {
container.pressKey((char)Key.KEY_RIGHT.code, Key.KEY_RIGHT);
container.doTick();
client.pressKey((char)Key.KEY_RIGHT.code, Key.KEY_RIGHT);
client.doTick();
}

container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();

assertFalse(colorPicker.hasValidColor());
assertEquals("#000", colorPicker.getText());

container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
container.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
container.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();
client.pressKey((char) Key.KEY_BACK.code, Key.KEY_BACK);
client.doTick();

assertFalse(colorPicker.hasValidColor());
assertEquals("#", colorPicker.getText());

container.pressKey('F', Key.KEY_F);
container.doTick();
container.pressKey('F', Key.KEY_F);
container.doTick();
container.pressKey('0', Key.KEY_0);
container.doTick();
container.pressKey('0', Key.KEY_0);
container.doTick();
container.pressKey('0', Key.KEY_0);
container.doTick();
container.pressKey('0', Key.KEY_0);
container.doTick();
client.pressKey('F', Key.KEY_F);
client.doTick();
client.pressKey('F', Key.KEY_F);
client.doTick();
client.pressKey('0', Key.KEY_0);
client.doTick();
client.pressKey('0', Key.KEY_0);
client.doTick();
client.pressKey('0', Key.KEY_0);
client.doTick();
client.pressKey('0', Key.KEY_0);
client.doTick();

assertTrue(colorPicker.hasValidColor());
assertEquals(RED, colorPicker.getColor());

container.pressKey('F', Key.KEY_F);
container.doTick();
container.pressKey('F', Key.KEY_F);
container.doTick();
client.pressKey('F', Key.KEY_F);
client.doTick();
client.pressKey('F', Key.KEY_F);
client.doTick();

assertTrue(colorPicker.hasValidColor());
assertEquals(RED, colorPicker.getColor());
*/
}

@Test
void setOnColorChange() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


import net.smyler.smylib.SmyLibTest;
import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.game.TestGameClient;
import net.smyler.smylib.gui.screen.Screen;
import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -15,43 +16,44 @@ public class TextButtonWidgetTest extends SmyLibTest {
@Test
public void testActivation() throws InterruptedException {
AtomicInteger clickCounter = new AtomicInteger();
TestingWidgetContainer screen = new TestingWidgetContainer(30, 400, 300);
TestGameClient client = this.getTestGameClient();
Screen screen = this.getTestGameClient().getCurrentScreen();
TextButtonWidget button = new TextButtonWidget(10, 10, 0, 200, "Test button", clickCounter::incrementAndGet);
screen.addWidget(button);

assertNull(screen.getFocusedWidget());

screen.moveMouse(30, 30, 500);
screen.click(0);
screen.doTick();
screen.doTick(); // We are giving it a frame to lose focus
client.moveMouse(30, 30, 500);
client.click(0);
client.doTick();
client.doTick(); // We are giving it a frame to lose focus
assertEquals(1, clickCounter.get());
assertNull(screen.getFocusedWidget());

screen.doubleClick(0);
screen.doTick();
screen.doTick();
client.doubleClick(0);
client.doTick();
client.doTick();
assertEquals(3, clickCounter.get());
assertNull(screen.getFocusedWidget());

screen.moveMouse(1, 1, 500);
screen.click(0);
screen.doTick();
screen.doTick();
client.moveMouse(1, 1, 500);
client.click(0);
client.doTick();
client.doTick();
assertEquals(3, clickCounter.get());
assertNull(screen.getFocusedWidget());

screen.moveMouse(30, 30, 500);
screen.click(0);
screen.doTick();
screen.doTick();
client.moveMouse(30, 30, 500);
client.click(0);
client.doTick();
client.doTick();
assertEquals(4, clickCounter.get());
assertNull(screen.getFocusedWidget());

button.setEnabled(false);
screen.click(0);
screen.doTick();
screen.doTick();
client.click(0);
client.doTick();
client.doTick();
assertEquals(4, clickCounter.get());
assertNull(screen.getFocusedWidget());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package net.smyler.smylib.gui.widgets.sliders;

import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.SmyLibTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class OptionSliderWidgetTest extends SmyLibTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package net.smyler.smylib.gui.widgets.text;

import net.smyler.smylib.gui.TestingWidgetContainer;
import net.smyler.smylib.SmyLibTest;
import net.smyler.smylib.game.Key;
import net.smyler.smylib.gui.DummyFont;
import net.smyler.smylib.gui.widgets.text.TextFieldWidget;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class TextFieldWidgetTest extends SmyLibTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.smyler.smylib.gui.*;
import net.smyler.smylib.gui.popups.Popup;
import net.smyler.smylib.gui.popups.PopupScreenImplementation;
import net.smyler.smylib.gui.screen.PopupScreenImplementation;
import net.smyler.smylib.gui.screen.*;
import net.smyler.smylib.gui.sprites.SpriteLibrary;

Expand Down
Loading

0 comments on commit 64de3fb

Please sign in to comment.