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

BaseRecyclerViewInstrumentationTest

public abstract class BaseRecyclerViewInstrumentationTest extends android.test.ActivityInstrumentationTestCase2

Fields Summary
private static final String
TAG
private boolean
mDebug
protected RecyclerView
mRecyclerView
protected AdapterHelper
mAdapterHelper
Throwable
mainThreadException
Constructors Summary
public BaseRecyclerViewInstrumentationTest()


      
        this(false);
    
public BaseRecyclerViewInstrumentationTest(boolean debug)

        super("android.support.v7.recyclerview", TestActivity.class);
        mDebug = debug;
    
Methods Summary
voidcheckForMainThreadException()

        if (mainThreadException != null) {
            throw mainThreadException;
        }
    
public android.graphics.RectgetDecoratedRecyclerViewBounds()

        return new Rect(
                mRecyclerView.getPaddingLeft(),
                mRecyclerView.getPaddingTop(),
                mRecyclerView.getPaddingLeft() + mRecyclerView.getWidth(),
                mRecyclerView.getPaddingTop() + mRecyclerView.getHeight()
        );
    
protected android.widget.FrameLayoutgetRecyclerViewContainer()

        return getActivity().mContainer;
    
public booleanisMainThread()

        return Looper.myLooper() == Looper.getMainLooper();
    
voidpostExceptionToInstrumentation(java.lang.Throwable t)

        if (mainThreadException != null) {
            Log.e(TAG, "receiving another main thread exception. dropping.", t);
        } else {
            Log.e(TAG, "captured exception on main thread", t);
            mainThreadException = t;
        }

        if (mRecyclerView != null && mRecyclerView
                .getLayoutManager() instanceof TestLayoutManager) {
            TestLayoutManager lm = (TestLayoutManager) mRecyclerView.getLayoutManager();
            // finish all layouts so that we get the correct exception
            while (lm.layoutLatch.getCount() > 0) {
                lm.layoutLatch.countDown();
            }
        }
    
public voidremoveRecyclerView()

        if (mRecyclerView == null) {
            return;
        }
        if (!isMainThread()) {
            getInstrumentation().waitForIdleSync();
        }
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
                    if (adapter instanceof AttachDetachCountingAdapter) {
                        ((AttachDetachCountingAdapter) adapter).getCounter()
                                .validateRemaining(mRecyclerView);
                    }
                    getActivity().mContainer.removeAllViews();
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }
        });
        mRecyclerView = null;
    
public voidrequestLayoutOnUIThread(android.view.View view)

        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    view.requestLayout();
                }
            });
        } catch (Throwable throwable) {
            Log.e(TAG, "", throwable);
        }
    
public voidrunTestOnUiThread(java.lang.Runnable r)

        if (Looper.myLooper() == Looper.getMainLooper()) {
            r.run();
        } else {
            super.runTestOnUiThread(r);
        }
    
public voidscrollBy(int dt)

        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (mRecyclerView.getLayoutManager().canScrollHorizontally()) {
                        mRecyclerView.scrollBy(dt, 0);
                    } else {
                        mRecyclerView.scrollBy(0, dt);
                    }

                }
            });
        } catch (Throwable throwable) {
            Log.e(TAG, "", throwable);
        }
    
voidscrollToPosition(int position)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.getLayoutManager().scrollToPosition(position);
            }
        });
    
voidsetAdapter(RecyclerView.Adapter adapter)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.setAdapter(adapter);
            }
        });
    
voidsetHasTransientState(android.view.View view, boolean value)

        try {
            runTestOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ViewCompat.setHasTransientState(view, value);
                }
            });
        } catch (Throwable throwable) {
            Log.e(TAG, "", throwable);
        }
    
public voidsetRecyclerView(RecyclerView recyclerView)

        setRecyclerView(recyclerView, true);
    
public voidsetRecyclerView(RecyclerView recyclerView, boolean assignDummyPool)

        setRecyclerView(recyclerView, true, true);
    
public voidsetRecyclerView(RecyclerView recyclerView, boolean assignDummyPool, boolean addPositionCheckItemAnimator)

        mRecyclerView = recyclerView;
        if (assignDummyPool) {
            RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool() {
                @Override
                public RecyclerView.ViewHolder getRecycledView(int viewType) {
                    RecyclerView.ViewHolder viewHolder = super.getRecycledView(viewType);
                    if (viewHolder == null) {
                        return null;
                    }
                    viewHolder.addFlags(RecyclerView.ViewHolder.FLAG_BOUND);
                    viewHolder.mPosition = 200;
                    viewHolder.mOldPosition = 300;
                    viewHolder.mPreLayoutPosition = 500;
                    return viewHolder;
                }

                @Override
                public void putRecycledView(RecyclerView.ViewHolder scrap) {
                    super.putRecycledView(scrap);
                }
            };
            mRecyclerView.setRecycledViewPool(pool);
        }
        if (addPositionCheckItemAnimator) {
            mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
                        RecyclerView.State state) {
                    RecyclerView.ViewHolder vh = parent.getChildViewHolder(view);
                    if (!vh.isRemoved()) {
                        assertNotSame("If getItemOffsets is called, child should have a valid"
                                            + " adapter position unless it is removed",
                                    vh.getAdapterPosition(), RecyclerView.NO_POSITION);
                    }
                }
            });
        }
        mAdapterHelper = recyclerView.mAdapterHelper;
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                getActivity().mContainer.addView(recyclerView);
            }
        });
    
voidsmoothScrollToPosition(int position)

        Log.d(TAG, "SMOOTH scrolling to " + position);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.smoothScrollToPosition(position);
            }
        });
        while (mRecyclerView.getLayoutManager().isSmoothScrolling() ||
                mRecyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
            if (mDebug) {
                Log.d(TAG, "SMOOTH scrolling step");
            }
            Thread.sleep(200);
        }
        Log.d(TAG, "SMOOTH scrolling done");
        getInstrumentation().waitForIdleSync();
    
voidswapAdapter(RecyclerView.Adapter adapter, boolean removeAndRecycleExistingViews)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    mRecyclerView.swapAdapter(adapter, removeAndRecycleExistingViews);
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }
        });
        checkForMainThreadException();
    
protected voidtearDown()

        if (mRecyclerView != null) {
            try {
                removeRecyclerView();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
        getInstrumentation().waitForIdleSync();
        super.tearDown();

        try {
            checkForMainThreadException();
        } catch (Exception e) {
            throw e;
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    
voidwaitForAnimations(int seconds)

        final CountDownLatch latch = new CountDownLatch(2);
        boolean running = mRecyclerView.mItemAnimator
                .isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
                    @Override
                    public void onAnimationsFinished() {
                        latch.countDown();
                    }
                });
        if (running) {
            latch.await(seconds, TimeUnit.SECONDS);
        }