Merge "Guard NPE when using search view"

This commit is contained in:
TreeHugger Robot
2019-05-31 04:27:43 +00:00
committed by Android (Google) Code Review
2 changed files with 36 additions and 3 deletions

View File

@@ -151,7 +151,8 @@ public class ManageApplications extends InstrumentedFragment
private static final String EXTRA_HAS_ENTRIES = "hasEntries"; private static final String EXTRA_HAS_ENTRIES = "hasEntries";
private static final String EXTRA_HAS_BRIDGE = "hasBridge"; private static final String EXTRA_HAS_BRIDGE = "hasBridge";
private static final String EXTRA_FILTER_TYPE = "filterType"; private static final String EXTRA_FILTER_TYPE = "filterType";
private static final String EXTRA_EXPAND_SEARCH_VIEW = "expand_search_view"; @VisibleForTesting
static final String EXTRA_EXPAND_SEARCH_VIEW = "expand_search_view";
// attributes used as keys when passing values to AppInfoDashboardFragment activity // attributes used as keys when passing values to AppInfoDashboardFragment activity
public static final String APP_CHG = "chg"; public static final String APP_CHG = "chg";
@@ -505,11 +506,13 @@ public class ManageApplications extends InstrumentedFragment
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
mResetAppsHelper.onSaveInstanceState(outState); mResetAppsHelper.onSaveInstanceState(outState);
outState.putInt(EXTRA_SORT_ORDER, mSortOrder); outState.putInt(EXTRA_SORT_ORDER, mSortOrder);
outState.putInt(EXTRA_FILTER_TYPE, mFilter.getFilterType());
outState.putBoolean(EXTRA_SHOW_SYSTEM, mShowSystem); outState.putBoolean(EXTRA_SHOW_SYSTEM, mShowSystem);
outState.putBoolean(EXTRA_HAS_ENTRIES, mApplications.mHasReceivedLoadEntries); outState.putBoolean(EXTRA_HAS_ENTRIES, mApplications.mHasReceivedLoadEntries);
outState.putBoolean(EXTRA_HAS_BRIDGE, mApplications.mHasReceivedBridgeCallback); outState.putBoolean(EXTRA_HAS_BRIDGE, mApplications.mHasReceivedBridgeCallback);
outState.putBoolean(EXTRA_EXPAND_SEARCH_VIEW, !mSearchView.isIconified()); if(mSearchView != null) {
outState.putInt(EXTRA_FILTER_TYPE, mFilter.getFilterType()); outState.putBoolean(EXTRA_EXPAND_SEARCH_VIEW, !mSearchView.isIconified());
}
if (mApplications != null) { if (mApplications != null) {
mApplications.onSaveInstanceState(outState); mApplications.onSaveInstanceState(outState);
} }

View File

@@ -494,6 +494,36 @@ public class ManageApplicationsTest {
assertThat(mFragment.mRecyclerView.getPaddingTop()).isEqualTo(0); assertThat(mFragment.mRecyclerView.getPaddingTop()).isEqualTo(0);
} }
@Test
public void onSaveInstanceState_noSearchView_shouldNotSetBundleValue() {
final Bundle bundle = new Bundle();
ReflectionHelpers.setField(mFragment, "mResetAppsHelper", mock(ResetAppsHelper.class));
ReflectionHelpers.setField(mFragment, "mFilter", mock(AppFilterItem.class));
ReflectionHelpers.setField(mFragment, "mApplications",
mock(ManageApplications.ApplicationsAdapter.class));
mFragment.onSaveInstanceState(bundle);
assertThat(bundle.containsKey(ManageApplications.EXTRA_EXPAND_SEARCH_VIEW)).isFalse();
}
@Test
public void onSaveInstanceState_searchViewSet_shouldSetBundleValue() {
final SearchView searchView = mock(SearchView.class);
final Bundle bundle = new Bundle();
ReflectionHelpers.setField(mFragment, "mResetAppsHelper", mock(ResetAppsHelper.class));
ReflectionHelpers.setField(mFragment, "mFilter", mock(AppFilterItem.class));
ReflectionHelpers.setField(mFragment, "mApplications",
mock(ManageApplications.ApplicationsAdapter.class));
ReflectionHelpers.setField(mFragment, "mSearchView", searchView);
when(searchView.isIconified()).thenReturn(true);
mFragment.onSaveInstanceState(bundle);
assertThat(bundle.containsKey(ManageApplications.EXTRA_EXPAND_SEARCH_VIEW)).isTrue();
assertThat(bundle.getBoolean(ManageApplications.EXTRA_EXPAND_SEARCH_VIEW)).isFalse();
}
private void setUpOptionMenus() { private void setUpOptionMenus() {
when(mMenu.findItem(anyInt())).thenAnswer(invocation -> { when(mMenu.findItem(anyInt())).thenAnswer(invocation -> {
final Object[] args = invocation.getArguments(); final Object[] args = invocation.getArguments();