Android Open Source - ListViewAnimations Swipe Undo Touch Listener Test






From Project

Back to project page ListViewAnimations.

License

The source code is released under:

Apache License

If you think the Android project ListViewAnimations listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Niek Haarman/*from ww w .  ja  v a  2  s .  com*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo;

import android.test.ActivityInstrumentationTestCase2;
import android.view.View;
import android.widget.AbsListView;

import com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeTouchListenerTestActivity;
import com.nhaarman.listviewanimations.util.AbsListViewWrapper;

import org.mockito.*;

import static com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.MotionEventUtils.dispatchSwipeMotionEvents;
import static com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.MotionEventUtils.dispatchSwipeMotionEventsAndWait;
import static org.mockito.AdditionalMatchers.*;
import static org.mockito.Mockito.*;

public class SwipeUndoTouchListenerTest extends ActivityInstrumentationTestCase2<SwipeTouchListenerTestActivity> {

    /**
     * An Activity hosting a ListView with items.
     */
    private SwipeTouchListenerTestActivity mActivity;

    /**
     * The AbsListView that is hosted in mActivity.
     */
    private AbsListView mAbsListView;

    @Mock
    private UndoCallback mUndoCallback;

    private SwipeUndoTouchListener mSwipeUndoTouchListener;


    public SwipeUndoTouchListenerTest() {
        super(SwipeTouchListenerTestActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();

        MockitoAnnotations.initMocks(this);
        when(mUndoCallback.getUndoView(any(View.class))).thenReturn(new View(getActivity()));
        when(mUndoCallback.getPrimaryView(any(View.class))).thenReturn(new View(getActivity()));

        mActivity = getActivity();
        mAbsListView = mActivity.getAbsListView();

        mSwipeUndoTouchListener = new SwipeUndoTouchListener(new AbsListViewWrapper(mAbsListView), mUndoCallback);
        mAbsListView.setOnTouchListener(mSwipeUndoTouchListener);

        getInstrumentation().waitForIdleSync();
    }

    /**
     * Tests whether swiping an item once triggers UndoCallback#onUndoShown.
     */
    public void testUndoShown() throws InterruptedException {
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        verify(mUndoCallback).onUndoShown(any(View.class), eq(0));
    }

    /**
     * Tests whether swiping an item twice triggers UndoCallback#onDismiss.
     */
    public void testDismiss() throws InterruptedException {
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        verify(mUndoCallback).onUndoShown(any(View.class), eq(0));
        verify(mUndoCallback, never()).onDismiss(any(View.class), anyInt());

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        verify(mUndoCallback).onDismiss(any(View.class), eq(0));
    }

    /**
     * Tests whether swiping multiple items triggers onUndoShown, but not onDismiss.
     */
    public void testMultipleUndo() throws InterruptedException {
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        verify(mUndoCallback).onUndoShown(any(View.class), eq(0));

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 1);

        verify(mUndoCallback).onUndoShown(any(View.class), eq(1));

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 2);

        verify(mUndoCallback).onUndoShown(any(View.class), eq(2));

        verify(mUndoCallback, never()).onDismiss(any(View.class), anyInt());
    }

    /**
     * Tests whether multiple dismisses are correctly handled.
     */
    public void testMultipleDismisses() throws InterruptedException {
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 1);
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 2);

        verify(mUndoCallback, times(3)).onUndoShown(any(View.class), anyInt());
        verify(mUndoCallback, never()).onDismiss(any(View.class), anyInt());

        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 1);
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 2);

        verify(mUndoCallback, times(3)).onDismiss(any(View.class), anyInt());
        verify(mUndoCallback).onDismiss(eq(mAbsListView), aryEq(new int[]{2, 1, 0}));
    }

    /**
     * Tests whether the last item is dismissable after some other items have been dismissed.
     */
    public void testLastItemDismissable_itemsDismissed() throws InterruptedException {
        /* Given some items are dismissed */
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 1);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 2);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
        dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 1);
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 2);

        /* When trying to dismiss the last item */
        int lastPosition = mAbsListView.getAdapter().getCount() - 1;
        mAbsListView.smoothScrollToPosition(lastPosition);

        Thread.sleep(15000); // Wait for the smooth scroll to settle;

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, lastPosition); // Swipe to show undo
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, lastPosition); // Swipe to dismiss

        /* Then I should be notified of dismissing the last item. */
        verify(mUndoCallback).onDismiss(any(View.class), eq(lastPosition));
    }

    /**
     * Tests whether the last item is dismissable after some an item has been dismissed and undone.
     */
    public void testLastItemDismissable_itemUndone() throws InterruptedException {
        /* Given an item is dismissed and undone */
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);
        mSwipeUndoTouchListener.undo(mAbsListView.getChildAt(0));

        /* When trying to dismiss the last item */
        int lastPosition = mAbsListView.getAdapter().getCount() - 1;
        mAbsListView.smoothScrollToPosition(lastPosition);

        Thread.sleep(5000); // Wait for the smooth scroll to settle;

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, lastPosition); // Swipe to show undo
        mAbsListView.smoothScrollToPosition(lastPosition);
        Thread.sleep(5000);
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, lastPosition); // Swipe to dismiss

        /* Then I should be notified of dismissing the last item. */
        verify(mUndoCallback).onDismiss(any(View.class), eq(lastPosition));
    }
}




