Add safety check for potential NPE.

Check null before access views since findViewById returns a nullable object.

Bug: 261017156
Fix: 261017156
Test: refactoring CL. Existing unit tests still pass.

Change-Id: I5866a987b7a94122eb94f6aac7bb99fd6fa3cb1b
This commit is contained in:
David Liu
2023-03-28 18:41:40 +00:00
parent defcfa838f
commit a1c680f8f4
2 changed files with 72 additions and 17 deletions

View File

@@ -59,32 +59,36 @@ public class FooterPreference extends Preference {
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
TextView title = holder.itemView.findViewById(android.R.id.title);
if (!TextUtils.isEmpty(mContentDescription)) {
if (title != null && !TextUtils.isEmpty(mContentDescription)) {
title.setContentDescription(mContentDescription);
}
TextView learnMore = holder.itemView.findViewById(R.id.settingslib_learn_more);
if (learnMore != null && mLearnMoreListener != null) {
learnMore.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(mLearnMoreText)) {
mLearnMoreText = learnMore.getText();
if (learnMore != null) {
if (mLearnMoreListener != null) {
learnMore.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(mLearnMoreText)) {
mLearnMoreText = learnMore.getText();
} else {
learnMore.setText(mLearnMoreText);
}
SpannableString learnMoreText = new SpannableString(mLearnMoreText);
if (mLearnMoreSpan != null) {
learnMoreText.removeSpan(mLearnMoreSpan);
}
mLearnMoreSpan = new FooterLearnMoreSpan(mLearnMoreListener);
learnMoreText.setSpan(mLearnMoreSpan, 0,
learnMoreText.length(), 0);
learnMore.setText(learnMoreText);
} else {
learnMore.setText(mLearnMoreText);
learnMore.setVisibility(View.GONE);
}
SpannableString learnMoreText = new SpannableString(mLearnMoreText);
if (mLearnMoreSpan != null) {
learnMoreText.removeSpan(mLearnMoreSpan);
}
mLearnMoreSpan = new FooterLearnMoreSpan(mLearnMoreListener);
learnMoreText.setSpan(mLearnMoreSpan, 0,
learnMoreText.length(), 0);
learnMore.setText(learnMoreText);
} else {
learnMore.setVisibility(View.GONE);
}
View icon = holder.itemView.findViewById(R.id.icon_frame);
icon.setVisibility(mIconVisibility);
if (icon != null) {
icon.setVisibility(mIconVisibility);
}
}
@Override

View File

@@ -18,6 +18,9 @@ package com.android.settingslib.widget;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
@@ -87,4 +90,52 @@ public class FooterPreferenceTest {
assertThat(mFooterPreference.mIconVisibility).isEqualTo(View.GONE);
}
@Test
public void onBindViewHolder_whenTitleIsNull_shouldNotRaiseNpe() {
PreferenceViewHolder viewHolder = spy(PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_footer, null)));
when(viewHolder.findViewById(R.id.title)).thenReturn(null);
Throwable actualThrowable = null;
try {
mFooterPreference.onBindViewHolder(viewHolder);
} catch (Throwable throwable) {
actualThrowable = throwable;
}
assertThat(actualThrowable).isNull();
}
@Test
public void onBindViewHolder_whenLearnMoreIsNull_shouldNotRaiseNpe() {
PreferenceViewHolder viewHolder = spy(PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_footer, null)));
when(viewHolder.findViewById(R.id.settingslib_learn_more)).thenReturn(null);
Throwable actualThrowable = null;
try {
mFooterPreference.onBindViewHolder(viewHolder);
} catch (Throwable throwable) {
actualThrowable = throwable;
}
assertThat(actualThrowable).isNull();
}
@Test
public void onBindViewHolder_whenIconFrameIsNull_shouldNotRaiseNpe() {
PreferenceViewHolder viewHolder = spy(PreferenceViewHolder.createInstanceForTests(
LayoutInflater.from(mContext).inflate(R.layout.preference_footer, null)));
when(viewHolder.findViewById(R.id.icon_frame)).thenReturn(null);
Throwable actualThrowable = null;
try {
mFooterPreference.onBindViewHolder(viewHolder);
} catch (Throwable throwable) {
actualThrowable = throwable;
}
assertThat(actualThrowable).isNull();
}
}