PersonLookup { public @NonNull PersonRecord lookupPerson(final String address) { if (TextUtils.isEmpty(address)) { return new PersonRecord(0, null, null, "-1"); } else if (!personCache.containsKey(address)) { final Uri personUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address)); Cursor c = null; try { c = resolver.query(personUri, new String[] { PhoneLookup._ID, PhoneLookup.DISPLAY_NAME }, null, null, null); } catch (IllegalArgumentException e) { Log.wtf(TAG, "avoided a crash with address: " + address, e); } final PersonRecord record; if (c != null && c.moveToFirst()) { final long id = c.getLong(0); record = new PersonRecord( id, c.getString(1), getPrimaryEmail(id), address ); } else { if (LOCAL_LOGV) Log.v(TAG, "Looked up unknown address: " + address); record = new PersonRecord(0, null, null, address); } personCache.put(address, record); if (c != null) c.close(); } return personCache.get(address); } PersonLookup(ContentResolver resolver); @NonNull PersonRecord lookupPerson(final String address);  }
@Test public void shouldLookupUnknownPerson() throws Exception { when(resolver.query(any(Uri.class), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull())).thenReturn(null); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.isUnknown()).isTrue(); assertThat(record.getEmail()).isEqualTo("1234@unknown.email"); assertThat(record.getName()).isEqualTo("1234"); }
@Test public void shouldLookupExistingPerson() throws Exception { when(resolver.query( eq(Uri.parse("content: (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.isUnknown()).isFalse(); assertThat(record.getEmail()).isEqualTo("1234@unknown.email"); assertThat(record.getName()).isEqualTo("Testor Test"); }
@Test public void shouldLookupExistingPersonWithEmail() throws Exception { when(resolver.query( eq(Uri.parse("content: (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); when(resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Email.DATA }, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { String.valueOf(1) }, ContactsContract.CommonDataKinds.Email.IS_PRIMARY + " DESC")) .thenReturn(email("foo@test.com") ); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.getEmail()).isEqualTo("foo@test.com"); }
@Test public void shouldLookupExistingPersonUsingGmailAsPrimaryEmail() throws Exception { when(resolver.query( eq(Uri.parse("content: (String) isNull(), (String[]) isNull(), (String) isNull() )).thenReturn(name("Testor Test")); when(resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Email.DATA }, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { String.valueOf(1) }, ContactsContract.CommonDataKinds.Email.IS_PRIMARY + " DESC")) .thenReturn(email("foo@test.com", "foo@gmail.com") ); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record).isNotNull(); assertThat(record.getEmail()).isEqualTo("foo@gmail.com"); }
@Test public void shouldIgnoreIllegalArgumentException() { when(resolver.query(any(Uri.class), any(String[].class), (String) isNull(), (String[]) isNull(), (String) isNull())) .thenThrow(new IllegalArgumentException("column 'data1' does not exist")); PersonRecord record = lookup.lookupPerson("1234"); assertThat(record.isUnknown()).isTrue(); }