Search in sources :

Example 1 with MockConnection

use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.

the class GrblCommunicatorTest method testCancelSend.

/**
 * Test of cancelSend method, of class GrblCommunicator.
 */
@Test
public void testCancelSend() {
    System.out.println("cancelSend");
    MockConnection mc = new MockConnection(mg.in, mg.out);
    GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
    String twentyCharString = "twenty characters...";
    String grblReceiveString;
    String[] arr;
    int expectedInt;
    Boolean expectedBool;
    // Queue some commands, but cancel before sending them.
    for (int i = 0; i < 30; i++) {
        instance.queueStringForComm(twentyCharString);
    }
    instance.cancelSend();
    // try streaming them and make sure nothing is received.
    instance.streamCommands();
    grblReceiveString = mg.readStringFromGrblBuffer();
    expectedInt = 0;
    assertEquals(expectedInt, grblReceiveString.length());
    // that there are active commands.
    for (int i = 0; i < 30; i++) {
        instance.queueStringForComm(twentyCharString);
    }
    instance.streamCommands();
    instance.cancelSend();
    // Make sure cancelSend clears out the commands
    expectedBool = false;
    assertEquals(expectedBool, instance.areActiveCommands());
    grblReceiveString = mg.readStringFromGrblBuffer();
    arr = grblReceiveString.split("\n");
    expectedInt = GrblUtils.GRBL_RX_BUFFER_SIZE / (twentyCharString.length() + 1);
    assertEquals(expectedInt, arr.length);
    for (String arr1 : arr) {
        mc.sendResponse("ok");
    }
    // Make sure canceled commands are not sent.
    instance.streamCommands();
    grblReceiveString = mg.readStringFromGrblBuffer();
    expectedInt = 0;
    assertEquals(expectedInt, grblReceiveString.length());
}
Also used : MockConnection(com.willwinder.universalgcodesender.mockobjects.MockConnection) Test(org.junit.Test)

Example 2 with MockConnection

use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.

the class GrblCommunicatorTest method testQueueStringForComm.

/**
 * Test of queueStringForComm method, of class GrblCommunicator.
 */
@Test
public void testQueueStringForComm() throws Exception {
    System.out.println("queueStringForComm");
    String input = "someCommand";
    MockConnection mc = new MockConnection(mg.in, mg.out);
    GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
    try {
        instance.queueStringForComm(input);
        // The cb preloads commands so the size represents queued commands.
        assertEquals(1, cb.size());
        // Test that instance adds newline to improperly formed command.
        String next = cb.peek();
        assertEquals(input, cb.peek());
        instance.queueStringForComm(input);
        instance.queueStringForComm(input);
        // Test that instance continues to queue inputs.
        assertEquals(3, cb.size());
        input = "someCommand";
        cb = new LinkedBlockingDeque<>();
        mc = new MockConnection(mg.in, mg.out);
        instance = new GrblCommunicator(cb, asl, mc);
        instance.queueStringForComm(input);
        // Test that instance doesn't add superfluous newlines.
        assertEquals(input, cb.peek());
    } catch (Exception e) {
        fail("queueStringForComm threw an exception: " + e.getMessage());
    }
}
Also used : MockConnection(com.willwinder.universalgcodesender.mockobjects.MockConnection) Test(org.junit.Test)

Example 3 with MockConnection

use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.

the class GrblCommunicatorTest method testPauseSendAndResumeSend.

/**
 * Test of pauseSend method, of class GrblCommunicator.
 */
