Fix "Play media to" popup close issue when screen rotate

- Avoid calling getListPreference() in onDialogClose() everytime.
  Due to exception in onDialogClose(). Move preference declaration into if condition.
- Add test case for crash case.

Bug: 119744621
Test: make -j50 RunSettingsRoboTests ROBOTEST_FILTER=UpdatableListPreferenceDialogFragmentTest
Change-Id: Ia20b8289a5863136ea74e02e32b537f6967474e8
This commit is contained in:
James Lu
2018-12-21 20:23:53 +08:00
parent 040124cca2
commit 7ce23b578a
2 changed files with 18 additions and 6 deletions

View File

@@ -84,8 +84,8 @@ public class UpdatableListPreferenceDialogFragment extends PreferenceDialogFragm
@Override @Override
public void onDialogClosed(boolean positiveResult) { public void onDialogClosed(boolean positiveResult) {
final ListPreference preference = getListPreference();
if (positiveResult && mClickedDialogEntryIndex >= 0) { if (positiveResult && mClickedDialogEntryIndex >= 0) {
final ListPreference preference = getListPreference();
final String value = mEntryValues[mClickedDialogEntryIndex].toString(); final String value = mEntryValues[mClickedDialogEntryIndex].toString();
if (preference.callChangeListener(value)) { if (preference.callChangeListener(value)) {
preference.setValue(value); preference.setValue(value);
@@ -144,7 +144,8 @@ public class UpdatableListPreferenceDialogFragment extends PreferenceDialogFragm
return mMetricsCategory; return mMetricsCategory;
} }
private ListPreference getListPreference() { @VisibleForTesting
ListPreference getListPreference() {
return (ListPreference) getPreference(); return (ListPreference) getPreference();
} }

View File

@@ -18,7 +18,9 @@ package com.android.settings.widget;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context; import android.content.Context;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@@ -31,6 +33,7 @@ import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
@@ -42,9 +45,10 @@ import java.util.ArrayList;
@Config(shadows = ShadowBluetoothUtils.class) @Config(shadows = ShadowBluetoothUtils.class)
public class UpdatableListPreferenceDialogFragmentTest { public class UpdatableListPreferenceDialogFragmentTest {
private Context mContext;
private UpdatableListPreferenceDialogFragment mUpdatableListPrefDlgFragment;
private static final String KEY = "Test_Key"; private static final String KEY = "Test_Key";
@Mock
private UpdatableListPreferenceDialogFragment mUpdatableListPrefDlgFragment;
private Context mContext;
private ArrayAdapter mAdapter; private ArrayAdapter mAdapter;
private ArrayList<CharSequence> mEntries; private ArrayList<CharSequence> mEntries;
@@ -53,8 +57,8 @@ public class UpdatableListPreferenceDialogFragmentTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mUpdatableListPrefDlgFragment = UpdatableListPreferenceDialogFragment mUpdatableListPrefDlgFragment = spy(UpdatableListPreferenceDialogFragment
.newInstance(KEY, MetricsProto.MetricsEvent.DIALOG_SWITCH_A2DP_DEVICES); .newInstance(KEY, MetricsProto.MetricsEvent.DIALOG_SWITCH_A2DP_DEVICES));
mEntries = spy(new ArrayList<>()); mEntries = spy(new ArrayList<>());
mUpdatableListPrefDlgFragment.setEntries(mEntries); mUpdatableListPrefDlgFragment.setEntries(mEntries);
mUpdatableListPrefDlgFragment. mUpdatableListPrefDlgFragment.
@@ -87,4 +91,11 @@ public class UpdatableListPreferenceDialogFragmentTest {
assertThat(mUpdatableListPrefDlgFragment.getAdapter().getCount()).isEqualTo(2); assertThat(mUpdatableListPrefDlgFragment.getAdapter().getCount()).isEqualTo(2);
} }
@Test
public void onDialogClosed_emptyPreference() {
mUpdatableListPrefDlgFragment.onDialogClosed(false);
verify(mUpdatableListPrefDlgFragment, never()).getListPreference();
}
} }