Skip to content

Commit

Permalink
Fix title screen screenshot in automated client test (FabricMC#4177)
Browse files Browse the repository at this point in the history
* Fix title screen screenshot

* Use accessor to detect when background fade is done
  • Loading branch information
Alexander01998 authored Oct 21, 2024
1 parent 7d94de0 commit 2127dc7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
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;
import java.io.UncheckedIOException;
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;
Expand Down Expand Up @@ -83,7 +85,8 @@ private void runTest() {

{
waitForScreen(TitleScreen.class);
takeScreenshot("title_screen");
waitForTitleScreenFade();
takeScreenshot("title_screen", Duration.ZERO);
clickScreenButton("menu.singleplayer");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -80,8 +82,12 @@ private static void setScreen(Function<MinecraftClient, Screen> 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) -> {
Expand Down Expand Up @@ -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<MinecraftClient> predicate) {
waitFor(what, predicate, Duration.ofSeconds(10));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"client": [
"CyclingButtonWidgetAccessor",
"MinecraftDedicatedServerMixin",
"ScreenAccessor"
"ScreenAccessor",
"TitleScreenAccessor"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 2127dc7

Please sign in to comment.