Fix null pointer in StarredContactsPrefController
Change-Id: I9b2209825b30a6cc8dfe355632201b8082572dbb Fixes: 80084826 Test: ZenModeStarredContactsPreferenceControllerTest.java
This commit is contained in:
@@ -117,6 +117,7 @@ public class ZenModeStarredContactsPreferenceController extends
|
||||
}
|
||||
}
|
||||
|
||||
// values in displayContacts must not be null
|
||||
mPreference.setSummary(ListFormatter.getInstance().format(displayContacts));
|
||||
}
|
||||
|
||||
@@ -130,22 +131,32 @@ public class ZenModeStarredContactsPreferenceController extends
|
||||
return true;
|
||||
}
|
||||
|
||||
private List<String> getStarredContacts() {
|
||||
@VisibleForTesting
|
||||
List<String> getStarredContacts(Cursor cursor) {
|
||||
List<String> starredContacts = new ArrayList<>();
|
||||
|
||||
Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
|
||||
ContactsContract.Data.STARRED + "=1", null,
|
||||
ContactsContract.Data.TIMES_CONTACTED);
|
||||
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
starredContacts.add(cursor.getString(0));
|
||||
String contact = cursor.getString(0);
|
||||
if (contact != null) {
|
||||
starredContacts.add(contact);
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
return starredContacts;
|
||||
}
|
||||
|
||||
private List<String> getStarredContacts() {
|
||||
return getStarredContacts(queryData());
|
||||
}
|
||||
|
||||
private Cursor queryData() {
|
||||
return mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
|
||||
ContactsContract.Data.STARRED + "=1", null,
|
||||
ContactsContract.Data.TIMES_CONTACTED);
|
||||
}
|
||||
|
||||
private boolean isIntentValid() {
|
||||
return mStarredContactsIntent.resolveActivity(mPackageManager) != null
|
||||
|| mFallbackIntent.resolveActivity(mPackageManager) != null;
|
||||
|
@@ -23,12 +23,25 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.CharArrayBuffer;
|
||||
import android.database.ContentObserver;
|
||||
import android.database.Cursor;
|
||||
import android.database.DataSetObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@@ -42,6 +55,8 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.shadows.ShadowApplication;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class ZenModeStarredContactsPreferenceControllerTest {
|
||||
@@ -148,4 +163,31 @@ public class ZenModeStarredContactsPreferenceControllerTest {
|
||||
|
||||
assertThat(mMessagesController.isAvailable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateSummary_nullCursorValues() {
|
||||
Cursor testCursorWithNullValues = mock(Cursor.class);
|
||||
when(testCursorWithNullValues.moveToFirst()).thenReturn(true);
|
||||
|
||||
doAnswer(new Answer<Boolean>() {
|
||||
int count = 0;
|
||||
@Override
|
||||
public Boolean answer(InvocationOnMock invocation) throws Throwable {
|
||||
if (count < 3) {
|
||||
count++;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}).when(testCursorWithNullValues).moveToNext();
|
||||
|
||||
when(testCursorWithNullValues.getString(0)).thenReturn(null);
|
||||
|
||||
// expected - no null values
|
||||
List<String> contacts = mMessagesController.getStarredContacts(testCursorWithNullValues);
|
||||
for (int i = 0 ; i < contacts.size(); i++) {
|
||||
assertThat(contacts.get(i)).isNotNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user