OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.device.nfc; | 5 package org.chromium.device.nfc; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertNotEquals; | 8 import static org.junit.Assert.assertNotEquals; |
| 9 import static org.junit.Assert.assertNotNull; |
9 import static org.junit.Assert.assertNull; | 10 import static org.junit.Assert.assertNull; |
10 import static org.mockito.ArgumentMatchers.any; | 11 import static org.mockito.ArgumentMatchers.any; |
11 import static org.mockito.ArgumentMatchers.anyInt; | 12 import static org.mockito.ArgumentMatchers.anyInt; |
12 import static org.mockito.ArgumentMatchers.anyString; | 13 import static org.mockito.ArgumentMatchers.anyString; |
13 import static org.mockito.ArgumentMatchers.isNull; | 14 import static org.mockito.ArgumentMatchers.isNull; |
14 import static org.mockito.Mockito.doNothing; | 15 import static org.mockito.Mockito.doNothing; |
15 import static org.mockito.Mockito.doReturn; | 16 import static org.mockito.Mockito.doReturn; |
| 17 import static org.mockito.Mockito.doThrow; |
16 import static org.mockito.Mockito.mock; | 18 import static org.mockito.Mockito.mock; |
17 import static org.mockito.Mockito.times; | 19 import static org.mockito.Mockito.times; |
18 import static org.mockito.Mockito.verify; | 20 import static org.mockito.Mockito.verify; |
19 | 21 |
20 import android.app.Activity; | 22 import android.app.Activity; |
21 import android.content.Context; | 23 import android.content.Context; |
22 import android.content.pm.PackageManager; | 24 import android.content.pm.PackageManager; |
23 import android.nfc.FormatException; | 25 import android.nfc.FormatException; |
24 import android.nfc.NdefMessage; | 26 import android.nfc.NdefMessage; |
25 import android.nfc.NdefRecord; | 27 import android.nfc.NdefRecord; |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 public void testCancelAllWatchesWithNoWathcers() { | 574 public void testCancelAllWatchesWithNoWathcers() { |
573 TestNfcImpl nfc = new TestNfcImpl(mContext); | 575 TestNfcImpl nfc = new TestNfcImpl(mContext); |
574 nfc.setActivityForTesting(mActivity); | 576 nfc.setActivityForTesting(mActivity); |
575 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl
ass); | 577 CancelAllWatchesResponse mockCallback = mock(CancelAllWatchesResponse.cl
ass); |
576 nfc.cancelAllWatches(mockCallback); | 578 nfc.cancelAllWatches(mockCallback); |
577 | 579 |
578 verify(mockCallback).call(mErrorCaptor.capture()); | 580 verify(mockCallback).call(mErrorCaptor.capture()); |
579 assertEquals(NfcErrorType.NOT_FOUND, mErrorCaptor.getValue().errorType); | 581 assertEquals(NfcErrorType.NOT_FOUND, mErrorCaptor.getValue().errorType); |
580 } | 582 } |
581 | 583 |
| 584 /** |
| 585 * Test that when tag is disconnected during read operation, IllegalStateExc
eption is handled. |
| 586 */ |
| 587 @Test |
| 588 @Feature({"NFCTest"}) |
| 589 public void testTagDisconnectedDuringRead() throws IOException, FormatExcept
ion { |
| 590 TestNfcImpl nfc = new TestNfcImpl(mContext); |
| 591 nfc.setActivityForTesting(mActivity); |
| 592 nfc.setClient(mNfcClient); |
| 593 WatchResponse mockWatchCallback = mock(WatchResponse.class); |
| 594 nfc.watch(createNfcWatchOptions(), mockWatchCallback); |
| 595 |
| 596 // Force read operation to fail |
| 597 doThrow(IllegalStateException.class).when(mNfcTagHandler).read(); |
| 598 |
| 599 // Mocks 'NFC tag found' event. |
| 600 nfc.processPendingOperationsForTesting(mNfcTagHandler); |
| 601 |
| 602 // Check that client was not notified. |
| 603 verify(mNfcClient, times(0)) |
| 604 .onWatch(mOnWatchCallbackCaptor.capture(), any(NfcMessage.class)
); |
| 605 } |
| 606 |
| 607 /** |
| 608 * Test that when tag is disconnected during write operation, IllegalStateEx
ception is handled. |
| 609 */ |
| 610 @Test |
| 611 @Feature({"NFCTest"}) |
| 612 public void testTagDisconnectedDuringWrite() throws IOException, FormatExcep
tion { |
| 613 TestNfcImpl nfc = new TestNfcImpl(mContext); |
| 614 nfc.setActivityForTesting(mActivity); |
| 615 PushResponse mockCallback = mock(PushResponse.class); |
| 616 |
| 617 // Force write operation to fail |
| 618 doThrow(IllegalStateException.class).when(mNfcTagHandler).write(any(Ndef
Message.class)); |
| 619 nfc.push(createNfcMessage(), createNfcPushOptions(), mockCallback); |
| 620 nfc.processPendingOperationsForTesting(mNfcTagHandler); |
| 621 verify(mockCallback).call(mErrorCaptor.capture()); |
| 622 |
| 623 // Test that correct error is returned. |
| 624 assertNotNull(mErrorCaptor.getValue()); |
| 625 assertEquals(NfcErrorType.IO_ERROR, mErrorCaptor.getValue().errorType); |
| 626 } |
| 627 |
582 private NfcPushOptions createNfcPushOptions() { | 628 private NfcPushOptions createNfcPushOptions() { |
583 NfcPushOptions pushOptions = new NfcPushOptions(); | 629 NfcPushOptions pushOptions = new NfcPushOptions(); |
584 pushOptions.target = NfcPushTarget.ANY; | 630 pushOptions.target = NfcPushTarget.ANY; |
585 pushOptions.timeout = 0; | 631 pushOptions.timeout = 0; |
586 pushOptions.ignoreRead = false; | 632 pushOptions.ignoreRead = false; |
587 return pushOptions; | 633 return pushOptions; |
588 } | 634 } |
589 | 635 |
590 private NfcWatchOptions createNfcWatchOptions() { | 636 private NfcWatchOptions createNfcWatchOptions() { |
591 NfcWatchOptions options = new NfcWatchOptions(); | 637 NfcWatchOptions options = new NfcWatchOptions(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
623 urlNfcRecord.mediaType = TEXT_MIME; | 669 urlNfcRecord.mediaType = TEXT_MIME; |
624 urlNfcRecord.data = TEST_URL.getBytes(); | 670 urlNfcRecord.data = TEST_URL.getBytes(); |
625 NfcMessage urlNfcMessage = createNfcMessage(TEST_URL, urlNfcRecord); | 671 NfcMessage urlNfcMessage = createNfcMessage(TEST_URL, urlNfcRecord); |
626 try { | 672 try { |
627 return NfcTypeConverter.toNdefMessage(urlNfcMessage); | 673 return NfcTypeConverter.toNdefMessage(urlNfcMessage); |
628 } catch (InvalidNfcMessageException e) { | 674 } catch (InvalidNfcMessageException e) { |
629 return null; | 675 return null; |
630 } | 676 } |
631 } | 677 } |
632 } | 678 } |
OLD | NEW |