Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c300ce96 authored by ManivelanK's avatar ManivelanK
Browse files

Test: Adding test cases for NfcBarcode

Bug: 372488488

Test: atest NfcManagerTests
{"args": "NfcManagerTests", "test_runner": {"AtestTradefedTestRunner": {"arm64-v8a NfcManagerTests": {"PASSED": [{"test_name": "android.nfc.NdefMessageTest#testToByteArray", "test_time": "(2ms)", "details": null}, {"test_name": "android.nfc.NdefMessageTest#testGetRecords", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NdefRecordTest#testCreateTextRecord", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NdefRecordTest#testNdefRecordConstructor", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.NdefRecordTest#testCreateMime", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.NdefRecordTest#testCreateUri", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NdefRecordTest#testCreateExternal", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NfcAntennaInfoTest#testGetDeviceHeight", "test_time": "(151ms)", "details": null}, {"test_name": "android.nfc.NfcAntennaInfoTest#testGetAvailableNfcAntennas", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.NfcAntennaInfoTest#testGetDeviceWidth", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.NfcAntennaInfoTest#testIsDeviceFoldable", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testRegister_FirstRegisterFails", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testRegister_RegisterUnregister", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testRegister_RegisterUnregisterWhenNotSupported", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testRegister_RegisterSameListenerTwice", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testNotify_AllListenersNotified", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.NfcControllerAlwaysOnListenerTest#testStateChange_CorrectValue", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.NfcManagerTest#testGetDefaultAdapter", "test_time": "(856ms)", "details": null}, {"test_name": "android.nfc.TechListParcelTest#testWriteParcelArrayEmpty", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.TechListParcelTest#testWriteParcel", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.TechListParcelTest#testWriteParcelElementEmpty", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.cardemulation.AidGroupTest#testWriteAsXml", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.cardemulation.AidGroupTest#testRightToParcel", "test_time": "(51ms)", "details": null}, {"test_name": "android.nfc.cardemulation.AidGroupTest#testGetAids", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.cardemulation.AidGroupTest#testGetCategory", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.cardemulation.CardemulationTest#testIsDefaultServiceForAid", "test_time": "(630ms)", "details": null}, {"test_name": "android.nfc.cardemulation.CardemulationTest#testIsDefaultServiceForCategory", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetTimeout", "test_time": "(51ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetMaxTransceiveLength", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testSetTimeoutInvalidTimeout", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetTimeoutRemoteException", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetNfcAWithTech", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetAtga", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testSetTimeoutRemoteException", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetNfcAWithoutTech", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testSetTimeout", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testGetSak", "test_time": "(25ms)", "details": null}, {"test_name": "android.nfc.tech.NfcATest#testTransceive", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetNfcBarcodeInstance", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetBarCodeTypeUnknown", "test_time": "(26ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetBarcodeWithTypeKovio", "test_time": "(0ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetNfcBarcodeWithoutTech", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetType", "test_time": "(1ms)", "details": null}, {"test_name": "android.nfc.tech.NfcBarcodeTest#testGetNfcBarcodeInstanceWithException", "test_time": "(0ms)", "details": null}], "summary": {"PASSED": 44, "FAILED": 0, "IGNORED": 0}}}}, "total_summary": {"PASSED": 44, "FAILED": 0, "IGNORED": 0}}

Change-Id: I2213e25032ece6bcd8f3e8d0ddf3ed6ab88a40f4
parent d068a0c0
Loading
Loading
Loading
Loading
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.nfc.tech;

import static android.nfc.tech.NfcBarcode.EXTRA_BARCODE_TYPE;
import static android.nfc.tech.NfcBarcode.TYPE_KOVIO;
import static android.nfc.tech.NfcBarcode.TYPE_UNKNOWN;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.nfc.Tag;
import android.os.Bundle;
import android.os.RemoteException;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class NfcBarcodeTest {
    @Mock
    private Tag mMockTag;
    @Mock
    private Bundle mMockBundle;
    private NfcBarcode mNfcBarcode;

    @Before
    public void setUp() throws RemoteException {
        MockitoAnnotations.initMocks(this);
        when(mMockBundle.getInt(EXTRA_BARCODE_TYPE)).thenReturn(TYPE_KOVIO);
        when(mMockTag.getTechExtras(TagTechnology.NFC_BARCODE)).thenReturn(mMockBundle);

        mNfcBarcode = new NfcBarcode(mMockTag);
    }

    @Test
    public void testGetNfcBarcodeInstance() {
        Tag mockTag = mock(Tag.class);
        when(mockTag.hasTech(TagTechnology.NFC_BARCODE)).thenReturn(true);
        when(mockTag.getTechExtras(TagTechnology.NFC_BARCODE)).thenReturn(mMockBundle);

        assertNotNull(NfcBarcode.get(mockTag));
        verify(mockTag).hasTech(TagTechnology.NFC_BARCODE);
        verify(mockTag).getTechExtras(TagTechnology.NFC_BARCODE);
    }

    @Test(expected = NullPointerException.class)
    public void testGetNfcBarcodeInstanceWithException() {
        Tag mockTag = mock(Tag.class);
        when(mockTag.hasTech(TagTechnology.NFC_BARCODE)).thenReturn(true);
        when(mockTag.getTechExtras(TagTechnology.NFC_BARCODE)).thenReturn(null);

        assertNull(NfcBarcode.get(mockTag));
        verify(mockTag).hasTech(TagTechnology.NFC_BARCODE);
        verify(mockTag).getTechExtras(TagTechnology.NFC_BARCODE);
    }

    @Test
    public void testGetNfcBarcodeWithoutTech() {
        when(mMockTag.hasTech(TagTechnology.NFC_BARCODE)).thenReturn(false);

        assertNull(NfcBarcode.get(mMockTag));
    }

    @Test
    public void testGetType() {
        int result = mNfcBarcode.getType();
        assertEquals(TYPE_KOVIO, result);
    }

    @Test
    public void testGetBarcodeWithTypeKovio() {
        byte[] sampleId = "sampleId".getBytes();
        when(mMockTag.getId()).thenReturn(sampleId);

        assertEquals(sampleId, mNfcBarcode.getBarcode());
        verify(mMockTag).getId();
    }

    @Test
    public void testGetBarCodeTypeUnknown() throws RemoteException {
        when(mMockBundle.getInt(EXTRA_BARCODE_TYPE)).thenReturn(TYPE_UNKNOWN);
        when(mMockTag.getTechExtras(TagTechnology.NFC_BARCODE)).thenReturn(mMockBundle);
        mNfcBarcode = new NfcBarcode(mMockTag);

        assertNull(mNfcBarcode.getBarcode());
        verify(mMockTag, never()).getId();
    }
}