Skip to content

Commit

Permalink
Create test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 14, 2024
1 parent cbe2758 commit dd5e229
Show file tree
Hide file tree
Showing 18 changed files with 611 additions and 30 deletions.
File renamed without changes.
52 changes: 52 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Gradle build
on: [push, pull_request, workflow_dispatch]

jobs:
test:
strategy:
matrix:
subproject:
- 1.17.1-forge
- 1.17.1-fabric
- 1.18.2-forge
- 1.18.2-fabric
- 1.19.2-forge
- 1.19.2-fabric
- 1.19.4-forge
- 1.19.4-fabric
- 1.20.1-forge
- 1.20.1-fabric
- 1.20.4-neoforge
- 1.20.4-fabric
- 1.20.6-neoforge
- 1.20.6-fabric
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Get cache key
id: cache_key
run: echo "gradle_key=gradle-`cat versions/mainProject`" >> $GITHUB_OUTPUT
- name: Cache Gradle stuff
uses: actions/cache@v3
with:
key: ${{ steps.cache_key.outputs.gradle_key }}-${{ hashFiles('*.gradle.kts', 'gradle.properties', 'gradle/**') }}
restore-keys: ${{ steps.cache_key.outputs.gradle_key }}-
path: |
~/.gradle/caches
~/.gradle/wrapper
.gradle/loom-cache
- name: Set up JDK 21
uses: actions/setup-java@v1
with:
java-version: 21
- name: Install Xvfb
run: apt install xvfb
- name: Launch Xvfb
run: screen -d -m Xvfb :1 -screen 0 1920x1080x24
- name: Run tests
run: |
./gradlew :${{ matrix.subproject }}:runTestHost --stacktrace &
./gradlew :${{ matrix.subproject }}:runTestJoiner --stacktrace &&
fg
- name: Stop Xvfb
run: screen -X quit
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.quiltmc.parsers.json.JsonWriter;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -32,7 +33,7 @@ public class WorldHostConfig {

private boolean announceFriendsOnline = true;

private final Set<UUID> friends = new LinkedHashSet<>();
private final Set<UUID> friends = Collections.synchronizedSet(new LinkedHashSet<>());

public void read(JsonReader reader) throws IOException {
reader.beginObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,22 @@ protected void init() {
}
if (usernameResponder == null) {
// Only set the responder here on first init
usernameField.setResponder(usernameResponder = text -> {
usernameField.setResponder(usernameResponder = username -> {
lastTyping = Util.getMillis();
usernameUpdate = true;
friendProfile = null;
addFriendButton.active = false;
if (VALID_USERNAME.matcher(username).matches()) {
usernameUpdate = true;
friendProfile = null;
addFriendButton.active = false;
} else if (VALID_UUID.matcher(username).matches()) {
usernameUpdate = false;
friendProfile = new GameProfile(UUID.fromString(username), "");
addFriendButton.active = true;
} else if (username.startsWith("o:")) {
usernameUpdate = false;
final String actualName = username.substring(2);
friendProfile = new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + actualName).getBytes(StandardCharsets.UTF_8)), actualName);
addFriendButton.active = true;
}
});
}
}
Expand Down Expand Up @@ -137,24 +148,15 @@ public void tick() {
if (Util.getMillis() - 300 > lastTyping && usernameUpdate) {
usernameUpdate = false;
final String username = usernameField.getValue();
if (VALID_USERNAME.matcher(username).matches()) {
WorldHost.getMaybeAsync(WorldHost.getProfileCache(), username, p -> {
if (p.isPresent()) {
assert minecraft != null;
friendProfile = WorldHost.fetchProfile(minecraft.getMinecraftSessionService(), p.get());
addFriendButton.active = true;
} else {
friendProfile = null;
}
});
} else if (VALID_UUID.matcher(username).matches()) {
friendProfile = new GameProfile(UUID.fromString(username), "");
addFriendButton.active = true;
} else if (username.startsWith("o:")) {
final String actualName = username.substring(2);
friendProfile = new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + actualName).getBytes(StandardCharsets.UTF_8)), actualName);
addFriendButton.active = true;
}
WorldHost.getMaybeAsync(WorldHost.getProfileCache(), username, p -> {
if (p.isPresent()) {
assert minecraft != null;
friendProfile = WorldHost.fetchProfile(minecraft.getMinecraftSessionService(), p.get());
addFriendButton.active = true;
} else {
friendProfile = null;
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,13 @@ public int getRowWidth() {
}

@Override
protected int getRowTop(int index) {
public int getRowTop(int index) {
return super.getRowTop(index);
}

public int getItemHeight() {
return itemHeight;
}
}

public class OnlineFriendsListEntry extends ObjectSelectionList.Entry<OnlineFriendsListEntry> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.gaming32.worldhost.mixin;

import net.minecraft.client.gui.screens.DisconnectedScreen;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(DisconnectedScreen.class)
public interface DisconnectedScreenAccessor {
@Accessor
Component getReason();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.gaming32.worldhost.mixin;

import com.mojang.blaze3d.platform.InputConstants;
import io.github.gaming32.worldhost.testing.WorldHostTesting;
import io.github.gaming32.worldhost.testing.WindowCallbackManager;
import org.lwjgl.glfw.GLFWCharModsCallbackI;
import org.lwjgl.glfw.GLFWCursorPosCallbackI;
import org.lwjgl.glfw.GLFWDropCallbackI;
import org.lwjgl.glfw.GLFWKeyCallbackI;
import org.lwjgl.glfw.GLFWMouseButtonCallbackI;
import org.lwjgl.glfw.GLFWScrollCallbackI;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(InputConstants.class)
public class MixinInputConstants {
@Inject(method = "setupKeyboardCallbacks", at = @At("HEAD"), cancellable = true)
private static void mockKeyboardCallbacks(
long window,
GLFWKeyCallbackI keyCallback,
GLFWCharModsCallbackI charModifierCallback,
CallbackInfo ci
) {
if (!WorldHostTesting.ENABLED) return;
WindowCallbackManager.keyCallback = keyCallback;
WindowCallbackManager.charModsCallback = charModifierCallback;
ci.cancel();
}

@Inject(method = "setupMouseCallbacks", at = @At("HEAD"), cancellable = true)
private static void mockMouseCallbacks(
long window,
GLFWCursorPosCallbackI cursorPositionCallback,
GLFWMouseButtonCallbackI mouseButtonCallback,
GLFWScrollCallbackI scrollCallback,
GLFWDropCallbackI dragAndDropCallback,
CallbackInfo ci
) {
if (!WorldHostTesting.ENABLED) return;
WindowCallbackManager.cursorPosCallback = cursorPositionCallback;
WindowCallbackManager.mouseButtonCallback = mouseButtonCallback;
WindowCallbackManager.scrollCallback = scrollCallback;
WindowCallbackManager.dropCallback = dragAndDropCallback;
ci.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import io.github.gaming32.worldhost.WorldHost;
import io.github.gaming32.worldhost.gui.screen.JoiningWorldHostScreen;
import io.github.gaming32.worldhost.testing.ScreenChain;
import io.github.gaming32.worldhost.testing.WorldHostTesting;
import io.github.gaming32.worldhost.toast.WHToast;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.ConnectScreen;
import net.minecraft.client.gui.screens.Overlay;
import net.minecraft.client.gui.screens.ProgressScreen;
import net.minecraft.client.gui.screens.Screen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.function.Function;

//#if MC < 1.19.4
//$$ import com.mojang.blaze3d.platform.Window;
//$$ import com.mojang.blaze3d.vertex.PoseStack;
Expand All @@ -36,23 +43,43 @@ public abstract class MixinMinecraft {
//$$ @Shadow public abstract Window getWindow();
//#endif

@Shadow public Screen screen;

@Unique
private Class<? extends Screen> wh$lastScreenClass;
@Unique
private boolean wh$readyForTesting;

@Inject(method = "setOverlay", at = @At("HEAD"))
private void deferredToastReady(Overlay loadingGui, CallbackInfo ci) {
if (loadingGui == null) {
WHToast.ready();
wh$readyForTesting = true;
}
}

@Inject(method = "tick", at = @At("HEAD"))
private void toastTick(CallbackInfo ci) {
private void preTick(CallbackInfo ci) {
WHToast.tick();
final var screenClass = ScreenChain.getScreenClass(screen);
if (WorldHostTesting.ENABLED && wh$readyForTesting && screenClass != wh$lastScreenClass) {
wh$lastScreenClass = screenClass;
WorldHostTesting.SCREEN_CHAIN.get().advance(screen);
}
}

@Inject(method = "tick", at = @At("RETURN"))
private void tickEvent(CallbackInfo ci) {
private void postTick(CallbackInfo ci) {
WorldHost.tickHandler();
}

@Inject(method = "addInitialScreens", at = @At("HEAD"), cancellable = true)
private void noOnboardingWhileTesting(List<Function<Runnable, Screen>> output, CallbackInfo ci) {
if (WorldHostTesting.ENABLED) {
ci.cancel();
}
}

//#if MC < 1.19.4
//$$ @Inject(
//$$ method = "runTick",
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/github/gaming32/worldhost/mixin/MixinOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.gaming32.worldhost.mixin;

import io.github.gaming32.worldhost.testing.WorldHostTesting;
import net.minecraft.client.Options;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Options.class)
public class MixinOptions {
@Shadow public boolean pauseOnLostFocus;

@Inject(method = "load", at = @At("RETURN"))
private void dontPauseWhileTesting(CallbackInfo ci) {
if (WorldHostTesting.ENABLED) {
pauseOnLostFocus = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public void listOnline(Collection<UUID> friends) {
}

public void publishedWorld(Collection<UUID> friends) {
WorldHost.LOGGER.info("Published world to friends: {}", friends);
enqueue(new WorldHostC2SMessage.PublishedWorld(friends));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -78,6 +79,14 @@ record FriendRequest(UUID fromUser) implements WorldHostS2CMessage {
public void handle(ProtocolClient client) {
if (!WorldHost.CONFIG.isEnableFriends()) return;
final boolean isFriend = WorldHost.isFriend(fromUser);
if (isFriend) {
Minecraft.getInstance().execute(() -> {
final var server = Minecraft.getInstance().getSingleplayerServer();
if (server != null && server.isPublished()) {
client.publishedWorld(Collections.singleton(fromUser));
}
});
}
if (!isFriend && !WorldHost.CONFIG.isAllowFriendRequests()) return;
WorldHost.showFriendOrOnlineToast(
fromUser,
Expand All @@ -100,10 +109,11 @@ public void handle(ProtocolClient client) {
record PublishedWorld(UUID user, long connectionId) implements WorldHostS2CMessage {
@Override
public void handle(ProtocolClient client) {
if (!WorldHost.CONFIG.isAnnounceFriendsOnline() || !WorldHost.isFriend(user)) return;
if (!WorldHost.isFriend(user)) return;
Minecraft.getInstance().execute(() -> {
WorldHost.ONLINE_FRIENDS.put(user, connectionId);
WorldHost.ONLINE_FRIEND_UPDATES.forEach(FriendsListUpdate::friendsListUpdate);
if (!WorldHost.CONFIG.isAnnounceFriendsOnline()) return;
WorldHost.showFriendOrOnlineToast(
user, "world-host.went_online", "world-host.went_online.desc", 200,
() -> WorldHost.join(connectionId, null)
Expand Down
Loading

0 comments on commit dd5e229

Please sign in to comment.