Merge "Fix NPE when calling getSuggestionV2 in adapter."
This commit is contained in:
committed by
Android (Google) Code Review
commit
10ba1f1ee7
@@ -220,6 +220,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;
|
||||||
@@ -230,6 +233,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;
|
||||||
|
@@ -203,7 +203,7 @@ public class SuggestionAdapterTest {
|
|||||||
public void onBindViewHolder_v2_itemViewShouldHandleClick()
|
public void onBindViewHolder_v2_itemViewShouldHandleClick()
|
||||||
throws PendingIntent.CanceledException {
|
throws PendingIntent.CanceledException {
|
||||||
final List<Suggestion> packages = makeSuggestionsV2("pkg1");
|
final List<Suggestion> packages = makeSuggestionsV2("pkg1");
|
||||||
setupSuggestions(mActivity, null /* suggestionV1 */ , packages);
|
setupSuggestions(mActivity, null /* suggestionV1 */, packages);
|
||||||
|
|
||||||
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
|
mSuggestionAdapter.onBindViewHolder(mSuggestionHolder, 0);
|
||||||
mSuggestionHolder.itemView.performClick();
|
mSuggestionHolder.itemView.performClick();
|
||||||
@@ -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