aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowUsbDeviceConnectionTest.java
blob: 52ba0285baefa44923ca98a9de0e48dcc5e14791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.hardware.usb.UsbConfiguration;
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 android.hardware.usb.UsbManager;
import androidx.annotation.Nullable;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;

/** Unit tests for {@link ShadowUsbDeviceConnection}. */
@RunWith(AndroidJUnit4.class)
public class ShadowUsbDeviceConnectionTest {
  private static final String DEVICE_NAME = "usb";

  private UsbManager usbManager;

  @Mock private UsbDevice usbDevice;
  @Mock private UsbConfiguration usbConfiguration;
  @Mock private UsbInterface usbInterface;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    usbManager =
        (UsbManager)
            ApplicationProvider.getApplicationContext().getSystemService(Context.USB_SERVICE);

    when(usbDevice.getDeviceName()).thenReturn(DEVICE_NAME);
    when(usbDevice.getConfigurationCount()).thenReturn(1);
    when(usbDevice.getConfiguration(0)).thenReturn(usbConfiguration);
    when(usbConfiguration.getInterfaceCount()).thenReturn(1);
    when(usbConfiguration.getInterface(0)).thenReturn(usbInterface);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void claimInterface() {
    UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
    UsbInterface usbInterface = selectInterface(usbDevice);

    assertThat(usbDeviceConnection.claimInterface(usbInterface, /* force= */ false)).isTrue();
    assertThat(usbDeviceConnection.releaseInterface(usbInterface)).isTrue();
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void setInterface() {
    UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
    UsbInterface usbInterface = selectInterface(usbDevice);

    assertThat(usbDeviceConnection.setInterface(usbInterface)).isTrue();
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void controlTransfer() {
    UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
    UsbInterface usbInterface = selectInterface(usbDevice);
    usbDeviceConnection.claimInterface(usbInterface, /* force= */ false);

    int len = 10;
    assertThat(
            usbDeviceConnection.controlTransfer(
                /* requestType= */ 0,
                /* request= */ 0,
                /* value= */ 0,
                /* index= */ 0,
                /* buffer= */ new byte[len],
                /* length= */ len,
                /* timeout= */ 0))
        .isEqualTo(len);
    assertThat(
            usbDeviceConnection.controlTransfer(
                /* requestType= */ 0,
                /* request= */ 0,
                /* value= */ 0,
                /* index= */ 0,
                /* buffer= */ new byte[len],
                /* offset= */ 0,
                /* length= */ len,
                /* timeout= */ 0))
        .isEqualTo(len);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void bulkTransfer() throws Exception {
    UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
    UsbInterface usbInterface = selectInterface(usbDevice);
    UsbEndpoint usbEndpointOut = getEndpoint(usbInterface, UsbConstants.USB_DIR_OUT);
    usbDeviceConnection.claimInterface(usbInterface, /* force= */ false);
    InputStream outgoingData = shadowOf(usbDeviceConnection).getOutgoingDataStream();

    byte[] msg = "Hello World".getBytes(UTF_8);
    assertThat(usbDeviceConnection.bulkTransfer(usbEndpointOut, msg, msg.length, /* timeout= */ 0))
        .isEqualTo(msg.length);

    byte[] buffer = new byte[1024];
    int read = outgoingData.read(buffer);
    assertThat(Arrays.copyOf(buffer, read)).isEqualTo(msg);

    msg = "Goodbye World".getBytes(UTF_8);
    assertThat(
            usbDeviceConnection.bulkTransfer(
                usbEndpointOut, msg, /* offset= */ 0, msg.length, /* timeout= */ 0))
        .isEqualTo(msg.length);

    buffer = new byte[1024];
    read = outgoingData.read(buffer);
    assertThat(Arrays.copyOf(buffer, read)).isEqualTo(msg);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void releaseInterface_closesOutgoingDataStream() throws Exception {
    UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
    UsbInterface usbInterface = selectInterface(usbDevice);
    UsbEndpoint usbEndpointOut = getEndpoint(usbInterface, UsbConstants.USB_DIR_OUT);
    usbDeviceConnection.claimInterface(usbInterface, /* force= */ false);
    InputStream outgoingData = shadowOf(usbDeviceConnection).getOutgoingDataStream();

    byte[] msg = "Hello World".getBytes(UTF_8);
    assertThat(usbDeviceConnection.bulkTransfer(usbEndpointOut, msg, msg.length, /* timeout= */ 0))
        .isEqualTo(msg.length);
    usbDeviceConnection.releaseInterface(usbInterface);

    byte[] buffer = new byte[1024];
    assertThrows(IOException.class, () -> outgoingData.read(buffer));
  }

  @Nullable
  private static UsbInterface selectInterface(UsbDevice device) {
    for (int i = 0; i < device.getConfigurationCount(); i++) {
      UsbConfiguration configuration = device.getConfiguration(i);
      for (int j = 0; j < configuration.getInterfaceCount(); j++) {
        return configuration.getInterface(i);
      }
    }
    return null;
  }

  @Nullable
  private static UsbEndpoint getEndpoint(UsbInterface usbInterface, int direction) {
    for (int i = 0; i < usbInterface.getEndpointCount(); i++) {
      UsbEndpoint endpoint = usbInterface.getEndpoint(i);
      if (endpoint.getDirection() == direction) {
        return endpoint;
      }
    }
    return null;
  }
}