Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 85b00fa9 authored by David Liu's avatar David Liu Committed by Android (Google) Code Review
Browse files

Merge "Add safety check for potential NPE." into udc-dev

parents fe6fd1dc a1c680f8
Loading
Loading
Loading
Loading
+21 −17
Original line number Diff line number Diff line
@@ -59,12 +59,13 @@ 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) {
        if (learnMore != null) {
            if (mLearnMoreListener != null) {
                learnMore.setVisibility(View.VISIBLE);
                if (TextUtils.isEmpty(mLearnMoreText)) {
                    mLearnMoreText = learnMore.getText();
@@ -82,10 +83,13 @@ public class FooterPreference extends Preference {
            } else {
                learnMore.setVisibility(View.GONE);
            }
        }

        View icon = holder.itemView.findViewById(R.id.icon_frame);
        if (icon != null) {
            icon.setVisibility(mIconVisibility);
        }
    }

    @Override
    public void setSummary(CharSequence summary) {
+51 −0
Original line number Diff line number Diff line
@@ -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();
    }
}