From 97da51876b67942fd412355fa8c6cac5b3f93eba Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Wed, 16 Aug 2017 12:40:01 -0700 Subject: [PATCH] Fix null pointer exception when logging suggestions. In monkey test, the suggestions list can become null. So, add check for valid suggestions list before trying to iterate through the suggestions. Fixes: 64757618 Test: make RunSettingsRoboTests Change-Id: Ib670808a4f222187b9cd53d7d939e71b5ce8dbae --- .../settings/dashboard/DashboardAdapter.java | 6 +++++- .../settings/dashboard/DashboardAdapterTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java index 2bf1c01f144..c06a58fe24c 100644 --- a/src/com/android/settings/dashboard/DashboardAdapter.java +++ b/src/com/android/settings/dashboard/DashboardAdapter.java @@ -329,7 +329,11 @@ public class DashboardAdapter extends RecyclerView.Adapter suggestions = mDashboardData.getSuggestions(); + if (suggestions == null) { + return; + } + for (Tile suggestion : suggestions) { final String suggestionId = mSuggestionFeatureProvider.getSuggestionIdentifier( mContext, suggestion); if (!mSuggestionsShownLogged.contains(suggestionId)) { diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java index 6b56c291324..e00908e52b3 100644 --- a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java @@ -318,6 +318,21 @@ public class DashboardAdapterTest { assertThat(mActionCategoryCaptor.getAllValues().toArray()).isEqualTo(expectedActions); } + @Test + public void testSuggestionsLogs_nullSuggestionsList_shouldNotCrash() { + setupSuggestions(makeSuggestions("pkg1", "pkg2", "pkg3", "pkg4", "pkg5")); + mDashboardAdapter.onBindSuggestionConditionHeader(mSuggestionHolder, mSuggestionHeaderData); + + // set suggestions to null + final DashboardData prevData = mDashboardAdapter.mDashboardData; + mDashboardAdapter.mDashboardData = new DashboardData.Builder(prevData) + .setSuggestions(null) + .build(); + + mSuggestionHolder.itemView.callOnClick(); + // no crash + } + @Test public void testSuggestionDismissed_notOnlySuggestion_updateSuggestionOnly() { final DashboardAdapter adapter =