public void testLoadContactWithContactIdUri() {
    // Use content Uris that only contain the ID
    final long contactId = 1;
    final long rawContactId = 11;
    final long dataId = 21;

    final String lookupKey = "aa%12%@!";
    final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
    final Uri entityUri = Uri.withAppendedPath(baseUri, Contacts.Entity.CONTENT_DIRECTORY);
    final Uri lookupUri =
        ContentUris.withAppendedId(
            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), contactId);

    ContactQueries queries = new ContactQueries();
    mContactsProvider.expectTypeQuery(baseUri, Contacts.CONTENT_ITEM_TYPE);
    queries.fetchAllData(entityUri, contactId, rawContactId, dataId, lookupKey);

    Contact contact = assertLoadContact(baseUri);

    assertEquals(contactId, contact.getId());
    assertEquals(rawContactId, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals(lookupKey, contact.getLookupKey());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    assertEquals(1, contact.getStatuses().size());
    mContactsProvider.verify();
  }
  public void testLoadContactWithContactLookupWithIncorrectIdUri() {
    // Use lookup-style Uris that contain incorrect Contact-ID
    // (we want to ensure that still the correct contact is chosen)
    final long wrongContactId = 2;
    final long wrongRawContactId = 12;

    final String wrongLookupKey = "ab%12%@!";
    final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, CONTACT_ID);
    final Uri wrongBaseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, wrongContactId);
    final Uri lookupUri =
        ContentUris.withAppendedId(
            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), CONTACT_ID);
    final Uri lookupWithWrongIdUri =
        ContentUris.withAppendedId(
            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), wrongContactId);
    final Uri entityUri =
        Uri.withAppendedPath(lookupWithWrongIdUri, Contacts.Entity.CONTENT_DIRECTORY);

    ContactQueries queries = new ContactQueries();
    mContactsProvider.expectTypeQuery(lookupWithWrongIdUri, Contacts.CONTENT_ITEM_TYPE);
    queries.fetchAllData(entityUri, CONTACT_ID, RAW_CONTACT_ID, DATA_ID, LOOKUP_KEY);

    Contact contact = assertLoadContact(lookupWithWrongIdUri);

    assertEquals(CONTACT_ID, contact.getId());
    assertEquals(RAW_CONTACT_ID, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals(LOOKUP_KEY, contact.getLookupKey());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    assertEquals(1, contact.getStatuses().size());

    mContactsProvider.verify();
  }
  public void testLoadContactWithOldStyleUri() {
    // Use content Uris that only contain the ID but use the format used in Donut
    final long contactId = 1;
    final long rawContactId = 11;
    final long dataId = 21;

    final String lookupKey = "aa%12%@!";
    final Uri legacyUri = ContentUris.withAppendedId(Uri.parse("content://contacts"), rawContactId);
    final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
    final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
    final Uri lookupUri =
        ContentUris.withAppendedId(
            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), contactId);
    final Uri entityUri = Uri.withAppendedPath(lookupUri, Contacts.Entity.CONTENT_DIRECTORY);

    ContactQueries queries = new ContactQueries();
    queries.fetchContactIdAndLookupFromRawContactUri(rawContactUri, contactId, lookupKey);
    queries.fetchAllData(entityUri, contactId, rawContactId, dataId, lookupKey);

    Contact contact = assertLoadContact(legacyUri);

    assertEquals(contactId, contact.getId());
    assertEquals(rawContactId, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals(lookupKey, contact.getLookupKey());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    assertEquals(1, contact.getStatuses().size());
    mContactsProvider.verify();
  }
  public void testLoadContactWithRawContactIdUri() {
    // Use content Uris that only contain the ID but use the format used in Donut
    final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, RAW_CONTACT_ID);
    final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, CONTACT_ID);
    final Uri lookupUri =
        ContentUris.withAppendedId(
            Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), CONTACT_ID);
    final Uri entityUri = Uri.withAppendedPath(lookupUri, Contacts.Entity.CONTENT_DIRECTORY);

    ContactQueries queries = new ContactQueries();
    mContactsProvider.expectTypeQuery(rawContactUri, RawContacts.CONTENT_ITEM_TYPE);
    queries.fetchContactIdAndLookupFromRawContactUri(rawContactUri, CONTACT_ID, LOOKUP_KEY);
    queries.fetchAllData(entityUri, CONTACT_ID, RAW_CONTACT_ID, DATA_ID, LOOKUP_KEY);

    Contact contact = assertLoadContact(rawContactUri);

    assertEquals(CONTACT_ID, contact.getId());
    assertEquals(RAW_CONTACT_ID, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals(LOOKUP_KEY, contact.getLookupKey());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    assertEquals(1, contact.getStatuses().size());
    mContactsProvider.verify();
  }