Fix improper dismiss of suggestion.

- when handling dismiss of suggestion card, need to check if the
dismissed suggestion can actually be removed from the current
suggestion list before trying to refresh the list.

Change-Id: Ia07b4cb76ef6c61a7d27184c3cba9d99fcc90bb3
Fixes: 76094746
Test: RunSettingsRoboTests
This commit is contained in:
Doris Ling
2018-05-04 11:09:34 -07:00
parent 0e197112e3
commit 1e9a979edd
2 changed files with 22 additions and 7 deletions

View File

@@ -146,15 +146,14 @@ public class DashboardAdapter extends RecyclerView.Adapter<DashboardAdapter.Dash
@Override @Override
public void onSuggestionClosed(Suggestion suggestion) { public void onSuggestionClosed(Suggestion suggestion) {
final List<Suggestion> list = mDashboardData.getSuggestions(); final List<Suggestion> list = mDashboardData.getSuggestions();
if (list == null || list.size() == 0) { if (list == null || list.size() == 0 || !list.remove(suggestion)) {
return; return;
} }
if (list.size() == 1) { if (list.isEmpty()) {
// The only suggestion is dismissed, and the the empty suggestion container will // The only suggestion is dismissed, and the the empty suggestion container will
// remain as the dashboard item. Need to refresh the dashboard list. // remain as the dashboard item. Need to refresh the dashboard list.
setSuggestions(null); setSuggestions(null);
} else { } else {
list.remove(suggestion);
setSuggestions(list); setSuggestions(list);
} }
} }

View File

@@ -101,7 +101,7 @@ public class DashboardAdapterTest {
} }
@Test @Test
public void testSuggestionDismissed_notOnlySuggestion_updateSuggestionOnly() { public void onSuggestionClosed_notOnlySuggestion_updateSuggestionOnly() {
final DashboardAdapter adapter = final DashboardAdapter adapter =
spy(new DashboardAdapter(mContext, null /* savedInstanceState */, spy(new DashboardAdapter(mContext, null /* savedInstanceState */,
null /* conditions */, null /* suggestionControllerMixin */, null /* conditions */, null /* suggestionControllerMixin */,
@@ -134,8 +134,8 @@ public class DashboardAdapterTest {
} }
@Test @Test
public void testSuggestionDismissed_onlySuggestion_updateDashboardData() { public void onSuggestionClosed_onlySuggestion_updateDashboardData() {
DashboardAdapter adapter = final DashboardAdapter adapter =
spy(new DashboardAdapter(mContext, null /* savedInstanceState */, spy(new DashboardAdapter(mContext, null /* savedInstanceState */,
null /* conditions */, null /* suggestionControllerMixin */, null /* conditions */, null /* suggestionControllerMixin */,
null /* lifecycle */)); null /* lifecycle */));
@@ -151,7 +151,23 @@ public class DashboardAdapterTest {
} }
@Test @Test
public void testBindSuggestion_shouldSetSuggestionAdapterAndNoCrash() { public void onSuggestionClosed_notInSuggestionList_shouldNotUpdateSuggestionList() {
final DashboardAdapter adapter =
spy(new DashboardAdapter(mContext, null /* savedInstanceState */,
null /* conditions */, null /* suggestionControllerMixin */,
null /* lifecycle */));
final List<Suggestion> suggestions = makeSuggestionsV2("pkg1");
adapter.setSuggestions(suggestions);
final DashboardData dashboardData = adapter.mDashboardData;
reset(adapter); // clear interactions tracking
adapter.onSuggestionClosed(mock(Suggestion.class));
verify(adapter, never()).setSuggestions(any());
}
@Test
public void onBindSuggestion_shouldSetSuggestionAdapterAndNoCrash() {
mDashboardAdapter = new DashboardAdapter(mContext, null /* savedInstanceState */, mDashboardAdapter = new DashboardAdapter(mContext, null /* savedInstanceState */,
null /* conditions */, null /* suggestionControllerMixin */, null /* lifecycle */); null /* conditions */, null /* suggestionControllerMixin */, null /* lifecycle */);
final List<Suggestion> suggestions = makeSuggestionsV2("pkg1"); final List<Suggestion> suggestions = makeSuggestionsV2("pkg1");