• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.nfc;
18 
19 import static android.nfc.tech.TagTechnology.ISO_DEP;
20 import static android.nfc.tech.TagTechnology.MIFARE_CLASSIC;
21 import static android.nfc.tech.TagTechnology.MIFARE_ULTRALIGHT;
22 import static android.nfc.tech.TagTechnology.NDEF;
23 import static android.nfc.tech.TagTechnology.NDEF_FORMATABLE;
24 import static android.nfc.tech.TagTechnology.NFC_A;
25 import static android.nfc.tech.TagTechnology.NFC_B;
26 import static android.nfc.tech.TagTechnology.NFC_BARCODE;
27 import static android.nfc.tech.TagTechnology.NFC_F;
28 import static android.nfc.tech.TagTechnology.NFC_V;
29 
30 import static org.junit.Assert.assertArrayEquals;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertFalse;
33 import static org.junit.Assert.assertNotNull;
34 import static org.junit.Assert.assertNull;
35 import static org.junit.Assert.assertThrows;
36 import static org.junit.Assert.assertTrue;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.when;
39 
40 import android.nfc.tech.IsoDep;
41 import android.nfc.tech.MifareClassic;
42 import android.nfc.tech.MifareUltralight;
43 import android.nfc.tech.Ndef;
44 import android.nfc.tech.NdefFormatable;
45 import android.nfc.tech.NfcA;
46 import android.nfc.tech.NfcB;
47 import android.nfc.tech.NfcBarcode;
48 import android.nfc.tech.NfcF;
49 import android.nfc.tech.NfcV;
50 import android.os.Bundle;
51 import android.os.RemoteException;
52 
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.mockito.Mock;
56 import org.mockito.MockitoAnnotations;
57 
58 import java.io.IOException;
59 
60 public class TagTest {
61     private final byte[] id = "id".getBytes();
62     @Mock
63     private INfcTag mTagService;
64     @Mock
65     private Bundle mBundle;
66     private Tag mTag;
67 
68     @Before
setUp()69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         mTag = createInstance(NFC_A, mTagService, null, null);
72     }
73 
createInstance(int tech, INfcTag tagService, int[] techList, Bundle[] techListExtras)74     private Tag createInstance(int tech, INfcTag tagService, int[] techList,
75             Bundle[] techListExtras) {
76         techList = techList == null ? new int[]{tech} : techList;
77         techListExtras = techListExtras == null ? new Bundle[]{mBundle} : techListExtras;
78         int serviceHandle = 1;
79         long cookie = (long) 1;
80         return new Tag(id, techList, techListExtras, serviceHandle, cookie, tagService);
81     }
82 
83     @Test
testTagInstance()84     public void testTagInstance() {
85         assertTrue(mTag.hasTech(NFC_A));
86         assertEquals(1, mTag.getServiceHandle());
87         assertArrayEquals(new int[]{NFC_A}, mTag.getTechCodeList());
88         assertEquals(id, mTag.getId());
89         assertArrayEquals(new String[]{NfcA.class.getName()}, mTag.getTechList());
90     }
91 
92     @Test
testTagInstanceWithTechNfcB()93     public void testTagInstanceWithTechNfcB() {
94         mTag = createInstance(NFC_B, mTagService, null, null);
95 
96         assertArrayEquals(new int[]{NFC_B}, mTag.getTechCodeList());
97         assertEquals(id, mTag.getId());
98         assertArrayEquals(new String[]{NfcB.class.getName()}, mTag.getTechList());
99     }
100 
101     @Test
testTagInstanceWithTechNfcF()102     public void testTagInstanceWithTechNfcF() {
103         mTag = createInstance(NFC_F, mTagService, null, null);
104 
105         assertArrayEquals(new int[]{NFC_F}, mTag.getTechCodeList());
106         assertEquals(id, mTag.getId());
107         assertArrayEquals(new String[]{NfcF.class.getName()}, mTag.getTechList());
108     }
109 
110     @Test
testTagInstanceWithTechNfcV()111     public void testTagInstanceWithTechNfcV() {
112         mTag = createInstance(NFC_V, mTagService, null, null);
113 
114         assertArrayEquals(new int[]{NFC_V}, mTag.getTechCodeList());
115         assertEquals(id, mTag.getId());
116         assertArrayEquals(new String[]{NfcV.class.getName()}, mTag.getTechList());
117     }
118 
119     @Test
testTagInstanceWithTechNfcBarcode()120     public void testTagInstanceWithTechNfcBarcode() {
121         mTag = createInstance(NFC_BARCODE, mTagService, null, null);
122 
123         assertArrayEquals(new int[]{NFC_BARCODE}, mTag.getTechCodeList());
124         assertEquals(id, mTag.getId());
125         assertArrayEquals(new String[]{NfcBarcode.class.getName()}, mTag.getTechList());
126     }
127 
128     @Test
testTagInstanceWithTechIsoDep()129     public void testTagInstanceWithTechIsoDep() {
130         mTag = createInstance(ISO_DEP, mTagService, null, null);
131 
132         assertArrayEquals(new int[]{ISO_DEP}, mTag.getTechCodeList());
133         assertEquals(id, mTag.getId());
134         assertArrayEquals(new String[]{IsoDep.class.getName()}, mTag.getTechList());
135     }
136 
137     @Test
testTagInstanceWithTechMifareClassic()138     public void testTagInstanceWithTechMifareClassic() {
139         mTag = createInstance(MIFARE_CLASSIC, mTagService, null, null);
140 
141         assertArrayEquals(new int[]{MIFARE_CLASSIC}, mTag.getTechCodeList());
142         assertEquals(id, mTag.getId());
143         assertArrayEquals(new String[]{MifareClassic.class.getName()}, mTag.getTechList());
144     }
145 
146     @Test
testTagInstanceWithTechMifareUltralight()147     public void testTagInstanceWithTechMifareUltralight() {
148         mTag = createInstance(MIFARE_ULTRALIGHT, mTagService, null, null);
149 
150         assertArrayEquals(new int[]{MIFARE_ULTRALIGHT}, mTag.getTechCodeList());
151         assertEquals(id, mTag.getId());
152         assertArrayEquals(new String[]{MifareUltralight.class.getName()}, mTag.getTechList());
153     }
154 
155     @Test
testTagInstanceWithTechNdef()156     public void testTagInstanceWithTechNdef() {
157         mTag = createInstance(NDEF, mTagService, null, null);
158 
159         assertArrayEquals(new int[]{NDEF}, mTag.getTechCodeList());
160         assertEquals(id, mTag.getId());
161         assertArrayEquals(new String[]{Ndef.class.getName()}, mTag.getTechList());
162     }
163 
164     @Test
testTagInstanceWithTechNdefFormatable()165     public void testTagInstanceWithTechNdefFormatable() {
166         mTag = createInstance(NDEF_FORMATABLE, mTagService, null, null);
167 
168         assertArrayEquals(new int[]{NDEF_FORMATABLE}, mTag.getTechCodeList());
169         assertEquals(id, mTag.getId());
170         assertArrayEquals(new String[]{NdefFormatable.class.getName()}, mTag.getTechList());
171     }
172 
173     @Test(expected = IllegalArgumentException.class)
testTagInstanceWithNoTech()174     public void testTagInstanceWithNoTech() {
175         mTag = createInstance(-1, mTagService, null, null);
176     }
177 
178     @Test(expected = IllegalStateException.class)
testRediscoverWithNoConnectedTechnology()179     public void testRediscoverWithNoConnectedTechnology() throws IOException {
180         mTag.setConnectedTechnology(NFC_A);
181         mTag.rediscover();
182     }
183 
184     @Test(expected = IOException.class)
testRediscoverWithNoTagServices()185     public void testRediscoverWithNoTagServices() throws IOException {
186         mTag = createInstance(NFC_A, null, null, null);
187         mTag.rediscover();
188     }
189 
190     @Test
testRediscover()191     public void testRediscover() throws RemoteException, IOException {
192         Tag mockTag = mock(Tag.class);
193         when(mTagService.rediscover(1)).thenReturn(mockTag);
194 
195         assertEquals(mockTag, mTag.rediscover());
196     }
197 
198     @Test(expected = IOException.class)
testNullRediscoverWhenINfcTagRediscoveryIsNull()199     public void testNullRediscoverWhenINfcTagRediscoveryIsNull()
200             throws RemoteException, IOException {
201         when(mTagService.rediscover(1)).thenReturn(null);
202         mTag.rediscover();
203     }
204 
205     @Test
testHasTechNegativeScenario()206     public void testHasTechNegativeScenario() {
207         assertFalse(mTag.hasTech(NFC_B));
208     }
209 
210     @Test
testGetTechExtrasReturnsCorrectBundle()211     public void testGetTechExtrasReturnsCorrectBundle() {
212         int[] techList = new int[]{1, 2, 3, 4};
213         Bundle[] techExtras = new Bundle[techList.length];
214         for (int i = 0; i < techExtras.length; i++) {
215             techExtras[i] = new Bundle();
216             techExtras[i].putString("key" + i, "value" + i);
217         }
218         mTag = createInstance(NFC_A, mTagService, techList, techExtras);
219 
220         Bundle result = mTag.getTechExtras(2);
221         assertNotNull(result);
222         assertEquals("value1", result.getString("key1"));
223     }
224 
225     @Test
testGetTechExtrasReturnsNullWhenTechNotFound()226     public void testGetTechExtrasReturnsNullWhenTechNotFound() {
227         Bundle result = mTag.getTechExtras(99);
228         assertNull(result);
229     }
230 
231     @Test
testGetTagServiceReturnsServiceWhenTagIsUpToDate()232     public void testGetTagServiceReturnsServiceWhenTagIsUpToDate() throws Exception {
233         when(mTagService.isTagUpToDate(1)).thenReturn(true);
234 
235         INfcTag result = mTag.getTagService();
236         assertNotNull(result);
237         assertEquals(mTagService, result);
238     }
239 
240     @Test(expected = SecurityException.class)
testGetTagServiceThrowsSecurityExceptionWhenTagIsOutOfDate()241     public void testGetTagServiceThrowsSecurityExceptionWhenTagIsOutOfDate() throws Exception {
242         when(mTagService.isTagUpToDate(1)).thenReturn(false);
243 
244         mTag.getTagService();
245     }
246 
247     @Test
testGetTagServiceReturnsNullWhenTagServiceIsNull()248     public void testGetTagServiceReturnsNullWhenTagServiceIsNull() {
249         mTag = createInstance(NFC_A, null, null, null);
250         INfcTag result = mTag.getTagService();
251         assertNull(result);
252     }
253 
254     @Test
testGetTagServiceThrowsRuntimeExceptionWhenRemoteExceptionOccurs()255     public void testGetTagServiceThrowsRuntimeExceptionWhenRemoteExceptionOccurs()
256             throws Exception {
257         when(mTagService.isTagUpToDate(1)).thenThrow(new RemoteException());
258 
259         RuntimeException exception = assertThrows(RuntimeException.class, () -> {
260             mTag.getTagService();
261         });
262         assertNotNull(exception);
263     }
264 
265     @Test
testToStringReturnsExpectedFormat()266     public void testToStringReturnsExpectedFormat() {
267         String expectedOutput = "TAG: Tech [" + NfcA.class.getName() + "]";
268 
269         String result = mTag.toString();
270         assertEquals(expectedOutput, result);
271     }
272 
273     @Test
testSetConnectedTechnologySuccess()274     public void testSetConnectedTechnologySuccess() {
275         boolean result = mTag.setConnectedTechnology(5);
276         assertTrue(result);
277         assertEquals(5, mTag.getConnectedTechnology());
278     }
279 
280     @Test
testSetConnectedTechnology_FailureWhenAlreadySet()281     public void testSetConnectedTechnology_FailureWhenAlreadySet() {
282         mTag.setConnectedTechnology(5);
283 
284         boolean result = mTag.setConnectedTechnology(10);
285         assertFalse(result);
286         assertEquals(5, mTag.getConnectedTechnology());
287     }
288 
289     @Test
testGetConnectedTechnologyDefaultValue()290     public void testGetConnectedTechnologyDefaultValue() {
291         int connectedTechnology = mTag.getConnectedTechnology();
292         assertEquals(-1, connectedTechnology);
293     }
294 
295     @Test
testSetTechnologyDisconnected()296     public void testSetTechnologyDisconnected() {
297         mTag.setConnectedTechnology(5);
298 
299         mTag.setTechnologyDisconnected();
300         assertEquals(-1, mTag.getConnectedTechnology());
301     }
302 }
303