Skip to content

Commit c434d06

Browse files
committed
More Dependency Injection Factories and Tests
1 parent 62642c7 commit c434d06

66 files changed

Lines changed: 1878 additions & 739 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ allprojects {
5858
events "failed"
5959
exceptionFormat "full"
6060
}
61+
doFirst {
62+
filter.includePatterns.each {
63+
include "${it.replaceAll('\\.', "\\${File.separator}")}.class"
64+
}
65+
filter.setIncludePatterns('*')
66+
}
6167
}
6268
}
6369

@@ -175,6 +181,7 @@ project(":ui") {
175181
ideProvider project(path: ':core', configuration: 'compile')
176182
compile group: 'org.controlsfx', name: 'controlsfx', version: '8.40.10'
177183
testCompile files(project(':core').sourceSets.test.output.classesDir)
184+
testCompile files(project(':core').sourceSets.test.output.resourcesDir)
178185
testCompile group: 'org.testfx', name: 'testfx-core', version: '4.0.+'
179186
testCompile group: 'org.testfx', name: 'testfx-junit', version: '4.0.+'
180187
}

core/src/main/java/edu/wpi/grip/core/GRIPCoreModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
4949

5050

5151
bind(Source.SourceFactory.class).to(Source.SourceFactoryImpl.class);
52+
bind(CameraSource.FrameGrabberFactory.class).to(CameraSource.FrameGrabberFactoryImpl.class);
5253
install(new FactoryModuleBuilder()
5354
.implement(CameraSource.class, CameraSource.class)
5455
.build(CameraSource.Factory.class));

