Fix NPE when calling getSuggestionV2 in adapter.
Change-Id: I3aa498c2255107ca2e33046f3df629c2ef43ec5c Fixes: 66111737 Test: robotest
This commit is contained in:
@@ -222,6 +222,9 @@ public class SuggestionAdapter extends RecyclerView.Adapter<DashboardItemHolder>
|
|||||||
|
|
||||||
public Tile getSuggestion(int position) {
|
public Tile getSuggestion(int position) {
|
||||||
final long itemId = getItemId(position);
|
final long itemId = getItemId(position);
|
||||||
|
if (mSuggestions == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
for (Tile tile : mSuggestions) {
|
for (Tile tile : mSuggestions) {
|
||||||
if (Objects.hash(tile.title) == itemId) {
|
if (Objects.hash(tile.title) == itemId) {
|
||||||
return tile;
|
return tile;
|
||||||
@@ -232,6 +235,9 @@ public class SuggestionAdapter extends RecyclerView.Adapter<DashboardItemHolder>
|
|||||||
|
|
||||||
public Suggestion getSuggestionsV2(int position) {
|
public Suggestion getSuggestionsV2(int position) {
|
||||||
final long itemId = getItemId(position);
|
final long itemId = getItemId(position);
|
||||||
|
if (mSuggestionsV2 == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
for (Suggestion suggestion : mSuggestionsV2) {
|
for (Suggestion suggestion : mSuggestionsV2) {
|
||||||
if (Objects.hash(suggestion.getId()) == itemId) {
|
if (Objects.hash(suggestion.getId()) == itemId) {
|
||||||
return suggestion;
|
return suggestion;
|
||||||
|
@@ -233,6 +233,22 @@ public class SuggestionAdapterTest {
|
|||||||
assertThat(itemView.getChildCount()).isEqualTo(1);
|
assertThat(itemView.getChildCount()).isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getSuggestionsV2_shouldReturnSuggestionWhenMatch() {
|
||||||
|
final List<Suggestion> suggestionsV2 = makeSuggestionsV2("pkg1");
|
||||||
|
setupSuggestions(mActivity, null /* suggestionV1 */, suggestionsV2);
|
||||||
|
|
||||||
|
assertThat(mSuggestionAdapter.getSuggestion(0)).isNull();
|
||||||
|
assertThat(mSuggestionAdapter.getSuggestionsV2(0)).isNotNull();
|
||||||
|
|
||||||
|
List<Tile> suggestionsV1 = makeSuggestions("pkg1");
|
||||||
|
setupSuggestions(mActivity, suggestionsV1, null /* suggestionV2 */);
|
||||||
|
|
||||||
|
assertThat(mSuggestionAdapter.getSuggestionsV2(0)).isNull();
|
||||||
|
assertThat(mSuggestionAdapter.getSuggestion(0)).isNotNull();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private void setupSuggestions(Context context, List<Tile> suggestions,
|
private void setupSuggestions(Context context, List<Tile> suggestions,
|
||||||
List<Suggestion> suggestionsV2) {
|
List<Suggestion> suggestionsV2) {
|
||||||
mSuggestionAdapter = new SuggestionAdapter(context, suggestions, suggestionsV2,
|
mSuggestionAdapter = new SuggestionAdapter(context, suggestions, suggestionsV2,
|
||||||
|
Reference in New Issue
Block a user