Search in sources :

Example 36 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblControllerTest method testCancelSend.

/**
 * Test of numCancelSendCalls method, of class GrblController.
 */
@Test
public void testCancelSend() throws Exception {
    System.out.println("cancelSend");
    GrblController instance = new GrblController(mgc);
    instance.openCommPort("blah", 1234);
    // 0. Test GRBL not returning to idle during cancel.
    instance.rawResponseHandler("Grbl 0.8c");
    instance.cancelSend();
    for (int i = 0; i < 50; i++) {
        instance.rawResponseHandler("<Running,MPos:1.0,2.0,3.0>");
    }
    assertEquals(1, mgc.numPauseSendCalls);
    assertEquals(1, mgc.numCancelSendCalls);
    assertEquals(0, mgc.numSoftResetCalls);
    instance.resumeStreaming();
    setState(instance, COMM_IDLE);
    // Test 1.1 Cancel when nothing is running (Grbl 0.7).
    instance.rawResponseHandler("Grbl 0.7");
    instance.cancelSend();
    assertEquals(2, mgc.numCancelSendCalls);
    assertEquals(0, mgc.numSoftResetCalls);
    // Test 1.2 Cancel when nothing is running (Grbl 0.8c).
    // Check for soft reset.
    instance.rawResponseHandler("Grbl 0.8c");
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(3, mgc.numCancelSendCalls);
    assertEquals(2, mgc.numPauseSendCalls);
    assertEquals(1, mgc.numSoftResetCalls);
    instance.resumeStreaming();
    // Test 2.1
    // Add 30 commands, start send, cancel before any sending. (Grbl 0.7)
    instance.rawResponseHandler("Grbl 0.7");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
    } catch (Exception ex) {
        fail("Unexpected exception from GrblController: " + ex.getMessage());
    }
    instance.cancelSend();
    assertEquals(4, mgc.numCancelSendCalls);
    assertEquals(2, mgc.numPauseSendCalls);
    assertEquals(1, mgc.numSoftResetCalls);
    assertEquals(30, instance.rowsInSend());
    assertEquals(30, instance.rowsRemaining());
    // Test 2.2
    // Add 30 commands, start send, cancel before any sending. (Grbl 0.8c)
    // setUp();
    // instance = new GrblController(mgc);
    instance.rawResponseHandler("Grbl 0.8c");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
    } catch (Exception ex) {
        fail("Unexpected exception from GrblController: " + ex.getMessage());
    }
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(0, instance.rowsInSend());
    assertEquals(0, instance.rowsRemaining());
    assertEquals(5, mgc.numCancelSendCalls);
    assertEquals(3, mgc.numPauseSendCalls);
    assertEquals(2, mgc.numSoftResetCalls);
    instance.resumeStreaming();
    // Test 3.1
    // Add 30 commands, start send, cancel after sending 15. (Grbl 0.7)
    instance.rawResponseHandler("Grbl 0.7");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X0"));
    }
    try {
        instance.beginStreaming();
        for (int i = 0; i < 15; i++) {
            GcodeCommand command = new GcodeCommand("G0X0");
            command.setSent(true);
            command.setResponse("ok");
            instance.commandSent(command);
        }
    } catch (Exception ex) {
        fail("Unexpected exception from command sent: " + ex.getMessage());
    }
    instance.cancelSend();
    assertEquals(6, mgc.numCancelSendCalls);
    assertEquals(3, mgc.numPauseSendCalls);
    assertEquals(2, mgc.numSoftResetCalls);
    assertEquals(30, instance.rowsInSend());
    assertEquals(30, instance.rowsRemaining());
    // wrap up
    wrapUp(instance, 15);
    // Test 3.2
    // Add 30 commands, start send, cancel after sending 15. (Grbl 0.8c)
    instance.rawResponseHandler("Grbl 0.8c");
    for (int i = 0; i < 30; i++) {
        instance.queueCommand(instance.createCommand("G0X" + i));
    }
    try {
        instance.beginStreaming();
        for (int i = 0; i < 15; i++) {
            GcodeCommand command = new GcodeCommand("G0 X1");
            command.setSent(true);
            command.setResponse("ok");
            instance.commandSent(command);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception from command sent: " + ex.getMessage());
    }
    instance.cancelSend();
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    instance.rawResponseHandler("<Hold,MPos:1.0,2.0,3.0>");
    assertEquals(15, instance.rowsSent());
    assertEquals(0, instance.rowsInSend());
    assertEquals(0, instance.rowsRemaining());
    assertEquals(7, mgc.numCancelSendCalls);
    assertEquals(4, mgc.numPauseSendCalls);
    assertEquals(3, mgc.numSoftResetCalls);
    assertEquals(new Byte(GrblUtils.GRBL_RESET_COMMAND), mgc.sentBytes.get(mgc.sentBytes.size() - 1));
    instance.resumeStreaming();
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 37 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblControllerTest method wrapUp.

private static void wrapUp(GrblController gc, int numCommands) {
    // wrap up
    try {
        GcodeCommand command = new GcodeCommand("blah");
        command.setSent(true);
        command.setResponse("ok");
        for (int i = 0; i < numCommands; i++) {
            gc.commandComplete(command.getCommandString());
        }
    } catch (Exception ex) {
        fail("Unexpected exception testing cancelSend: " + ex.getMessage());
    }
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) IOException(java.io.IOException)

Example 38 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class GrblControllerTest method testCommandComplete.

/**
 * Test of commandComplete method, of class GrblController.
 */
@Test
public void testCommandComplete() {
    System.out.println("commandComplete");
    GcodeCommand command = null;
    GrblController instance = new GrblController(mgc);
    // Test 1. Complete a command that was marked as sent but never declared
    // within commandSent(command).
    command = new GcodeCommand("blah");
    command.setSent(true);
    boolean hitException = false;
    try {
        instance.commandComplete(command.getCommandString());
    } catch (UnexpectedCommand ex) {
        hitException = true;
    }
    assertEquals(true, hitException);
// TODO: Test that command complete triggers a listener event.
// TODO: Test that command complete triggers fileStreamComplete.
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) UnexpectedCommand(com.willwinder.universalgcodesender.AbstractController.UnexpectedCommand) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 39 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class AbstractControllerTest method testCommandSent.

/**
 * Test of commandSent method, of class AbstractController.
 */
@Test
public void testCommandSent() throws Exception {
    System.out.println("commandSent");
    String port = "/some/port";
    int baud = 1234;
    String command = "command";
    // Setup instance with commands buffered on the communicator.
    startStreamExpectation(port, baud, command, false);
    EasyMock.replay(instance, mockCommunicator);
    startStream(port, baud, command);
    EasyMock.reset(instance, mockCommunicator, mockListener);
    // Make sure the events are triggered.
    Capture<GcodeCommand> gc1 = EasyMock.newCapture();
    Capture<GcodeCommand> gc2 = EasyMock.newCapture();
    mockListener.commandSent(EasyMock.capture(gc1));
    EasyMock.expect(EasyMock.expectLastCall());
    mockListener.commandSent(EasyMock.capture(gc2));
    EasyMock.expect(EasyMock.expectLastCall());
    EasyMock.replay(instance, mockCommunicator, mockListener);
    // Run test.
    // assertEquals(0, instance.rowsSent());
    instance.commandSent(new GcodeCommand(command));
    // assertEquals(1, instance.rowsSent());
    instance.commandSent(new GcodeCommand(command));
    // assertEquals(2, instance.rowsSent());
    assertTrue(gc1.getValue().isSent());
    assertTrue(gc2.getValue().isSent());
    EasyMock.verify(mockListener);
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) EasyMock.anyString(org.easymock.EasyMock.anyString) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Example 40 with GcodeCommand

use of com.willwinder.universalgcodesender.types.GcodeCommand in project Universal-G-Code-Sender by winder.

the class AbstractControllerTest method testCommandComplete.

/**
 * Test of commandComplete method, of class AbstractController.
 */
@Test
public void testCommandComplete() throws Exception {
    System.out.println("commandComplete");
    // Setup test with commands sent by communicator waiting on response.
    testCommandSent();
    reset(instance, mockCommunicator, mockListener);
    expect(instance.handlesAllStateChangeEvents()).andReturn(true).anyTimes();
    // Make sure the events are triggered.
    Capture<GcodeCommand> gc1 = newCapture();
    Capture<GcodeCommand> gc2 = newCapture();
    mockListener.commandComplete(capture(gc1));
    expect(expectLastCall());
    mockListener.commandComplete(capture(gc2));
    expect(expectLastCall());
    expect(expectLastCall());
    mockListener.messageForConsole(anyObject(), anyString());
    expect(expectLastCall());
    mockListener.fileStreamComplete("queued commands", true);
    mockListener.controlStateChange(UGSEvent.ControlState.COMM_IDLE);
    expect(expectLastCall());
    expect(mockCommunicator.areActiveCommands()).andReturn(true);
    expect(mockCommunicator.areActiveCommands()).andReturn(false);
    expect(mockCommunicator.numActiveCommands()).andReturn(0);
    replay(instance, mockCommunicator, mockListener);
    GcodeCommand first = instance.getActiveCommand().get();
    instance.commandComplete("ok");
    GcodeCommand second = instance.getActiveCommand().get();
    instance.commandComplete("ok");
    assertEquals(true, gc1.getValue().isDone());
    assertEquals(true, gc2.getValue().isDone());
    assertEquals("ok", gc1.getValue().getResponse());
    assertEquals("ok", gc2.getValue().getResponse());
    assertEquals(first, gc1.getValue());
    assertEquals(second, gc2.getValue());
    EasyMock.verify(mockListener);
}
Also used : GcodeCommand(com.willwinder.universalgcodesender.types.GcodeCommand) Test(org.junit.Test) GcodeStreamTest(com.willwinder.universalgcodesender.utils.GcodeStreamTest)

Aggregations

GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)51 Test (org.junit.Test)26 GcodeStreamTest (com.willwinder.universalgcodesender.utils.GcodeStreamTest)19 IOException (java.io.IOException)11 Connection (com.willwinder.universalgcodesender.connection.Connection)4 Position (com.willwinder.universalgcodesender.model.Position)4 ControllerListener (com.willwinder.universalgcodesender.listeners.ControllerListener)3 GcodeStreamReader (com.willwinder.universalgcodesender.utils.GcodeStreamReader)3 File (java.io.File)3 LinkedList (java.util.LinkedList)3 GcodeParser (com.willwinder.universalgcodesender.gcode.GcodeParser)2 EasyMock.anyString (org.easymock.EasyMock.anyString)2 UnexpectedCommand (com.willwinder.universalgcodesender.AbstractController.UnexpectedCommand)1 GcodeMeta (com.willwinder.universalgcodesender.gcode.GcodeParser.GcodeMeta)1 ArcExpander (com.willwinder.universalgcodesender.gcode.processors.ArcExpander)1 CommentProcessor (com.willwinder.universalgcodesender.gcode.processors.CommentProcessor)1 LineSplitter (com.willwinder.universalgcodesender.gcode.processors.LineSplitter)1 M30Processor (com.willwinder.universalgcodesender.gcode.processors.M30Processor)1 MeshLeveler (com.willwinder.universalgcodesender.gcode.processors.MeshLeveler)1 Translator (com.willwinder.universalgcodesender.gcode.processors.Translator)1