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

Commit 85065d08 authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Add tests for BluetoothPbapSimVcardManager (2/2)

Bug: 237548430
Test: atest BluetoothPbapSimVcardManagerTest
Ignore-AOSP-First: Merging into internal branches first
        in order to resolve merge conflict
Change-Id: If90c5b7767011ea96300ffd33349d1e342d16cf8
Merged-In: If90c5b7767011ea96300ffd33349d1e342d16cf8
parent c7ec5716
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -1307,8 +1307,8 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
                        appParamValue.ignorefilter ? null : appParamValue.propertySelector);
                return pushBytes(op, ownerVcard);
            } else {
                return mVcardSimManager.composeAndSendSIMPhonebookOneVcard(op, intIndex,
                        vcard21, null, mOrderBy);
                return BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookOneVcard(
                        mContext, op, intIndex, vcard21, null, mOrderBy);
            }
        } else {
            if (intIndex <= 0 || intIndex > size) {
@@ -1425,12 +1425,12 @@ public class BluetoothPbapObexServer extends ServerRequestHandler {
                if (endPoint == 0) {
                    return pushBytes(op, ownerVcard);
                } else {
                    return mVcardSimManager.composeAndSendSIMPhonebookVcards(op, 1, endPoint,
                        vcard21, ownerVcard);
                    return BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(
                            mContext, op, 1, endPoint, vcard21, ownerVcard);
                }
            } else {
                return mVcardSimManager.composeAndSendSIMPhonebookVcards(op, startPoint,
                        endPoint, vcard21, null);
                return BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(
                        mContext, op, startPoint, endPoint, vcard21, null);
            }
        } else {
            return mVcardManager.composeAndSendSelectedCallLogVcards(appParamValue.needTag, op,
+5 −1
Original line number Diff line number Diff line
@@ -43,7 +43,6 @@ import android.bluetooth.BluetoothSocket;
import android.bluetooth.IBluetoothPbap;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -555,6 +554,11 @@ public class BluetoothPbapService extends ProfileService implements IObexConnect
        return sLocalPhoneNum;
    }

    @VisibleForTesting
    static void setLocalPhoneName(String localPhoneName) {
        sLocalPhoneName = localPhoneName;
    }

    static String getLocalPhoneName() {
        return sLocalPhoneName;
    }
+11 −10
Original line number Diff line number Diff line
@@ -92,8 +92,10 @@ public class BluetoothPbapSimVcardManager {
        CommonDataKinds.Phone.LABEL
    };

    private static final int NAME_COLUMN_INDEX = 0;
    private static final int NUMBER_COLUMN_INDEX = 1;
    @VisibleForTesting
    public static final int NAME_COLUMN_INDEX = 0;
    @VisibleForTesting
    public static final int NUMBER_COLUMN_INDEX = 1;
    private static final int NUMBERTYPE_COLUMN_INDEX = 2;
    private static final int NUMBERLABEL_COLUMN_INDEX = 3;

@@ -377,7 +379,7 @@ public class BluetoothPbapSimVcardManager {
        return nameList;
    }

    public final int composeAndSendSIMPhonebookVcards(Operation op,
    public static final int composeAndSendSIMPhonebookVcards(Context context, Operation op,
            final int startPoint, final int endPoint, final boolean vcardType21,
            String ownerVCard) {
        if (startPoint < 1 || startPoint > endPoint) {
@@ -387,10 +389,10 @@ public class BluetoothPbapSimVcardManager {
        BluetoothPbapSimVcardManager composer = null;
        HandlerForStringBuffer buffer = null;
        try {
            composer = new BluetoothPbapSimVcardManager(mContext);
            composer = new BluetoothPbapSimVcardManager(context);
            buffer = new HandlerForStringBuffer(op, ownerVCard);

            if (!composer.init(SIM_URI, null, null, null) || !buffer.onInit(mContext)) {
            if (!composer.init(SIM_URI, null, null, null) || !buffer.onInit(context)) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            composer.moveToPosition(startPoint -1, false);
@@ -422,7 +424,7 @@ public class BluetoothPbapSimVcardManager {
    /**
     * Handler to emit vCards to PCE.
     */
    public class HandlerForStringBuffer {
    public static class HandlerForStringBuffer {
        private Operation operation;

        private OutputStream outputStream;
@@ -475,7 +477,7 @@ public class BluetoothPbapSimVcardManager {
        }
    }

    public final int composeAndSendSIMPhonebookOneVcard(Operation op,
    public static final int composeAndSendSIMPhonebookOneVcard(Context context, Operation op,
            final int offset, final boolean vcardType21, String ownerVCard,
            int orderByWhat) {
        if (offset < 1) {
@@ -486,10 +488,9 @@ public class BluetoothPbapSimVcardManager {
        BluetoothPbapSimVcardManager composer = null;
        HandlerForStringBuffer buffer = null;
        try {
            composer = new BluetoothPbapSimVcardManager(mContext);
            composer = new BluetoothPbapSimVcardManager(context);
            buffer = new HandlerForStringBuffer(op, ownerVCard);
            if (!composer.init(SIM_URI, null, null,null)||
                               !buffer.onInit(mContext)) {
            if (!composer.init(SIM_URI, null, null, null) || !buffer.onInit(context)) {
                return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
            }
            if (orderByWhat == BluetoothPbapObexServer.ORDER_BY_INDEXED) {
+205 −20
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@ import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -32,15 +30,11 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.UserManager;
import android.util.Log;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.obex.HeaderSet;
import com.android.obex.Operation;
import com.android.obex.ResponseCodes;

@@ -48,18 +42,14 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.android.bluetooth.pbap.BluetoothPbapObexServer.AppParamValue;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@@ -78,7 +68,7 @@ public class BluetoothPbapSimVcardManagerTest {
    private static final Uri WRONG_URI = Uri.parse("content://some/wrong/uri");

    @Before
    public void setUp() throws Exception {
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        BluetoothPbapMethodProxy.setInstanceForTesting(mPbapMethodProxy);
        mContext =  InstrumentationRegistry.getTargetContext();
@@ -86,12 +76,12 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @After
    public void tearDown() throws Exception {
    public void tearDown() {
        BluetoothPbapMethodProxy.setInstanceForTesting(null);
    }

    @Test
    public void testInit_whenUriIsUnsupported() throws Exception {
    public void testInit_whenUriIsUnsupported() {
        assertThat(mManager.init(WRONG_URI, null, null, null))
                .isFalse();
        assertThat(mManager.getErrorReason())
@@ -99,7 +89,7 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @Test
    public void testInit_whenCursorIsNull() throws Exception {
    public void testInit_whenCursorIsNull() {
        doReturn(null).when(mPbapMethodProxy)
                .contentResolverQuery(any(), any(), any(), any(), any(), any());

@@ -110,7 +100,7 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @Test
    public void testInit_whenCursorHasNoEntry() throws Exception {
    public void testInit_whenCursorHasNoEntry() {
        Cursor cursor = mock(Cursor.class);
        when(cursor.getCount()).thenReturn(0);
        doReturn(cursor).when(mPbapMethodProxy)
@@ -124,7 +114,7 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @Test
    public void testInit_success() throws Exception {
    public void testInit_success() {
        Cursor cursor = mock(Cursor.class);
        when(cursor.getCount()).thenReturn(1);
        when(cursor.moveToFirst()).thenReturn(true);
@@ -137,14 +127,14 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @Test
    public void testCreateOneEntry_whenNotInitialized() throws Exception {
    public void testCreateOneEntry_whenNotInitialized() {
        assertThat(mManager.createOneEntry(true)).isNull();
        assertThat(mManager.getErrorReason())
                .isEqualTo(BluetoothPbapSimVcardManager.FAILURE_REASON_NOT_INITIALIZED);
    }

    @Test
    public void testCreateOneEntry_success() throws Exception {
    public void testCreateOneEntry_success() {
        Cursor cursor = initManager();

        assertThat(mManager.createOneEntry(true)).isNotNull();
@@ -153,7 +143,7 @@ public class BluetoothPbapSimVcardManagerTest {
    }

    @Test
    public void testTerminate() throws Exception {
    public void testTerminate() {
        Cursor cursor = initManager();
        mManager.terminate();

@@ -247,6 +237,201 @@ public class BluetoothPbapSimVcardManagerTest {
        verify(cursor).close();
    }

    @Test
    public void testGetSIMPhonebookNameList_orderByIndexed() {
        final String localPhoneName = "test_local_phone_name";
        Cursor cursor = initManager();
        List<String> nameList = Arrays.asList("D", "C", "A", "B");

        // Implement Cursor iteration
        final int size = nameList.size();
        AtomicInteger currentPosition = new AtomicInteger(0);
        when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
            currentPosition.set(0);
            return true;
        });
        when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
            return currentPosition.get() >= size;
        });
        when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
            currentPosition.getAndAdd(1);
            return true;
        });
        when(cursor.getString(anyInt())).then((Answer<String>) i -> {
            return nameList.get(currentPosition.get());
        });

        ArrayList<String> result = mManager.getSIMPhonebookNameList(
                BluetoothPbapObexServer.ORDER_BY_INDEXED);

        ArrayList<String> expectedResult = new ArrayList<>();
        expectedResult.add(localPhoneName);
        expectedResult.addAll(nameList);

        assertThat(result).isEqualTo(expectedResult);
    }

    @Test
    public void testGetSIMPhonebookNameList_orderByAlphabet() {
        final String localPhoneName = "test_local_phone_name";
        BluetoothPbapService.setLocalPhoneName(localPhoneName);
        Cursor cursor = initManager();
        List<String> nameList = Arrays.asList("D", "C", "A", "B");

        // Implement Cursor iteration
        final int size = nameList.size();
        AtomicInteger currentPosition = new AtomicInteger(0);
        when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
            currentPosition.set(0);
            return true;
        });
        when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
            return currentPosition.get() >= size;
        });
        when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
            currentPosition.getAndAdd(1);
            return true;
        });
        when(cursor.getString(anyInt())).then((Answer<String>) i -> {
            return nameList.get(currentPosition.get());
        });

        List<String> result = mManager.getSIMPhonebookNameList(
                BluetoothPbapObexServer.ORDER_BY_ALPHABETICAL);

        List<String> expectedResult = new ArrayList<>(nameList);
        Collections.sort(expectedResult, String.CASE_INSENSITIVE_ORDER);
        expectedResult.add(0, localPhoneName);

        assertThat(result).isEqualTo(expectedResult);
    }

    @Test
    public void testGetSIMContactNamesByNumber() {
        Cursor cursor = initManager();
        List<String> nameList = Arrays.asList("A", "B", "C", "D");
        List<String> numberList = Arrays.asList(
                "000123456789",
                "123456789000",
                "000111111000",
                "123456789123");
        final String query = "000";

        // Implement Cursor iteration
        final int size = nameList.size();
        AtomicInteger currentPosition = new AtomicInteger(0);
        when(cursor.moveToFirst()).then((Answer<Boolean>) i -> {
            currentPosition.set(0);
            return true;
        });
        when(cursor.isAfterLast()).then((Answer<Boolean>) i -> {
            return currentPosition.get() >= size;
        });
        when(cursor.moveToNext()).then((Answer<Boolean>) i -> {
            currentPosition.getAndAdd(1);
            return true;
        });
        when(cursor.getString(BluetoothPbapSimVcardManager.NAME_COLUMN_INDEX)).then(
                (Answer<String>) i -> {
                    return nameList.get(currentPosition.get());
                });
        when(cursor.getString(BluetoothPbapSimVcardManager.NUMBER_COLUMN_INDEX)).then(
                (Answer<String>) i -> {
                    return numberList.get(currentPosition.get());
                });

        // Find the names whose number ends with 'query', and then
        // also the names whose number starts with 'query'.
        List<String> result = mManager.getSIMContactNamesByNumber(query);
        List<String> expectedResult = Arrays.asList("B", "C", "A");
        assertThat(result).isEqualTo(expectedResult);
    }

    @Test
    public void testComposeAndSendSIMPhonebookVcards_whenStartPointIsNotCorrect() {
        Operation operation = mock(Operation.class);
        final int incorrectStartPoint = 0; // Should be greater than zero

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(mContext,
                operation, incorrectStartPoint, 0, /*vcardType21=*/false, /*ownerVCard=*/null);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR);
    }

    @Test
    public void testComposeAndSendSIMPhonebookVcards_whenEndPointIsLessThanStartpoint() {
        Operation operation = mock(Operation.class);
        final int startPoint = 1;
        final int endPoint = 0; // Should be equal or greater than startPoint

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(mContext,
                operation, startPoint, endPoint, /*vcardType21=*/false, /*ownerVCard=*/null);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR);
    }

    @Test
    public void testComposeAndSendSIMPhonebookVcards_whenCursorInitFailed() {
        Operation operation = mock(Operation.class);
        final int startPoint = 1;
        final int endPoint = 1;
        doReturn(null).when(mPbapMethodProxy)
                .contentResolverQuery(any(), any(), any(), any(), any(), any());

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(mContext,
                operation, startPoint, endPoint, /*vcardType21=*/false, /*ownerVCard=*/null);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR);
    }

    @Test
    public void testComposeAndSendSIMPhonebookVcards_success() throws Exception {
        Cursor cursor = mock(Cursor.class);
        when(cursor.getCount()).thenReturn(10);
        when(cursor.moveToFirst()).thenReturn(true);
        when(cursor.isAfterLast()).thenReturn(false);
        doReturn(cursor).when(mPbapMethodProxy)
                .contentResolverQuery(any(), any(), any(), any(), any(), any());
        Operation operation = mock(Operation.class);
        OutputStream outputStream = mock(OutputStream.class);
        when(operation.openOutputStream()).thenReturn(outputStream);
        final int startPoint = 1;
        final int endPoint = 1;
        final String testOwnerVcard = "owner_v_card";

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookVcards(mContext,
                operation, startPoint, endPoint, /*vcardType21=*/false, testOwnerVcard);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_OK);
    }

    @Test
    public void testComposeAndSendSIMPhonebookOneVcard_whenOffsetIsNotCorrect() {
        Operation operation = mock(Operation.class);
        final int offset = 0; // Should be greater than zero

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookOneVcard(mContext,
                operation, offset, /*vcardType21=*/false, /*ownerVCard=*/null,
                BluetoothPbapObexServer.ORDER_BY_INDEXED);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR);
    }

    @Test
    public void testComposeAndSendSIMPhonebookOneVcard_success() throws Exception {
        Cursor cursor = mock(Cursor.class);
        when(cursor.getCount()).thenReturn(10);
        when(cursor.moveToFirst()).thenReturn(true);
        when(cursor.isAfterLast()).thenReturn(false);
        doReturn(cursor).when(mPbapMethodProxy)
                .contentResolverQuery(any(), any(), any(), any(), any(), any());
        Operation operation = mock(Operation.class);
        OutputStream outputStream = mock(OutputStream.class);
        when(operation.openOutputStream()).thenReturn(outputStream);
        final int offset = 1;
        final String testOwnerVcard = "owner_v_card";

        int result = BluetoothPbapSimVcardManager.composeAndSendSIMPhonebookOneVcard(mContext,
                operation, offset, /*vcardType21=*/false, testOwnerVcard,
                BluetoothPbapObexServer.ORDER_BY_INDEXED);
        assertThat(result).isEqualTo(ResponseCodes.OBEX_HTTP_OK);
    }

    private Cursor initManager() {
        Cursor cursor = mock(Cursor.class);
        when(cursor.getCount()).thenReturn(10);