Search in sources :

Example 1 with IFirmwareSettingsListener

use of com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsTest method settingMessagesShouldBeSentAsEvents.

@Test
public void settingMessagesShouldBeSentAsEvents() {
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    ArgumentCaptor<FirmwareSetting> firmwareSettingArgumentCaptor = ArgumentCaptor.forClass(FirmwareSetting.class);
    doNothing().when(firmwareSettingsListener).onUpdatedFirmwareSetting(firmwareSettingArgumentCaptor.capture());
    // Emulate settings messages from the controller
    target.rawResponseListener("$0=10");
    target.rawResponseListener("$0=11");
    List<FirmwareSetting> settingUpdates = firmwareSettingArgumentCaptor.getAllValues();
    assertEquals(2, settingUpdates.size());
    assertEquals("10", settingUpdates.get(0).getValue());
    assertEquals("11", settingUpdates.get(1).getValue());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) Test(org.junit.Test)

Example 2 with IFirmwareSettingsListener

use of com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsTest method setValueShouldNotUpdateOnError.

@Test
public void setValueShouldNotUpdateOnError() throws Exception {
    // Given
    when(controller.isStreaming()).thenReturn(false);
    when(controller.isCommOpen()).thenReturn(true);
    target.rawResponseListener("$0=10");
    // Add a listener
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    // When
    // Try to update the value in it's own thread
    Future<?> setValueFuture = Executors.newCachedThreadPool().submit(() -> {
        try {
            return target.setValue("$0", "11");
        } catch (FirmwareSettingsException e) {
            fail("Should never get here but got exception: " + e);
        }
        return null;
    });
    // Simulate the response from the controller
    Thread.sleep(200);
    target.rawResponseListener("error");
    // Wait until the value gets updated
    FirmwareSetting setting = (FirmwareSetting) setValueFuture.get();
    // Then
    assertNotNull(setting);
    assertEquals("10", setting.getValue());
    verify(controller, times(1)).sendCommandImmediately(any());
    verify(firmwareSettingsListener, times(0)).onUpdatedFirmwareSetting(any());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) Test(org.junit.Test)

Example 3 with IFirmwareSettingsListener

use of com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsTest method setValueWithSameValueShouldNotUpdate.

@Test
public void setValueWithSameValueShouldNotUpdate() throws FirmwareSettingsException {
    // Given
    target.rawResponseListener("$0=10");
    // When
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    target.setValue("$0", "10");
    // Then
    verifyNoInteractions(firmwareSettingsListener);
    verifyNoInteractions(controller);
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) Test(org.junit.Test)

Example 4 with IFirmwareSettingsListener

use of com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsTest method setValueShouldUpdateOnController.

@Test
public void setValueShouldUpdateOnController() throws Exception {
    // Given
    when(controller.isStreaming()).thenReturn(false);
    when(controller.isCommOpen()).thenReturn(true);
    target.rawResponseListener("$0=10");
    // Add a listener
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    ArgumentCaptor<FirmwareSetting> firmwareSettingArgumentCaptor = ArgumentCaptor.forClass(FirmwareSetting.class);
    doNothing().when(firmwareSettingsListener).onUpdatedFirmwareSetting(firmwareSettingArgumentCaptor.capture());
    target.addListener(firmwareSettingsListener);
    // When
    // Try to update the value in it's own thread
    Future<?> setValueFuture = Executors.newCachedThreadPool().submit(() -> {
        try {
            return target.setValue("$0", "11");
        } catch (FirmwareSettingsException e) {
            fail("Should never get here but got exception: " + e);
        }
        return null;
    });
    // Simulate the response from the controller
    Thread.sleep(200);
    target.rawResponseListener("ok");
    // Wait until the value gets updated
    FirmwareSetting firmwareSetting = (FirmwareSetting) setValueFuture.get();
    // Then
    assertEquals("11", firmwareSetting.getValue());
    verify(controller, times(1)).sendCommandImmediately(any());
    verify(firmwareSettingsListener, times(1)).onUpdatedFirmwareSetting(any());
    assertEquals(1, firmwareSettingArgumentCaptor.getAllValues().size());
    assertEquals("11", firmwareSettingArgumentCaptor.getAllValues().get(0).getValue());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSetting(com.willwinder.universalgcodesender.firmware.FirmwareSetting) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) Test(org.junit.Test)

Example 5 with IFirmwareSettingsListener

use of com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener in project Universal-G-Code-Sender by winder.

the class GrblFirmwareSettingsTest method setValueShouldTimeoutIfNoResponseFromController.

@Test
public void setValueShouldTimeoutIfNoResponseFromController() throws Exception {
    // Given
    when(controller.isStreaming()).thenReturn(false);
    when(controller.isCommOpen()).thenReturn(true);
    target.rawResponseListener("$0=10");
    // Add a listener
    IFirmwareSettingsListener firmwareSettingsListener = mock(IFirmwareSettingsListener.class);
    target.addListener(firmwareSettingsListener);
    // When
    try {
        target.setValue("$0", "11");
    } catch (FirmwareSettingsException e) {
        assertTrue("Make sure the exception contains the word 'Timeout'", e.getMessage().contains("Timeout"));
    }
    // Then
    verify(controller, times(1)).sendCommandImmediately(any());
    verify(firmwareSettingsListener, times(0)).onUpdatedFirmwareSetting(any());
}
Also used : IFirmwareSettingsListener(com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener) FirmwareSettingsException(com.willwinder.universalgcodesender.firmware.FirmwareSettingsException) Test(org.junit.Test)

Aggregations

IFirmwareSettingsListener (com.willwinder.universalgcodesender.firmware.IFirmwareSettingsListener)5 Test (org.junit.Test)5 FirmwareSetting (com.willwinder.universalgcodesender.firmware.FirmwareSetting)3 FirmwareSettingsException (com.willwinder.universalgcodesender.firmware.FirmwareSettingsException)3