@Test
public void testPauseSendAndResumeSend() {
    System.out.println("pauseSend");
    MockConnection mc = new MockConnection(mg.in, mg.out);
    GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
    String twentyCharString = "twenty characters...";
    String grblReceiveString;
    String[] arr;
    int expectedInt;
    // Queue a bunch of commands
    for (int i = 0; i < 30; i++) {
        instance.queueStringForComm(twentyCharString);
    }
    // Fill initial buffer, then pause.
    instance.streamCommands();
    instance.pauseSend();
    // Check that the correct number of commands were buffered (even though
    // that isn't pause/resume).
    grblReceiveString = mg.readStringFromGrblBuffer();
    arr = grblReceiveString.split("\n");
    expectedInt = GrblUtils.GRBL_RX_BUFFER_SIZE / (twentyCharString.length() + 1);
    assertEquals(expectedInt, arr.length);
    for (String arr1 : arr) {
        mc.sendResponse("ok");
    }
    // Make sure we don't stream anymore.
    instance.streamCommands();
    grblReceiveString = mg.readStringFromGrblBuffer();
    expectedInt = 0;
    assertEquals(expectedInt, grblReceiveString.length());
    instance.resumeSend();
    // Make sure streaming continued after resuming.
    instance.streamCommands();
    grblReceiveString = mg.readStringFromGrblBuffer();
    arr = grblReceiveString.split("\n");
    expectedInt = GrblUtils.GRBL_RX_BUFFER_SIZE / (twentyCharString.length() + 1);
    assertEquals(expectedInt, arr.length);
}
Also used : MockConnection(com.willwinder.universalgcodesender.mockobjects.MockConnection) Test(org.junit.Test)

Example 4 with MockConnection

use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.

the class GrblCommunicatorTest method errorResponseOnLastCommandInStreamShouldNotPauseTheCommunicator.

@Test
public void errorResponseOnLastCommandInStreamShouldNotPauseTheCommunicator() {
    System.out.println("streamCommands");
    MockConnection mc = new MockConnection(mg.in, mg.out);
    GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
    instance.setSingleStepMode(true);
    String thirtyEightCharString = "thirty-nine character command here....";
    // Add a bunch of commands
    instance.queueStringForComm("1" + thirtyEightCharString);
    instance.queueStringForComm("2" + thirtyEightCharString);
    // We should have no active comands, only buffered commands
    assertEquals(0, asl.size());
    assertEquals(2, cb.size());
    assertFalse(instance.isPaused());
    // First command active, second is cached
    instance.streamCommands();
    assertEquals(1, asl.size());
    assertEquals(0, cb.size());
    assertFalse(instance.isPaused());
    // Complete first command, second becomes active
    // First command ok
    mc.sendResponse("ok");
    assertEquals(1, asl.size());
    assertEquals(0, cb.size());
    assertFalse(instance.isPaused());
    // After an error on the second command it's completed
    // Second command error
    mc.sendResponse("error");
    assertEquals(0, asl.size());
    assertEquals(0, cb.size());
    assertFalse("The communicator should not be paused when last command has an error", instance.isPaused());
}
Also used : MockConnection(com.willwinder.universalgcodesender.mockobjects.MockConnection) Test(org.junit.Test)

Example 5 with MockConnection

use of com.willwinder.universalgcodesender.mockobjects.MockConnection in project Universal-G-Code-Sender by winder.

the class GrblCommunicatorTest method testSoftReset.

/**
 * Test of softReset method, of class GrblCommunicator.
 */
@Test
public void testSoftReset() {
    System.out.println("softReset");
    MockConnection mc = new MockConnection(mg.in, mg.out);
    GrblCommunicator instance = new GrblCommunicator(cb, asl, mc);
    String twentyCharString = "twenty characters...";
    int expectedInt;
    Boolean expectedBool;
    // Send some commands, make them active, then soft-reset to reset them.
    for (int i = 0; i < 30; i++) {
        instance.queueStringForComm(twentyCharString);
    }
    instance.streamCommands();
    instance.softReset();
    // Verify that there are several active commands.
    expectedBool = false;
    assertEquals(expectedBool, instance.areActiveCommands());
}
Also used : MockConnection(com.willwinder.universalgcodesender.mockobjects.MockConnection) Test(org.junit.Test)

Aggregations

MockConnection (com.willwinder.universalgcodesender.mockobjects.MockConnection)9 Test (org.junit.Test)9 GcodeCommand (com.willwinder.universalgcodesender.types.GcodeCommand)1 LinkedList (java.util.LinkedList)1