|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 InventIt Inc. |
| 3 | + */ |
| 4 | +package com.yourinventit.processing.android.serial; |
| 5 | + |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertNull; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | + |
| 15 | +import processing.core.PApplet; |
| 16 | +import android.content.Context; |
| 17 | +import android.hardware.usb.UsbDevice; |
| 18 | +import android.hardware.usb.UsbDeviceConnection; |
| 19 | +import android.hardware.usb.UsbManager; |
| 20 | + |
| 21 | +import com.hoho.android.usbserial.driver.UsbId; |
| 22 | +import com.hoho.android.usbserial.driver.UsbSerialDriver; |
| 23 | +import com.hoho.android.usbserial.driver.UsbSerialProber; |
| 24 | +import com.yourinventit.dmc.api.moat.android.MoatRobolectricTestRunner; |
| 25 | + |
| 26 | +/** |
| 27 | + * |
| 28 | + * @author dbaba@yourinventit.com |
| 29 | + * |
| 30 | + */ |
| 31 | +@RunWith(MoatRobolectricTestRunner.class) |
| 32 | +public class UsbSerialCommunicatorTest { |
| 33 | + |
| 34 | + private UsbSerialCommunicator communicator; |
| 35 | + |
| 36 | + private final PApplet pApplet = mock(PApplet.class); |
| 37 | + private final UsbDevice usbDevice = mock(UsbDevice.class); |
| 38 | + private final UsbManager usbManager = mock(UsbManager.class); |
| 39 | + private final UsbDeviceConnection usbDeviceConnection = mock(UsbDeviceConnection.class); |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws Exception { |
| 43 | + when(pApplet.getApplicationContext()).thenReturn(pApplet); |
| 44 | + when(pApplet.getSystemService(Context.USB_SERVICE)).thenReturn( |
| 45 | + usbManager); |
| 46 | + communicator = new UsbSerialCommunicator(pApplet); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Regression Test |
| 51 | + */ |
| 52 | + @Test |
| 53 | + public void test_getDevice_ok1() { |
| 54 | + when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_ARDUINO); |
| 55 | + when(usbDevice.getProductId()).thenReturn(UsbId.ARDUINO_LEONARDO); |
| 56 | + when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection); |
| 57 | + final UsbSerialDriver driver = communicator.getDevice( |
| 58 | + UsbSerialProber.CDC_ACM_SERIAL, usbDevice); |
| 59 | + assertNotNull(driver); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Arduino Due Test |
| 64 | + */ |
| 65 | + @Test |
| 66 | + public void test_getDevice_ok2() { |
| 67 | + when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_ARDUINO); |
| 68 | + // http://www.devtal.de/wiki/Benutzer:Rdiez/ArduinoDue |
| 69 | + // 0x003e for Arduino Due |
| 70 | + when(usbDevice.getProductId()).thenReturn(0x003e); |
| 71 | + when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection); |
| 72 | + final UsbSerialDriver driver = communicator.getDevice( |
| 73 | + UsbSerialProber.CDC_ACM_SERIAL, usbDevice); |
| 74 | + assertNotNull(driver); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Ensure not working with other vendor ID than Arduino's. |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void test_getDevice_null() { |
| 82 | + when(usbDevice.getVendorId()).thenReturn(UsbId.VENDOR_FTDI); |
| 83 | + when(usbManager.openDevice(usbDevice)).thenReturn(usbDeviceConnection); |
| 84 | + final UsbSerialDriver driver = communicator.getDevice( |
| 85 | + UsbSerialProber.CDC_ACM_SERIAL, usbDevice); |
| 86 | + assertNull(driver); |
| 87 | + } |
| 88 | +} |
0 commit comments