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

DefaultItemAnimatorTest

public class DefaultItemAnimatorTest extends android.test.ActivityInstrumentationTestCase2

Fields Summary
private static final String
TAG
Throwable
mainThreadException
DefaultItemAnimator
mAnimator
Adapter
mAdapter
android.view.ViewGroup
mDummyParent
List
mExpectedItems
Set
mRemoveFinished
Set
mAddFinished
Set
mMoveFinished
Set
mChangeFinished
Semaphore
mExpectedItemCount
Constructors Summary
public DefaultItemAnimatorTest()


      
        super("android.support.v7.recyclerview", TestActivity.class);
    
Methods Summary
booleananimateAdd(RecyclerView.ViewHolder vh)

        final boolean[] result = new boolean[1];
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                result[0] = mAnimator.animateAdd(vh);
            }
        });
        return result[0];
    
booleananimateChange(RecyclerView.ViewHolder oldHolder, RecyclerView.ViewHolder newHolder, int fromX, int fromY, int toX, int toY)

        final boolean[] result = new boolean[1];
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                result[0] = mAnimator.animateChange(oldHolder, newHolder, fromX, fromY, toX, toY);
            }
        });
        return result[0];
    
booleananimateMove(RecyclerView.ViewHolder vh, int fromX, int fromY, int toX, int toY)

        final boolean[] result = new boolean[1];
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                result[0] = mAnimator.animateMove(vh, fromX, fromY, toX, toY);
            }
        });
        return result[0];
    
booleananimateRemove(RecyclerView.ViewHolder vh)

        final boolean[] result = new boolean[1];
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                result[0] = mAnimator.animateRemove(vh);
            }
        });
        return result[0];
    
public voidcancelAfter(int count, RecyclerView.ViewHolder toCancel)

        cancelTest(false, count, toCancel);
    
public voidcancelBefore(int count, RecyclerView.ViewHolder toCancel)

        cancelTest(true, count, toCancel);
    
public voidcancelChangeBothTest(boolean before)

        ViewHolder vh = createViewHolder(1);
        ViewHolder vh2 = createViewHolder(1);
        expectItems(vh, vh2);
        assertTrue(animateChange(vh, vh2, 20, 20, 100, 100));
        cancelTest(before, 2, vh, vh2);
    
public voidcancelChangeNewTest(boolean before)

        ViewHolder vh = createViewHolder(1);
        ViewHolder vh2 = createViewHolder(1);
        expectItems(vh, vh2);
        assertTrue(animateChange(vh, vh2, 20, 20, 100, 100));
        cancelTest(before, 2, vh2);
    
public voidcancelChangeOldTest(boolean before)

        ViewHolder vh = createViewHolder(1);
        ViewHolder vh2 = createViewHolder(1);
        expectItems(vh, vh2);
        assertTrue(animateChange(vh, vh2, 20, 20, 100, 100));
        cancelTest(before, 2, vh);
    
public voidcancelTest(boolean before, int count, RecyclerView.ViewHolder toCancel)

        if (before) {
            endAnimations(toCancel);
            runAndWait(count, 1);
        } else {
            runAndWait(count, 1, new ThrowingRunnable() {
                @Override
                public void run() throws Throwable {
                    endAnimations(toCancel);
                }
            });
        }
    
voidcheckForMainThreadException()

        if (mainThreadException != null) {
            throw mainThreadException;
        }
    
private android.support.v7.widget.DefaultItemAnimatorTest$ViewHoldercreateViewHolder(int pos)

        final ViewHolder vh = mAdapter.createViewHolder(mDummyParent, 1);
        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAdapter.bindViewHolder(vh, pos);
                mDummyParent.addView(vh.itemView);
            }
        });

        return vh;
    
voidendAnimations(RecyclerView.ViewHolder vhs)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (RecyclerView.ViewHolder vh : vhs) {
                    mAnimator.endAnimation(vh);
                }
            }
        });
    
voidexpectItems(RecyclerView.ViewHolder viewHolders)

        mExpectedItems.addAll(Arrays.asList(viewHolders));
    
voidpostExceptionToInstrumentation(java.lang.Throwable t)

        if (mainThreadException == null) {
            mainThreadException = t;
        } else {
            Log.e(TAG, "skipping secondary main thread exception", t);
        }
    
voidrunAndWait(int itemCount, int seconds)

        runAndWait(itemCount, seconds, null);
    
