下面列出了怎么用android.view.AbsSavedState的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testPanelDataIsParcelable() {
final GuidanceStreetLabelData data = createData(STREET_NAME, COLOR);
GuidanceStreetLabelView.SavedState savedState = new GuidanceStreetLabelView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setStateToSave(data);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceStreetLabelView.SavedState createdFromParcel = GuidanceStreetLabelView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedState());
// test for null
savedState.setStateToSave(null);
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
createdFromParcel = GuidanceStreetLabelView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedState());
}
@Test
public void testViewDataIsParcelable() {
final GuidanceSpeedData data = new GuidanceSpeedData(VELOCITY, SPEED_LIMIT);
GuidanceSpeedView.SavedState savedState = new GuidanceSpeedView.SavedState(
AbsSavedState.EMPTY_STATE);
savedState.setStateToSave(data, RED_COLOR, BLUE_COLOR);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceSpeedView.SavedState createdFromParcel = GuidanceSpeedView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedSpeedData());
assertThat(createdFromParcel.getSavedValueTextColor(), is(RED_COLOR));
assertThat(createdFromParcel.getSavedUnitTextColor(), is(BLUE_COLOR));
}
@Test
public void testPanelDataIsParcelable() {
final GuidanceManeuverData data = createData(mIconId, mDistance, mInfo1, mInfo2);
GuidanceManeuverView.SavedState savedState = new GuidanceManeuverView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setViewState(new GuidanceManeuverView.State(data));
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceManeuverView.SavedState createdFromParcel = GuidanceManeuverView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getViewState());
}
@Test
public void testPanelNoDataIsParcelable() {
GuidanceManeuverView.SavedState savedState = new GuidanceManeuverView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setViewState(GuidanceManeuverView.State.NO_DATA);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceManeuverView.SavedState createdFromParcel = GuidanceManeuverView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getViewState());
assertThat(createdFromParcel.getViewState(), equalTo(GuidanceManeuverView.State.NO_DATA));
}
@Test
public void testPanelUpdatingStateIsParcelable() {
GuidanceManeuverView.SavedState savedState = new GuidanceManeuverView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setViewState(GuidanceManeuverView.State.UPDATING);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceManeuverView.SavedState createdFromParcel = GuidanceManeuverView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getViewState());
assertThat(createdFromParcel.getViewState(), equalTo(GuidanceManeuverView.State.UPDATING));
}
@Test
public void testViewDataIsParcelable() {
final GuidanceEstimatedArrivalViewData data = new GuidanceEstimatedArrivalViewData(ETA_DATE, DISTANCE, DURATION);
GuidanceEstimatedArrivalView.SavedState savedState = new GuidanceEstimatedArrivalView.SavedState(
AbsSavedState.EMPTY_STATE);
savedState.setStateToSave(data);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceEstimatedArrivalView.SavedState createdFromParcel = GuidanceEstimatedArrivalView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedState());
}
@Test
public void testViewDataIsParcelable() {
final GuidanceSpeedData data = new GuidanceSpeedData(VELOCITY, SPEED_LIMIT);
GuidanceSpeedLimitView.SavedState savedState = new GuidanceSpeedLimitView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setStateToSave(data);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceSpeedLimitView.SavedState createdFromParcel = GuidanceSpeedLimitView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedState());
}
@Test
public void testPanelDataIsParcelable() {
final GuidanceNextManeuverData data = createData(mIconId, mDistance, mStreetName);
GuidanceNextManeuverView.SavedState savedState = new GuidanceNextManeuverView.SavedState(AbsSavedState.EMPTY_STATE);
savedState.setStateToSave(data);
Parcel parcel = Parcel.obtain();
savedState.writeToParcel(parcel, savedState.describeContents());
parcel.setDataPosition(0);
GuidanceNextManeuverView.SavedState createdFromParcel = GuidanceNextManeuverView.SavedState.CREATOR.createFromParcel(
parcel);
assertNotNull(createdFromParcel.getSavedState());
}
@Override
protected void onRestoreInstanceState(final Parcelable state) {
if (DEBUG) Log.v(TAG, "onRestoreInstanceState:");
super.onRestoreInstanceState(state);
if (state instanceof AbsSavedState) {
super.onRestoreInstanceState(((AbsSavedState) state).getSuperState());
}
mDelegater.onRestoreInstanceState(state);
}
@Override
protected void onRestoreInstanceState(final Parcelable state) {
if (DEBUG) Log.v(TAG, "onRestoreInstanceState:");
super.onRestoreInstanceState(state);
if (state instanceof AbsSavedState) {
super.onRestoreInstanceState(((AbsSavedState) state).getSuperState());
}
final IViewTransformer transformer = getViewTransformer();
if (transformer instanceof ViewTransformDelegater) {
((ViewTransformDelegater) transformer).onRestoreInstanceState(state);
}
}
@Test
public void testWriteToParcel() {
final Parcelable parcelable = new View.BaseSavedState(AbsSavedState.EMPTY_STATE);
final ExpandableSavedState ss = new ExpandableSavedState(parcelable);
ss.setSize(1000);
ss.setWeight(0.5f);
final Parcel parcel = Parcel.obtain();
ss.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
final ExpandableSavedState target = ExpandableSavedState.CREATOR.createFromParcel(parcel);
assertThat(target.getSize(), is(1000));
assertThat(target.getWeight(), is(0.5f));
}
public void testGridViewSavedState() throws Throwable {
Parcel parcel = Parcel.obtain();
ObservableGridView.SavedState state1 = new ObservableGridView.SavedState(AbsSavedState.EMPTY_STATE);
state1.prevFirstVisiblePosition = 1;
state1.prevFirstVisibleChildHeight = 2;
state1.prevScrolledChildrenHeight = 3;
state1.prevScrollY = 4;
state1.scrollY = 5;
state1.childrenHeights = new SparseIntArray();
state1.childrenHeights.put(0, 10);
state1.childrenHeights.put(1, 20);
state1.childrenHeights.put(2, 30);
state1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ObservableGridView.SavedState state2 = ObservableGridView.SavedState.CREATOR.createFromParcel(parcel);
assertNotNull(state2);
assertEquals(state1.prevFirstVisiblePosition, state2.prevFirstVisiblePosition);
assertEquals(state1.prevFirstVisibleChildHeight, state2.prevFirstVisibleChildHeight);
assertEquals(state1.prevScrolledChildrenHeight, state2.prevScrolledChildrenHeight);
assertEquals(state1.prevScrollY, state2.prevScrollY);
assertEquals(state1.scrollY, state2.scrollY);
assertNotNull(state1.childrenHeights);
assertEquals(3, state1.childrenHeights.size());
assertEquals(10, state1.childrenHeights.get(0));
assertEquals(20, state1.childrenHeights.get(1));
assertEquals(30, state1.childrenHeights.get(2));
}
public void testListViewSavedState() throws Throwable {
Parcel parcel = Parcel.obtain();
ObservableListView.SavedState state1 = new ObservableListView.SavedState(AbsSavedState.EMPTY_STATE);
state1.prevFirstVisiblePosition = 1;
state1.prevFirstVisibleChildHeight = 2;
state1.prevScrolledChildrenHeight = 3;
state1.prevScrollY = 4;
state1.scrollY = 5;
state1.childrenHeights = new SparseIntArray();
state1.childrenHeights.put(0, 10);
state1.childrenHeights.put(1, 20);
state1.childrenHeights.put(2, 30);
state1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ObservableListView.SavedState state2 = ObservableListView.SavedState.CREATOR.createFromParcel(parcel);
assertNotNull(state2);
assertEquals(state1.prevFirstVisiblePosition, state2.prevFirstVisiblePosition);
assertEquals(state1.prevFirstVisibleChildHeight, state2.prevFirstVisibleChildHeight);
assertEquals(state1.prevScrolledChildrenHeight, state2.prevScrolledChildrenHeight);
assertEquals(state1.prevScrollY, state2.prevScrollY);
assertEquals(state1.scrollY, state2.scrollY);
assertNotNull(state1.childrenHeights);
assertEquals(3, state1.childrenHeights.size());
assertEquals(10, state1.childrenHeights.get(0));
assertEquals(20, state1.childrenHeights.get(1));
assertEquals(30, state1.childrenHeights.get(2));
}
public void testRecyclerViewSavedState() throws Throwable {
Parcel parcel = Parcel.obtain();
ObservableRecyclerView.SavedState state1 = new ObservableRecyclerView.SavedState(AbsSavedState.EMPTY_STATE);
state1.prevFirstVisiblePosition = 1;
state1.prevFirstVisibleChildHeight = 2;
state1.prevScrolledChildrenHeight = 3;
state1.prevScrollY = 4;
state1.scrollY = 5;
state1.childrenHeights = new SparseIntArray();
state1.childrenHeights.put(0, 10);
state1.childrenHeights.put(1, 20);
state1.childrenHeights.put(2, 30);
state1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ObservableRecyclerView.SavedState state2 = ObservableRecyclerView.SavedState.CREATOR.createFromParcel(parcel);
assertNotNull(state2);
assertEquals(state1.prevFirstVisiblePosition, state2.prevFirstVisiblePosition);
assertEquals(state1.prevFirstVisibleChildHeight, state2.prevFirstVisibleChildHeight);
assertEquals(state1.prevScrolledChildrenHeight, state2.prevScrolledChildrenHeight);
assertEquals(state1.prevScrollY, state2.prevScrollY);
assertEquals(state1.scrollY, state2.scrollY);
assertNotNull(state1.childrenHeights);
assertEquals(3, state1.childrenHeights.size());
assertEquals(10, state1.childrenHeights.get(0));
assertEquals(20, state1.childrenHeights.get(1));
assertEquals(30, state1.childrenHeights.get(2));
}
public void testScrollViewSavedState() throws Throwable {
Parcel parcel = Parcel.obtain();
ObservableScrollView.SavedState state1 = new ObservableScrollView.SavedState(AbsSavedState.EMPTY_STATE);
state1.prevScrollY = 1;
state1.scrollY = 2;
state1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ObservableScrollView.SavedState state2 = ObservableScrollView.SavedState.CREATOR.createFromParcel(parcel);
assertNotNull(state2);
assertEquals(state1.prevScrollY, state2.prevScrollY);
assertEquals(state1.scrollY, state2.scrollY);
}
public void testWebViewSavedState() throws Throwable {
Parcel parcel = Parcel.obtain();
ObservableWebView.SavedState state1 = new ObservableWebView.SavedState(AbsSavedState.EMPTY_STATE);
state1.prevScrollY = 1;
state1.scrollY = 2;
state1.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
ObservableWebView.SavedState state2 = ObservableWebView.SavedState.CREATOR.createFromParcel(parcel);
assertNotNull(state2);
assertEquals(state1.prevScrollY, state2.prevScrollY);
assertEquals(state1.scrollY, state2.scrollY);
}
@Override public Parcelable superOnSaveInstanceState() {
return Mockito.mock(AbsSavedState.class);
}
@Override public Parcelable superOnSaveInstanceState() {
return Mockito.mock(AbsSavedState.class);
}