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

Commit 59b14b44 authored by Kihong Seong's avatar Kihong Seong
Browse files

Add tests for setter methods in BluetoothMapContent

Bug: 237467631
Test: atest BluetoothMapContentTest
Change-Id: Ia9c7d0cf0e7ccce73564404df6aed40ae3251fac
parent 9602cf6a
Loading
Loading
Loading
Loading
+22 −12
Original line number Original line Diff line number Diff line
@@ -69,7 +69,8 @@ public class BluetoothMapContent {


    // Parameter Mask for selection of parameters to return in listings
    // Parameter Mask for selection of parameters to return in listings
    private static final int MASK_SUBJECT = 0x00000001;
    private static final int MASK_SUBJECT = 0x00000001;
    private static final int MASK_DATETIME = 0x00000002;
    @VisibleForTesting
    static final int MASK_DATETIME = 0x00000002;
    private static final int MASK_SENDER_NAME = 0x00000004;
    private static final int MASK_SENDER_NAME = 0x00000004;
    private static final int MASK_SENDER_ADDRESSING = 0x00000008;
    private static final int MASK_SENDER_ADDRESSING = 0x00000008;
    private static final int MASK_RECIPIENT_NAME = 0x00000010;
    private static final int MASK_RECIPIENT_NAME = 0x00000010;
@@ -78,7 +79,8 @@ public class BluetoothMapContent {
    private static final int MASK_SIZE = 0x00000080;
    private static final int MASK_SIZE = 0x00000080;
    private static final int MASK_RECEPTION_STATUS = 0x00000100;
    private static final int MASK_RECEPTION_STATUS = 0x00000100;
    private static final int MASK_TEXT = 0x00000200;
    private static final int MASK_TEXT = 0x00000200;
    private static final int MASK_ATTACHMENT_SIZE = 0x00000400;
    @VisibleForTesting
    static final int MASK_ATTACHMENT_SIZE = 0x00000400;
    private static final int MASK_PRIORITY = 0x00000800;
    private static final int MASK_PRIORITY = 0x00000800;
    private static final int MASK_READ = 0x00001000;
    private static final int MASK_READ = 0x00001000;
    private static final int MASK_SENT = 0x00002000;
    private static final int MASK_SENT = 0x00002000;
@@ -86,7 +88,8 @@ public class BluetoothMapContent {
    // private static final int MASK_REPLYTO_ADDRESSING = 0x00008000;
    // private static final int MASK_REPLYTO_ADDRESSING = 0x00008000;
    // TODO: Duplicate in proposed spec
    // TODO: Duplicate in proposed spec
    // private static final int MASK_RECEPTION_STATE       = 0x00010000;
    // private static final int MASK_RECEPTION_STATE       = 0x00010000;
    private static final int MASK_DELIVERY_STATUS = 0x00010000;
    @VisibleForTesting
    static final int MASK_DELIVERY_STATUS = 0x00010000;
    private static final int MASK_CONVERSATION_ID = 0x00020000;
    private static final int MASK_CONVERSATION_ID = 0x00020000;
    private static final int MASK_CONVERSATION_NAME = 0x00040000;
    private static final int MASK_CONVERSATION_NAME = 0x00040000;
    // private static final int MASK_FOLDER_TYPE = 0x00100000;
    // private static final int MASK_FOLDER_TYPE = 0x00100000;
@@ -152,7 +155,8 @@ public class BluetoothMapContent {
    private String mMessageVersion = BluetoothMapUtils.MAP_V10_STR;
    private String mMessageVersion = BluetoothMapUtils.MAP_V10_STR;


    private int mRemoteFeatureMask = BluetoothMapUtils.MAP_FEATURE_DEFAULT_BITMASK;
    private int mRemoteFeatureMask = BluetoothMapUtils.MAP_FEATURE_DEFAULT_BITMASK;
    private int mMsgListingVersion = BluetoothMapUtils.MAP_MESSAGE_LISTING_FORMAT_V10;
    @VisibleForTesting
    int mMsgListingVersion = BluetoothMapUtils.MAP_MESSAGE_LISTING_FORMAT_V10;


    static final String[] SMS_PROJECTION = new String[]{
    static final String[] SMS_PROJECTION = new String[]{
            BaseColumns._ID,
            BaseColumns._ID,
@@ -578,7 +582,8 @@ public class BluetoothMapContent {
     * the total message size. To provide a more accurate attachment size, one could
     * the total message size. To provide a more accurate attachment size, one could
     * extract the length (in bytes) of the text parts.
     * extract the length (in bytes) of the text parts.
     */
     */
    private void setAttachment(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
    @VisibleForTesting
    void setAttachment(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
            BluetoothMapAppParams ap) {
            BluetoothMapAppParams ap) {
        if ((ap.getParameterMask() & MASK_ATTACHMENT_SIZE) != 0) {
        if ((ap.getParameterMask() & MASK_ATTACHMENT_SIZE) != 0) {
            int size = 0;
            int size = 0;
@@ -672,7 +677,8 @@ public class BluetoothMapContent {
        }
        }
    }
    }


    private void setDeliveryStatus(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
    @VisibleForTesting
    void setDeliveryStatus(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
            BluetoothMapAppParams ap) {
            BluetoothMapAppParams ap) {
        if ((ap.getParameterMask() & MASK_DELIVERY_STATUS) != 0) {
        if ((ap.getParameterMask() & MASK_DELIVERY_STATUS) != 0) {
            String deliveryStatus = "delivered";
            String deliveryStatus = "delivered";
@@ -1173,8 +1179,8 @@ public class BluetoothMapContent {
        }
        }
    }
    }



    @VisibleForTesting
    private void setDateTime(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
    void setDateTime(BluetoothMapMessageListingElement e, Cursor c, FilterInfo fi,
            BluetoothMapAppParams ap) {
            BluetoothMapAppParams ap) {
        if ((ap.getParameterMask() & MASK_DATETIME) != 0) {
        if ((ap.getParameterMask() & MASK_DATETIME) != 0) {
            long date = 0;
            long date = 0;
@@ -1931,7 +1937,8 @@ public class BluetoothMapContent {




    /* Used only for SMS/MMS */
    /* Used only for SMS/MMS */
    private void setConvoWhereFilterSmsMms(StringBuilder selection, FilterInfo fi,
    @VisibleForTesting
    void setConvoWhereFilterSmsMms(StringBuilder selection, FilterInfo fi,
            BluetoothMapAppParams ap) {
            BluetoothMapAppParams ap) {


        if (smsSelected(fi, ap) || mmsSelected(ap)) {
        if (smsSelected(fi, ap) || mmsSelected(ap)) {
@@ -1982,7 +1989,8 @@ public class BluetoothMapContent {
     *
     *
     * @return boolean true if sms is selected, false if not
     * @return boolean true if sms is selected, false if not
     */
     */
    private boolean smsSelected(FilterInfo fi, BluetoothMapAppParams ap) {
    @VisibleForTesting
    boolean smsSelected(FilterInfo fi, BluetoothMapAppParams ap) {
        int msgType = ap.getFilterMessageType();
        int msgType = ap.getFilterMessageType();
        int phoneType = fi.mPhoneType;
        int phoneType = fi.mPhoneType;


@@ -2018,7 +2026,8 @@ public class BluetoothMapContent {
     *
     *
     * @return boolean true if mms is selected, false if not
     * @return boolean true if mms is selected, false if not
     */
     */
    private boolean mmsSelected(BluetoothMapAppParams ap) {
    @VisibleForTesting
    boolean mmsSelected(BluetoothMapAppParams ap) {
        int msgType = ap.getFilterMessageType();
        int msgType = ap.getFilterMessageType();


        if (D) {
        if (D) {
@@ -2084,7 +2093,8 @@ public class BluetoothMapContent {
        return false;
        return false;
    }
    }


    private void setFilterInfo(FilterInfo fi) {
    @VisibleForTesting
    void setFilterInfo(FilterInfo fi) {
        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
        if (tm != null) {
        if (tm != null) {
            fi.mPhoneType = tm.getPhoneType();
            fi.mPhoneType = tm.getPhoneType();
+258 −6
Original line number Original line Diff line number Diff line
@@ -26,13 +26,17 @@ import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Context;
import android.database.Cursor;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract;
import android.provider.Telephony;
import android.provider.Telephony;
import android.provider.Telephony.Threads;
import android.telephony.TelephonyManager;


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


import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.BluetoothMethodProxy;
import com.android.bluetooth.SignedLongLong;
import com.android.bluetooth.map.BluetoothMapContent.FilterInfo;


import org.junit.After;
import org.junit.After;
import org.junit.Before;
import org.junit.Before;
@@ -46,22 +50,33 @@ import org.mockito.Spy;
public class BluetoothMapContentTest {
public class BluetoothMapContentTest {
    private static final String TEST_TEXT = "text";
    private static final String TEST_TEXT = "text";


    private Context mTargetContext;

    @Mock
    @Mock
    private BluetoothMapAccountItem mAccountItem;
    private BluetoothMapAccountItem mAccountItem;
    @Mock
    @Mock
    private BluetoothMapMasInstance mMasInstance;
    private BluetoothMapMasInstance mMasInstance;
    @Mock
    @Mock
    private Context mContext;
    @Mock
    private TelephonyManager mTelephonyManager;
    @Mock
    private ContentResolver mContentResolver;
    private ContentResolver mContentResolver;
    @Mock
    private BluetoothMapAppParams mParams;
    @Spy
    @Spy
    private BluetoothMethodProxy mMapMethodProxy = BluetoothMethodProxy.getInstance();
    private BluetoothMethodProxy mMapMethodProxy = BluetoothMethodProxy.getInstance();


    private BluetoothMapContent mContent;
    private FilterInfo mInfo;
    private BluetoothMapMessageListingElement mElement;

    @Before
    @Before
    public void setUp() {
    public void setUp() {
        mTargetContext = InstrumentationRegistry.getTargetContext();
        MockitoAnnotations.initMocks(this);
        MockitoAnnotations.initMocks(this);
        BluetoothMethodProxy.setInstanceForTesting(mMapMethodProxy);
        BluetoothMethodProxy.setInstanceForTesting(mMapMethodProxy);

        mContent = new BluetoothMapContent(mContext, mAccountItem, mMasInstance);
        mInfo = new FilterInfo();
        mElement = new BluetoothMapMessageListingElement();
    }
    }


    @After
    @After
@@ -71,7 +86,7 @@ public class BluetoothMapContentTest {


    @Test
    @Test
    public void constructor_withNonNullAccountItem() {
    public void constructor_withNonNullAccountItem() {
        BluetoothMapContent content = new BluetoothMapContent(mTargetContext, mAccountItem,
        BluetoothMapContent content = new BluetoothMapContent(mContext, mAccountItem,
                mMasInstance);
                mMasInstance);


        assertThat(content.mBaseUri).isNotNull();
        assertThat(content.mBaseUri).isNotNull();
@@ -79,7 +94,7 @@ public class BluetoothMapContentTest {


    @Test
    @Test
    public void constructor_withNullAccountItem() {
    public void constructor_withNullAccountItem() {
        BluetoothMapContent content = new BluetoothMapContent(mTargetContext, null, mMasInstance);
        BluetoothMapContent content = new BluetoothMapContent(mContext, null, mMasInstance);


        assertThat(content.mBaseUri).isNull();
        assertThat(content.mBaseUri).isNull();
    }
    }
@@ -144,4 +159,241 @@ public class BluetoothMapContentTest {
        assertThat(BluetoothMapContent.getAddressMms(mContentResolver, id, type)).isEqualTo(
        assertThat(BluetoothMapContent.getAddressMms(mContentResolver, id, type)).isEqualTo(
                TEST_TEXT);
                TEST_TEXT);
    }
    }

    @Test
    public void setAttachment_withTypeMms() {
        when(mParams.getParameterMask()).thenReturn(
                (long) BluetoothMapContent.MASK_ATTACHMENT_SIZE);
        mInfo.mMsgType = FilterInfo.TYPE_MMS;
        mInfo.mMmsColTextOnly = 0;
        mInfo.mMmsColAttachmentSize = 1;
        MatrixCursor cursor = new MatrixCursor(
                new String[]{"MmsColTextOnly", "MmsColAttachmentSize"});
        cursor.addRow(new Object[]{0, -1});
        cursor.moveToFirst();

        mContent.setAttachment(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getAttachmentSize()).isEqualTo(1);
    }

    @Test
    public void setAttachment_withTypeEmail() {
        when(mParams.getParameterMask()).thenReturn(
                (long) BluetoothMapContent.MASK_ATTACHMENT_SIZE);
        mInfo.mMsgType = FilterInfo.TYPE_EMAIL;
        mInfo.mMessageColAttachment = 0;
        mInfo.mMessageColAttachmentSize = 1;
        MatrixCursor cursor = new MatrixCursor(new String[]{"MessageColAttachment",
                "MessageColAttachmentSize"});
        cursor.addRow(new Object[]{1, 0});
        cursor.moveToFirst();

        mContent.setAttachment(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getAttachmentSize()).isEqualTo(1);
    }

    @Test
    public void setAttachment_withTypeIm() {
        int featureMask = 1 << 9;
        long parameterMask = 0x00100400;
        when(mParams.getParameterMask()).thenReturn(parameterMask);
        mInfo.mMsgType = FilterInfo.TYPE_IM;
        mInfo.mMessageColAttachment = 0;
        mInfo.mMessageColAttachmentSize = 1;
        mInfo.mMessageColAttachmentMime = 2;
        MatrixCursor cursor = new MatrixCursor(new String[]{"MessageColAttachment",
                "MessageColAttachmentSize",
                "MessageColAttachmentMime"});
        cursor.addRow(new Object[]{1, 0, "test_mime_type"});
        cursor.moveToFirst();

        mContent.setRemoteFeatureMask(featureMask);
        mContent.setAttachment(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getAttachmentSize()).isEqualTo(1);
        assertThat(mElement.getAttachmentMimeTypes()).isEqualTo("test_mime_type");
    }

    @Test
    public void setRemoteFeatureMask() {
        int featureMask = 1 << 9;

        mContent.setRemoteFeatureMask(featureMask);

        assertThat(mContent.getRemoteFeatureMask()).isEqualTo(featureMask);
        assertThat(mContent.mMsgListingVersion).isEqualTo(
                BluetoothMapUtils.MAP_MESSAGE_LISTING_FORMAT_V11);
    }

    @Test
    public void setConvoWhereFilterSmsMms() throws Exception {
        when(mParams.getFilterMessageType()).thenReturn(0);
        when(mParams.getFilterReadStatus()).thenReturn(0x03);
        long lastActivity = 1L;
        when(mParams.getFilterLastActivityBegin()).thenReturn(lastActivity);
        when(mParams.getFilterLastActivityEnd()).thenReturn(lastActivity);
        String convoId = "1111";
        when(mParams.getFilterConvoId()).thenReturn(SignedLongLong.fromString(convoId));
        StringBuilder selection = new StringBuilder();

        mContent.setConvoWhereFilterSmsMms(selection, mInfo, mParams);

        StringBuilder expected = new StringBuilder();
        expected.append(" AND ").append(Threads.READ).append(" = 0");
        expected.append(" AND ").append(Threads.READ).append(" = 1");
        expected.append(" AND ")
                .append(Threads.DATE)
                .append(" >= ")
                .append(lastActivity);
        expected.append(" AND ")
                .append(Threads.DATE)
                .append(" <= ")
                .append(lastActivity);
        expected.append(" AND ")
                .append(Threads._ID)
                .append(" = ")
                .append(SignedLongLong.fromString(convoId).getLeastSignificantBits());
        assertThat(selection.toString()).isEqualTo(expected.toString());
    }

    @Test
    public void setDateTime_withTypeSms() {
        when(mParams.getParameterMask()).thenReturn((long) BluetoothMapContent.MASK_DATETIME);
        mInfo.mMsgType = FilterInfo.TYPE_SMS;
        mInfo.mSmsColDate = 0;
        MatrixCursor cursor = new MatrixCursor(new String[]{"SmsColDate"});
        cursor.addRow(new Object[]{2L});
        cursor.moveToFirst();

        mContent.setDateTime(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getDateTime()).isEqualTo(2L);
    }

    @Test
    public void setDateTime_withTypeMms() {
        when(mParams.getParameterMask()).thenReturn((long) BluetoothMapContent.MASK_DATETIME);
        mInfo.mMsgType = FilterInfo.TYPE_MMS;
        mInfo.mMmsColDate = 0;
        MatrixCursor cursor = new MatrixCursor(new String[]{"MmsColDate"});
        cursor.addRow(new Object[]{2L});
        cursor.moveToFirst();

        mContent.setDateTime(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getDateTime()).isEqualTo(2L * 1000L);
    }

    @Test
    public void setDateTime_withTypeIM() {
        when(mParams.getParameterMask()).thenReturn((long) BluetoothMapContent.MASK_DATETIME);
        mInfo.mMsgType = FilterInfo.TYPE_IM;
        mInfo.mMessageColDate = 0;
        MatrixCursor cursor = new MatrixCursor(new String[]{"MessageColDate"});
        cursor.addRow(new Object[]{2L});
        cursor.moveToFirst();

        mContent.setDateTime(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getDateTime()).isEqualTo(2L);
    }

    @Test
    public void setDeliveryStatus() {
        when(mParams.getParameterMask()).thenReturn(
                (long) BluetoothMapContent.MASK_DELIVERY_STATUS);
        mInfo.mMsgType = FilterInfo.TYPE_EMAIL;
        mInfo.mMessageColDelivery = 0;
        MatrixCursor cursor = new MatrixCursor(new String[]{"MessageColDelivery"});
        cursor.addRow(new Object[]{"test_delivery_status"});
        cursor.moveToFirst();

        mContent.setDeliveryStatus(mElement, cursor, mInfo, mParams);

        assertThat(mElement.getDeliveryStatus()).isEqualTo("test_delivery_status");
    }

    @Test
    public void setFilterInfo() {
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getSystemServiceName(TelephonyManager.class))
                .thenReturn(Context.TELEPHONY_SERVICE);
        when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM);

        mContent.setFilterInfo(mInfo);

        assertThat(mInfo.mPhoneType).isEqualTo(TelephonyManager.PHONE_TYPE_GSM);
    }

    @Test
    public void smsSelected_withInvalidFilter() {
        when(mParams.getFilterMessageType()).thenReturn(
                BluetoothMapAppParams.INVALID_VALUE_PARAMETER);

        assertThat(mContent.smsSelected(mInfo, mParams)).isTrue();
    }

    @Test
    public void smsSelected_withNoFilter() {
        int noFilter = 0;
        when(mParams.getFilterMessageType()).thenReturn(noFilter);

        assertThat(mContent.smsSelected(mInfo, mParams)).isTrue();
    }

    @Test
    public void smsSelected_withSmsCdmaExcludeFilter_andPhoneTypeGsm() {
        when(mParams.getFilterMessageType()).thenReturn(BluetoothMapAppParams.FILTER_NO_SMS_CDMA);

        mInfo.mPhoneType = TelephonyManager.PHONE_TYPE_GSM;
        assertThat(mContent.smsSelected(mInfo, mParams)).isTrue();

        mInfo.mPhoneType = TelephonyManager.PHONE_TYPE_CDMA;
        assertThat(mContent.smsSelected(mInfo, mParams)).isFalse();
    }

    @Test
    public void smsSelected_witSmsGsmExcludeFilter_andPhoneTypeCdma() {
        when(mParams.getFilterMessageType()).thenReturn(BluetoothMapAppParams.FILTER_NO_SMS_GSM);

        mInfo.mPhoneType = TelephonyManager.PHONE_TYPE_CDMA;
        assertThat(mContent.smsSelected(mInfo, mParams)).isTrue();

        mInfo.mPhoneType = TelephonyManager.PHONE_TYPE_GSM;
        assertThat(mContent.smsSelected(mInfo, mParams)).isFalse();
    }

    @Test
    public void smsSelected_withGsmAndCdmaExcludeFilter() {
        int noSms =
                BluetoothMapAppParams.FILTER_NO_SMS_CDMA | BluetoothMapAppParams.FILTER_NO_SMS_GSM;
        when(mParams.getFilterMessageType()).thenReturn(noSms);

        assertThat(mContent.smsSelected(mInfo, mParams)).isFalse();
    }

    @Test
    public void mmsSelected_withInvalidFilter() {
        when(mParams.getFilterMessageType()).thenReturn(
                BluetoothMapAppParams.INVALID_VALUE_PARAMETER);

        assertThat(mContent.mmsSelected(mParams)).isTrue();
    }

    @Test
    public void mmsSelected_withNoFilter() {
        int noFilter = 0;
        when(mParams.getFilterMessageType()).thenReturn(noFilter);

        assertThat(mContent.mmsSelected(mParams)).isTrue();
    }

    @Test
    public void mmsSelected_withMmsExcludeFilter() {
        when(mParams.getFilterMessageType()).thenReturn(BluetoothMapAppParams.FILTER_NO_MMS);

        assertThat(mContent.mmsSelected(mParams)).isFalse();
    }
}
}
 No newline at end of file