BaseRecyclerViewInstrumentationTest.java revision 629687bba8da1a73c48d8fe87393a13581ce46ca
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.widget;
18
19import android.app.Instrumentation;
20import android.graphics.Rect;
21import android.os.Handler;
22import android.os.Looper;
23import android.support.v4.view.ViewCompat;
24import android.test.ActivityInstrumentationTestCase2;
25import android.util.Log;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.FrameLayout;
30import android.widget.TextView;
31
32import java.lang.reflect.InvocationTargetException;
33import java.lang.reflect.Method;
34import java.util.ArrayList;
35import java.util.HashSet;
36import java.util.List;
37import java.util.Set;
38import java.util.concurrent.CountDownLatch;
39import java.util.concurrent.TimeUnit;
40import java.util.concurrent.atomic.AtomicInteger;
41import java.util.concurrent.locks.ReentrantLock;
42import android.support.v7.recyclerview.test.R;
43
44abstract public class BaseRecyclerViewInstrumentationTest extends
45        ActivityInstrumentationTestCase2<TestActivity> {
46
47    private static final String TAG = "RecyclerViewTest";
48
49    private boolean mDebug;
50
51    protected RecyclerView mRecyclerView;
52
53    protected AdapterHelper mAdapterHelper;
54
55    Throwable mainThreadException;
56
57    Thread mInstrumentationThread;
58
59    public BaseRecyclerViewInstrumentationTest() {
60        this(false);
61    }
62
63    public BaseRecyclerViewInstrumentationTest(boolean debug) {
64        super("android.support.v7.recyclerview", TestActivity.class);
65        mDebug = debug;
66    }
67
68    void checkForMainThreadException() throws Throwable {
69        if (mainThreadException != null) {
70            throw mainThreadException;
71        }
72    }
73
74    @Override
75    protected void setUp() throws Exception {
76        super.setUp();
77        mInstrumentationThread = Thread.currentThread();
78    }
79
80    void setHasTransientState(final View view, final boolean value) {
81        try {
82            runTestOnUiThread(new Runnable() {
83                @Override
84                public void run() {
85                    ViewCompat.setHasTransientState(view, value);
86                }
87            });
88        } catch (Throwable throwable) {
89            Log.e(TAG, "", throwable);
90        }
91    }
92
93    protected void enableAccessibility()
94            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
95        Method getUIAutomation = Instrumentation.class.getMethod("getUiAutomation");
96        getUIAutomation.invoke(getInstrumentation());
97    }
98
99    void setAdapter(final RecyclerView.Adapter adapter) throws Throwable {
100        runTestOnUiThread(new Runnable() {
101            @Override
102            public void run() {
103                mRecyclerView.setAdapter(adapter);
104            }
105        });
106    }
107
108    protected WrappedRecyclerView inflateWrappedRV() {
109        return (WrappedRecyclerView)
110                LayoutInflater.from(getActivity()).inflate(R.layout.wrapped_test_rv,
111                        getRecyclerViewContainer(), false);
112    }
113
114    void swapAdapter(final RecyclerView.Adapter adapter,
115            final boolean removeAndRecycleExistingViews) throws Throwable {
116        runTestOnUiThread(new Runnable() {
117            @Override
118            public void run() {
119                try {
120                    mRecyclerView.swapAdapter(adapter, removeAndRecycleExistingViews);
121                } catch (Throwable t) {
122                    postExceptionToInstrumentation(t);
123                }
124            }
125        });
126        checkForMainThreadException();
127    }
128
129    void postExceptionToInstrumentation(Throwable t) {
130        if (mInstrumentationThread == Thread.currentThread()) {
131            throw new RuntimeException(t);
132        }
133        if (mainThreadException != null) {
134            Log.e(TAG, "receiving another main thread exception. dropping.", t);
135        } else {
136            Log.e(TAG, "captured exception on main thread", t);
137            mainThreadException = t;
138        }
139
140        if (mRecyclerView != null && mRecyclerView
141                .getLayoutManager() instanceof TestLayoutManager) {
142            TestLayoutManager lm = (TestLayoutManager) mRecyclerView.getLayoutManager();
143            // finish all layouts so that we get the correct exception
144            while (lm.layoutLatch.getCount() > 0) {
145                lm.layoutLatch.countDown();
146            }
147        }
148    }
149
150    @Override
151    protected void tearDown() throws Exception {
152        if (mRecyclerView != null) {
153            try {
154                removeRecyclerView();
155            } catch (Throwable throwable) {
156                throwable.printStackTrace();
157            }
158        }
159        getInstrumentation().waitForIdleSync();
160        super.tearDown();
161
162        try {
163            checkForMainThreadException();
164        } catch (Exception e) {
165            throw e;
166        } catch (Throwable throwable) {
167            throw new Exception(throwable);
168        }
169    }
170
171    public Rect getDecoratedRecyclerViewBounds() {
172        return new Rect(
173                mRecyclerView.getPaddingLeft(),
174                mRecyclerView.getPaddingTop(),
175                mRecyclerView.getPaddingLeft() + mRecyclerView.getWidth(),
176                mRecyclerView.getPaddingTop() + mRecyclerView.getHeight()
177        );
178    }
179
180    public void removeRecyclerView() throws Throwable {
181        if (mRecyclerView == null) {
182            return;
183        }
184        if (!isMainThread()) {
185            getInstrumentation().waitForIdleSync();
186        }
187        runTestOnUiThread(new Runnable() {
188            @Override
189            public void run() {
190                try {
191                    final RecyclerView.Adapter adapter = mRecyclerView.getAdapter();
192                    if (adapter instanceof AttachDetachCountingAdapter) {
193                        ((AttachDetachCountingAdapter) adapter).getCounter()
194                                .validateRemaining(mRecyclerView);
195                    }
196                    getActivity().mContainer.removeAllViews();
197                } catch (Throwable t) {
198                    postExceptionToInstrumentation(t);
199                }
200            }
201        });
202        mRecyclerView = null;
203    }
204
205    void waitForAnimations(int seconds) throws InterruptedException {
206        final CountDownLatch latch = new CountDownLatch(2);
207        boolean running = mRecyclerView.mItemAnimator
208                .isRunning(new RecyclerView.ItemAnimator.ItemAnimatorFinishedListener() {
209                    @Override
210                    public void onAnimationsFinished() {
211                        latch.countDown();
212                    }
213                });
214        if (running) {
215            latch.await(seconds, TimeUnit.SECONDS);
216        }
217    }
218
219    public boolean requestFocus(final View view) {
220        final boolean[] result = new boolean[1];
221        getActivity().runOnUiThread(new Runnable() {
222            @Override
223            public void run() {
224                result[0] = view.requestFocus();
225            }
226        });
227        return result[0];
228    }
229
230    public void setRecyclerView(final RecyclerView recyclerView) throws Throwable {
231        setRecyclerView(recyclerView, true);
232    }
233    public void setRecyclerView(final RecyclerView recyclerView, boolean assignDummyPool)
234            throws Throwable {
235        setRecyclerView(recyclerView, assignDummyPool, true);
236    }
237    public void setRecyclerView(final RecyclerView recyclerView, boolean assignDummyPool,
238            boolean addPositionCheckItemAnimator)
239            throws Throwable {
240        mRecyclerView = recyclerView;
241        if (assignDummyPool) {
242            RecyclerView.RecycledViewPool pool = new RecyclerView.RecycledViewPool() {
243                @Override
244                public RecyclerView.ViewHolder getRecycledView(int viewType) {
245                    RecyclerView.ViewHolder viewHolder = super.getRecycledView(viewType);
246                    if (viewHolder == null) {
247                        return null;
248                    }
249                    viewHolder.addFlags(RecyclerView.ViewHolder.FLAG_BOUND);
250                    viewHolder.mPosition = 200;
251                    viewHolder.mOldPosition = 300;
252                    viewHolder.mPreLayoutPosition = 500;
253                    return viewHolder;
254                }
255
256                @Override
257                public void putRecycledView(RecyclerView.ViewHolder scrap) {
258                    assertNull(scrap.mOwnerRecyclerView);
259                    super.putRecycledView(scrap);
260                }
261            };
262            mRecyclerView.setRecycledViewPool(pool);
263        }
264        if (addPositionCheckItemAnimator) {
265            mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
266                @Override
267                public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
268                        RecyclerView.State state) {
269                    RecyclerView.ViewHolder vh = parent.getChildViewHolder(view);
270                    if (!vh.isRemoved()) {
271                        assertNotSame("If getItemOffsets is called, child should have a valid"
272                                            + " adapter position unless it is removed : " + vh,
273                                    vh.getAdapterPosition(), RecyclerView.NO_POSITION);
274                    }
275                }
276            });
277        }
278        mAdapterHelper = recyclerView.mAdapterHelper;
279        runTestOnUiThread(new Runnable() {
280            @Override
281            public void run() {
282                getActivity().mContainer.addView(recyclerView);
283            }
284        });
285    }
286
287    protected FrameLayout getRecyclerViewContainer() {
288        return getActivity().mContainer;
289    }
290
291    public void requestLayoutOnUIThread(final View view) {
292        try {
293            runTestOnUiThread(new Runnable() {
294                @Override
295                public void run() {
296                    view.requestLayout();
297                }
298            });
299        } catch (Throwable throwable) {
300            Log.e(TAG, "", throwable);
301        }
302    }
303
304    public void scrollBy(final int dt) {
305        try {
306            runTestOnUiThread(new Runnable() {
307                @Override
308                public void run() {
309                    if (mRecyclerView.getLayoutManager().canScrollHorizontally()) {
310                        mRecyclerView.scrollBy(dt, 0);
311                    } else {
312                        mRecyclerView.scrollBy(0, dt);
313                    }
314
315                }
316            });
317        } catch (Throwable throwable) {
318            Log.e(TAG, "", throwable);
319        }
320    }
321
322    void scrollToPosition(final int position) throws Throwable {
323        runTestOnUiThread(new Runnable() {
324            @Override
325            public void run() {
326                mRecyclerView.getLayoutManager().scrollToPosition(position);
327            }
328        });
329    }
330
331    void smoothScrollToPosition(final int position)
332            throws Throwable {
333        if (mDebug) {
334            Log.d(TAG, "SMOOTH scrolling to " + position);
335        }
336        runTestOnUiThread(new Runnable() {
337            @Override
338            public void run() {
339                mRecyclerView.smoothScrollToPosition(position);
340            }
341        });
342        getInstrumentation().waitForIdleSync();
343        Thread.sleep(200); //give scroller some time so start
344        while (mRecyclerView.getLayoutManager().isSmoothScrolling() ||
345                mRecyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
346            if (mDebug) {
347                Log.d(TAG, "SMOOTH scrolling step");
348            }
349            Thread.sleep(200);
350        }
351        if (mDebug) {
352            Log.d(TAG, "SMOOTH scrolling done");
353        }
354        getInstrumentation().waitForIdleSync();
355    }
356
357    class TestViewHolder extends RecyclerView.ViewHolder {
358
359        Item mBoundItem;
360
361        public TestViewHolder(View itemView) {
362            super(itemView);
363            itemView.setFocusable(true);
364        }
365
366        @Override
367        public String toString() {
368            return super.toString() + " item:" + mBoundItem;
369        }
370    }
371    class DumbLayoutManager extends TestLayoutManager {
372        ReentrantLock mLayoutLock = new ReentrantLock();
373        public void blockLayout() {
374            mLayoutLock.lock();
375        }
376
377        public void unblockLayout() {
378            mLayoutLock.unlock();
379        }
380        @Override
381        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
382            mLayoutLock.lock();
383            detachAndScrapAttachedViews(recycler);
384            layoutRange(recycler, 0, state.getItemCount());
385            if (layoutLatch != null) {
386                layoutLatch.countDown();
387            }
388            mLayoutLock.unlock();
389        }
390    }
391    public class TestLayoutManager extends RecyclerView.LayoutManager {
392        int mScrollVerticallyAmount;
393        int mScrollHorizontallyAmount;
394        protected CountDownLatch layoutLatch;
395
396        public void expectLayouts(int count) {
397            layoutLatch = new CountDownLatch(count);
398        }
399
400        public void waitForLayout(long timeout, TimeUnit timeUnit, boolean waitForIdle)
401                throws Throwable {
402            layoutLatch.await(timeout * (mDebug ? 100 : 1), timeUnit);
403            assertEquals("all expected layouts should be executed at the expected time",
404                    0, layoutLatch.getCount());
405            if (waitForIdle) {
406                getInstrumentation().waitForIdleSync();
407            }
408        }
409
410        public void waitForLayout(long timeout, TimeUnit timeUnit)
411                throws Throwable {
412            waitForLayout(timeout, timeUnit, true);
413        }
414
415        public void assertLayoutCount(int count, String msg, long timeout) throws Throwable {
416            layoutLatch.await(timeout, TimeUnit.SECONDS);
417            assertEquals(msg, count, layoutLatch.getCount());
418        }
419
420        public void assertNoLayout(String msg, long timeout) throws Throwable {
421            layoutLatch.await(timeout, TimeUnit.SECONDS);
422            assertFalse(msg, layoutLatch.getCount() == 0);
423        }
424
425        public void waitForLayout(long timeout) throws Throwable {
426            waitForLayout(timeout * (mDebug ? 10000 : 1), TimeUnit.SECONDS, true);
427        }
428
429        public void waitForLayout(long timeout, boolean waitForIdle) throws Throwable {
430            waitForLayout(timeout * (mDebug ? 10000 : 1), TimeUnit.SECONDS, waitForIdle);
431        }
432
433        @Override
434        public RecyclerView.LayoutParams generateDefaultLayoutParams() {
435            return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
436                    ViewGroup.LayoutParams.WRAP_CONTENT);
437        }
438
439        void assertVisibleItemPositions() {
440            int i = getChildCount();
441            TestAdapter testAdapter = (TestAdapter) mRecyclerView.getAdapter();
442            while (i-- > 0) {
443                View view = getChildAt(i);
444                RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();
445                Item item = ((TestViewHolder) lp.mViewHolder).mBoundItem;
446                if (mDebug) {
447                    Log.d(TAG, "testing item " + i);
448                }
449                if (!lp.isItemRemoved()) {
450                    RecyclerView.ViewHolder vh = mRecyclerView.getChildViewHolder(view);
451                    assertSame("item position in LP should match adapter value :" + vh,
452                            testAdapter.mItems.get(vh.mPosition), item);
453                }
454            }
455        }
456
457        RecyclerView.LayoutParams getLp(View v) {
458            return (RecyclerView.LayoutParams) v.getLayoutParams();
459        }
460
461        protected void layoutRange(RecyclerView.Recycler recycler, int start, int end) {
462            assertScrap(recycler);
463            if (mDebug) {
464                Log.d(TAG, "will layout items from " + start + " to " + end);
465            }
466            int diff = end > start ? 1 : -1;
467            int top = 0;
468            for (int i = start; i != end; i+=diff) {
469                if (mDebug) {
470                    Log.d(TAG, "laying out item " + i);
471                }
472                View view = recycler.getViewForPosition(i);
473                assertNotNull("view should not be null for valid position. "
474                        + "got null view at position " + i, view);
475                if (!mRecyclerView.mState.isPreLayout()) {
476                    RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view
477                            .getLayoutParams();
478                    assertFalse("In post layout, getViewForPosition should never return a view "
479                            + "that is removed", layoutParams != null
480                            && layoutParams.isItemRemoved());
481
482                }
483                assertEquals("getViewForPosition should return correct position",
484                        i, getPosition(view));
485                addView(view);
486
487                measureChildWithMargins(view, 0, 0);
488                if (getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL) {
489                    layoutDecorated(view, getWidth() - getDecoratedMeasuredWidth(view), top,
490                            getWidth(), top + getDecoratedMeasuredHeight(view));
491                } else {
492                    layoutDecorated(view, 0, top, getDecoratedMeasuredWidth(view)
493                            , top + getDecoratedMeasuredHeight(view));
494                }
495
496                top += view.getMeasuredHeight();
497            }
498        }
499
500        private void assertScrap(RecyclerView.Recycler recycler) {
501            if (mRecyclerView.getAdapter() != null &&
502                    !mRecyclerView.getAdapter().hasStableIds()) {
503                for (RecyclerView.ViewHolder viewHolder : recycler.getScrapList()) {
504                    assertFalse("Invalid scrap should be no kept", viewHolder.isInvalid());
505                }
506            }
507        }
508
509        @Override
510        public boolean canScrollHorizontally() {
511            return true;
512        }
513
514        @Override
515        public boolean canScrollVertically() {
516            return true;
517        }
518
519        @Override
520        public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler,
521                RecyclerView.State state) {
522            mScrollHorizontallyAmount += dx;
523            return dx;
524        }
525
526        @Override
527        public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler,
528                RecyclerView.State state) {
529            mScrollVerticallyAmount += dy;
530            return dy;
531        }
532    }
533
534    static class Item {
535        final static AtomicInteger idCounter = new AtomicInteger(0);
536        final public int mId = idCounter.incrementAndGet();
537
538        int mAdapterIndex;
539
540        final String mText;
541
542        Item(int adapterIndex, String text) {
543            mAdapterIndex = adapterIndex;
544            mText = text;
545        }
546
547        @Override
548        public String toString() {
549            return "Item{" +
550                    "mId=" + mId +
551                    ", originalIndex=" + mAdapterIndex +
552                    ", text='" + mText + '\'' +
553                    '}';
554        }
555    }
556
557    public class TestAdapter extends RecyclerView.Adapter<TestViewHolder>
558            implements AttachDetachCountingAdapter {
559
560        ViewAttachDetachCounter mAttachmentCounter = new ViewAttachDetachCounter();
561        List<Item> mItems;
562
563        public TestAdapter(int count) {
564            mItems = new ArrayList<Item>(count);
565            for (int i = 0; i < count; i++) {
566                mItems.add(new Item(i, "Item " + i));
567            }
568        }
569
570        @Override
571        public void onViewAttachedToWindow(TestViewHolder holder) {
572            super.onViewAttachedToWindow(holder);
573            mAttachmentCounter.onViewAttached(holder);
574        }
575
576        @Override
577        public void onViewDetachedFromWindow(TestViewHolder holder) {
578            super.onViewDetachedFromWindow(holder);
579            mAttachmentCounter.onViewDetached(holder);
580        }
581
582        @Override
583        public void onAttachedToRecyclerView(RecyclerView recyclerView) {
584            super.onAttachedToRecyclerView(recyclerView);
585            mAttachmentCounter.onAttached(recyclerView);
586        }
587
588        @Override
589        public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
590            super.onDetachedFromRecyclerView(recyclerView);
591            mAttachmentCounter.onDetached(recyclerView);
592        }
593
594        @Override
595        public TestViewHolder onCreateViewHolder(ViewGroup parent,
596                int viewType) {
597            return new TestViewHolder(new TextView(parent.getContext()));
598        }
599
600        @Override
601        public void onBindViewHolder(TestViewHolder holder, int position) {
602            assertNotNull(holder.mOwnerRecyclerView);
603            assertEquals(position, holder.getAdapterPosition());
604            final Item item = mItems.get(position);
605            ((TextView) (holder.itemView)).setText(item.mText + "(" + item.mAdapterIndex + ")");
606            holder.mBoundItem = item;
607        }
608
609        public Item getItemAt(int position) {
610            return mItems.get(position);
611        }
612
613        @Override
614        public void onViewRecycled(TestViewHolder holder) {
615            super.onViewRecycled(holder);
616            final int adapterPosition = holder.getAdapterPosition();
617            final boolean shouldHavePosition = !holder.isRemoved() && holder.isBound() &&
618                    !holder.isAdapterPositionUnknown() && !holder.isInvalid();
619            String log = "Position check for " + holder.toString();
620            assertEquals(log, shouldHavePosition, adapterPosition != RecyclerView.NO_POSITION);
621            if (shouldHavePosition) {
622                assertTrue(log, mItems.size() > adapterPosition);
623                assertSame(log, holder.mBoundItem, mItems.get(adapterPosition));
624            }
625        }
626
627        public void deleteAndNotify(final int start, final int count) throws Throwable {
628            deleteAndNotify(new int[]{start, count});
629        }
630
631        /**
632         * Deletes items in the given ranges.
633         * <p>
634         * Note that each operation affects the one after so you should offset them properly.
635         * <p>
636         * For example, if adapter has 5 items (A,B,C,D,E), and then you call this method with
637         * <code>[1, 2],[2, 1]</code>, it will first delete items B,C and the new adapter will be
638         * A D E. Then it will delete 2,1 which means it will delete E.
639         */
640        public void deleteAndNotify(final int[]... startCountTuples) throws Throwable {
641            for (int[] tuple : startCountTuples) {
642                tuple[1] = -tuple[1];
643            }
644            new AddRemoveRunnable(startCountTuples).runOnMainThread();
645        }
646
647        @Override
648        public long getItemId(int position) {
649            return hasStableIds() ? mItems.get(position).mId : super.getItemId(position);
650        }
651
652        public void offsetOriginalIndices(int start, int offset) {
653            for (int i = start; i < mItems.size(); i++) {
654                mItems.get(i).mAdapterIndex += offset;
655            }
656        }
657
658        /**
659         * @param start inclusive
660         * @param end exclusive
661         * @param offset
662         */
663        public void offsetOriginalIndicesBetween(int start, int end, int offset) {
664            for (int i = start; i < end && i < mItems.size(); i++) {
665                mItems.get(i).mAdapterIndex += offset;
666            }
667        }
668
669        public void addAndNotify(final int start, final int count) throws Throwable {
670            addAndNotify(new int[]{start, count});
671        }
672
673        public void addAndNotify(final int[]... startCountTuples) throws Throwable {
674            new AddRemoveRunnable(startCountTuples).runOnMainThread();
675        }
676
677        public void dispatchDataSetChanged() throws Throwable {
678            runTestOnUiThread(new Runnable() {
679                @Override
680                public void run() {
681                    notifyDataSetChanged();
682                }
683            });
684        }
685
686        public void changeAndNotify(final int start, final int count) throws Throwable {
687            runTestOnUiThread(new Runnable() {
688                @Override
689                public void run() {
690                    notifyItemRangeChanged(start, count);
691                }
692            });
693        }
694
695        public void changePositionsAndNotify(final int... positions) throws Throwable {
696            runTestOnUiThread(new Runnable() {
697                @Override
698                public void run() {
699                    for (int i = 0; i < positions.length; i += 1) {
700                        TestAdapter.super.notifyItemRangeChanged(positions[i], 1);
701                    }
702                }
703            });
704        }
705
706        /**
707         * Similar to other methods but negative count means delete and position count means add.
708         * <p>
709         * For instance, calling this method with <code>[1,1], [2,-1]</code> it will first add an
710         * item to index 1, then remove an item from index 2 (updated index 2)
711         */
712        public void addDeleteAndNotify(final int[]... startCountTuples) throws Throwable {
713            new AddRemoveRunnable(startCountTuples).runOnMainThread();
714        }
715
716        @Override
717        public int getItemCount() {
718            return mItems.size();
719        }
720
721        /**
722         * Uses notifyDataSetChanged
723         */
724        public void moveItems(boolean notifyChange, int[]... fromToTuples) throws Throwable {
725            for (int i = 0; i < fromToTuples.length; i += 1) {
726                int[] tuple = fromToTuples[i];
727                moveItem(tuple[0], tuple[1], false);
728            }
729            if (notifyChange) {
730                dispatchDataSetChanged();
731            }
732        }
733
734        /**
735         * Uses notifyDataSetChanged
736         */
737        public void moveItem(final int from, final int to, final boolean notifyChange)
738                throws Throwable {
739            runTestOnUiThread(new Runnable() {
740                @Override
741                public void run() {
742                    moveInUIThread(from, to);
743                    if (notifyChange) {
744                        notifyDataSetChanged();
745                    }
746                }
747            });
748        }
749
750        /**
751         * Uses notifyItemMoved
752         */
753        public void moveAndNotify(final int from, final int to) throws Throwable {
754            runTestOnUiThread(new Runnable() {
755                @Override
756                public void run() {
757                    moveInUIThread(from, to);
758                    notifyItemMoved(from, to);
759                }
760            });
761        }
762
763        protected void moveInUIThread(int from, int to) {
764            Item item = mItems.remove(from);
765            offsetOriginalIndices(from, -1);
766            mItems.add(to, item);
767            offsetOriginalIndices(to + 1, 1);
768            item.mAdapterIndex = to;
769        }
770
771
772        @Override
773        public ViewAttachDetachCounter getCounter() {
774            return mAttachmentCounter;
775        }
776
777
778        private class AddRemoveRunnable implements Runnable {
779            final int[][] mStartCountTuples;
780
781            public AddRemoveRunnable(int[][] startCountTuples) {
782                mStartCountTuples = startCountTuples;
783            }
784
785            public void runOnMainThread() throws Throwable {
786                if (Looper.myLooper() == Looper.getMainLooper()) {
787                    run();
788                } else {
789                    runTestOnUiThread(this);
790                }
791            }
792
793            @Override
794            public void run() {
795                for (int[] tuple : mStartCountTuples) {
796                    if (tuple[1] < 0) {
797                        delete(tuple);
798                    } else {
799                        add(tuple);
800                    }
801                }
802            }
803
804            private void add(int[] tuple) {
805                // offset others
806                offsetOriginalIndices(tuple[0], tuple[1]);
807                for (int i = 0; i < tuple[1]; i++) {
808                    mItems.add(tuple[0], new Item(i, "new item " + i));
809                }
810                notifyItemRangeInserted(tuple[0], tuple[1]);
811            }
812
813            private void delete(int[] tuple) {
814                final int count = -tuple[1];
815                offsetOriginalIndices(tuple[0] + count, tuple[1]);
816                for (int i = 0; i < count; i++) {
817                    mItems.remove(tuple[0]);
818                }
819                notifyItemRangeRemoved(tuple[0], count);
820            }
821        }
822    }
823
824    public boolean isMainThread() {
825        return Looper.myLooper() == Looper.getMainLooper();
826    }
827
828    @Override
829    public void runTestOnUiThread(Runnable r) throws Throwable {
830        if (Looper.myLooper() == Looper.getMainLooper()) {
831            r.run();
832        } else {
833            super.runTestOnUiThread(r);
834        }
835    }
836
837    static class TargetTuple {
838
839        final int mPosition;
840
841        final int mLayoutDirection;
842
843        TargetTuple(int position, int layoutDirection) {
844            this.mPosition = position;
845            this.mLayoutDirection = layoutDirection;
846        }
847
848        @Override
849        public String toString() {
850            return "TargetTuple{" +
851                    "mPosition=" + mPosition +
852                    ", mLayoutDirection=" + mLayoutDirection +
853                    '}';
854        }
855    }
856
857    public interface AttachDetachCountingAdapter {
858
859        ViewAttachDetachCounter getCounter();
860    }
861
862    public class ViewAttachDetachCounter {
863
864        Set<RecyclerView.ViewHolder> mAttachedSet = new HashSet<RecyclerView.ViewHolder>();
865
866        public void validateRemaining(RecyclerView recyclerView) {
867            final int childCount = recyclerView.getChildCount();
868            for (int i = 0; i < childCount; i++) {
869                View view = recyclerView.getChildAt(i);
870                RecyclerView.ViewHolder vh = recyclerView.getChildViewHolder(view);
871                assertTrue("remaining view should be in attached set " + vh,
872                        mAttachedSet.contains(vh));
873            }
874            assertEquals("there should not be any views left in attached set",
875                    childCount, mAttachedSet.size());
876        }
877
878        public void onViewDetached(RecyclerView.ViewHolder viewHolder) {
879            try {
880                assertTrue("view holder should be in attached set",
881                        mAttachedSet.remove(viewHolder));
882            } catch (Throwable t) {
883                postExceptionToInstrumentation(t);
884            }
885        }
886
887        public void onViewAttached(RecyclerView.ViewHolder viewHolder) {
888            try {
889                assertTrue("view holder should not be in attached set",
890                        mAttachedSet.add(viewHolder));
891            } catch (Throwable t) {
892                postExceptionToInstrumentation(t);
893            }
894        }
895
896        public void onAttached(RecyclerView recyclerView) {
897            // when a new RV is attached, clear the set and add all view holders
898            mAttachedSet.clear();
899            final int childCount = recyclerView.getChildCount();
900            for (int i = 0; i < childCount; i ++) {
901                View view = recyclerView.getChildAt(i);
902                mAttachedSet.add(recyclerView.getChildViewHolder(view));
903            }
904        }
905
906        public void onDetached(RecyclerView recyclerView) {
907            validateRemaining(recyclerView);
908        }
909    }
910}
911