1 /* 2 * Copyright (C) 2024 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 com.android.nfc; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 24 import org.junit.Test; 25 26 public class NfcProprietaryCapsTest { 27 28 private static final int PASSIVE_OBSERVE_MODE = 0; 29 private static final int POLLING_FRAME_NTF = 1; 30 private static final int POWER_SAVING_MODE = 2; 31 private static final int AUTOTRANSACT_POLLING_LOOP_FILTER = 3; 32 private static final int NUMBER_OF_EXIT_FRAMES_SUPPORTED = 4; 33 34 @Test testCreateFromByteArraySupportWithoutRfDeactivation()35 public void testCreateFromByteArraySupportWithoutRfDeactivation() { 36 byte[] inputCaps = { 37 (byte) PASSIVE_OBSERVE_MODE, 1, 2, 38 (byte) POLLING_FRAME_NTF, 1, 1, 39 (byte) POWER_SAVING_MODE, 1, 0, 40 (byte) AUTOTRANSACT_POLLING_LOOP_FILTER, 1, 1, 41 (byte) NUMBER_OF_EXIT_FRAMES_SUPPORTED, 1, 5 42 }; 43 44 NfcProprietaryCaps result = NfcProprietaryCaps.createFromByteArray(inputCaps); 45 assertNotNull(result); 46 assertEquals(NfcProprietaryCaps.PassiveObserveMode.SUPPORT_WITHOUT_RF_DEACTIVATION, 47 result.getPassiveObserveMode()); 48 assertTrue(result.isPollingFrameNotificationSupported()); 49 assertFalse(result.isPowerSavingModeSupported()); 50 assertTrue(result.isAutotransactPollingLoopFilterSupported()); 51 assertEquals(5, result.getNumberOfExitFramesSupported()); 52 } 53 54 @Test testCreateFromByteArraySupportWithRfDeactivation()55 public void testCreateFromByteArraySupportWithRfDeactivation() { 56 byte[] inputCaps = { 57 (byte) PASSIVE_OBSERVE_MODE, 1, 1, 58 (byte) POLLING_FRAME_NTF, 1, 1, 59 (byte) POWER_SAVING_MODE, 1, 0, 60 (byte) AUTOTRANSACT_POLLING_LOOP_FILTER, 1, 1, 61 (byte) NUMBER_OF_EXIT_FRAMES_SUPPORTED, 1, 5 62 }; 63 64 NfcProprietaryCaps result = NfcProprietaryCaps.createFromByteArray(inputCaps); 65 assertNotNull(result); 66 assertEquals(NfcProprietaryCaps.PassiveObserveMode.SUPPORT_WITH_RF_DEACTIVATION, 67 result.getPassiveObserveMode()); 68 assertTrue(result.isPollingFrameNotificationSupported()); 69 assertFalse(result.isPowerSavingModeSupported()); 70 assertTrue(result.isAutotransactPollingLoopFilterSupported()); 71 assertEquals(5, result.getNumberOfExitFramesSupported()); 72 } 73 74 @Test testCreateFromByteArrayNotSupported()75 public void testCreateFromByteArrayNotSupported() { 76 byte[] inputCaps = { 77 (byte) PASSIVE_OBSERVE_MODE, 1, 0, 78 (byte) POLLING_FRAME_NTF, 1, 1, 79 (byte) POWER_SAVING_MODE, 1, 0, 80 (byte) AUTOTRANSACT_POLLING_LOOP_FILTER, 1, 1, 81 (byte) NUMBER_OF_EXIT_FRAMES_SUPPORTED, 1, 5 82 }; 83 84 NfcProprietaryCaps result = NfcProprietaryCaps.createFromByteArray(inputCaps); 85 assertNotNull(result); 86 assertEquals(NfcProprietaryCaps.PassiveObserveMode.NOT_SUPPORTED, 87 result.getPassiveObserveMode()); 88 assertTrue(result.isPollingFrameNotificationSupported()); 89 assertFalse(result.isPowerSavingModeSupported()); 90 assertTrue(result.isAutotransactPollingLoopFilterSupported()); 91 assertEquals(5, result.getNumberOfExitFramesSupported()); 92 } 93 94 @Test testCreateFromByteArrayWithInvalidData()95 public void testCreateFromByteArrayWithInvalidData() { 96 byte[] invalidCaps = {(byte) PASSIVE_OBSERVE_MODE, 2, 3}; // Invalid length 97 98 NfcProprietaryCaps result = NfcProprietaryCaps.createFromByteArray(invalidCaps); 99 assertNotNull(result); 100 assertEquals(NfcProprietaryCaps.PassiveObserveMode.NOT_SUPPORTED, 101 result.getPassiveObserveMode()); 102 assertFalse(result.isPollingFrameNotificationSupported()); 103 assertFalse(result.isPowerSavingModeSupported()); 104 assertFalse(result.isAutotransactPollingLoopFilterSupported()); 105 assertEquals(0, result.getNumberOfExitFramesSupported()); 106 } 107 108 @Test testCreateFromByteArrayWithEmptyArray()109 public void testCreateFromByteArrayWithEmptyArray() { 110 byte[] emptyCaps = {}; 111 112 NfcProprietaryCaps result = NfcProprietaryCaps.createFromByteArray(emptyCaps); 113 assertNotNull(result); 114 assertEquals(NfcProprietaryCaps.PassiveObserveMode.NOT_SUPPORTED, 115 result.getPassiveObserveMode()); 116 assertFalse(result.isPollingFrameNotificationSupported()); 117 assertFalse(result.isPowerSavingModeSupported()); 118 assertFalse(result.isAutotransactPollingLoopFilterSupported()); 119 assertEquals(0, result.getNumberOfExitFramesSupported()); 120 } 121 122 @Test testToString()123 public void testToString() { 124 NfcProprietaryCaps caps = new NfcProprietaryCaps( 125 NfcProprietaryCaps.PassiveObserveMode.SUPPORT_WITHOUT_RF_DEACTIVATION, 126 true, 127 false, 128 true, 129 5, 130 false 131 ); 132 String expected = "NfcProprietaryCaps{" + 133 "passiveObserveMode=SUPPORT_WITHOUT_RF_DEACTIVATION, " + 134 "isPollingFrameNotificationSupported=true, " + 135 "isPowerSavingModeSupported=false, " + 136 "isAutotransactPollingLoopFilterSupported=true, " + 137 "mIsReaderModeAnnotationSupported=false}"; 138 139 assertEquals(expected, caps.toString()); 140 } 141 } 142