voidrunAndWait(int itemCount, int seconds, android.support.v7.widget.DefaultItemAnimatorTest$ThrowingRunnable postRun)

        runTestOnUiThread(new Runnable() {
            @Override
            public void run() {
                mAnimator.runPendingAnimations();
                if (postRun != null) {
                    try {
                        postRun.run();
                    } catch (Throwable e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });
        waitForItems(itemCount, seconds);
        checkForMainThreadException();
    
public voidrunTestOnUiThread(java.lang.Runnable r)

        if (Looper.myLooper() == Looper.getMainLooper()) {
            r.run();
        } else {
            super.runTestOnUiThread(r);
        }
    
protected voidsetUp()

        super.setUp();
        mAnimator = new DefaultItemAnimator();
        mAdapter = new Adapter(20);
        mDummyParent = getActivity().mContainer;
        mAnimator.setListener(new RecyclerView.ItemAnimator.ItemAnimatorListener() {
            @Override
            public void onRemoveFinished(RecyclerView.ViewHolder item) {
                try {
                    assertTrue(mRemoveFinished.add(item));
                    onFinished(item);
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }

            @Override
            public void onAddFinished(RecyclerView.ViewHolder item) {
                try {
                    assertTrue(mAddFinished.add(item));
                    onFinished(item);
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }

            @Override
            public void onMoveFinished(RecyclerView.ViewHolder item) {
                try {
                    assertTrue(mMoveFinished.add(item));
                    onFinished(item);
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }

            @Override
            public void onChangeFinished(RecyclerView.ViewHolder item) {
                try {
                    assertTrue(mChangeFinished.add(item));
                    onFinished(item);
                } catch (Throwable t) {
                    postExceptionToInstrumentation(t);
                }
            }

            private void onFinished(RecyclerView.ViewHolder item) {
                assertNotNull(mExpectedItems.remove(item));
                mExpectedItemCount.release(1);
            }
        });
    
protected voidtearDown()

        getInstrumentation().waitForIdleSync();
        super.tearDown();
        try {
            checkForMainThreadException();
        } catch (Exception e) {
            throw e;
        } catch (Throwable throwable) {
            throw new Exception(throwable);
        }
    
public voidtestAnimateAdd()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateAdd(vh));
        assertTrue(mAnimator.isRunning());
        runAndWait(1, 1);
    
public voidtestAnimateChange()

        ViewHolder vh = createViewHolder(1);
        ViewHolder vh2 = createViewHolder(2);
        expectItems(vh, vh2);
        assertTrue(animateChange(vh, vh2, 0, 0, 100, 100));
        assertTrue(mAnimator.isRunning());
        runAndWait(2, 1);
    
public voidtestAnimateMove()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateMove(vh, 0, 0, 100, 100));
        assertTrue(mAnimator.isRunning());
        runAndWait(1, 1);
    
public voidtestAnimateRemove()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateRemove(vh));
        assertTrue(mAnimator.isRunning());
        runAndWait(1, 1);
    
public voidtestCancelAddAfter()

        final ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateAdd(vh));
        cancelAfter(1, vh);
    
public voidtestCancelAddBefore()

        final ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateAdd(vh));
        cancelBefore(1, vh);
    
public voidtestCancelChangeBothAfter()

        cancelChangeBothTest(false);
    
public voidtestCancelChangeBothBefore()

        cancelChangeBothTest(true);
    
public voidtestCancelChangeNewAfter()

        cancelChangeNewTest(false);
    
public voidtestCancelChangeNewBefore()

        cancelChangeNewTest(true);
    
public voidtestCancelChangeOldAfter()

        cancelChangeOldTest(false);
    
public voidtestCancelChangeOldBefore()

        cancelChangeOldTest(true);
    
public voidtestCancelMoveAfter()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateMove(vh, 10, 10, 100, 100));
        cancelAfter(1, vh);
    
public voidtestCancelMoveBefore()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateMove(vh, 10, 10, 100, 100));
        cancelBefore(1, vh);
    
public voidtestCancelRemove()

        ViewHolder vh = createViewHolder(1);
        expectItems(vh);
        assertTrue(animateRemove(vh));
        endAnimations(vh);
        runAndWait(1, 1);
    
voidwaitForItems(int itemCount, int seconds)

        assertTrue("all vh animations should end",
                mExpectedItemCount.tryAcquire(itemCount, seconds, TimeUnit.SECONDS));
        assertEquals("all expected finish events should happen", 0, mExpectedItems.size());
        // wait one more second for unwanted
        assertFalse("should not receive any more permits",
                mExpectedItemCount.tryAcquire(1, 2, TimeUnit.SECONDS));