From 2127dc72fef2a46a573d968eb508025b22f2ba9d Mon Sep 17 00:00:00 2001 From: Alexander01998 Date: Mon, 21 Oct 2024 19:36:57 +0200 Subject: [PATCH] Fix title screen screenshot in automated client test (#4177) * Fix title screen screenshot * Use accessor to detect when background fade is done --- .../base/client/FabricApiAutoTestClient.java | 5 +++- .../base/client/FabricClientTestHelper.java | 18 +++++++++++- .../client/mixin/TitleScreenAccessor.java | 28 +++++++++++++++++++ ...fabric-api-base-testmod.client.mixins.json | 3 +- 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/mixin/TitleScreenAccessor.java diff --git a/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricApiAutoTestClient.java b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricApiAutoTestClient.java index f08959dfd1..8f2c40f628 100644 --- a/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricApiAutoTestClient.java +++ b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricApiAutoTestClient.java @@ -27,6 +27,7 @@ import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.takeScreenshot; import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForLoadingComplete; import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForScreen; +import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForTitleScreenFade; import static net.fabricmc.fabric.test.base.client.FabricClientTestHelper.waitForWorldTicks; import java.io.IOException; @@ -34,6 +35,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.time.Duration; import com.mojang.authlib.GameProfile; import org.spongepowered.asm.mixin.MixinEnvironment; @@ -83,7 +85,8 @@ private void runTest() { { waitForScreen(TitleScreen.class); - takeScreenshot("title_screen"); + waitForTitleScreenFade(); + takeScreenshot("title_screen", Duration.ZERO); clickScreenButton("menu.singleplayer"); } diff --git a/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricClientTestHelper.java b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricClientTestHelper.java index 82e8383542..fdb316d28c 100644 --- a/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricClientTestHelper.java +++ b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/FabricClientTestHelper.java @@ -27,6 +27,7 @@ import net.minecraft.client.gui.Drawable; import net.minecraft.client.gui.screen.GameMenuScreen; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.TitleScreen; import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; import net.minecraft.client.gui.screen.ingame.InventoryScreen; import net.minecraft.client.gui.screen.multiplayer.ConnectScreen; @@ -44,6 +45,7 @@ import net.fabricmc.fabric.test.base.client.mixin.CyclingButtonWidgetAccessor; import net.fabricmc.fabric.test.base.client.mixin.ScreenAccessor; +import net.fabricmc.fabric.test.base.client.mixin.TitleScreenAccessor; import net.fabricmc.loader.api.FabricLoader; // Provides thread safe utils for interacting with a running game. @@ -80,8 +82,12 @@ private static void setScreen(Function screenSupplier) } public static void takeScreenshot(String name) { + takeScreenshot(name, Duration.ofSeconds(1)); + } + + public static void takeScreenshot(String name, Duration delay) { // Allow time for any screens to open - waitFor(Duration.ofSeconds(1)); + waitFor(delay); submitAndWait(client -> { ScreenshotRecorder.saveScreenshot(FabricLoader.getInstance().getGameDir().toFile(), name + ".png", client.getFramebuffer(), (message) -> { @@ -166,6 +172,16 @@ public static void connectToServer(TestDedicatedServer server) { }); } + public static void waitForTitleScreenFade() { + waitFor("Title screen fade", client -> { + if (!(client.currentScreen instanceof TitleScreen titleScreen)) { + return false; + } + + return !((TitleScreenAccessor) titleScreen).getDoBackgroundFade(); + }); + } + private static void waitFor(String what, Predicate predicate) { waitFor(what, predicate, Duration.ofSeconds(10)); } diff --git a/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/mixin/TitleScreenAccessor.java b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/mixin/TitleScreenAccessor.java new file mode 100644 index 0000000000..6b2696aa7b --- /dev/null +++ b/fabric-api-base/src/testmodClient/java/net/fabricmc/fabric/test/base/client/mixin/TitleScreenAccessor.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016, 2017, 2018, 2019 FabricMC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.fabricmc.fabric.test.base.client.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.client.gui.screen.TitleScreen; + +@Mixin(TitleScreen.class) +public interface TitleScreenAccessor { + @Accessor + boolean getDoBackgroundFade(); +} diff --git a/fabric-api-base/src/testmodClient/resources/fabric-api-base-testmod.client.mixins.json b/fabric-api-base/src/testmodClient/resources/fabric-api-base-testmod.client.mixins.json index d3a8d77423..ee12e340ba 100644 --- a/fabric-api-base/src/testmodClient/resources/fabric-api-base-testmod.client.mixins.json +++ b/fabric-api-base/src/testmodClient/resources/fabric-api-base-testmod.client.mixins.json @@ -5,7 +5,8 @@ "client": [ "CyclingButtonWidgetAccessor", "MinecraftDedicatedServerMixin", - "ScreenAccessor" + "ScreenAccessor", + "TitleScreenAccessor" ], "injectors": { "defaultRequire": 1