|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.gametest; |
| 9 | + |
| 10 | +import java.nio.file.Path; |
| 11 | + |
| 12 | +import org.lwjgl.glfw.GLFW; |
| 13 | +import org.slf4j.Logger; |
| 14 | + |
| 15 | +import net.fabricmc.fabric.api.client.gametest.v1.TestInput; |
| 16 | +import net.fabricmc.fabric.api.client.gametest.v1.context.ClientGameTestContext; |
| 17 | +import net.fabricmc.fabric.api.client.gametest.v1.context.TestClientWorldContext; |
| 18 | +import net.fabricmc.fabric.api.client.gametest.v1.context.TestServerContext; |
| 19 | +import net.fabricmc.fabric.api.client.gametest.v1.context.TestSingleplayerContext; |
| 20 | +import net.minecraft.world.item.Item; |
| 21 | +import net.minecraft.world.item.ItemStack; |
| 22 | +import net.minecraft.world.level.block.Block; |
| 23 | + |
| 24 | +public abstract class SingleplayerTest |
| 25 | +{ |
| 26 | + protected final ClientGameTestContext context; |
| 27 | + protected final TestSingleplayerContext spContext; |
| 28 | + protected final TestInput input; |
| 29 | + protected final TestClientWorldContext world; |
| 30 | + protected final TestServerContext server; |
| 31 | + protected final Logger logger = WurstTest.LOGGER; |
| 32 | + |
| 33 | + public SingleplayerTest(ClientGameTestContext context, |
| 34 | + TestSingleplayerContext spContext) |
| 35 | + { |
| 36 | + this.context = context; |
| 37 | + this.spContext = spContext; |
| 38 | + this.input = context.getInput(); |
| 39 | + this.world = spContext.getClientWorld(); |
| 40 | + this.server = spContext.getServer(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Runs the test and verifies cleanup afterward. |
| 45 | + */ |
| 46 | + public final void run() |
| 47 | + { |
| 48 | + runImpl(); |
| 49 | + assertScreenshotEquals( |
| 50 | + getClass().getSimpleName().toLowerCase() + "_cleanup", |
| 51 | + "https://i.imgur.com/i2Nr9is.png"); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Implement the actual test logic here. The test is responsible for |
| 56 | + * cleaning up after itself (disabling hacks, removing blocks, clearing |
| 57 | + * chat/inventory/particles, etc.). |
| 58 | + */ |
| 59 | + protected abstract void runImpl(); |
| 60 | + |
| 61 | + protected final void runCommand(String command) |
| 62 | + { |
| 63 | + WurstClientTestHelper.runCommand(server, command); |
| 64 | + } |
| 65 | + |
| 66 | + protected final void runWurstCommand(String command) |
| 67 | + { |
| 68 | + WurstClientTestHelper.runWurstCommand(context, command); |
| 69 | + } |
| 70 | + |
| 71 | + protected final void waitForBlock(int relX, int relY, int relZ, Block block) |
| 72 | + { |
| 73 | + context.waitFor(mc -> mc.level |
| 74 | + .getBlockState(mc.player.blockPosition().offset(relX, relY, relZ)) |
| 75 | + .getBlock() == block); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Waits for the hand swing and equip animations to finish. Call this |
| 80 | + * after any action that changes/clears the currently held item, if a |
| 81 | + * screenshot is taken soon after (<1 second). |
| 82 | + */ |
| 83 | + protected final void waitForHandSwing() |
| 84 | + { |
| 85 | + context.waitFor(mc -> { |
| 86 | + var renderer = |
| 87 | + mc.getEntityRenderDispatcher().getItemInHandRenderer(); |
| 88 | + return !mc.player.swinging && renderer.mainHandHeight == 1 |
| 89 | + && renderer.oMainHandHeight == 1; |
| 90 | + }, 20); |
| 91 | + } |
| 92 | + |
| 93 | + protected final void clearChat() |
| 94 | + { |
| 95 | + context.runOnClient(mc -> mc.gui.getChat().clearMessages(true)); |
| 96 | + } |
| 97 | + |
| 98 | + protected final void clearInventory() |
| 99 | + { |
| 100 | + input.pressKey(GLFW.GLFW_KEY_T); |
| 101 | + input.typeChars("/clear"); |
| 102 | + input.pressKey(GLFW.GLFW_KEY_ENTER); |
| 103 | + } |
| 104 | + |
| 105 | + protected final void clearParticles() |
| 106 | + { |
| 107 | + context.runOnClient(mc -> mc.particleEngine.clearParticles()); |
| 108 | + } |
| 109 | + |
| 110 | + protected final void clearToasts() |
| 111 | + { |
| 112 | + context.runOnClient(mc -> mc.getToastManager().clear()); |
| 113 | + } |
| 114 | + |
| 115 | + protected final void assertOneItemInSlot(int slot, Item item) |
| 116 | + { |
| 117 | + ItemStack stack = context |
| 118 | + .computeOnClient(mc -> mc.player.getInventory().getItem(slot)); |
| 119 | + if(!stack.is(item) || stack.getCount() != 1) |
| 120 | + throw new RuntimeException( |
| 121 | + "Expected 1 " + item.getName().getString() + " at slot " + slot |
| 122 | + + ", found " + stack.getCount() + " " |
| 123 | + + stack.getItem().getName().getString() + " instead"); |
| 124 | + } |
| 125 | + |
| 126 | + protected final void assertScreenshotEquals(String fileName, |
| 127 | + String templateUrl) |
| 128 | + { |
| 129 | + WurstClientTestHelper.assertScreenshotEquals(context, fileName, |
| 130 | + templateUrl); |
| 131 | + } |
| 132 | + |
| 133 | + protected final void failWithScreenshot(String fileName, String title, |
| 134 | + String errorMessage) |
| 135 | + { |
| 136 | + Path screenshotPath = context.takeScreenshot(fileName); |
| 137 | + |
| 138 | + WurstClientTestHelper |
| 139 | + .ghSummary("### " + title + "\n" + errorMessage + "\n"); |
| 140 | + String url = WurstClientTestHelper.tryUploadToImgur(screenshotPath); |
| 141 | + if(url != null) |
| 142 | + WurstClientTestHelper.ghSummary(""); |
| 143 | + else |
| 144 | + WurstClientTestHelper.ghSummary("Couldn't upload " + fileName |
| 145 | + + ".png to Imgur. Check the Test Screenshots.zip artifact."); |
| 146 | + |
| 147 | + throw new RuntimeException(title + ": " + errorMessage); |
| 148 | + } |
| 149 | + |
| 150 | + protected final void assertNoItemInSlot(int slot) |
| 151 | + { |
| 152 | + ItemStack stack = context |
| 153 | + .computeOnClient(mc -> mc.player.getInventory().getItem(slot)); |
| 154 | + if(!stack.isEmpty()) |
| 155 | + throw new RuntimeException("Expected no item in slot " + slot |
| 156 | + + ", found " + stack.getCount() + " " |
| 157 | + + stack.getItem().getName().getString() + " instead"); |
| 158 | + } |
| 159 | +} |
0 commit comments