Java Source Code List

com.haarman.listviewanimations.BaseActivity.java
com.haarman.listviewanimations.MainActivity.java
com.haarman.listviewanimations.MyListActivity.java
com.haarman.listviewanimations.MyListAdapter.java
com.haarman.listviewanimations.StickyListHeadersActivity.java
com.haarman.listviewanimations.appearance.AppearanceExamplesActivity.java
com.haarman.listviewanimations.googlecards.GoogleCardsActivity.java
com.haarman.listviewanimations.googlecards.GoogleCardsAdapter.java
com.haarman.listviewanimations.gridview.GridViewActivity.java
com.haarman.listviewanimations.gridview.GridViewAdapter.java
com.haarman.listviewanimations.itemmanipulation.DynamicListViewActivity.java
com.haarman.listviewanimations.itemmanipulation.ItemManipulationsExamplesActivity.java
com.haarman.listviewanimations.itemmanipulation.expandablelistitems.ExpandableListItemActivity.java
com.haarman.listviewanimations.itemmanipulation.expandablelistitems.MyExpandableListItemAdapter.java
com.haarman.listviewanimations.util.BitmapCache.java
com.nhaarman.listviewanimations.ArrayAdapterTest.java
com.nhaarman.listviewanimations.ArrayAdapter.java
com.nhaarman.listviewanimations.BaseAdapterDecoratorTest.java
com.nhaarman.listviewanimations.BaseAdapterDecorator.java
com.nhaarman.listviewanimations.appearance.AnimationAdapter.java
com.nhaarman.listviewanimations.appearance.ResourceAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.SingleAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.StickyListHeadersAdapterDecorator.java
com.nhaarman.listviewanimations.appearance.ViewAnimatorTest.java
com.nhaarman.listviewanimations.appearance.ViewAnimator.java
com.nhaarman.listviewanimations.appearance.simple.AlphaInAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.simple.ScaleInAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.simple.SwingLeftInAnimationAdapter.java
com.nhaarman.listviewanimations.appearance.simple.SwingRightInAnimationAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.DynamicListView.java
com.nhaarman.listviewanimations.itemmanipulation.TouchEventHandler.java
com.nhaarman.listviewanimations.itemmanipulation.animateaddition.AnimateAdditionAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.animateaddition.InsertQueueTest.java
com.nhaarman.listviewanimations.itemmanipulation.animateaddition.InsertQueue.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.BitmapUtils.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DragAndDropHandler.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DragAndDropListViewWrapper.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DraggableManager.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListViewDragAndDropTest.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListViewTestActivity.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.DynamicListViewWrapper.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.HoverDrawableTest.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.HoverDrawable.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.MotionEventUtils.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovedListener.java
com.nhaarman.listviewanimations.itemmanipulation.dragdrop.TouchViewDraggableManager.java
com.nhaarman.listviewanimations.itemmanipulation.expandablelistitem.ExpandableListItemAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.matchers.Matchers.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.DismissableManager.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.MotionEventUtils.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.OnDismissCallback.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissTouchListenerTest.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeDismissTouchListener.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeTouchListenerTestActivity.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeTouchListenerTest.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.SwipeTouchListener.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SimpleSwipeUndoAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SwipeUndoAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SwipeUndoTouchListenerTest.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SwipeUndoTouchListener.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.SwipeUndoView.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.TimedUndoAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.UndoAdapter.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.UndoCallback.java
com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.undo.Util.java
com.nhaarman.listviewanimations.util.AbsListViewWrapperTest.java
com.nhaarman.listviewanimations.util.AbsListViewWrapper.java
com.nhaarman.listviewanimations.util.AdapterViewUtilTest.java
com.nhaarman.listviewanimations.util.AdapterViewUtil.java
com.nhaarman.listviewanimations.util.AnimatorUtilTest.java
com.nhaarman.listviewanimations.util.AnimatorUtil.java
com.nhaarman.listviewanimations.util.Insertable.java
com.nhaarman.listviewanimations.util.ListViewWrapperSetter.java
com.nhaarman.listviewanimations.util.ListViewWrapper.java
com.nhaarman.listviewanimations.util.StickyListHeadersListViewWrapper.java
com.nhaarman.listviewanimations.util.Swappable.java