use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.
the class GrblControllerTest method testGetSendDuration.
/**
* Test of getSendDuration method, of class GrblController.
*/
@Test
public void testGetSendDuration() throws Exception {
System.out.println("getSendDuration");
GrblController instance = new GrblController(mgc);
instance.openCommPort("blah", 1234);
instance.rawResponseHandler("Grbl 0.8c");
// Test 1.
// Result when not sending and nothing has been sent.
long expResult = 0L;
long result = instance.getSendDuration();
assertEquals(expResult, result);
// Test 2.
// Result when stream has begun but not completed.
instance.queueCommand(instance.createCommand("G0X1"));
try {
instance.beginStreaming();
} catch (Exception ex) {
fail("Unexpected exception from GrblController: " + ex.getMessage());
}
try {
// Sleep for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException ex) {
fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
}
// Send duration should be around 2 seconds.
expResult = 2000L;
result = instance.getSendDuration();
// Assert that result is within 0.5 seconds of expected value.
assert (expResult <= result);
assert (result <= (expResult + 500));
try {
// Sleep for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException ex) {
fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
}
// Test 3.
// Wrap up the send and check the duration.
// Whitespace removed.
GcodeCommand command = new GcodeCommand("G0X1");
command.setSent(true);
command.setResponse("ok");
try {
instance.commandSent(command);
instance.commandComplete(command.getCommandString());
} catch (Exception ex) {
ex.printStackTrace();
fail("Unexpected exception from command complete: " + ex.getMessage());
}
expResult = 4000L;
result = instance.getSendDuration();
// Assert that result is within 0.5 seconds of expected value.
assert (expResult <= result);
assert (result <= (expResult + 500));
// Make sure the duration is no longer increasing.
try {
// Sleep for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException ex) {
fail("Unexpected exception from Thread.sleep: " + ex.getMessage());
}
result = instance.getSendDuration();
// Assert that result is within 0.5 seconds of expected value.
assert (expResult <= result);
assert (result <= (expResult + 500));
}
use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.
the class GrblControllerTest method testSendCommandImmediately.
/**
* Test of numQueueStringForCommCalls method, of class GrblController.
*/
@Test
public void testSendCommandImmediately() throws Exception {
System.out.println("queueStringForComm");
String str = "G0 X0 ";
GrblController instance = new GrblController(mgc);
instance.openCommPort("blah", 123);
instance.rawResponseHandler("Grbl 0.8c");
// $G and $$ get queued on startup
assertEquals(2, mgc.numQueueStringForCommCalls);
assertEquals(2, mgc.numStreamCommandsCalls);
GcodeCommand command = instance.createCommand(str);
instance.sendCommandImmediately(command);
assertEquals(3, mgc.numQueueStringForCommCalls);
assertEquals(3, mgc.numStreamCommandsCalls);
assertEquals(str + "\n", mgc.queuedString);
}
use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.
the class GrblControllerTest method errorInCheckModeSending.
@Test
public void errorInCheckModeSending() throws Exception {
// Given
AbstractCommunicator communicator = mock(AbstractCommunicator.class);
ControllerListener controllerListener = mock(ControllerListener.class);
GrblController gc = new GrblController(communicator);
gc.addListener(controllerListener);
// We will assume that we are using version Grbl 1.0 with streaming support
gc.rawResponseHandler("Grbl 1.1f");
gc.rawResponseHandler("<Check|MPos:0.000,0.000,0.000|FS:0,0|Pn:XYZ>");
doReturn(true).when(communicator).isCommOpen();
// When
gc.queueCommand(new GcodeCommand("G0 X10"));
gc.beginStreaming();
gc.communicatorPausedOnError();
gc.rawResponseHandler("error:1");
// Then
assertEquals(gc.getControlState(), COMM_CHECK);
assertFalse(gc.isPaused());
verify(communicator, times(1)).sendByteImmediately(GRBL_PAUSE_COMMAND);
verify(controllerListener, times(1)).controlStateChange(COMM_SENDING_PAUSED);
}
use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.
the class BufferedCommunicatorTest method responseMessageOnErrorShouldDispatchPauseEvent.
@Test
public void responseMessageOnErrorShouldDispatchPauseEvent() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
SerialCommunicatorListener serialCommunicatorListener = mock(SerialCommunicatorListener.class);
instance.addCommandEventListener(serialCommunicatorListener);
asl.add(new GcodeCommand("G0"));
asl.add(new GcodeCommand("G0"));
instance.streamCommands();
// When
instance.responseMessage("error");
// Then
assertTrue(instance.isPaused());
verify(serialCommunicatorListener, times(1)).communicatorPausedOnError();
}
use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.
the class BufferedCommunicatorTest method softResetShouldClearBuffersAndResumeOperation.
@Test
public void softResetShouldClearBuffersAndResumeOperation() {
// Given
Connection connection = mock(Connection.class);
instance.setConnection(connection);
asl.add(new GcodeCommand("G0"));
cb.add("G0");
instance.pauseSend();
assertTrue(instance.isPaused());
assertEquals(1, instance.numActiveCommands());
assertEquals(1, instance.numBufferedCommands());
// When
instance.softReset();
// Then
assertFalse("The communicator should resume operation after a reset", instance.isPaused());
assertEquals("There should be no active commands", 0, instance.numActiveCommands());
assertEquals("There should be no buffered manual commands", 0, instance.numBufferedCommands());
}
Aggregations