diff --git a/usbSerialForAndroid/src/test/java/android/util/Log.java b/usbSerialForAndroid/src/test/java/android/util/Log.java new file mode 100644 index 0000000..c7c87da --- /dev/null +++ b/usbSerialForAndroid/src/test/java/android/util/Log.java @@ -0,0 +1,24 @@ +// without this class Mockito complains about non-mocked Log methods +package android.util; + +public class Log { + public static int d(String tag, String msg) { + System.out.println("DEBUG: " + tag + ": " + msg); + return 0; + } + + public static int i(String tag, String msg) { + System.out.println("INFO: " + tag + ": " + msg); + return 0; + } + + public static int w(String tag, String msg) { + System.out.println("WARN: " + tag + ": " + msg); + return 0; + } + + public static int e(String tag, String msg) { + System.out.println("ERROR: " + tag + ": " + msg); + return 0; + } +} diff --git a/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/driver/CdcAcmSerialDriverTest.java b/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/driver/CdcAcmSerialDriverTest.java new file mode 100644 index 0000000..d952eb6 --- /dev/null +++ b/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/driver/CdcAcmSerialDriverTest.java @@ -0,0 +1,247 @@ +package com.hoho.android.usbserial.driver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import android.hardware.usb.UsbConstants; +import android.hardware.usb.UsbDevice; +import android.hardware.usb.UsbDeviceConnection; +import android.hardware.usb.UsbEndpoint; +import android.hardware.usb.UsbInterface; + +import org.junit.Test; + +import java.io.IOException; + +public class CdcAcmSerialDriverTest { + + @Test + public void standardDevice() throws Exception { + UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class); + UsbDevice usbDevice = mock(UsbDevice.class); + UsbInterface controlInterface = mock(UsbInterface.class); + UsbInterface dataInterface = mock(UsbInterface.class); + UsbEndpoint controlEndpoint = mock(UsbEndpoint.class); + UsbEndpoint readEndpoint = mock(UsbEndpoint.class); + UsbEndpoint writeEndpoint = mock(UsbEndpoint.class); + + when(usbDeviceConnection.claimInterface(controlInterface,true)).thenReturn(true); + when(usbDeviceConnection.claimInterface(dataInterface,true)).thenReturn(true); + when(usbDevice.getInterfaceCount()).thenReturn(2); + when(usbDevice.getInterface(0)).thenReturn(controlInterface); + when(usbDevice.getInterface(1)).thenReturn(dataInterface); + when(controlInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_COMM); + when(controlInterface.getEndpointCount()).thenReturn(1); + when(controlInterface.getEndpoint(0)).thenReturn(controlEndpoint); + when(dataInterface.getInterfaceClass()).thenReturn(UsbConstants.USB_CLASS_CDC_DATA); + when(dataInterface.getEndpointCount()).thenReturn(2); + when(dataInterface.getEndpoint(0)).thenReturn(writeEndpoint); + when(dataInterface.getEndpoint(1)).thenReturn(readEndpoint); + when(controlEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(controlEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_INT); + when(readEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(readEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + when(writeEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_OUT); + when(writeEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + + CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice); + CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0); + port.mConnection = usbDeviceConnection; + port.openInt(); + assertEquals(readEndpoint, port.mReadEndpoint); + assertEquals(writeEndpoint, port.mWriteEndpoint); + } + + @Test + public void singleInterfaceDevice() throws Exception { + UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class); + UsbDevice usbDevice = mock(UsbDevice.class); + UsbInterface usbInterface = mock(UsbInterface.class); + UsbEndpoint controlEndpoint = mock(UsbEndpoint.class); + UsbEndpoint readEndpoint = mock(UsbEndpoint.class); + UsbEndpoint writeEndpoint = mock(UsbEndpoint.class); + + when(usbDeviceConnection.claimInterface(usbInterface,true)).thenReturn(true); + when(usbDevice.getInterfaceCount()).thenReturn(1); + when(usbDevice.getInterface(0)).thenReturn(usbInterface); + when(usbInterface.getEndpointCount()).thenReturn(3); + when(usbInterface.getEndpoint(0)).thenReturn(controlEndpoint); + when(usbInterface.getEndpoint(1)).thenReturn(readEndpoint); + when(usbInterface.getEndpoint(2)).thenReturn(writeEndpoint); + when(controlEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(controlEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_INT); + when(readEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(readEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + when(writeEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_OUT); + when(writeEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + + CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice); + CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0); + port.mConnection = usbDeviceConnection; + port.openInt(); + assertEquals(readEndpoint, port.mReadEndpoint); + assertEquals(writeEndpoint, port.mWriteEndpoint); + } + + @Test + public void multiPortDevice() throws Exception { + int n = 4; + UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class); + UsbDevice usbDevice = mock(UsbDevice.class); + UsbInterface[] controlInterfaces = new UsbInterface[n]; + UsbInterface[] dataInterfaces = new UsbInterface[n]; + UsbEndpoint[] controlEndpoints = new UsbEndpoint[n]; + UsbEndpoint[] readEndpoints = new UsbEndpoint[n]; + UsbEndpoint[] writeEndpoints = new UsbEndpoint[n]; + + when(usbDevice.getInterfaceCount()).thenReturn(2*n); + for(int i=0; iport.openInt()); + } + + @Test + public void invalidSingleInterfaceDevice() throws Exception { + UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class); + UsbDevice usbDevice = mock(UsbDevice.class); + UsbInterface usbInterface = mock(UsbInterface.class); + UsbEndpoint controlEndpoint = mock(UsbEndpoint.class); + UsbEndpoint readEndpoint = mock(UsbEndpoint.class); + //UsbEndpoint writeEndpoint = mock(UsbEndpoint.class); + + when(usbDeviceConnection.claimInterface(usbInterface,true)).thenReturn(true); + when(usbDevice.getInterfaceCount()).thenReturn(1); + when(usbDevice.getInterface(0)).thenReturn(usbInterface); + when(usbInterface.getEndpointCount()).thenReturn(2); + when(usbInterface.getEndpoint(0)).thenReturn(controlEndpoint); + when(usbInterface.getEndpoint(1)).thenReturn(readEndpoint); + //when(usbInterface.getEndpoint(2)).thenReturn(writeEndpoint); + when(controlEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(controlEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_INT); + when(readEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_IN); + when(readEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + //when(writeEndpoint.getDirection()).thenReturn(UsbConstants.USB_DIR_OUT); + //when(writeEndpoint.getType()).thenReturn(UsbConstants.USB_ENDPOINT_XFER_BULK); + + CdcAcmSerialDriver driver = new CdcAcmSerialDriver(usbDevice); + CdcAcmSerialDriver.CdcAcmSerialPort port = (CdcAcmSerialDriver.CdcAcmSerialPort) driver.getPorts().get(0); + port.mConnection = usbDeviceConnection; + port.openInt(); + assertNull(port.mWriteEndpoint); + } + +}