FileDocCategorySizeDatePackage
RecyclerViewBasicTest.javaAPI DocAndroid 5.1 API12481Thu Mar 12 22:22:56 GMT 2015android.support.v7.widget

RecyclerViewBasicTest

public class RecyclerViewBasicTest extends android.test.AndroidTestCase

Fields Summary
RecyclerView
mRecyclerView
Constructors Summary
Methods Summary
private voidfocusSearch()

        mRecyclerView.focusSearch(1);
    
private voidlayout()

        mRecyclerView.layout(0, 0, 320, 320);
    
private voidmeasure()

        mRecyclerView.measure(View.MeasureSpec.AT_MOST | 320, View.MeasureSpec.AT_MOST | 240);
    
protected voidsetUp()

        super.setUp();
        mRecyclerView = new RecyclerView(mContext);
    
public voidtestAdapterChangeCallbacks()

        MockLayoutManager layoutManager = new MockLayoutManager();
        mRecyclerView.setLayoutManager(layoutManager);
        MockAdapter adapterOld = new MockAdapter(1);
        mRecyclerView.setAdapter(adapterOld);
        layoutManager.assertPrevNextAdapters(null, adapterOld);

        MockAdapter adapterNew = new MockAdapter(2);
        mRecyclerView.setAdapter(adapterNew);
        layoutManager.assertPrevNextAdapters("switching adapters should trigger correct callbacks"
                , adapterOld, adapterNew);

        mRecyclerView.setAdapter(null);
        layoutManager.assertPrevNextAdapters(
                "Setting adapter null should trigger correct callbacks",
                adapterNew, null);
    
public voidtestFocusWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
        focusSearch();
    
public voidtestLayout()

        MockLayoutManager layoutManager = new MockLayoutManager();
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(new MockAdapter(3));
        layout();
        assertEquals("when both layout manager and activity is set, recycler view should call"
                + " layout manager's layout method", 1, layoutManager.mLayoutCount);
    
public voidtestLayoutWithoutAdapter()

        MockLayoutManager layoutManager = new MockLayoutManager();
        mRecyclerView.setLayoutManager(layoutManager);
        layout();
        assertEquals("layout manager should not be called if there is no adapter attached",
                0, layoutManager.mLayoutCount);
    
public voidtestLayoutWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
    
public voidtestMeasureWithoutLayoutManager()

        measure();
    
public voidtestObservingAdapters()

        MockAdapter adapterOld = new MockAdapter(1);
        mRecyclerView.setAdapter(adapterOld);
        assertTrue("attached adapter should have observables", adapterOld.hasObservers());

        MockAdapter adapterNew = new MockAdapter(2);
        mRecyclerView.setAdapter(adapterNew);
        assertFalse("detached adapter should lose observable", adapterOld.hasObservers());
        assertTrue("new adapter should have observers", adapterNew.hasObservers());

        mRecyclerView.setAdapter(null);
        assertNull("adapter should be removed successfully", mRecyclerView.getAdapter());
        assertFalse("when adapter is removed, observables should be removed too",
                adapterNew.hasObservers());
    
public voidtestSavedState()

        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 voidtestSavedStateWithStatelessLayoutManager()

        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());

    
public voidtestScrollToPositionWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
        mRecyclerView.scrollToPosition(5);
    
public voidtestScrollWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
        mRecyclerView.scrollBy(10, 10);
    
public voidtestSmoothScrollToPositionWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
        mRecyclerView.smoothScrollToPosition(5);
    
public voidtestSmoothScrollWithoutLayoutManager()

        mRecyclerView.setAdapter(new MockAdapter(20));
        measure();
        layout();
        mRecyclerView.smoothScrollBy(10, 10);