Skip to content

Commit 98aa278

Browse files
committed
Adds tests for AddSourceView
1 parent 0689137 commit 98aa278

4 files changed

Lines changed: 296 additions & 9 deletions

File tree

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Provides a way to generate a constantly updated {@link Mat} from a camera
2929
*/
3030
@XStreamAlias(value = "grip:Camera")
31-
public final class CameraSource extends Source implements StartStoppable {
31+
public class CameraSource extends Source implements StartStoppable {
3232

3333
private final static String DEVICE_NUMBER_PROPERTY = "deviceNumber";
3434
private final static String ADDRESS_PROPERTY = "address";
@@ -216,7 +216,7 @@ public void start() throws IOException, IllegalStateException {
216216
* @throws IOException If there is a problem stopping the Source
217217
* @throws IllegalStateException If the camera is already stopped.
218218
*/
219-
public final void stop() throws TimeoutException, IOException {
219+
public void stop() throws TimeoutException, IOException {
220220
synchronized (this) {
221221
if (frameThread.isPresent()) {
222222
final Thread ex = frameThread.get();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package edu.wpi.grip.core.sources;
2+
3+
import com.google.common.eventbus.EventBus;
4+
import org.bytedeco.javacv.FrameGrabber;
5+
6+
import java.io.IOException;
7+
import java.net.MalformedURLException;
8+
9+
public class MockCameraSource extends CameraSource {
10+
11+
private boolean started = false;
12+
13+
static class FrameGrabberFactory implements CameraSource.FrameGrabberFactory {
14+
15+
@Override
16+
public FrameGrabber create(int deviceNumber) {
17+
return null;
18+
}
19+
20+
@Override
21+
public FrameGrabber create(String addressProperty) throws MalformedURLException {
22+
return null;
23+
}
24+
}
25+
26+
public MockCameraSource(EventBus eventBus, String address) throws IOException {
27+
super(eventBus, new FrameGrabberFactory(), address);
28+
}
29+
30+
public MockCameraSource(EventBus eventBus, int deviceNumber) throws IOException {
31+
super(eventBus, new FrameGrabberFactory(), deviceNumber);
32+
}
33+
34+
@Override
35+
public void start() throws IOException {
36+
started = true;
37+
}
38+
39+
@Override
40+
public void stop() {
41+
started = false;
42+
}
43+
44+
public boolean isStarted() {
45+
return started;
46+
}
47+
}

ui/src/main/java/edu/wpi/grip/ui/pipeline/AddSourceView.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.grip.ui.pipeline;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import com.google.common.eventbus.EventBus;
45
import com.google.inject.Inject;
56
import edu.wpi.grip.core.events.SourceAddedEvent;
@@ -27,6 +28,7 @@
2728
import java.net.URISyntaxException;
2829
import java.net.URL;
2930
import java.util.List;
31+
import java.util.Optional;
3032
import java.util.function.Consumer;
3133
import java.util.function.Predicate;
3234

@@ -37,21 +39,30 @@
3739
*/
3840
public class AddSourceView extends HBox {
3941

42+
@VisibleForTesting
43+
static final String SOURCE_DIALOG_STYLE_CLASS = "source-dialog";
4044
private final EventBus eventBus;
4145
private final MultiImageFileSource.Factory multiImageSourceFactory;
4246
private final ImageFileSource.Factory imageSourceFactory;
4347
private final CameraSource.Factory cameraSourceFactory;
4448

49+
private final Button webcamButton;
50+
private final Button ipcamButton;
51+
private Optional<Dialog> activeDialog = Optional.empty();
52+
4553
@FunctionalInterface
4654
private interface SupplierWithIO<T> {
4755
T getWithIO() throws IOException;
4856
}
4957

50-
private class SourceDialog extends Dialog<ButtonType> {
58+
private static class SourceDialog extends Dialog<ButtonType> {
5159
private final Text errorText = new Text();
5260

5361
private SourceDialog(final Parent root, Control inputField) {
5462
super();
63+
64+
this.getDialogPane().getStyleClass().add(SOURCE_DIALOG_STYLE_CLASS);
65+
5566
final GridPane gridContent = new GridPane();
5667
gridContent.setMaxWidth(Double.MAX_VALUE);
5768
GridPane.setHgrow(inputField, Priority.ALWAYS);
@@ -112,7 +123,7 @@ public interface Factory {
112123
}
113124
});
114125

115-
addButton("Add\nWebcam", getClass().getResource("/edu/wpi/grip/ui/icons/add-webcam.png"), mouseEvent -> {
126+
webcamButton = addButton("Add\nWebcam", getClass().getResource("/edu/wpi/grip/ui/icons/add-webcam.png"), mouseEvent -> {
116127
final Parent root = this.getScene().getRoot();
117128

118129
// Show a dialog for the user to pick a camera index
@@ -123,6 +134,8 @@ public interface Factory {
123134
dialog.setHeaderText("Choose a camera");
124135
dialog.setContentText("index");
125136

137+
dialog.getDialogPane().lookupButton(ButtonType.OK).requestFocus();
138+
126139
// If the user clicks OK, add a new camera source
127140
loadCamera(dialog,
128141
() -> {
@@ -135,7 +148,7 @@ public interface Factory {
135148
});
136149
});
137150

138-
addButton("Add IP\nCamera", getClass().getResource("/edu/wpi/grip/ui/icons/add-webcam.png"), mouseEvent -> {
151+
ipcamButton = addButton("Add IP\nCamera", getClass().getResource("/edu/wpi/grip/ui/icons/add-webcam.png"), mouseEvent -> {
139152
final Parent root = this.getScene().getRoot();
140153

141154
// Show a dialog for the user to pick a camera URL
@@ -181,6 +194,7 @@ public interface Factory {
181194
*/
182195
private void loadCamera(Dialog<ButtonType> dialog, SupplierWithIO<CameraSource> cameraSourceSupplier, Consumer<IOException> failureCallback) {
183196
assert Platform.isFxApplicationThread() : "Should only run in FX thread";
197+
activeDialog = Optional.of(dialog);
184198
dialog.showAndWait().filter(Predicate.isEqual(ButtonType.OK)).ifPresent(result -> {
185199
try {
186200
// Will try to create the camera with the values from the supplier
@@ -192,13 +206,14 @@ private void loadCamera(Dialog<ButtonType> dialog, SupplierWithIO<CameraSource>
192206
loadCamera(dialog, cameraSourceSupplier, failureCallback);
193207
}
194208
});
209+
activeDialog = Optional.empty();
195210
}
196211

197212

198213
/**
199214
* Add a new button for adding a source. This method takes care of setting the event handler.
200215
*/
201-
private void addButton(String text, URL graphicURL, EventHandler<? super MouseEvent> onMouseClicked) {
216+
private Button addButton(String text, URL graphicURL, EventHandler<? super MouseEvent> onMouseClicked) {
202217
final ImageView graphic = new ImageView(graphicURL.toString());
203218
graphic.setFitWidth(DPIUtility.SMALL_ICON_SIZE);
204219
graphic.setFitHeight(DPIUtility.SMALL_ICON_SIZE);
@@ -209,5 +224,29 @@ private void addButton(String text, URL graphicURL, EventHandler<? super MouseEv
209224
button.setOnMouseClicked(onMouseClicked);
210225

211226
this.getChildren().add(button);
227+
return button;
228+
}
229+
230+
@VisibleForTesting
231+
Button getWebcamButton() {
232+
return webcamButton;
233+
}
234+
235+
@VisibleForTesting
236+
Button getIpcamButton() {
237+
return ipcamButton;
238+
}
239+
240+
@VisibleForTesting
241+
void closeDialogs() {
242+
activeDialog.ifPresent(dialog -> {
243+
for ( ButtonType bt : dialog.getDialogPane().getButtonTypes() ) {
244+
if ( bt.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE ) {
245+
Button cancelButton = ( Button ) dialog.getDialogPane().lookupButton( bt );
246+
Platform.runLater(() -> cancelButton.fire());
247+
break;
248+
}
249+
}
250+
});
212251
}
213252
}

0 commit comments

Comments
 (0)