Skip to content

Commit 09a3f95

Browse files
author
TinaTiel
committed
Moved common test helpers and vars into abstract test, cleaned up repetition
1 parent f6d0ef6 commit 09a3f95

File tree

3 files changed

+223
-186
lines changed

3 files changed

+223
-186
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package net.twasi.obsremotejava.test;
2+
3+
import static org.junit.jupiter.api.Assertions.fail;
4+
5+
import java.util.concurrent.BlockingQueue;
6+
import java.util.concurrent.LinkedBlockingQueue;
7+
import java.util.function.Consumer;
8+
import net.twasi.obsremotejava.OBSRemoteController;
9+
import org.junit.jupiter.api.BeforeAll;
10+
11+
public abstract class AbstractObsE2ETest {
12+
13+
static OBSRemoteController remote;
14+
BlockingQueue resultQueue = new LinkedBlockingQueue();
15+
16+
// Scenes
17+
final static String SCENE1 = "scene1";
18+
final static String SCENE2 = "scene2";
19+
final static String SCENE3 = "emptyscene";
20+
21+
// Sources
22+
final static String SOURCE_TEXT_SCENE1 = "scenename1";
23+
final static String SOURCE_TEXT_SCENE2 = "scenename2";
24+
final static String SOURCE_MEDIA = "media";
25+
final static String SOURCE_VLC_MEDIA = "vlc-media";
26+
final static String SOURCE_RED_SQUARE = "red_square";
27+
final static String SOURCE_RED_SQUARE_FILTER = "Color Correction";
28+
final static String SOURCE_BROWSER = "browser";
29+
final static String SOURCE_GROUP = "group";
30+
final static String SOURCE_GROUP_TEXT = "grouptext";
31+
32+
// Transitions and other OBS hard-coded names
33+
final static String TRANSITION_SLIDE = "Slide";
34+
final static String TRANSITION_CUT = "Cut";
35+
final static String TRANSITION_FADE = "Fade";
36+
final static String SOURCE_OBS_MIC = "Mic/Aux";
37+
final static String SOURCE_OBS_AUDIO = "Desktop Audio";
38+
39+
@BeforeAll
40+
static void beforeAll() {
41+
42+
// Connect to OBS
43+
remote = new OBSRemoteController("ws://localhost:4444", false);
44+
remote.registerConnectionFailedCallback(message -> {
45+
fail("Failed to connect to OBS: " + message);
46+
});
47+
remote.registerOnError((message, throwable) -> {
48+
fail("Failed to connect to OBS due to error: " + message);
49+
});
50+
remote.connect();
51+
52+
try {
53+
Thread.sleep(1000);
54+
} catch (InterruptedException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
59+
// Test Helpers
60+
void obsShould(String expected) {
61+
obsShould(expected, 3);
62+
}
63+
64+
void obsShould(String expected, int secondsTimeout) {
65+
System.out.println(">>> OBS SHOULD: " + expected);
66+
countDownFrom(secondsTimeout);
67+
}
68+
69+
void countDownFrom(int seconds) {
70+
for(int i = seconds; i > 0; i--) {
71+
System.out.println("> " + i);
72+
try {
73+
Thread.sleep(1000);
74+
} catch (InterruptedException e) {
75+
e.printStackTrace();
76+
}
77+
}
78+
}
79+
80+
static void connectToObs() {
81+
remote = new OBSRemoteController("ws://localhost:4444", false);
82+
remote.registerConnectionFailedCallback(message -> {
83+
fail("Failed to connect to OBS: " + message);
84+
});
85+
remote.registerOnError((message, throwable) -> {
86+
fail("Failed to connect to OBS due to error: " + message);
87+
});
88+
remote.connect();
89+
90+
try {
91+
Thread.sleep(1000);
92+
} catch (InterruptedException e) {
93+
e.printStackTrace();
94+
}
95+
}
96+
97+
void setupObs() {
98+
99+
// Cleanup all scenes
100+
cleanupScenes();
101+
102+
// Change back to base scene
103+
remote.changeSceneWithTransition("scene1", "Cut", result -> {
104+
if(result.getError() != null && !result.getError().isEmpty()) {
105+
fail("Failed to switch to base scene");
106+
}
107+
});
108+
}
109+
110+
void cleanupScenes() {
111+
// Hide all visible elements in all scenes
112+
remote.getScenes(sceneListResponse -> {
113+
sceneListResponse.getScenes().forEach(scene -> {
114+
scene.getSources().forEach(source -> {
115+
if(!source.getName().startsWith("scenename")) {
116+
remote.setSourceVisibility(scene.getName(), source.getName(), false, result -> {
117+
if(result.getError() != null && !result.getError().isEmpty()) {
118+
fail(String.format("Failed to hide source '%s' on scene '%s'", source.getName(), scene.getName()));
119+
}
120+
});
121+
}
122+
});
123+
});
124+
});
125+
}
126+
127+
Consumer loggingCallback = (obj) -> {
128+
System.out.println("Received response: " + obj);
129+
};
130+
131+
void waitReasonably() {
132+
waitReasonably(50);
133+
}
134+
135+
void waitReasonably(long ms) {
136+
try {
137+
Thread.sleep(ms);
138+
} catch (InterruptedException e) {
139+
e.printStackTrace();
140+
}
141+
}
142+
143+
Consumer capturingCallback = (obj) -> {
144+
System.out.println("Received response: " + obj + "(" + obj.getClass().getSimpleName() + ")");
145+
resultQueue.add(obj);
146+
};
147+
148+
<T> T getResponseAs(Class<T> clazz) {
149+
return clazz.cast(resultQueue.remove());
150+
}
151+
152+
}

src/integrationTest/java/net/twasi/obsremotejava/test/ObsRemoteE2eIT.java

Lines changed: 67 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
44
import static org.junit.jupiter.api.Assertions.fail;
55

6-
import java.io.File;
7-
import java.util.HashMap;
8-
import java.util.Map;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Set;
911
import java.util.concurrent.BlockingQueue;
1012
import java.util.concurrent.LinkedBlockingQueue;
1113
import java.util.function.Consumer;
14+
import java.util.stream.Collectors;
1215
import net.twasi.obsremotejava.OBSRemoteController;
1316
import net.twasi.obsremotejava.objects.Scene;
17+
import net.twasi.obsremotejava.objects.Source;
1418
import net.twasi.obsremotejava.requests.GetSceneList.GetSceneListResponse;
19+
import net.twasi.obsremotejava.requests.GetSourcesList.GetSourcesListResponse;
1520
import org.junit.jupiter.api.AfterAll;
1621
import org.junit.jupiter.api.AfterEach;
1722
import org.junit.jupiter.api.BeforeAll;
@@ -22,38 +27,17 @@
2227
* This test should be run manually, following the prompts in the command-line and
2328
* observing OBS for the desired behavior. Authentication should be disabled.
2429
*/
25-
public class ObsRemoteE2eIT {
26-
27-
static OBSRemoteController remote;
28-
29-
BlockingQueue resultQueue = new LinkedBlockingQueue();
30+
public class ObsRemoteE2eIT extends AbstractObsE2ETest {
3031

3132
@BeforeAll
3233
static void beforeAll() {
33-
34-
// Connect
35-
remote = new OBSRemoteController("ws://localhost:4444", false);
36-
remote.registerConnectionFailedCallback(message -> {
37-
fail("Failed to connect to OBS: " + message);
38-
});
39-
remote.registerOnError((message, throwable) -> {
40-
fail("Failed to connect to OBS due to error: " + message);
41-
});
42-
remote.connect();
43-
try {
44-
Thread.sleep(1000);
45-
} catch (InterruptedException e) {
46-
e.printStackTrace();
47-
}
34+
connectToObs();
4835
}
4936

5037
@BeforeEach
5138
public void beforeEach() {
52-
System.out.println("===============================");
53-
System.out.println(">> Resetting...");
5439
setupObs();
5540
resultQueue.clear();
56-
System.out.println(">> ...Ready");
5741
}
5842

5943
@AfterAll
@@ -62,83 +46,76 @@ static void afterAll() {
6246
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "Debug");
6347
}
6448

65-
@AfterEach
66-
public void afterEach() {
67-
try {
68-
Thread.sleep(1000);
69-
} catch (InterruptedException e) {
70-
e.printStackTrace();
71-
}
72-
System.out.println("<< Test Complete");
73-
74-
}
75-
7649
@Test
7750
void getScenes() {
7851

79-
remote.getScenes(capturingConsumer);
80-
81-
waitMs(50);
82-
52+
// Given expected scenes and sources
53+
List<String> scenes = Arrays.asList(SCENE1, SCENE2, SCENE3);
54+
List<String> scene1Sources = Arrays.asList(
55+
SOURCE_TEXT_SCENE1,
56+
SOURCE_RED_SQUARE,
57+
SOURCE_MEDIA,
58+
SOURCE_VLC_MEDIA,
59+
SOURCE_BROWSER,
60+
SOURCE_GROUP
61+
);
62+
List<String> scene1SourcesWithChildren = new ArrayList<>(scene1Sources);
63+
scene1SourcesWithChildren.add(SOURCE_GROUP_TEXT);
64+
List<String> scene2Sources = Arrays.asList(SOURCE_TEXT_SCENE2);
65+
66+
// When retrieved
67+
remote.getScenes(capturingCallback);
68+
waitReasonably();
69+
70+
// Then scenes match as expected
8371
GetSceneListResponse res = getResponseAs(GetSceneListResponse.class);
84-
assertThat(res.getScenes().size()).isEqualTo(3);
72+
assertThat(res.getScenes().stream().map(Scene::getName).collect(Collectors.toList()))
73+
.usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(scenes);
8574

75+
// And their sources match as expected
8676
Scene scene1 = res.getScenes().get(0);
87-
assertThat(scene1.getSources().size()).isEqualTo(6);
88-
assertThat(scene1.getSourcesIncludingGroupChildren().size()).isEqualTo(7);
77+
assertThat(scene1.getSources().stream().map(Source::getName).collect(Collectors.toList()))
78+
.usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(scene1Sources);
79+
assertThat(scene1.getSourcesIncludingGroupChildren().stream().map(Source::getName).collect(Collectors.toList()))
80+
.usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(scene1SourcesWithChildren);
81+
82+
Scene scene2 = res.getScenes().get(1);
83+
assertThat(scene2.getSources().stream().map(Source::getName).collect(Collectors.toList()))
84+
.usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(scene2Sources);
8985

9086
Scene emptyScene = res.getScenes().get(2);
9187
assertThat(emptyScene.getSources().size()).isZero();
9288

9389
}
9490

95-
// Private Test Helpers
96-
97-
private void setupObs() {
98-
99-
// Cleanup all scenes
100-
cleanupScenes();
101-
102-
// Change back to base scene
103-
remote.changeSceneWithTransition("scene1", "Cut", result -> {
104-
if(result.getError() != null && !result.getError().isEmpty()) {
105-
fail("Failed to switch to base scene");
106-
}
107-
});
108-
}
109-
110-
private void cleanupScenes() {
111-
// Hide all visible elements in all scenes
112-
remote.getScenes(sceneListResponse -> {
113-
sceneListResponse.getScenes().forEach(scene -> {
114-
scene.getSources().forEach(source -> {
115-
if(!source.getName().startsWith("scenename")) {
116-
remote.setSourceVisibility(scene.getName(), source.getName(), false, result -> {
117-
if(result.getError() != null && !result.getError().isEmpty()) {
118-
fail(String.format("Failed to hide source '%s' on scene '%s'", source.getName(), scene.getName()));
119-
}
120-
});
121-
}
122-
});
123-
});
124-
});
125-
}
126-
127-
void waitMs(long ms) {
128-
try {
129-
Thread.sleep(ms);
130-
} catch (InterruptedException e) {
131-
e.printStackTrace();
132-
}
133-
}
134-
135-
Consumer capturingConsumer = (obj) -> {
136-
System.out.println("Received response: " + obj + "(" + obj.getClass().getSimpleName() + ")");
137-
resultQueue.add(obj);
138-
};
91+
@Test
92+
void getSourcesList() {
93+
94+
// Given expected sources (all custom sources + mic and desktop audio default obs sources)
95+
List<String> expectedNames = Arrays.asList(
96+
SOURCE_TEXT_SCENE1,
97+
SOURCE_TEXT_SCENE2,
98+
SOURCE_RED_SQUARE,
99+
SOURCE_MEDIA,
100+
SOURCE_VLC_MEDIA,
101+
SOURCE_BROWSER,
102+
SOURCE_GROUP,
103+
SOURCE_GROUP_TEXT,
104+
"Mic/Aux",
105+
"Desktop Audio"
106+
);
107+
108+
// When retrieved
109+
remote.getSourcesList(capturingCallback);
110+
waitReasonably();
111+
112+
// Then it matches as expected
113+
GetSourcesListResponse res = getResponseAs(GetSourcesListResponse.class);
114+
List<Source> sources = res.getSources();
115+
List<String> actualNames = sources.stream().map(Source::getName).collect(Collectors.toList());
116+
assertThat(actualNames.size()).isEqualTo(sources.size());
117+
assertThat(actualNames).usingRecursiveComparison().ignoringCollectionOrder().isEqualTo(expectedNames);
139118

140-
<T> T getResponseAs(Class<T> clazz) {
141-
return clazz.cast(resultQueue.remove());
142119
}
143120

144121
}

0 commit comments

Comments
 (0)