diff --git a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java index 0996534b9d9..28475b6b5e3 100644 --- a/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java +++ b/src/com/android/settings/notification/ZenModeStarredContactsPreferenceController.java @@ -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 getStarredContacts() { + @VisibleForTesting + List getStarredContacts(Cursor cursor) { List 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 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; diff --git a/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java index 9a2bccd6b15..064c0911319 100644 --- a/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/notification/ZenModeStarredContactsPreferenceControllerTest.java @@ -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() { + 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 contacts = mMessagesController.getStarredContacts(testCursorWithNullValues); + for (int i = 0 ; i < contacts.size(); i++) { + assertThat(contacts.get(i)).isNotNull(); + } + } }