[SuwLib] Check instance state type before casting

Bug: 28390168
Change-Id: Ic8cb9c35b29fe05f1edc6c8fa7c0bd7d23cd4fdf
This commit is contained in:
Maurice Lam 2016-04-27 16:22:26 -07:00
parent 84979a6b28
commit 7f2a6e95f2
2 changed files with 60 additions and 0 deletions

View file

@ -152,6 +152,12 @@ public class SetupWizardLayout extends TemplateLayout {
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
Log.w(TAG, "Ignoring restore instance state " + state);
super.onRestoreInstanceState(state);
return;
}
final SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
final boolean isProgressBarShown = ss.mIsProgressBarShown;

View file

@ -19,8 +19,11 @@ package com.android.setupwizardlib.test;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Parcelable;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.SparseArray;
import android.view.AbsSavedState;
import android.view.ContextThemeWrapper;
import android.view.InflateException;
import android.view.LayoutInflater;
@ -159,6 +162,57 @@ public class SetupWizardLayoutTest extends InstrumentationTestCase {
}
}
@SmallTest
public void testOnRestoreFromInstanceState() {
final SetupWizardLayout layout = new SetupWizardLayout(mContext);
// noinspection ResourceType
layout.setId(1234);
SparseArray<Parcelable> container = new SparseArray<>();
layout.saveHierarchyState(container);
final SetupWizardLayout layout2 = new SetupWizardLayout(mContext);
// noinspection ResourceType
layout2.setId(1234);
layout2.restoreHierarchyState(container);
assertFalse("Progress bar should not be shown", layout2.isProgressBarShown());
}
@SmallTest
public void testOnRestoreFromInstanceStateProgressBarShown() {
final SetupWizardLayout layout = new SetupWizardLayout(mContext);
// noinspection ResourceType
layout.setId(1234);
layout.setProgressBarShown(true);
SparseArray<Parcelable> container = new SparseArray<>();
layout.saveHierarchyState(container);
final SetupWizardLayout layout2 = new SetupWizardLayout(mContext);
// noinspection ResourceType
layout2.setId(1234);
layout2.restoreHierarchyState(container);
assertTrue("Progress bar should be shown", layout2.isProgressBarShown());
}
@SmallTest
public void testOnRestoreFromIncompatibleInstanceState() {
final SetupWizardLayout layout = new SetupWizardLayout(mContext);
// noinspection ResourceType
layout.setId(1234);
SparseArray<Parcelable> container = new SparseArray<>();
container.put(1234, AbsSavedState.EMPTY_STATE);
layout.restoreHierarchyState(container);
// SetupWizardLayout shouldn't crash with incompatible Parcelable
assertFalse("Progress bar should not be shown", layout.isProgressBarShown());
}
private void assertDefaultTemplateInflated(SetupWizardLayout layout) {
View decorView = layout.findViewById(R.id.suw_layout_decor);
View navbar = layout.findViewById(R.id.suw_layout_navigation_bar);