mirror of
https://github.com/mik3y/usb-serial-for-android
synced 2025-06-06 15:36:46 +00:00
test coverage
This commit is contained in:
parent
b794092c81
commit
b6e1833270
5
codecov.yml
Normal file
5
codecov.yml
Normal file
@ -0,0 +1,5 @@
|
||||
codecov:
|
||||
max_report_age: off
|
||||
require_ci_to_pass: no
|
||||
notify:
|
||||
wait_for_ci: no
|
@ -235,6 +235,12 @@ public class DeviceTest {
|
||||
|
||||
@Test
|
||||
public void openClose() throws Exception {
|
||||
try {
|
||||
usb.serialPort.open(null);
|
||||
fail("null connection error expected");
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
}
|
||||
|
||||
usb.open();
|
||||
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
@ -1528,10 +1534,18 @@ public class DeviceTest {
|
||||
|
||||
// with internal SerialTimeoutException
|
||||
TestBuffer tbuf = new TestBuffer(usb.writeBufferSize + 2*usb.writePacketSize);
|
||||
byte[] pbuf1 = new byte[tbuf.buf.length - 4];
|
||||
byte[] pbuf2 = new byte[1];
|
||||
System.arraycopy(tbuf.buf, 0,pbuf1, 0, pbuf1.length);
|
||||
usb.ioManager.setWriteTimeout(20); // tbuf len >= 128, needs 133msec @ 9600 baud
|
||||
usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
telnet.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
usb.ioManager.writeAsync(tbuf.buf);
|
||||
usb.ioManager.writeAsync(pbuf1);
|
||||
for(int i = pbuf1.length; i < tbuf.buf.length; i++) {
|
||||
Thread.sleep(20);
|
||||
pbuf2[0] = tbuf.buf[i];
|
||||
usb.ioManager.writeAsync(pbuf2);
|
||||
}
|
||||
while(!tbuf.testRead(telnet.read(-1)))
|
||||
;
|
||||
}
|
||||
@ -1577,7 +1591,7 @@ public class DeviceTest {
|
||||
telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE);
|
||||
|
||||
int longTimeout = 1000;
|
||||
int shortTimeout = 10;
|
||||
int shortTimeout = 20;
|
||||
time = System.currentTimeMillis();
|
||||
len = usb.serialPort.read(readBuf, shortTimeout);
|
||||
assertEquals(0, len);
|
||||
@ -1728,6 +1742,8 @@ public class DeviceTest {
|
||||
wrongSerialPort.open(wrongDeviceConnection);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
assertEquals(usb.serialDriver.getDevice(), wrongSerialDriver.getDevice());
|
||||
assertEquals(wrongSerialDriver, wrongSerialPort.getDriver());
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0));
|
||||
assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines());
|
||||
try {
|
||||
@ -1743,6 +1759,8 @@ public class DeviceTest {
|
||||
wrongSerialPort.open(wrongDeviceConnection);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
assertEquals(usb.serialDriver.getDevice(), wrongSerialDriver.getDevice());
|
||||
assertEquals(wrongSerialDriver, wrongSerialPort.getDriver());
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0));
|
||||
assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines());
|
||||
try {
|
||||
@ -2093,7 +2111,9 @@ public class DeviceTest {
|
||||
public void commonMethods() throws Exception {
|
||||
String s;
|
||||
assertNotNull(usb.serialPort.getDriver());
|
||||
assertEquals(usb.serialDriver, usb.serialPort.getDriver());
|
||||
assertNotNull(usb.serialPort.getDevice());
|
||||
assertEquals(usb.serialDriver.getDevice(), usb.serialPort.getDevice());
|
||||
assertEquals(test_device_port, usb.serialPort.getPortNumber());
|
||||
s = usb.serialDriver.toString();
|
||||
assertNotEquals(0, s.length());
|
||||
@ -2124,6 +2144,26 @@ public class DeviceTest {
|
||||
usb.serialPort.read(buffer, UsbWrapper.USB_READ_WAIT);
|
||||
fail("read buffer to small expected");
|
||||
} catch(IllegalArgumentException ignored) {}
|
||||
try {
|
||||
byte[] buffer = new byte[1];
|
||||
usb.serialPort.read(buffer, 0, UsbWrapper.USB_READ_WAIT);
|
||||
fail("read length to small expected");
|
||||
} catch(IllegalArgumentException ignored) {}
|
||||
|
||||
// use driver that does not override base class
|
||||
UsbSerialDriver wrongSerialDriver = new ChromeCcdSerialDriver(usb.serialDriver.getDevice());
|
||||
UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0);
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getCD);
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getCTS);
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getDSR);
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getDTR);
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setDTR(true));
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getRI);
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getRTS);
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setRTS(true));
|
||||
assertThrows(UnsupportedOperationException.class, wrongSerialPort::getControlLines);
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.purgeHwBuffers(true, true));
|
||||
assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setBreak(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -27,6 +27,9 @@ public class HexDump {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
|
||||
};
|
||||
|
||||
private HexDump() {
|
||||
}
|
||||
|
||||
public static String dumpHexString(byte[] array) {
|
||||
return dumpHexString(array, 0, array.length);
|
||||
}
|
||||
|
@ -6,6 +6,9 @@ import java.util.ArrayList;
|
||||
|
||||
public class UsbUtils {
|
||||
|
||||
private UsbUtils() {
|
||||
}
|
||||
|
||||
public static ArrayList<byte[]> getDescriptors(UsbDeviceConnection connection) {
|
||||
ArrayList<byte[]> descriptors = new ArrayList<>();
|
||||
byte[] rawDescriptors = connection.getRawDescriptors();
|
||||
|
@ -0,0 +1,44 @@
|
||||
package com.hoho.android.usbserial.util;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
public class HexDumpText {
|
||||
|
||||
@Test
|
||||
public void toByteArray() throws Exception {
|
||||
assertThat(HexDump.toByteArray((byte)0x4a), equalTo(new byte[]{ 0x4A}));
|
||||
assertThat(HexDump.toByteArray((short)0x4a5b), equalTo(new byte[]{ 0x4A, 0x5B}));
|
||||
assertThat(HexDump.toByteArray((int)0x4a5b6c7d), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toHexString() throws Exception {
|
||||
assertEquals("4A", HexDump.toHexString((byte)0x4a));
|
||||
assertEquals("4A 5B", HexDump.toHexString((short)0x4a5b));
|
||||
assertEquals("4A 5B 6C 7D", HexDump.toHexString((int)0x4a5b6c7d));
|
||||
assertEquals("4A 5B 6C 7D", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
||||
assertEquals("5B 6C", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}, 1, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dumpHexString() throws Exception {
|
||||
assertEquals("10 31 32 33 34 35 36 37 .1234567\n18 39 .9", HexDump.dumpHexString(new byte[]{ 0x10, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x18, 0x39}));
|
||||
assertEquals("31 32 12", HexDump.dumpHexString(new byte[]{ 0x30, 0x31, 0x32, 0x33}, 1, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toByte() throws Exception {
|
||||
assertThat(HexDump.hexStringToByteArray("4a 5B-6c\n7d"), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}));
|
||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3 "));
|
||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3z"));
|
||||
assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3Z"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user