|
| 1 | +package net.twasi.obsremotejava.test; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import net.twasi.obsremotejava.OBSRemoteController; |
| 7 | +import org.junit.jupiter.api.AfterAll; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.TestInstance; |
| 13 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 14 | + |
| 15 | +/** |
| 16 | + * This test should be run manually, following the prompts in the command-line and |
| 17 | + * observing OBS for the desired behavior. Authentication should be disabled. |
| 18 | + */ |
| 19 | +@TestInstance(Lifecycle.PER_CLASS) |
| 20 | +public class E2EUnsecuredIT { |
| 21 | + |
| 22 | + OBSRemoteController remote; |
| 23 | + |
| 24 | + @BeforeAll |
| 25 | + public void beforeAll() { |
| 26 | + |
| 27 | + // Connect |
| 28 | + remote = new OBSRemoteController("ws://localhost:4444", false); |
| 29 | + remote.registerConnectionFailedCallback(message -> { |
| 30 | + fail("Failed to connect to OBS: " + message); |
| 31 | + }); |
| 32 | + remote.registerOnError((message, throwable) -> { |
| 33 | + fail("Failed to connect to OBS due to error: " + message); |
| 34 | + }); |
| 35 | + remote.connect(); |
| 36 | + try { |
| 37 | + Thread.sleep(1000); |
| 38 | + } catch (InterruptedException e) { |
| 39 | + e.printStackTrace(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @BeforeEach |
| 44 | + public void beforeEach() { |
| 45 | + System.out.println("==============================="); |
| 46 | + System.out.println(">> Resetting..."); |
| 47 | + setupObs(); |
| 48 | + System.out.println(">> ...Ready"); |
| 49 | + } |
| 50 | + |
| 51 | + @AfterAll |
| 52 | + public void afterAll() { |
| 53 | + remote.disconnect(); |
| 54 | + System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "Debug"); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + public void afterEach() { |
| 59 | + try { |
| 60 | + Thread.sleep(1000); |
| 61 | + } catch (InterruptedException e) { |
| 62 | + e.printStackTrace(); |
| 63 | + } |
| 64 | + System.out.println("<< Test Complete"); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void switchScene() { |
| 70 | + obsShould("Switch to scene2"); |
| 71 | + remote.changeSceneWithTransition("scene2", "Cut", res -> {}); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void showHideSceneItem() { |
| 76 | + obsShould("Show the red square"); |
| 77 | + remote.setSourceVisibility(null, "red_square", true, res -> {}); |
| 78 | + obsShould("Hide the red square"); |
| 79 | + remote.setSourceVisibility(null, "red_square", false, res -> {}); |
| 80 | + } |
| 81 | + |
| 82 | + // Private Test Helpers |
| 83 | + private void obsShould(String expected) { |
| 84 | + obsShould(expected, 3); |
| 85 | + } |
| 86 | + |
| 87 | + private void obsShould(String expected, int secondsTimeout) { |
| 88 | + System.out.println(">>> OBS SHOULD: " + expected); |
| 89 | + countDownFrom(secondsTimeout); |
| 90 | + } |
| 91 | + |
| 92 | + private void countDownFrom(int seconds) { |
| 93 | + for(int i = seconds; i > 0; i--) { |
| 94 | + System.out.println("> " + i); |
| 95 | + try { |
| 96 | + Thread.sleep(1000); |
| 97 | + } catch (InterruptedException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private interface EachTickCallback { |
| 104 | + void run(int i); |
| 105 | + } |
| 106 | + |
| 107 | + private void setupObs() { |
| 108 | + |
| 109 | + // Cleanup all scenes |
| 110 | + cleanupScenes(); |
| 111 | + |
| 112 | + // Change back to base scene |
| 113 | + remote.changeSceneWithTransition("scene1", "Cut", result -> { |
| 114 | + if(result.getError() != null && !result.getError().isEmpty()) { |
| 115 | + fail("Failed to switch to base scene"); |
| 116 | + } |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + private void cleanupScenes() { |
| 121 | + // Hide all visible elements in all scenes |
| 122 | + remote.getScenes(sceneListResponse -> { |
| 123 | + sceneListResponse.getScenes().forEach(scene -> { |
| 124 | + scene.getSources().forEach(source -> { |
| 125 | + if(!source.getName().startsWith("scenename")) { |
| 126 | + remote.setSourceVisibility(scene.getName(), source.getName(), false, result -> { |
| 127 | + if(result.getError() != null && !result.getError().isEmpty()) { |
| 128 | + fail(String.format("Failed to hide source '%s' on scene '%s'", source.getName(), scene.getName())); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments