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

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

Powered by Google App Engine
This is Rietveld 408576698