diff --git a/src/com/android/settings/dashboard/DashboardAdapter.java b/src/com/android/settings/dashboard/DashboardAdapter.java index 490abc39bfa..59fef3c66fd 100644 --- a/src/com/android/settings/dashboard/DashboardAdapter.java +++ b/src/com/android/settings/dashboard/DashboardAdapter.java @@ -67,6 +67,8 @@ public class DashboardAdapter extends RecyclerView.Adapter suggestions = mDashboardData.getSuggestions(); - if (suggestions != null && suggestions.size() == 1) { + if (suggestions == null || suggestions.isEmpty()) { + return; + } + if (suggestions.size() == 1) { // The only suggestion is dismissed, and the the empty suggestion container will // remain as the dashboard item. Need to refresh the dashboard list. final DashboardData prevData = mDashboardData; @@ -204,6 +210,9 @@ public class DashboardAdapter extends RecyclerView.Adapter> { diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java index d78cff63ac9..a86c3c95305 100644 --- a/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/DashboardAdapterTest.java @@ -321,17 +321,33 @@ public class DashboardAdapterTest { } @Test - public void testSuggestionDismissed_notOnlySuggestion_doNothing() { + public void testSuggestionDismissed_notOnlySuggestion_updateSuggestionOnly() { final DashboardAdapter adapter = spy(new DashboardAdapter(mContext, null, null, null, null)); - adapter.setCategoriesAndSuggestions( - null /* category */, makeSuggestions("pkg1", "pkg2", "pkg3")); + final List suggestions = makeSuggestions("pkg1", "pkg2", "pkg3"); + adapter.setCategoriesAndSuggestions(null /* category */, suggestions); + + final RecyclerView data = mock(RecyclerView.class); + when(data.getResources()).thenReturn(mResources); + when(data.getContext()).thenReturn(mContext); + when(mResources.getDisplayMetrics()).thenReturn(mock(DisplayMetrics.class)); + final View itemView = mock(View.class); + when(itemView.findViewById(R.id.data)).thenReturn(data); + final DashboardAdapter.SuggestionAndConditionContainerHolder holder = + new DashboardAdapter.SuggestionAndConditionContainerHolder(itemView); + + adapter.onBindConditionAndSuggestion( + holder, DashboardAdapter.SUGGESTION_CONDITION_HEADER_POSITION); + final DashboardData dashboardData = adapter.mDashboardData; reset(adapter); // clear interactions tracking - adapter.onSuggestionDismissed(); + final Tile suggestionToRemove = suggestions.get(1); + adapter.onSuggestionDismissed(suggestionToRemove); assertThat(adapter.mDashboardData).isEqualTo(dashboardData); + assertThat(suggestions.size()).isEqualTo(2); + assertThat(suggestions.contains(suggestionToRemove)).isFalse(); verify(adapter, never()).notifyDashboardDataChanged(any()); } @@ -339,11 +355,12 @@ public class DashboardAdapterTest { public void testSuggestionDismissed_onlySuggestion_updateDashboardData() { DashboardAdapter adapter = spy(new DashboardAdapter(mContext, null, null, null, null)); - adapter.setCategoriesAndSuggestions(null /* category */, makeSuggestions("pkg1")); + final List suggestions = makeSuggestions("pkg1"); + adapter.setCategoriesAndSuggestions(null /* category */, suggestions); final DashboardData dashboardData = adapter.mDashboardData; reset(adapter); // clear interactions tracking - adapter.onSuggestionDismissed(); + adapter.onSuggestionDismissed(suggestions.get(0)); assertThat(adapter.mDashboardData).isNotEqualTo(dashboardData); verify(adapter).notifyDashboardDataChanged(any()); @@ -385,6 +402,16 @@ public class DashboardAdapterTest { verify(mockIcon).setTint(eq(0x89000000)); } + @Test + public void testSetCategoriesAndSuggestions_limitSuggestionSize() { + List packages = + makeSuggestions("pkg1", "pkg2", "pkg3", "pkg4", "pkg5", "pkg6", "pkg7"); + mDashboardAdapter.setCategoriesAndSuggestions(null /* category */, packages); + + assertThat(mDashboardAdapter.mDashboardData.getSuggestions().size()) + .isEqualTo(DashboardAdapter.MAX_SUGGESTION_TO_SHOW); + } + @Test public void testBindConditionAndSuggestion_shouldSetSuggestionAdapterAndNoCrash() { mDashboardAdapter = new DashboardAdapter(mContext, null, null, null, null); diff --git a/tests/robotests/src/com/android/settings/dashboard/DashboardSummaryTest.java b/tests/robotests/src/com/android/settings/dashboard/DashboardSummaryTest.java index 093d5a23361..967ed241a2a 100644 --- a/tests/robotests/src/com/android/settings/dashboard/DashboardSummaryTest.java +++ b/tests/robotests/src/com/android/settings/dashboard/DashboardSummaryTest.java @@ -118,9 +118,8 @@ public class DashboardSummaryTest { } @Test - public void onSuggestionDismissed_categoryShouldBeRefreshed() { - doNothing().when(mSummary).rebuildUI(); + public void onSuggestionDismissed_shouldNotRebuildUI() { mSummary.onSuggestionDismissed(mock(Tile.class)); - verify(mSummary).rebuildUI(); + verify(mSummary, never()).rebuildUI(); } } \ No newline at end of file