public void testSavedState() throws InterruptedException { MockLayoutManager mlm = new MockLayoutManager(); mRecyclerView.setLayoutManager(mlm); mRecyclerView.setAdapter(new MockAdapter(3)); layout(); Parcelable savedState = mRecyclerView.onSaveInstanceState(); // we append a suffix to the parcelable to test out of bounds String parcelSuffix = UUID.randomUUID().toString(); Parcel parcel = Parcel.obtain(); savedState.writeToParcel(parcel, 0); parcel.writeString(parcelSuffix); // reset for reading parcel.setDataPosition(0); // re-create savedState = RecyclerView.SavedState.CREATOR.createFromParcel(parcel); RecyclerView restored = new RecyclerView(mContext); MockLayoutManager mlmRestored = new MockLayoutManager(); restored.setLayoutManager(mlmRestored); restored.setAdapter(new MockAdapter(3)); restored.onRestoreInstanceState(savedState); assertEquals("Parcel reading should not go out of bounds", parcelSuffix, parcel.readString()); assertEquals("When unmarshalling, all of the parcel should be read", 0, parcel.dataAvail()); assertEquals( "uuid in layout manager should be preserved properly", mlm.mUuid, mlmRestored.mUuid); assertNotSame( "stateless parameter should not be preserved", mlm.mLayoutCount, mlmRestored.mLayoutCount); layout(); }
public void testSavedStateWithStatelessLayoutManager() throws InterruptedException { mRecyclerView.setLayoutManager( new MockLayoutManager() { @Override public Parcelable onSaveInstanceState() { return null; } }); mRecyclerView.setAdapter(new MockAdapter(3)); Parcel parcel = Parcel.obtain(); String parcelSuffix = UUID.randomUUID().toString(); Parcelable savedState = mRecyclerView.onSaveInstanceState(); savedState.writeToParcel(parcel, 0); parcel.writeString(parcelSuffix); // reset position for reading parcel.setDataPosition(0); RecyclerView restored = new RecyclerView(mContext); restored.setLayoutManager(new MockLayoutManager()); mRecyclerView.setAdapter(new MockAdapter(3)); // restore savedState = RecyclerView.SavedState.CREATOR.createFromParcel(parcel); restored.onRestoreInstanceState(savedState); assertEquals("Parcel reading should not go out of bounds", parcelSuffix, parcel.readString()); assertEquals("When unmarshalling, all of the parcel should be read", 0, parcel.dataAvail()); }