1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Pocket_Computer_Test.kt
Jonathan Coates 3a96aea894
Add a couple of tests for pocket computers
- Ensure they're correctly synced to the client. This definitely isn't
   comprehensive, but doing anything further probably involves multiple
   players, which is tricky.

 - Quick rendering test for in-hand computers.
2022-11-21 21:34:35 +00:00

101 lines
4.1 KiB
Kotlin

package dan200.computercraft.gametest
import dan200.computercraft.api.lua.ObjectArguments
import dan200.computercraft.client.pocket.ClientPocketComputers
import dan200.computercraft.core.apis.TermAPI
import dan200.computercraft.gametest.api.*
import dan200.computercraft.mixin.gametest.GameTestHelperAccessor
import dan200.computercraft.shared.ModRegistry
import dan200.computercraft.shared.computer.core.ComputerState
import dan200.computercraft.shared.pocket.items.PocketComputerItem
import dan200.computercraft.test.core.computer.getApi
import net.minecraft.core.BlockPos
import net.minecraft.gametest.framework.GameTestHelper
import net.minecraft.gametest.framework.GameTestSequence
import org.junit.jupiter.api.Assertions.assertEquals
import kotlin.random.Random
class Pocket_Computer_Test {
/**
* Checks pocket computer state is synced to the holding player.
*/
@ClientGameTest(template = Structures.DEFAULT)
fun Sync_state(context: GameTestHelper) = context.sequence {
// We use a unique label for each test run as computers from previous runs may not have been disposed yet.
val unique = java.lang.Long.toHexString(Random.nextLong())
// Give the player a pocket computer.
thenExecute {
context.positionAt(BlockPos(2, 2, 2))
context.givePocketComputer(unique)
}
// Write some text to the computer.
thenOnComputer(unique) { getApi<TermAPI>().write(ObjectArguments("Hello, world!")) }
// And ensure its synced to the client.
thenIdle(4)
thenOnClient {
val pocketComputer = ClientPocketComputers.get(minecraft.player!!.mainHandItem)
assertEquals(ComputerState.ON, pocketComputer.state)
val term = pocketComputer.terminal
assertEquals("Hello, world!", term.getLine(0).toString().trim(), "Terminal contents is synced")
}
// Update the terminal contents again.
thenOnComputer(unique) {
val term = getApi<TermAPI>()
term.setCursorPos(1, 1)
term.setCursorBlink(true)
term.write(ObjectArguments("Updated text :)"))
}
// And ensure the new computer state and terminal are sent.
thenIdle(4)
thenOnClient {
val pocketComputer = ClientPocketComputers.get(minecraft.player!!.mainHandItem)
assertEquals(ComputerState.BLINKING, pocketComputer.state)
val term = pocketComputer.terminal
assertEquals("Updated text :)", term.getLine(0).toString().trim(), "Terminal contents is synced")
}
}
/**
* Checks pocket computers are rendered when being held like a map.
*/
@ClientGameTest(template = Structures.DEFAULT)
fun Renders_map_view(context: GameTestHelper) = context.sequence {
// We use a unique label for each test run as computers from previous runs may not have been disposed yet.
val unique = java.lang.Long.toHexString(Random.nextLong())
// Give the player a pocket computer.
thenExecute {
context.positionAt(BlockPos(2, 2, 2), xRot = 90.0f)
context.givePocketComputer(unique)
}
thenOnComputer(unique) {
val terminal = getApi<TermAPI>().terminal
terminal.write("Hello, world!")
terminal.setCursorPos(1, 2)
terminal.textColour = 2
terminal.backgroundColour = 3
terminal.write("Some coloured text")
}
thenIdle(4)
thenScreenshot(showGui = true)
}
/**
* Give the current player a pocket computer, suitable to be controlled by [GameTestSequence.thenOnComputer].
*/
private fun GameTestHelper.givePocketComputer(name: String? = null) {
val player = level.randomPlayer!!
player.inventory.clearContent()
val testName = (this as GameTestHelperAccessor).testInfo.testName
val label = testName + (if (name == null) "" else ".$name")
val item = ModRegistry.Items.POCKET_COMPUTER_ADVANCED.get().create(1, label, -1, null)
item.getOrCreateTag().putBoolean(PocketComputerItem.NBT_ON, true)
player.inventory.setItem(0, item)
}
}