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

Commit 1ea2c8c9 authored by kai's avatar kai Committed by Kai
Browse files

UnitTest for pull phonebook

Test PhonebookPullRequest

Bug: 73498075
Test: runtest bluetooth
Change-Id: Ide815527ef8abf371730f3fdd875b8d3fb23e047
(cherry picked from commit 85252cf2b4c90de3454f4518909e46e8c000236c)
(cherry picked from commit 8164b5b5ad4ab9eb08a58d38ba8d9d8c63271bc6)
parent badfa948
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
BEGIN:VCARD
VERSION:3.0
FN:And Roid
N:And;Roid;;;
ORG:Open;Handset; Alliance
SORT-STRING:android
TEL;TYPE=PREF;TYPE=VOICE:0300000000
CLASS:PUBLIC
X-GNO:0
X-GN:group0
X-REDUCTION:0
REV:20081031T065854Z
END:VCARD
+42 −3
Original line number Diff line number Diff line
@@ -21,7 +21,9 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CallLog.Calls;
import android.provider.ContactsContract;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;
@@ -62,6 +64,7 @@ public class PbapParserTest {
            Assert.fail("Setup Failure Unable to get resources" + e.toString());
        }
        cleanupCallLog();
        cleanupPhonebook();
    }

    // testNoTimestamp should parse 1 poorly formed vcard and not crash.
@@ -75,7 +78,7 @@ public class PbapParserTest {
        Assert.assertEquals(1, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
                    new HashMap<>(), mAccount);
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.
@@ -97,7 +100,7 @@ public class PbapParserTest {
        Assert.assertEquals(1, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
                    new HashMap<>(), mAccount);
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.
@@ -118,7 +121,7 @@ public class PbapParserTest {
        Assert.assertEquals(2, pbapVCardList.getCount());
        CallLogPullRequest processor =
                new CallLogPullRequest(mTargetContext, PbapClientConnectionHandler.MCH_PATH,
                    new HashMap<>());
                    new HashMap<>(), mAccount);
        processor.setResults(pbapVCardList.getList());

        // Verify that these entries aren't in the call log to start.
@@ -131,10 +134,30 @@ public class PbapParserTest {
        Assert.assertTrue(verifyCallLog("", "1483232580000", "3"));
    }

    @Test
    public void testPullPhoneBook() throws IOException {
        InputStream fileStream;
        fileStream = mTestResources.openRawResource(
                com.android.bluetooth.tests.R.raw.v30_simple);
        BluetoothPbapVcardList pbapVCardList = new BluetoothPbapVcardList(mAccount, fileStream,
                PbapClientConnectionHandler.VCARD_TYPE_30);
        Assert.assertEquals(1, pbapVCardList.getCount());
        PhonebookPullRequest processor = new PhonebookPullRequest(mTargetContext, mAccount);
        processor.setResults(pbapVCardList.getList());
        Assert.assertFalse(verifyPhonebook("Roid And", "0300000000"));
        processor.onPullComplete();
        Assert.assertTrue(verifyPhonebook("Roid And", "0300000000"));
    }

    private void cleanupCallLog() {
        mTargetContext.getContentResolver().delete(Calls.CONTENT_URI, null, null);
    }

    private void cleanupPhonebook() {
        mTargetContext.getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI,
                null, null);
    }

    // Find Entries in call log with type matching number and date.
    // If number or date is null it will match any number or date respectively.
    private boolean verifyCallLog(String number, String date, String type) {
@@ -165,4 +188,20 @@ public class PbapParserTest {
        long dt = Long.valueOf(date) - tz.getRawOffset();
        return Long.toString(dt);
    }

    private boolean verifyPhonebook(String name, String number) {
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));
        Cursor c = mTargetContext.getContentResolver().query(uri, null, null, null);
        if (c != null && c.getCount() > 0) {
            c.moveToNext();
            String displayName = c.getString(
                    c.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
            if (displayName.equals(name)) {
                return true;
            }
        }
        return false;
    }

}