core/src/main/java/edu/wpi/grip/core/Step.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Step {
2626
private OutputSocket<?>[] outputSockets;
2727
private Optional<?> data;
2828

29-
interface Factory {
29+
public interface Factory {
3030
Step create(Operation operation);
3131
}
3232

@@ -35,7 +35,7 @@ interface Factory {
3535
* @param operation The operation that is performed at this step.
3636
*/
3737
@Inject
38-
public Step(EventBus eventBus, @Assisted Operation operation) {
38+
Step(EventBus eventBus, @Assisted Operation operation) {
3939
this.operation = operation;
4040

4141
checkNotNull(eventBus);

core/src/main/java/edu/wpi/grip/core/serialization/StepConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class StepConverter implements Converter {
2525

2626
@Inject private EventBus eventBus;
2727
@Inject private Palette palette;
28+
@Inject private Step.Factory stepFactory;
2829

2930
@Override
3031
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
@@ -53,7 +54,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
5354

5455
// Instead of simply returning the step and having XStream insert it into the pipeline using reflection, send a
5556
// StepAddedEvent. This allows other interested classes (such as PipelineView) to also know when steps are added.
56-
this.eventBus.post(new StepAddedEvent(new Step(this.eventBus, operation.get())));
57+
this.eventBus.post(new StepAddedEvent(stepFactory.create(operation.get())));
5758

5859
while (reader.hasMoreChildren()) {
5960
context.convertAnother(this, Socket.class);

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,46 @@ public final class CameraSource extends Source implements StartStoppable {
4747
private Optional<Thread> frameThread;
4848

4949

50-
5150
public interface Factory {
5251
CameraSource create(int deviceNumber) throws IOException;
52+
5353
CameraSource create(String address) throws IOException;
54+
5455
CameraSource create(Properties properties) throws IOException;
5556
}
5657

58+
public interface FrameGrabberFactory {
59+
FrameGrabber create(int deviceNumber);
60+
FrameGrabber create(String addressProperty) throws MalformedURLException;
61+
}
62+
63+
public static class FrameGrabberFactoryImpl implements FrameGrabberFactory {
64+
FrameGrabberFactoryImpl() { /* no-op */ }
65+
66+
public FrameGrabber create(int deviceNumber) {
67+
// On Windows, videoInput is much more reliable for webcam capture. On other platforms, OpenCV's frame
68+
// grabber class works fine.
69+
if (StandardSystemProperty.OS_NAME.value().contains("Windows")) {
70+
return new VideoInputFrameGrabber(deviceNumber);
71+
} else {
72+
return new OpenCVFrameGrabber(deviceNumber);
73+
}
74+
}
75+
76+
public FrameGrabber create(String addressProperty) throws MalformedURLException {
77+
return new IPCameraFrameGrabber(addressProperty);
78+
}
79+
}
80+
5781
/**
5882
* Creates a camera source that can be used as an input to a pipeline
5983
*
6084
* @param eventBus The EventBus to attach to
6185
* @param deviceNumber The device number of the webcam
6286
*/
6387
@AssistedInject
64-
CameraSource(EventBus eventBus, @Assisted int deviceNumber) throws IOException {
65-
this(eventBus, createProperties(deviceNumber));
88+
CameraSource(EventBus eventBus, FrameGrabberFactory grabberFactory, @Assisted int deviceNumber) throws IOException {
89+
this(eventBus, grabberFactory, createProperties(deviceNumber));
6690
}
6791

6892
/**
@@ -72,39 +96,31 @@ public interface Factory {
7296
* @param address A URL to stream video from an IP camera
7397
*/
7498
@AssistedInject
75-
CameraSource(EventBus eventBus, @Assisted String address) throws IOException {
76-
this(eventBus, createProperties(address));
99+
CameraSource(EventBus eventBus, FrameGrabberFactory grabberFactory, @Assisted String address) throws IOException {
100+
this(eventBus, grabberFactory, createProperties(address));
77101
}
78102

79103
/**
80104
* Used for serialization
81105
*/
82106
@AssistedInject
83-
CameraSource(EventBus eventBus, @Assisted Properties properties) throws MalformedURLException {
107+
CameraSource(EventBus eventBus, FrameGrabberFactory grabberFactory, @Assisted Properties properties) throws MalformedURLException {
84108
this.frameThread = Optional.empty();
85109
this.eventBus = eventBus;
86110
this.frameOutputSocket = new OutputSocket<>(eventBus, imageOutputHint);
87111
this.frameRateOutputSocket = new OutputSocket<>(eventBus, frameRateOutputHint);
88112
this.properties = properties;
89113

90-
91114
final String deviceNumberProperty = properties.getProperty(DEVICE_NUMBER_PROPERTY);
92115
final String addressProperty = properties.getProperty(ADDRESS_PROPERTY);
93116

94117
if (deviceNumberProperty != null) {
95118
final int deviceNumber = Integer.valueOf(deviceNumberProperty);
96119
this.name = "Webcam " + deviceNumber;
97-
98-
// On Windows, videoInput is much more reliable for webcam capture. On other platforms, OpenCV's frame
99-
// grabber class works fine.
100-
if (StandardSystemProperty.OS_NAME.value().contains("Windows")) {
101-
this.grabber = new VideoInputFrameGrabber(deviceNumber);
102-
} else {
103-
this.grabber = new OpenCVFrameGrabber(deviceNumber);
104-
}
120+
this.grabber = grabberFactory.create(deviceNumber);
105121
} else if (addressProperty != null) {
106122
this.name = "IP Camera " + new URL(addressProperty).getHost();
107-
this.grabber = new IPCameraFrameGrabber(addressProperty);
123+
this.grabber = grabberFactory.create(addressProperty);
108124
} else {
109125
throw new IllegalArgumentException("Cannot initialize CameraSource without either a device number or " +
110126
"address");
@@ -196,7 +212,7 @@ public void start() throws IOException, IllegalStateException {
196212
*
197213
* @return The source that was stopped
198214
* @throws TimeoutException if the thread running the source fails to stop.
199-
* @throws IOException If there is a problem stopping the Source
215+
* @throws IOException If there is a problem stopping the Source
200216
*/
201217
public final void stop() throws TimeoutException, IllegalStateException {
202218
synchronized (this) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package edu.wpi.grip.core;
2+
3+
import com.google.common.eventbus.EventBus;
4+
import com.google.inject.assistedinject.Assisted;
5+
6+
import java.util.Optional;
7+
8+
public class MockStep extends Step {
9+
10+
public MockStep(EventBus eventBus, @Assisted Operation operation) {
11+
super(eventBus, operation);
12+
}
13+
14+
public MockStep() {
15+
super(new EventBus(), new Operation() {
16+
@Override
17+
public String getName() {
18+
return "Mock Operation";
19+
}
20+
21+
@Override
22+
public String getDescription() {
23+
return "A mock operation description";
24+
}
25+
26+
@Override
27+
public InputSocket<?>[] createInputSockets(EventBus eventBus) {
28+
return new InputSocket<?>[0];
29+
}
30+
31+
@Override
32+
public OutputSocket<?>[] createOutputSockets(EventBus eventBus) {
33+
return new OutputSocket<?>[0];
34+
}
35+
36+
@Override
37+
public void perform(InputSocket<?>[] inputs, OutputSocket<?>[] outputs, Optional<?> data){
38+
39+
}
40+
});
41+
}
42+
}

core/src/test/java/edu/wpi/grip/core/operations/OperationsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import com.google.common.base.Throwables;
44
import com.google.common.eventbus.EventBus;
55
import com.google.common.eventbus.Subscribe;
6+
import edu.wpi.grip.core.MockStep;
67
import edu.wpi.grip.core.Operation;
7-
import edu.wpi.grip.core.Step;
88
import edu.wpi.grip.core.events.OperationAddedEvent;
99
import edu.wpi.grip.generated.CVOperations;
1010
import org.junit.After;
@@ -48,15 +48,15 @@ public void afterTest() {
4848
public void testCreateAllCVSteps() {
4949
CVOperations.addOperations(eventBus);
5050
for (Operation operation : operationList) {
51-
new Step(eventBus, operation);
51+
new MockStep(eventBus, operation);
5252
}
5353
}
5454

5555
@Test
5656
public void testCreateAllCoreSteps() {
5757
Operations.addOperations(eventBus);
5858
for (Operation operation : operationList) {
59-
new Step(eventBus, operation);
59+
new MockStep(eventBus, operation);
6060
}
6161
}
6262
}

core/src/test/java/edu/wpi/grip/core/serialization/ProjectTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ public void testSerializeEmptyPipeline() throws Exception {
8484

8585
@Test
8686
public void testSerializePipelineWithSteps() throws Exception {
87-
eventBus.post(new StepAddedEvent(new Step(eventBus, additionOperation)));
88-
eventBus.post(new StepAddedEvent(new Step(eventBus, pythonAdditionOperationFromSource)));
89-
eventBus.post(new StepAddedEvent(new Step(eventBus, pythonAdditionOperationFromURL)));
87+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, additionOperation)));
88+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, pythonAdditionOperationFromSource)));
89+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, pythonAdditionOperationFromURL)));
9090

9191
serializeAndDeserialize();
9292

@@ -99,12 +99,12 @@ public void testSerializePipelineWithSteps() throws Exception {
9999
@Test
100100
@SuppressWarnings("unchecked")
101101
public void testSerializePipelineWithStepsAndConnections() throws Exception {
102-
Step step1 = new Step(eventBus, pythonAdditionOperationFromSource);
102+
Step step1 = new MockStep(eventBus, pythonAdditionOperationFromSource);
103103
InputSocket<Number> a1 = (InputSocket<Number>) step1.getInputSockets()[0];
104104
InputSocket<Number> b1 = (InputSocket<Number>) step1.getInputSockets()[1];
105105
OutputSocket<Number> sum1 = (OutputSocket<Number>) step1.getOutputSockets()[0];
106106

107-
Step step2 = new Step(eventBus, pythonAdditionOperationFromURL);
107+
Step step2 = new MockStep(eventBus, pythonAdditionOperationFromURL);
108108
InputSocket<Number> a2 = (InputSocket<Number>) step2.getInputSockets()[0];
109109
InputSocket<Number> b2 = (InputSocket<Number>) step2.getInputSockets()[1];
110110
OutputSocket<Number> sum2 = (OutputSocket<Number>) step2.getOutputSockets()[0];
@@ -128,7 +128,7 @@ public void testSerializePipelineWithStepsAndConnections() throws Exception {
128128
@Test
129129
@SuppressWarnings("unchecked")
130130
public void testPerformSerializedStep() throws Exception {
131-
eventBus.post(new StepAddedEvent(new Step(eventBus, additionOperation)));
131+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, additionOperation)));
132132
serializeAndDeserialize();
133133

134134
InputSocket<Number> a = (InputSocket<Number>) pipeline.getSteps().get(0).getInputSockets()[0];
@@ -143,7 +143,7 @@ public void testPerformSerializedStep() throws Exception {
143143
@Test
144144
@SuppressWarnings("unchecked")
145145
public void testPerformSerializedPythonStepFromURL() throws Exception {
146-
eventBus.post(new StepAddedEvent(new Step(eventBus, pythonAdditionOperationFromURL)));
146+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, pythonAdditionOperationFromURL)));
147147
serializeAndDeserialize();
148148

149149
InputSocket<Number> a = (InputSocket<Number>) pipeline.getSteps().get(0).getInputSockets()[0];
@@ -158,7 +158,7 @@ public void testPerformSerializedPythonStepFromURL() throws Exception {
158158
@Test
159159
@SuppressWarnings("unchecked")
160160
public void testPerformSerializedPythonStepFromSource() throws Exception {
161-
eventBus.post(new StepAddedEvent(new Step(eventBus, pythonAdditionOperationFromSource)));
161+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, pythonAdditionOperationFromSource)));
162162
serializeAndDeserialize();
163163

164164
InputSocket<Number> a = (InputSocket<Number>) pipeline.getSteps().get(0).getInputSockets()[0];
@@ -173,8 +173,8 @@ public void testPerformSerializedPythonStepFromSource() throws Exception {
173173
@Test
174174
@SuppressWarnings("unchecked")
175175
public void testPerformSerializedPipeline() throws Exception {
176-
Step step1 = new Step(eventBus, pythonAdditionOperationFromURL);
177-
Step step2 = new Step(eventBus, pythonAdditionOperationFromSource);
176+
Step step1 = new MockStep(eventBus, pythonAdditionOperationFromURL);
177+
Step step2 = new MockStep(eventBus, pythonAdditionOperationFromSource);
178178
eventBus.post(new StepAddedEvent(step1));
179179
eventBus.post(new StepAddedEvent(step2));
180180
eventBus.post(new ConnectionAddedEvent(
@@ -198,7 +198,7 @@ public void testPerformSerializedPipeline() throws Exception {
198198
@Test
199199
@SuppressWarnings("unchecked")
200200
public void testPerformSerializedPipelineWithMats() throws Exception {
201-
eventBus.post(new StepAddedEvent(new Step(eventBus, opencvAddOperation)));
201+
eventBus.post(new StepAddedEvent(new MockStep(eventBus, opencvAddOperation)));
202202
serializeAndDeserialize();
203203

204204
Step step1 = pipeline.getSteps().get(0);

0 commit comments

Comments
 (0)