package com.projectsacrifice.game; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import android.app.Instrumentation; import androidx.test.core.app.ActivityScenario; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) public final class PuzzleSystemIntegrationTest { @Test public void pressureBodyPublishesSignalAndDoorEventsThroughProductionInput() { Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); try (ActivityScenario activity = ActivityScenario.launch(MainActivity.class)) { GameView[] view = new GameView[1]; activity.onActivity(current -> view[0] = current.gameViewForAutomation()); LevelPlaytestBot.Result result = new LevelPlaytestBot().run( new AndroidDriver(instrumentation, view[0]), EarlyLevelScenarios.levelThree(), 25_000L); assertTrue(result.diagnostic(), result.completed()); PuzzleSystem.Diagnostics[] diagnostics = new PuzzleSystem.Diagnostics[1]; instrumentation.runOnMainSync( () -> diagnostics[0] = view[0].puzzleDiagnosticsForAutomation()); assertTrue(diagnostics[0].updates() > 0L); assertTrue(diagnostics[0].signalActivatedTransitions() >= 1L); assertTrue(diagnostics[0].doorOpenedTransitions() >= 1L); assertEquals(1, diagnostics[0].openDoors()); } } private static final class AndroidDriver implements LevelPlaytestBot.Driver { private final Instrumentation instrumentation; private final GameView view; AndroidDriver(Instrumentation instrumentation, GameView view) { this.instrumentation = instrumentation; this.view = view; } @Override public void startLevel(int oneBasedLevelNumber) { instrumentation.runOnMainSync( () -> view.startLevelForAutomation(oneBasedLevelNumber)); } @Override public void setInput(boolean moveLeft, boolean moveRight, boolean jump) { instrumentation.runOnMainSync( () -> view.setAutomationInput(moveLeft, moveRight, jump)); } @Override public GameView.AutomationSnapshot snapshot() { GameView.AutomationSnapshot[] result = new GameView.AutomationSnapshot[1]; instrumentation.runOnMainSync(() -> result[0] = view.automationSnapshot()); return result[0]; } @Override public void waitForNextDecision(long millis) { try { Thread.sleep(millis); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new AssertionError("Puzzle integration bot interrupted", interrupted); } } } }