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 WeightedObjectPlaythroughTest { @Test public void allFiveObjectTypesOpenTheMassNineGateThreeTimes() { try (ActivityScenario activity = ActivityScenario.launch(MainActivity.class)) { Driver driver = Driver.from(activity); for (int run = 1; run <= 3; run++) { LevelPlaytestBot.Result result = new LevelPlaytestBot().run( driver, WeightedObjectScenarios.levelSeventeen(), 50_000L); assertTrue("run=" + run + "; " + result.diagnostic(), result.completed()); assertEquals("weighted route is deathless", 0, result.deaths()); assertEquals("weighted route does not create bodies", 0, result.corpseCount()); } } } private static final class Driver implements LevelPlaytestBot.Driver { private final Instrumentation instrumentation; private final GameView view; private Driver(Instrumentation instrumentation, GameView view) { this.instrumentation = instrumentation; this.view = view; } static Driver from(ActivityScenario activity) { GameView[] target = new GameView[1]; activity.onActivity(current -> target[0] = current.gameViewForAutomation()); return new Driver(InstrumentationRegistry.getInstrumentation(), target[0]); } @Override public void startLevel(int oneBasedLevelNumber) { instrumentation.runOnMainSync(() -> view.startLevelForAutomation(oneBasedLevelNumber)); } @Override public void setInput(boolean left, boolean right, boolean jump) { instrumentation.runOnMainSync(() -> view.setAutomationInput(left, right, jump)); } @Override public GameView.AutomationSnapshot snapshot() { GameView.AutomationSnapshot[] snapshot = new GameView.AutomationSnapshot[1]; instrumentation.runOnMainSync(() -> snapshot[0] = view.automationSnapshot()); return snapshot[0]; } @Override public void waitForNextDecision(long millis) { try { Thread.sleep(Math.max(32L, millis)); } catch (InterruptedException interrupted) { Thread.currentThread().interrupt(); throw new AssertionError("Weighted-object bot interrupted", interrupted); } } } }