OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.device.nfc; | |
6 | |
7 import static org.junit.Assert.assertEquals; | |
8 import static org.junit.Assert.assertNull; | |
9 import static org.mockito.ArgumentMatchers.any; | |
10 import static org.mockito.ArgumentMatchers.anyInt; | |
11 import static org.mockito.ArgumentMatchers.anyString; | |
12 import static org.mockito.ArgumentMatchers.isNull; | |
13 import static org.mockito.Mockito.doNothing; | |
14 import static org.mockito.Mockito.doReturn; | |
15 import static org.mockito.Mockito.mock; | |
16 import static org.mockito.Mockito.times; | |
17 import static org.mockito.Mockito.verify; | |
18 | |
19 import android.app.Activity; | |
20 import android.content.Context; | |
21 import android.content.pm.PackageManager; | |
22 import android.nfc.FormatException; | |
23 import android.nfc.NdefMessage; | |
24 import android.nfc.NdefRecord; | |
25 import android.nfc.NfcAdapter; | |
26 import android.nfc.NfcAdapter.ReaderCallback; | |
27 import android.nfc.NfcManager; | |
28 | |
29 import org.chromium.base.test.util.Feature; | |
30 import org.chromium.mojom.device.nfc.Nfc.CancelAllWatchesResponse; | |
31 import org.chromium.mojom.device.nfc.Nfc.CancelPushResponse; | |
32 import org.chromium.mojom.device.nfc.Nfc.CancelWatchResponse; | |
33 import org.chromium.mojom.device.nfc.Nfc.PushResponse; | |
34 import org.chromium.mojom.device.nfc.Nfc.WatchResponse; | |
35 import org.chromium.mojom.device.nfc.NfcClient; | |
36 import org.chromium.mojom.device.nfc.NfcError; | |
37 import org.chromium.mojom.device.nfc.NfcErrorType; | |
38 import org.chromium.mojom.device.nfc.NfcMessage; | |
39 import org.chromium.mojom.device.nfc.NfcPushOptions; | |
40 import org.chromium.mojom.device.nfc.NfcPushTarget; | |
41 import org.chromium.mojom.device.nfc.NfcRecord; | |
42 import org.chromium.mojom.device.nfc.NfcRecordType; | |
43 import org.chromium.mojom.device.nfc.NfcRecordTypeFilter; | |
44 import org.chromium.mojom.device.nfc.NfcWatchMode; | |
45 import org.chromium.mojom.device.nfc.NfcWatchOptions; | |
46 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
47 | |
48 import org.junit.Before; | |
49 import org.junit.Test; | |
50 import org.junit.runner.RunWith; | |
51 import org.mockito.ArgumentCaptor; | |
52 import org.mockito.Captor; | |
53 import org.mockito.Mock; | |
54 import org.mockito.MockitoAnnotations; | |
55 import org.robolectric.annotation.Config; | |
56 | |
57 import java.io.IOException; | |
58 import java.io.UnsupportedEncodingException; | |
59 | |
60 /** | |
61 * Unit tests for NfcImpl and NfcTypeConverter classes. | |
62 */ | |
63 @RunWith(LocalRobolectricTestRunner.class) | |
64 @Config(manifest = Config.NONE) | |
65 public class NFCTest { | |
66 @Mock | |
67 private Context mContext; | |
68 @Mock | |
69 private NfcManager mNfcManager; | |
70 @Mock | |
71 private NfcAdapter mNfcAdapter; | |
72 @Mock | |
73 private Activity mActivity; | |
74 @Mock | |
75 private NfcClient mNfcClient; | |
76 @Mock | |
77 private NfcTagHandler mNfcTagHandler; | |
78 @Captor | |
79 private ArgumentCaptor<NfcError> mErrorCaptor; | |
80 @Captor | |
81 private ArgumentCaptor<Integer> mWatchCaptor; | |
82 @Captor | |
83 private ArgumentCaptor<int[]> mOnWatchCallbackCaptor; | |
84 | |
85 // Constants used for the test. | |
86 private static final String TEST_TEXT = "test"; | |
87 private static final String TEST_URL = "https://google.com"; | |
88 private static final String TEST_JSON = "{\"key1\":\"value1\",\"key2\":2}"; | |
89 private static final String DOMAIN = "w3.org"; | |
90 private static final String TYPE = "webnfc"; | |
91 private static final String TEXT_MIME = "text/plain"; | |
92 private static final String JSON_MIME = "application/json"; | |
93 private static final String CHARSET_UTF8 = ";charset=UTF-8"; | |
94 private static final String CHARSET_UTF16 = ";charset=UTF-16"; | |
95 private static final String LANG_EN_US = "en-US"; | |
96 | |
97 /** | |
98 * Class that is used test NfcImpl implementation | |
99 */ | |
100 private static class TestNfcImpl extends NfcImpl { | |
101 public TestNfcImpl(Context context) { | |
102 super(context); | |
103 } | |
104 | |
105 public void setActivityForTesting(Activity activity) { | |
106 super.setActivity(activity); | |
107 } | |
108 | |
109 public void processPendingOperationsForTesting(NfcTagHandler handler) { | |
110 super.processPendingOperations(handler); | |
111 } | |
112 } | |
113 | |
114 @Before | |
115 public void setUp() { | |
116 MockitoAnnotations.initMocks(this); | |
117 doReturn(mNfcManager).when(mContext).getSystemService(Context.NFC_SERVIC E); | |
118 doReturn(mNfcAdapter).when(mNfcManager).getDefaultAdapter(); | |
119 doReturn(true).when(mNfcAdapter).isEnabled(); | |
120 doReturn(PackageManager.PERMISSION_GRANTED) | |
121 .when(mContext) | |
122 .checkPermission(anyString(), anyInt(), anyInt()); | |
123 doNothing() | |
124 .when(mNfcAdapter) | |
125 .enableReaderMode( | |
126 any(Activity.class), any(ReaderCallback.class), anyInt() , isNull()); | |
127 doNothing().when(mNfcAdapter).disableReaderMode(any(Activity.class)); | |
128 // Tag handler overrides used to mock connected tag. | |
129 doReturn(true).when(mNfcTagHandler).isConnected(); | |
130 doReturn(false).when(mNfcTagHandler).isTagOutOfRange(); | |
131 try { | |
132 doNothing().when(mNfcTagHandler).connect(); | |
133 doNothing().when(mNfcTagHandler).write(any(NdefMessage.class)); | |
134 doReturn(createUrlWebNFCNdefMessage()).when(mNfcTagHandler).read(); | |
135 doNothing().when(mNfcTagHandler).close(); | |
136 } catch (IOException | FormatException e) { | |
137 } | |
138 } | |
139 | |
140 /** | |
141 * Test that error with type NOT_SUPPORTED is returned if NFC is not support ed. | |
142 */ | |
143 @Test | |
144 @Feature({"NFCTest"}) | |
145 public void testNFCNotSupported() { | |
146 doReturn(null).when(mNfcManager).getDefaultAdapter(); | |
147 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
148 nfc.setActivityForTesting(mActivity); | |
149 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl ass); | |
150 nfc.cancelAllWatches(mockCallback); | |
151 verify(mockCallback).call(mErrorCaptor.capture()); | |
152 assertEquals(NfcErrorType.NOT_SUPPORTED, mErrorCaptor.getValue().errorTy pe); | |
153 } | |
154 | |
155 /** | |
156 * Test that error with type SECURITY is returned if permission to use NFC i s not granted. | |
157 */ | |
158 @Test | |
159 @Feature({"NFCTest"}) | |
160 public void testNFCIsNotPermitted() { | |
161 doReturn(PackageManager.PERMISSION_DENIED) | |
162 .when(mContext) | |
163 .checkPermission(anyString(), anyInt(), anyInt()); | |
164 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
165 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl ass); | |
166 nfc.cancelAllWatches(mockCallback); | |
167 verify(mockCallback).call(mErrorCaptor.capture()); | |
168 assertEquals(NfcErrorType.SECURITY, mErrorCaptor.getValue().errorType); | |
169 } | |
170 | |
171 /** | |
172 * Test that method can be invoked successfully if NFC is supported and adap ter is enabled. | |
173 */ | |
174 @Test | |
175 @Feature({"NFCTest"}) | |
176 public void testNFCIsSupported() { | |
177 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
178 nfc.setActivityForTesting(mActivity); | |
179 WatchResponse mockCallback = mock(WatchResponse.class); | |
180 nfc.watch(createNfcWatchOptions(), mockCallback); | |
181 verify(mockCallback).call(anyInt(), mErrorCaptor.capture()); | |
182 assertNull(mErrorCaptor.getValue()); | |
183 } | |
184 | |
185 /** | |
186 * Test conversion from NdefMessage to mojo NfcMessage. | |
187 */ | |
188 @Test | |
189 @Feature({"NFCTest"}) | |
190 public void testNdefToMojoConversion() throws UnsupportedEncodingException { | |
191 // Test EMPTY record conversion. | |
192 NdefMessage emptyNdefMessage = | |
193 new NdefMessage(new NdefRecord(NdefRecord.TNF_EMPTY, null, null, null)); | |
194 NfcMessage emptyNfcMessage = NfcTypeConverter.toNfcMessage(emptyNdefMess age); | |
195 assertNull(emptyNfcMessage.url); | |
196 assertEquals(1, emptyNfcMessage.data.length); | |
197 assertEquals(NfcRecordType.EMPTY, emptyNfcMessage.data[0].recordType); | |
198 assertEquals(true, emptyNfcMessage.data[0].mediaType.isEmpty()); | |
199 assertEquals(0, emptyNfcMessage.data[0].data.length); | |
200 | |
201 // Test URL record conversion. | |
202 NdefMessage urlNdefMessage = new NdefMessage(NdefRecord.createUri(TEST_U RL)); | |
203 NfcMessage urlNfcMessage = NfcTypeConverter.toNfcMessage(urlNdefMessage) ; | |
204 assertNull(urlNfcMessage.url); | |
205 assertEquals(1, urlNfcMessage.data.length); | |
206 assertEquals(NfcRecordType.URL, urlNfcMessage.data[0].recordType); | |
207 assertEquals(TEXT_MIME, urlNfcMessage.data[0].mediaType); | |
208 assertEquals(TEST_URL, new String(urlNfcMessage.data[0].data)); | |
209 | |
210 // Test TEXT record conversion. | |
211 NdefMessage textNdefMessage = | |
212 new NdefMessage(NdefRecord.createTextRecord(LANG_EN_US, TEST_TEX T)); | |
213 NfcMessage textNfcMessage = NfcTypeConverter.toNfcMessage(textNdefMessag e); | |
214 assertNull(textNfcMessage.url); | |
215 assertEquals(1, textNfcMessage.data.length); | |
216 assertEquals(NfcRecordType.TEXT, textNfcMessage.data[0].recordType); | |
217 assertEquals(TEXT_MIME, textNfcMessage.data[0].mediaType); | |
218 assertEquals(TEST_TEXT, new String(textNfcMessage.data[0].data)); | |
219 | |
220 // Test MIME record conversion. | |
221 NdefMessage mimeNdefMessage = | |
222 new NdefMessage(NdefRecord.createMime(TEXT_MIME, TEST_TEXT.getBy tes())); | |
223 NfcMessage mimeNfcMessage = NfcTypeConverter.toNfcMessage(mimeNdefMessag e); | |
224 assertNull(mimeNfcMessage.url); | |
225 assertEquals(1, mimeNfcMessage.data.length); | |
226 assertEquals(NfcRecordType.OPAQUE_RECORD, mimeNfcMessage.data[0].recordT ype); | |
227 assertEquals(TEXT_MIME, textNfcMessage.data[0].mediaType); | |
228 assertEquals(TEST_TEXT, new String(textNfcMessage.data[0].data)); | |
229 | |
230 // Test JSON record conversion. | |
231 NdefMessage jsonNdefMessage = | |
232 new NdefMessage(NdefRecord.createMime(JSON_MIME, TEST_JSON.getBy tes())); | |
233 NfcMessage jsonNfcMessage = NfcTypeConverter.toNfcMessage(jsonNdefMessag e); | |
234 assertNull(jsonNfcMessage.url); | |
235 assertEquals(1, jsonNfcMessage.data.length); | |
236 assertEquals(NfcRecordType.JSON, jsonNfcMessage.data[0].recordType); | |
237 assertEquals(JSON_MIME, jsonNfcMessage.data[0].mediaType); | |
238 assertEquals(TEST_JSON, new String(jsonNfcMessage.data[0].data)); | |
239 | |
240 // Test NfcMessage with WebNFC external type. | |
241 NdefRecord jsonNdefRecord = NdefRecord.createMime(JSON_MIME, TEST_JSON.g etBytes()); | |
242 NdefRecord extNdefRecord = NdefRecord.createExternal(DOMAIN, TYPE, TEST_ URL.getBytes()); | |
243 NdefMessage webNdefMessage = new NdefMessage(jsonNdefRecord, extNdefReco rd); | |
244 NfcMessage webNfcMessage = NfcTypeConverter.toNfcMessage(webNdefMessage) ; | |
245 assertEquals(TEST_URL, webNfcMessage.url); | |
246 assertEquals(1, webNfcMessage.data.length); | |
247 assertEquals(NfcRecordType.JSON, webNfcMessage.data[0].recordType); | |
248 assertEquals(JSON_MIME, webNfcMessage.data[0].mediaType); | |
249 assertEquals(TEST_JSON, new String(webNfcMessage.data[0].data)); | |
250 } | |
251 | |
252 /** | |
253 * Test conversion from mojo NfcMessage to android NdefMessage. | |
254 */ | |
255 @Test | |
256 @Feature({"NFCTest"}) | |
257 public void testMojoToNdefConversion() throws InvalidNfcMessageException { | |
258 // Test URL record conversion. | |
259 NdefMessage urlNdefMessage = createUrlWebNFCNdefMessage(); | |
260 assertEquals(2, urlNdefMessage.getRecords().length); | |
261 assertEquals(NdefRecord.TNF_WELL_KNOWN, urlNdefMessage.getRecords()[0].g etTnf()); | |
262 assertEquals(TEST_URL, urlNdefMessage.getRecords()[0].toUri().toString() ); | |
263 assertEquals(NdefRecord.TNF_EXTERNAL_TYPE, urlNdefMessage.getRecords()[1 ].getTnf()); | |
264 assertEquals(DOMAIN + ":" + TYPE, new String(urlNdefMessage.getRecords() [1].getType())); | |
265 | |
266 // Test TEXT record conversion. | |
267 NfcRecord textNfcRecord = new NfcRecord(); | |
268 textNfcRecord.recordType = NfcRecordType.TEXT; | |
269 textNfcRecord.mediaType = TEXT_MIME; | |
270 textNfcRecord.data = TEST_TEXT.getBytes(); | |
271 NfcMessage textNfcMessage = createNfcMessage(TEST_URL, textNfcRecord); | |
272 NdefMessage textNdefMessage = NfcTypeConverter.toNdefMessage(textNfcMess age); | |
273 assertEquals(2, textNdefMessage.getRecords().length); | |
274 short tnf = textNdefMessage.getRecords()[0].getTnf(); | |
275 boolean isWellKnownOrMime = | |
276 (tnf == NdefRecord.TNF_WELL_KNOWN || tnf == NdefRecord.TNF_MIME_ MEDIA); | |
277 assertEquals(true, isWellKnownOrMime); | |
278 assertEquals(NdefRecord.TNF_EXTERNAL_TYPE, textNdefMessage.getRecords()[ 1].getTnf()); | |
279 | |
280 // Test MIME record conversion. | |
281 NfcRecord mimeNfcRecord = new NfcRecord(); | |
282 mimeNfcRecord.recordType = NfcRecordType.OPAQUE_RECORD; | |
283 mimeNfcRecord.mediaType = TEXT_MIME; | |
284 mimeNfcRecord.data = TEST_TEXT.getBytes(); | |
285 NfcMessage mimeNfcMessage = createNfcMessage(TEST_URL, mimeNfcRecord); | |
286 NdefMessage mimeNdefMessage = NfcTypeConverter.toNdefMessage(mimeNfcMess age); | |
287 assertEquals(2, mimeNdefMessage.getRecords().length); | |
288 assertEquals(NdefRecord.TNF_MIME_MEDIA, mimeNdefMessage.getRecords()[0]. getTnf()); | |
289 assertEquals(TEXT_MIME, mimeNdefMessage.getRecords()[0].toMimeType()); | |
290 assertEquals(TEST_TEXT, new String(mimeNdefMessage.getRecords()[0].getPa yload())); | |
291 assertEquals(NdefRecord.TNF_EXTERNAL_TYPE, mimeNdefMessage.getRecords()[ 1].getTnf()); | |
292 | |
293 // Test JSON record conversion. | |
294 NfcRecord jsonNfcRecord = new NfcRecord(); | |
295 jsonNfcRecord.recordType = NfcRecordType.OPAQUE_RECORD; | |
296 jsonNfcRecord.mediaType = JSON_MIME; | |
297 jsonNfcRecord.data = TEST_JSON.getBytes(); | |
298 NfcMessage jsonNfcMessage = createNfcMessage(TEST_URL, jsonNfcRecord); | |
299 NdefMessage jsonNdefMessage = NfcTypeConverter.toNdefMessage(jsonNfcMess age); | |
300 assertEquals(2, jsonNdefMessage.getRecords().length); | |
301 assertEquals(NdefRecord.TNF_MIME_MEDIA, jsonNdefMessage.getRecords()[0]. getTnf()); | |
302 assertEquals(JSON_MIME, jsonNdefMessage.getRecords()[0].toMimeType()); | |
303 assertEquals(TEST_JSON, new String(jsonNdefMessage.getRecords()[0].getPa yload())); | |
304 assertEquals(NdefRecord.TNF_EXTERNAL_TYPE, jsonNdefMessage.getRecords()[ 1].getTnf()); | |
305 } | |
306 | |
307 /** | |
308 * Test that invalid NfcMessage is rejected with INVALID_MESSAGE error code. | |
309 */ | |
310 @Test | |
311 @Feature({"NFCTest"}) | |
312 public void testInvalidNfcMessage() { | |
313 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
314 nfc.setActivityForTesting(mActivity); | |
315 PushResponse mockCallback = mock(PushResponse.class); | |
316 nfc.push(new NfcMessage(), createNfcPushOptions(), mockCallback); | |
317 nfc.processPendingOperationsForTesting(mNfcTagHandler); | |
318 verify(mockCallback).call(mErrorCaptor.capture()); | |
319 assertEquals(NfcErrorType.INVALID_MESSAGE, mErrorCaptor.getValue().error Type); | |
320 } | |
321 | |
322 /** | |
323 * Test that Nfc.suspendNfcOperations() and Nfc.resumeNfcOperations() work c orrectly. | |
324 */ | |
325 @Test | |
326 @Feature({"NFCTest"}) | |
327 public void testResumeSuspend() { | |
328 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
329 // No activity / client or active pending operations | |
330 nfc.suspendNfcOperations(); | |
331 nfc.resumeNfcOperations(); | |
332 | |
333 nfc.setActivityForTesting(mActivity); | |
334 nfc.setClient(mNfcClient); | |
335 WatchResponse mockCallback = mock(WatchResponse.class); | |
336 nfc.watch(createNfcWatchOptions(), mockCallback); | |
337 nfc.suspendNfcOperations(); | |
338 verify(mNfcAdapter, times(1)).disableReaderMode(mActivity); | |
339 nfc.resumeNfcOperations(); | |
340 // 1. Enable after watch is called, 2. after resumeNfcOperations is call ed. | |
341 verify(mNfcAdapter, times(2)) | |
342 .enableReaderMode( | |
343 any(Activity.class), any(ReaderCallback.class), anyInt() , isNull()); | |
344 | |
345 nfc.processPendingOperationsForTesting(mNfcTagHandler); | |
346 // Check that watch request was completed successfully. | |
347 verify(mockCallback).call(mWatchCaptor.capture(), mErrorCaptor.capture() ); | |
348 assertNull(mErrorCaptor.getValue()); | |
349 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
350 | |
351 // Check that client was notified and watch with correct id was triggere d. | |
352 verify(mNfcClient, times(1)) | |
353 .onWatch(mOnWatchCallbackCaptor.capture(), any(NfcMessage.class) ); | |
354 assertEquals(1, mOnWatchCallbackCaptor.getValue()[0]); | |
355 } | |
356 | |
357 /** | |
358 * Test that Nfc.push() successful when NFC tag is connected. | |
359 */ | |
360 @Test | |
361 @Feature({"NFCTest"}) | |
362 public void testPush() { | |
363 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
364 nfc.setActivityForTesting(mActivity); | |
365 PushResponse mockCallback = mock(PushResponse.class); | |
366 nfc.push(createNfcMessage(), createNfcPushOptions(), mockCallback); | |
367 nfc.processPendingOperationsForTesting(mNfcTagHandler); | |
368 verify(mockCallback).call(mErrorCaptor.capture()); | |
369 assertNull(mErrorCaptor.getValue()); | |
370 } | |
371 | |
372 /** | |
373 * Test that Nfc.cancelPush() cancels pending push opration and completes su ccessfully. | |
374 */ | |
375 @Test | |
376 @Feature({"NFCTest"}) | |
377 public void testCancelPush() { | |
378 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
379 nfc.setActivityForTesting(mActivity); | |
380 PushResponse mockPushCallback = mock(PushResponse.class); | |
381 CancelPushResponse mockCancelPushCallback = mock(CancelPushResponse.clas s); | |
382 nfc.push(createNfcMessage(), createNfcPushOptions(), mockPushCallback); | |
383 nfc.cancelPush(NfcPushTarget.ANY, mockCancelPushCallback); | |
384 | |
385 // Check that push request was cancelled with OPERATION_CANCELLED. | |
386 verify(mockPushCallback).call(mErrorCaptor.capture()); | |
387 assertEquals(NfcErrorType.OPERATION_CANCELLED, mErrorCaptor.getValue().e rrorType); | |
388 | |
389 // Check that cancel request was successfuly completed. | |
390 verify(mockCancelPushCallback).call(mErrorCaptor.capture()); | |
391 assertNull(mErrorCaptor.getValue()); | |
392 } | |
393 | |
394 /** | |
395 * Test that Nfc.watch() works correctly and client is notified. | |
396 */ | |
397 @Test | |
398 @Feature({"NFCTest"}) | |
399 public void testWatch() { | |
400 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
401 nfc.setActivityForTesting(mActivity); | |
402 nfc.setClient(mNfcClient); | |
403 WatchResponse mockWatchCallback = mock(WatchResponse.class); | |
404 nfc.watch(createNfcWatchOptions(), mockWatchCallback); | |
405 nfc.processPendingOperationsForTesting(mNfcTagHandler); | |
406 | |
407 // Check that watch request was completed successfully. | |
408 verify(mockWatchCallback).call(mWatchCaptor.capture(), mErrorCaptor.capt ure()); | |
409 assertNull(mErrorCaptor.getValue()); | |
410 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
Reilly Grant (use Gerrit)
2016/09/29 04:38:02
The particular watcher ID assigned seems like an i
shalamov
2016/09/30 11:51:35
Modified tests to avoid checking exact value. Inst
| |
411 | |
412 // Check that client was notified and watch with correct id was triggere d. | |
413 verify(mNfcClient, times(1)) | |
414 .onWatch(mOnWatchCallbackCaptor.capture(), any(NfcMessage.class) ); | |
415 assertEquals(1, mOnWatchCallbackCaptor.getValue()[0]); | |
416 } | |
417 | |
418 /** | |
419 * Test that Nfc.watch() matching function works correctly. | |
420 */ | |
421 @Test | |
422 @Feature({"NFCTest"}) | |
423 public void testlWatchMatching() { | |
424 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
425 nfc.setActivityForTesting(mActivity); | |
426 nfc.setClient(mNfcClient); | |
427 | |
428 // Should match by WebNFC Id. | |
429 NfcWatchOptions options1 = createNfcWatchOptions(); | |
430 options1.mode = NfcWatchMode.WEBNFC_ONLY; | |
431 options1.url = TEST_URL; | |
432 WatchResponse mockWatchCallback1 = mock(WatchResponse.class); | |
433 nfc.watch(options1, mockWatchCallback1); | |
434 verify(mockWatchCallback1).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
435 assertNull(mErrorCaptor.getValue()); | |
436 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
437 | |
438 // Should match by media type. | |
439 NfcWatchOptions options2 = createNfcWatchOptions(); | |
440 options2.mode = NfcWatchMode.ANY; | |
441 options2.mediaType = TEXT_MIME; | |
442 WatchResponse mockWatchCallback2 = mock(WatchResponse.class); | |
443 nfc.watch(options2, mockWatchCallback2); | |
444 verify(mockWatchCallback2).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
445 assertNull(mErrorCaptor.getValue()); | |
446 assertEquals(2, mWatchCaptor.getValue().intValue()); | |
447 | |
448 // Should match by record type. | |
449 NfcWatchOptions options3 = createNfcWatchOptions(); | |
450 options3.mode = NfcWatchMode.ANY; | |
451 NfcRecordTypeFilter typeFilter = new NfcRecordTypeFilter(); | |
452 typeFilter.recordType = NfcRecordType.URL; | |
453 options3.recordFilter = typeFilter; | |
454 WatchResponse mockWatchCallback3 = mock(WatchResponse.class); | |
455 nfc.watch(options3, mockWatchCallback3); | |
456 verify(mockWatchCallback3).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
457 assertNull(mErrorCaptor.getValue()); | |
458 assertEquals(3, mWatchCaptor.getValue().intValue()); | |
459 | |
460 // Should not match | |
461 NfcWatchOptions options4 = createNfcWatchOptions(); | |
462 options4.mode = NfcWatchMode.WEBNFC_ONLY; | |
463 options4.url = DOMAIN; | |
464 WatchResponse mockWatchCallback4 = mock(WatchResponse.class); | |
465 nfc.watch(options4, mockWatchCallback4); | |
466 verify(mockWatchCallback4).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
467 assertNull(mErrorCaptor.getValue()); | |
468 assertEquals(4, mWatchCaptor.getValue().intValue()); | |
469 | |
470 nfc.processPendingOperationsForTesting(mNfcTagHandler); | |
471 | |
472 // Check that client was notified and watch with correct id was triggere d. | |
473 verify(mNfcClient, times(1)) | |
474 .onWatch(mOnWatchCallbackCaptor.capture(), any(NfcMessage.class) ); | |
475 assertEquals(3, mOnWatchCallbackCaptor.getValue().length); | |
476 assertEquals(1, mOnWatchCallbackCaptor.getValue()[0]); | |
477 assertEquals(2, mOnWatchCallbackCaptor.getValue()[1]); | |
478 assertEquals(3, mOnWatchCallbackCaptor.getValue()[2]); | |
479 } | |
480 | |
481 /** | |
482 * Test that Nfc.watch() can be cancelled with Nfc.cancelWatch(). | |
483 */ | |
484 @Test | |
485 @Feature({"NFCTest"}) | |
486 public void testCancelWatch() { | |
487 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
488 nfc.setActivityForTesting(mActivity); | |
489 WatchResponse mockWatchCallback = mock(WatchResponse.class); | |
490 nfc.watch(createNfcWatchOptions(), mockWatchCallback); | |
491 | |
492 verify(mockWatchCallback).call(mWatchCaptor.capture(), mErrorCaptor.capt ure()); | |
493 assertNull(mErrorCaptor.getValue()); | |
494 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
495 | |
496 CancelWatchResponse mockCancelWatchCallback = mock(CancelWatchResponse.c lass); | |
497 nfc.cancelWatch(mWatchCaptor.getValue().intValue(), mockCancelWatchCallb ack); | |
498 | |
499 // Check that cancel request was successfuly completed. | |
500 verify(mockCancelWatchCallback).call(mErrorCaptor.capture()); | |
501 assertNull(mErrorCaptor.getValue()); | |
Reilly Grant (use Gerrit)
2016/09/29 04:38:02
Test that the client is no longer notified after t
shalamov
2016/09/30 11:51:35
Done.
| |
502 } | |
503 | |
504 /** | |
505 * Test that Nfc.cancelAllWatches() cancels all pending watch operations. | |
506 */ | |
507 @Test | |
508 @Feature({"NFCTest"}) | |
509 public void testCancelAllWatches() { | |
510 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
511 nfc.setActivityForTesting(mActivity); | |
512 WatchResponse mockWatchCallback1 = mock(WatchResponse.class); | |
513 WatchResponse mockWatchCallback2 = mock(WatchResponse.class); | |
514 nfc.watch(createNfcWatchOptions(), mockWatchCallback1); | |
515 verify(mockWatchCallback1).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
516 assertNull(mErrorCaptor.getValue()); | |
517 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
518 | |
519 nfc.watch(createNfcWatchOptions(), mockWatchCallback2); | |
520 verify(mockWatchCallback2).call(mWatchCaptor.capture(), mErrorCaptor.cap ture()); | |
521 assertNull(mErrorCaptor.getValue()); | |
522 assertEquals(2, mWatchCaptor.getValue().intValue()); | |
523 | |
524 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl ass); | |
525 nfc.cancelAllWatches(mockCallback); | |
526 | |
527 // Check that cancel request was successfuly completed. | |
528 verify(mockCallback).call(mErrorCaptor.capture()); | |
529 assertNull(mErrorCaptor.getValue()); | |
530 } | |
531 | |
532 /** | |
533 * Test that Nfc.cancelWatch() with invalid id is failing with NOT_FOUND err or. | |
534 */ | |
535 @Test | |
536 @Feature({"NFCTest"}) | |
537 public void testCancelWatchInvalidId() { | |
538 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
539 nfc.setActivityForTesting(mActivity); | |
540 WatchResponse mockWatchCallback = mock(WatchResponse.class); | |
541 nfc.watch(createNfcWatchOptions(), mockWatchCallback); | |
542 | |
543 verify(mockWatchCallback).call(mWatchCaptor.capture(), mErrorCaptor.capt ure()); | |
544 assertNull(mErrorCaptor.getValue()); | |
545 assertEquals(1, mWatchCaptor.getValue().intValue()); | |
546 | |
547 CancelWatchResponse mockCancelWatchCallback = mock(CancelWatchResponse.c lass); | |
548 nfc.cancelWatch(2, mockCancelWatchCallback); | |
549 | |
550 verify(mockCancelWatchCallback).call(mErrorCaptor.capture()); | |
551 assertEquals(NfcErrorType.NOT_FOUND, mErrorCaptor.getValue().errorType); | |
552 } | |
553 | |
554 /** | |
555 * Test that Nfc.cancelAllWatches() is failing with NOT_FOUND error if there are no active | |
556 * watch opeartions. | |
557 */ | |
558 @Test | |
559 @Feature({"NFCTest"}) | |
560 public void testCancelAllWatchesWithNoWathcers() { | |
561 TestNfcImpl nfc = new TestNfcImpl(mContext); | |
562 nfc.setActivityForTesting(mActivity); | |
563 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl ass); | |
564 nfc.cancelAllWatches(mockCallback); | |
565 | |
566 verify(mockCallback).call(mErrorCaptor.capture()); | |
567 assertEquals(NfcErrorType.NOT_FOUND, mErrorCaptor.getValue().errorType); | |
568 } | |
569 | |
570 private NfcPushOptions createNfcPushOptions() { | |
571 NfcPushOptions pushOptions = new NfcPushOptions(); | |
572 pushOptions.target = NfcPushTarget.ANY; | |
573 pushOptions.timeout = 0; | |
574 pushOptions.ignoreRead = false; | |
575 return pushOptions; | |
576 } | |
577 | |
578 private NfcWatchOptions createNfcWatchOptions() { | |
579 NfcWatchOptions options = new NfcWatchOptions(); | |
580 options.url = ""; | |
581 options.mediaType = ""; | |
582 options.mode = NfcWatchMode.ANY; | |
583 options.recordFilter = null; | |
584 return options; | |
585 } | |
586 | |
587 private NfcMessage createNfcMessage() { | |
588 NfcMessage message = new NfcMessage(); | |
589 message.url = ""; | |
590 message.data = new NfcRecord[1]; | |
591 | |
592 NfcRecord nfcRecord = new NfcRecord(); | |
593 nfcRecord.recordType = NfcRecordType.TEXT; | |
594 nfcRecord.mediaType = TEXT_MIME; | |
595 nfcRecord.data = TEST_TEXT.getBytes(); | |
596 message.data[0] = nfcRecord; | |
597 return message; | |
598 } | |
599 | |
600 private NfcMessage createNfcMessage(String url, NfcRecord record) { | |
601 NfcMessage message = new NfcMessage(); | |
602 message.url = url; | |
603 message.data = new NfcRecord[1]; | |
604 message.data[0] = record; | |
605 return message; | |
606 } | |
607 | |
608 private NdefMessage createUrlWebNFCNdefMessage() { | |
609 NfcRecord urlNfcRecord = new NfcRecord(); | |
610 urlNfcRecord.recordType = NfcRecordType.URL; | |
611 urlNfcRecord.mediaType = TEXT_MIME; | |
612 urlNfcRecord.data = TEST_URL.getBytes(); | |
613 NfcMessage urlNfcMessage = createNfcMessage(TEST_URL, urlNfcRecord); | |
614 try { | |
615 return NfcTypeConverter.toNdefMessage(urlNfcMessage); | |
616 } catch (InvalidNfcMessageException e) { | |
617 return null; | |
618 } | |
619 } | |
620 } | |
OLD | NEW |