-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
catch all Throwables from SerialInputOutputManager.Listener methods (#…
…601) to avoid breaking Interface changes, Error from onNewData() is wrapped into Exception when calling onRunError()
- Loading branch information
1 parent
9f93e19
commit 9311b3a
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...orAndroid/src/test/java/com/hoho/android/usbserial/util/SerialInputOutputManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.hoho.android.usbserial.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import android.hardware.usb.UsbEndpoint; | ||
import android.os.Process; | ||
|
||
import com.hoho.android.usbserial.driver.CommonUsbSerialPort; | ||
|
||
import org.junit.Test; | ||
|
||
public class SerialInputOutputManagerTest { | ||
|
||
|
||
// catch all Throwables in onNewData() and onRunError() | ||
@Test | ||
public void throwable() throws Exception { | ||
|
||
class ExceptionListener implements SerialInputOutputManager.Listener { | ||
public Exception e; | ||
@Override public void onNewData(byte[] data) { throw new RuntimeException("exception1"); } | ||
@Override public void onRunError(Exception e) { this.e = e; throw new RuntimeException("exception2"); } | ||
} | ||
class ErrorListener implements SerialInputOutputManager.Listener { | ||
public Exception e; | ||
@Override public void onNewData(byte[] data) { throw new UnknownError("error1"); } | ||
@Override public void onRunError(Exception e) { this.e = e; throw new UnknownError("error2");} | ||
} | ||
|
||
UsbEndpoint readEndpoint = mock(UsbEndpoint.class); | ||
when(readEndpoint.getMaxPacketSize()).thenReturn(16); | ||
CommonUsbSerialPort port = mock(CommonUsbSerialPort.class); | ||
when(port.getReadEndpoint()).thenReturn(readEndpoint); | ||
when(port.read(new byte[16], 0)).thenReturn(1); | ||
SerialInputOutputManager manager = new SerialInputOutputManager(port); | ||
manager.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); | ||
|
||
ExceptionListener exceptionListener = new ExceptionListener(); | ||
manager.setListener(exceptionListener); | ||
manager.run(); | ||
assertEquals(RuntimeException.class, exceptionListener.e.getClass()); | ||
assertEquals("exception1", exceptionListener.e.getMessage()); | ||
|
||
ErrorListener errorListener = new ErrorListener(); | ||
manager.setListener(errorListener); | ||
manager.run(); | ||
assertEquals(Exception.class, errorListener.e.getClass()); | ||
assertEquals("java.lang.UnknownError: error1", errorListener.e.getMessage()); | ||
assertEquals(UnknownError.class, errorListener.e.getCause().getClass()); | ||
assertEquals("error1", errorListener.e.getCause().getMessage()); | ||
} | ||
} |