Android Open Source - ListViewAnimations Swipe 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 w w w  .  java2s. c  o  m
 *
 * 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;

import android.support.annotation.NonNull;
import android.test.ActivityInstrumentationTestCase2;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;

import com.nhaarman.listviewanimations.util.AbsListViewWrapper;

import java.util.List;

import static com.nhaarman.listviewanimations.itemmanipulation.swipedismiss.MotionEventUtils.dispatchSwipeMotionEventsAndWait;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;


@SuppressWarnings({"AnonymousInnerClass", "AnonymousInnerClassMayBeStatic"})
public class SwipeTouchListenerTest extends ActivityInstrumentationTestCase2<SwipeTouchListenerTestActivity> {

    /**
     * The SwipeTouchListener under test.
     */
    private TestSwipeTouchListener mSwipeTouchListener;

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

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

    /**
     * The width of the AbsListView.
     */
    private float mViewWidth;

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

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

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

        mSwipeTouchListener = new TestSwipeTouchListener(new AbsListViewWrapper(mAbsListView));
        mAbsListView.setOnTouchListener(mSwipeTouchListener);

        getInstrumentation().waitForIdleSync();
    }


    /**
     * Tests whether retrieving the AbsListView yields the original AbsListView that was set.
     */
    public void testAbsListViewSet() {
        assertThat(mSwipeTouchListener.getListViewWrapper().getListView(), is((ViewGroup) mAbsListView));
    }

    /**
     * Tests whether swiping the first View triggers a call to SwipeTouchListener#afterViewFling.
     */
    public void testSwipeFirstViewCallback() throws InterruptedException {
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
        assertThat(mSwipeTouchListener.position, is(0));
    }

    /**
     * Tests whether swiping the first View from right to left triggers a call to SwipeTouchListener#afterViewFling.
     */
    public void testReverseSwipeFirstViewCallback() throws InterruptedException {
        MotionEventUtils.dispatchReverseSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
        assertThat(mSwipeTouchListener.position, is(0));
    }

    /**
     * Tests whether swiping the last View triggers a call to SwipeTouchListener#afterViewFling.
     */
    public void testSwipeLastViewCallback() throws InterruptedException {
        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, mAbsListView.getLastVisiblePosition() - 1);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
        assertThat(mSwipeTouchListener.position, is(mAbsListView.getLastVisiblePosition() - 1));
    }

    /**
     * Tests whether swiping shorter than half of the view width doesn't trigger a call to SwipeTouchLister#afterViewFling.
     */
    public void testShortSwipe() throws InterruptedException {
        List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, 10, mViewWidth / 2 - mViewWidth / 10);
        MotionEventUtils.dispatchMotionEventsAndWait(getInstrumentation(), motionEvents);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
    }

    /**
     * Tests whether swiping shorter than half of the view width from right to left doesn't trigger a call to SwipeTouchLister#afterViewFling.
     */
    public void testReverseShortSwipe() throws InterruptedException {
        List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, mViewWidth - 10, mViewWidth / 2 + mViewWidth / 10);
        MotionEventUtils.dispatchMotionEventsAndWait(getInstrumentation(), motionEvents);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
    }

    /**
     * Tests whether calling SwipeTouchListener#fling(int) triggers a call to SwipeTouchListener#afterViewFling.
     */
    public void testFling() throws InterruptedException {
        mActivity.runOnUiThread(
                new Runnable() {

                    @Override
                    public void run() {
                        mSwipeTouchListener.fling(0);
                    }
                }
        );

        Thread.sleep(1000);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
        assertThat(mSwipeTouchListener.position, is(0));
    }

    /**
     * Tests whether trying to dismiss an item that is specified not to be dismissable doesn't trigger a call to SwipeTouchListener#afterViewFling.
     */
    public void testDismissableManager() throws InterruptedException {
        mSwipeTouchListener.setDismissableManager(
                new DismissableManager() {

                    @Override
                    public boolean isDismissable(final long id, final int position) {
                        return false;
                    }
                }
        );

        List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, 10, mViewWidth - 10);
        MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
    }

    /**
     * Tests whether the isSwiping method returns proper values.
     */
    public void testIsSwiping() throws InterruptedException {
        List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, 10, mViewWidth - 10);

        assertThat(mSwipeTouchListener.isSwiping(), is(false));

        /* Send first half of the MotionEvents */
        MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents.subList(0, motionEvents.size() / 2));

        assertThat(mSwipeTouchListener.isSwiping(), is(true));

        /* Send second half of the MotionEvents */
        MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents.subList(motionEvents.size() / 2, motionEvents.size()));

        assertThat(mSwipeTouchListener.isSwiping(), is(false));
    }

    /**
     * Test whether disabling swipe and swiping an item does not trigger SwipeTouchListener#afterViewFling, and enabling it again does trigger the call.
     */
    public void testEnableDisableSwipe() throws InterruptedException {
        mSwipeTouchListener.disableSwipe();

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));

        mSwipeTouchListener.enableSwipe();

        dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

        assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
    }

    private static class TestSwipeTouchListener extends SwipeTouchListener {

        boolean afterViewFlingCalled;

        int position;

        TestSwipeTouchListener(final AbsListViewWrapper absListViewWrapper) {
            super(absListViewWrapper);
        }

        @Override
        protected boolean willLeaveDataSetOnFling(@NonNull final View view, final int position) {
            return true;
        }

        @Override
        protected void afterViewFling(@NonNull final View view, final int position) {
            afterViewFlingCalled = true;
            this.position = position;
        }
    }
}




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