Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: device/nfc/android/junit/src/org/chromium/device/nfc/NFCTest.java

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

Powered by Google App Engine
This is Rietveld 408576698