public void testLoadContactReturnDirectoryContactWithoutDisplayName() throws JSONException {
    // Use lookup-style Uri that contains encoded json object which encapsulates the
    // directory contact. The test json object is:
    // {
    //   display_name_source": 40,
    //   "vnd.android.cursor.item\/contact":{"email":{"data1":"*****@*****.**" }}
    // }
    JSONObject itemJson = new JSONObject();
    itemJson.put("email", new JSONObject().put("data1", "*****@*****.**"));
    JSONObject json = new JSONObject();
    json.put(Contacts.NAME_RAW_CONTACT_ID, CONTACT_ID);
    json.put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME);
    json.put(Contacts.CONTENT_ITEM_TYPE, itemJson);

    final Uri lookupUri =
        Contacts.CONTENT_LOOKUP_URI
            .buildUpon()
            .encodedFragment(json.toString())
            .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, "1")
            .appendPath(Constants.LOOKUP_URI_ENCODED)
            .build();

    mContactsProvider.expectTypeQuery(lookupUri, Contacts.CONTENT_ITEM_TYPE);
    Contact contact = assertLoadContact(lookupUri);

    assertEquals(-1, contact.getId());
    assertEquals(-1, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals("", contact.getDisplayName());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().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();
  }
  @Override
  public void deliverResult(Contact result) {
    unregisterObserver();

    // The creator isn't interested in any further updates
    if (isReset() || result == null) {
      return;
    }

    mContact = result;

    if (result.isLoaded()) {
      mLookupUri = result.getLookupUri();

      if (!result.isDirectoryEntry()) {
        Log.i(TAG, "Registering content observer for " + mLookupUri);
        if (mObserver == null) {
          mObserver = new ForceLoadContentObserver();
        }
        getContext().getContentResolver().registerContentObserver(mLookupUri, true, mObserver);
      }

      if (mPostViewNotification) {
        // inform the source of the data that this contact is being looked at
        postViewNotificationToSyncAdapter();
      }
    }

    super.deliverResult(mContact);
  }
  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();
  }
  @Override
  public Contact loadInBackground() {
    try {
      final ContentResolver resolver = getContext().getContentResolver();
      final Uri uriCurrentFormat = ContactLoaderUtils.ensureIsContactUri(resolver, mLookupUri);
      final Contact cachedResult = sCachedResult;
      sCachedResult = null;
      // Is this the same Uri as what we had before already? In that case, reuse that result
      final Contact result;
      final boolean resultIsCached;
      if (cachedResult != null && UriUtils.areEqual(cachedResult.getLookupUri(), mLookupUri)) {
        // We are using a cached result from earlier. Below, we should make sure
        // we are not doing any more network or disc accesses
        result = new Contact(mRequestedUri, cachedResult);
        resultIsCached = true;
      } else {
        if (uriCurrentFormat.getLastPathSegment().equals(Constants.LOOKUP_URI_ENCODED)) {
          result = loadEncodedContactEntity(uriCurrentFormat);
        } else {
          result = loadContactEntity(resolver, uriCurrentFormat);
        }
        resultIsCached = false;
      }
      if (result.isLoaded()) {
        if (result.isDirectoryEntry()) {
          if (!resultIsCached) {
            loadDirectoryMetaData(result);
          }
        } else if (mLoadGroupMetaData) {
          if (result.getGroupMetaData() == null) {
            loadGroupMetaData(result);
          }
        }
        if (mComputeFormattedPhoneNumber) {
          computeFormattedPhoneNumbers(result);
        }
        if (!resultIsCached) loadPhotoBinaryData(result);

        // Note ME profile should never have "Add connection"
        if (mLoadInvitableAccountTypes && result.getInvitableAccountTypes() == null) {
          loadInvitableAccountTypes(result);
        }
      }
      return result;
    } catch (Exception e) {
      Log.e(TAG, "Error loading the contact: " + mLookupUri, e);
      return Contact.forError(mRequestedUri, e);
    }
  }