aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowIsoDepTest.java
blob: 3d7636fd0cad8a2e97a1293de2b58c158fc0fb92 (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
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.KITKAT;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.robolectric.Shadows.shadowOf;

import android.nfc.tech.IsoDep;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import java.util.concurrent.Callable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

/** Unit tests for {@link ShadowIsoDep}. */
@RunWith(AndroidJUnit4.class)
@Config(minSdk = KITKAT)
public final class ShadowIsoDepTest {

  private IsoDep isoDep;

  @Before
  public void setUp() {
    isoDep = ShadowIsoDep.newInstance();
  }

  @Test
  public void transceive() throws Exception {
    shadowOf(isoDep).setTransceiveResponse(new byte[] {1, 2, 3});
    assertThat(isoDep.transceive(new byte[0])).isEqualTo(new byte[] {1, 2, 3});
  }

  @Test
  public void nextTransceive() throws Exception {
    shadowOf(isoDep).setNextTransceiveResponse(new byte[] {1, 2, 3});
    assertThat(isoDep.transceive(new byte[0])).isEqualTo(new byte[] {1, 2, 3});
    assertThrows(IOException.class, () -> isoDep.transceive(new byte[0]));
  }

  @Test
  public void timeout() {
    isoDep.setTimeout(1000);
    assertThat(isoDep.getTimeout()).isEqualTo(1000);
  }

  @Test
  public void maxTransceiveLength() {
    shadowOf(isoDep).setMaxTransceiveLength(1000);
    assertThat(isoDep.getMaxTransceiveLength()).isEqualTo(1000);
  }

  @Test
  public void isExtendedLengthApduSupported() {
    shadowOf(isoDep).setExtendedLengthApduSupported(true);
    assertThat(isoDep.isExtendedLengthApduSupported()).isTrue();
    shadowOf(isoDep).setExtendedLengthApduSupported(false);
    assertThat(isoDep.isExtendedLengthApduSupported()).isFalse();
  }

  private static <T extends Throwable> void assertThrows(Class<T> clazz, Callable<?> callable) {
    try {
      callable.call();
    } catch (Throwable t) {
      if (clazz.isInstance(t)) {
        // expected
        return;
      } else {
        fail("did not throw " + clazz.getName() + ", threw " + t + " instead");
      }
    }
    fail("did not throw " + clazz.getName());
  }
}