protected HorizontalOverScrollBounceEffectDecorator getUUT(float touchDragRatioFwd, float touchDragRatioBck) { HorizontalOverScrollBounceEffectDecorator uut = new HorizontalOverScrollBounceEffectDecorator(mViewAdapter, touchDragRatioFwd, touchDragRatioBck, DEFAULT_DECELERATE_FACTOR); uut.setOverScrollStateListener(mStateListener); uut.setOverScrollUpdateListener(mUpdateListener); return uut; } }
@Test public void detach_decoratorIsAttached_detachFromView() throws Exception { // Arrange HorizontalOverScrollBounceEffectDecorator uut = new HorizontalOverScrollBounceEffectDecorator(mViewAdapter); // Act uut.detach(); // Assert verify(mView).setOnTouchListener(eq((View.OnTouchListener) null)); verify(mView).setOverScrollMode(View.OVER_SCROLL_ALWAYS); }
@Test public void onTouchMoveAction_notInViewEnds_ignoreTouchEvent() throws Exception { // Arrange MotionEvent event = createShortRightMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(false); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(false); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act boolean ret = uut.onTouch(mView, event); // Assert verify(mView, never()).setTranslationX(anyFloat()); verify(mView, never()).setTranslationY(anyFloat()); assertFalse(ret); assertEquals(STATE_IDLE, uut.getCurrentState()); verify(mStateListener, never()).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); verify(mUpdateListener, never()).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
public static IOverScrollDecor setUpOverScroll(ViewPager viewPager) { return new HorizontalOverScrollBounceEffectDecorator(new ViewPagerOverScrollDecorAdapter(viewPager)); }
@Test public void detach_overScrollInEffect_detachFromView() throws Exception { when(mViewAdapter.isInAbsoluteStart()).thenReturn(true); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(false); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); uut.onTouch(mView, createShortRightMoveEvent()); // Act uut.detach(); // Assert verify(mView).setOnTouchListener(eq((View.OnTouchListener) null)); verify(mView).setOverScrollMode(View.OVER_SCROLL_ALWAYS); }
/** * TODO: Make this work using a decent animation shadows / newer Robolectric * @throws Exception */ @Ignore @Test public void onTouchUpAction_eventWhenLeftOverscrolling_smoothScrollBackToRightEnd() throws Exception { // Arrange // Bring UUT to a left-overscroll state MotionEvent moveEvent = createShortLeftMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(false); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(true); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); uut.onTouch(mView, moveEvent); reset(mView); // Make the view as though it's been moved by the move event float viewX = moveEvent.getX(); when(mView.getTranslationX()).thenReturn(viewX); MotionEvent upEvent = createDefaultUpActionEvent(); // Act boolean ret = uut.onTouch(mView, upEvent); // Assert assertTrue(ret); verify(mView, atLeastOnce()).setTranslationX(anyFloat()); }
@Test public void onTouchMoveAction_dragLeftInRightEnd_overscrollLeft() throws Exception { // Arrange MotionEvent event = createShortLeftMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(false); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(true); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act final boolean ret = uut.onTouch(mView, event); // Assert final float expectedTransX = (event.getX() - event.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; verify(mView).setTranslationX(expectedTransX); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_DRAG_END_SIDE, uut.getCurrentState()); verify(mStateListener).onOverScrollStateChange(eq(uut), eq(STATE_IDLE), eq(STATE_DRAG_END_SIDE)); verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_END_SIDE), eq(expectedTransX)); }
public static IOverScrollDecor setUpOverScroll(HorizontalScrollView scrollView) { return new HorizontalOverScrollBounceEffectDecorator(new HorizontalScrollViewOverScrollDecorAdapter(scrollView)); }
@Test public void onTouchMoveAction_dragLeftInLeftEnd_ignoreTouchEvent() throws Exception { // Arrange MotionEvent event = createShortLeftMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(true); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(false); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act final boolean ret = uut.onTouch(mView, event); // Assert verify(mView, never()).setTranslationX(anyFloat()); verify(mView, never()).setTranslationY(anyFloat()); assertFalse(ret); assertEquals(STATE_IDLE, uut.getCurrentState()); verify(mStateListener, never()).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); verify(mUpdateListener, never()).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
protected HorizontalOverScrollBounceEffectDecorator getUUT() { HorizontalOverScrollBounceEffectDecorator uut = new HorizontalOverScrollBounceEffectDecorator(mViewAdapter); uut.setOverScrollStateListener(mStateListener); uut.setOverScrollUpdateListener(mUpdateListener); return uut; }
@Test public void detach_decoratorIsAttached_detachFromView() throws Exception { // Arrange HorizontalOverScrollBounceEffectDecorator uut = new HorizontalOverScrollBounceEffectDecorator(mViewAdapter); // Act uut.detach(); // Assert verify(mView).setOnTouchListener(eq((View.OnTouchListener) null)); verify(mView).setOverScrollMode(View.OVER_SCROLL_ALWAYS); }
/** * Set up the over-scroll over a generic view, assumed to always be over-scroll ready (e.g. * a plain text field, image view). * * @param view The view. * @param orientation One of {@link #ORIENTATION_HORIZONTAL} or {@link #ORIENTATION_VERTICAL}. * * @return The over-scroll effect 'decorator', enabling further effect configuration. */ public static IOverScrollDecor setUpStaticOverScroll(View view, int orientation) { switch (orientation) { case ORIENTATION_HORIZONTAL: return new HorizontalOverScrollBounceEffectDecorator(new StaticOverScrollDecorAdapter(view)); case ORIENTATION_VERTICAL: return new VerticalOverScrollBounceEffectDecorator(new StaticOverScrollDecorAdapter(view)); default: throw new IllegalArgumentException("orientation"); } }
@Test public void onTouchMoveAction_dragRightInLeftEnd_overscrollRight() throws Exception { // Arrange MotionEvent event = createShortRightMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(true); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(false); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act final boolean ret = uut.onTouch(mView, event); // Assert final float expectedTransX = (event.getX() - event.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; verify(mView).setTranslationX(expectedTransX); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_DRAG_START_SIDE, uut.getCurrentState()); verify(mStateListener).onOverScrollStateChange(eq(uut), eq(STATE_IDLE), eq(STATE_DRAG_START_SIDE)); verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_START_SIDE), eq(expectedTransX)); }
/** * Set up the over-scroll effect over a specified {@link RecyclerView} view. * <br/>Only recycler-views using <b>native</b> Android layout managers (i.e. {@link LinearLayoutManager}, * {@link GridLayoutManager} and {@link StaggeredGridLayoutManager}) are currently supported * by this convenience method. * * @param recyclerView The view. * @param orientation Either {@link #ORIENTATION_HORIZONTAL} or {@link #ORIENTATION_VERTICAL}. * * @return The over-scroll effect 'decorator', enabling further effect configuration. */ public static IOverScrollDecor setUpOverScroll(RecyclerView recyclerView, int orientation) { switch (orientation) { case ORIENTATION_HORIZONTAL: return new HorizontalOverScrollBounceEffectDecorator(new RecyclerViewOverScrollDecorAdapter(recyclerView)); case ORIENTATION_VERTICAL: return new VerticalOverScrollBounceEffectDecorator(new RecyclerViewOverScrollDecorAdapter(recyclerView)); default: throw new IllegalArgumentException("orientation"); } }
@Test public void onTouchMoveAction_dragRightInRightEnd_ignoreTouchEvent() throws Exception { // Arrange MotionEvent event = createShortRightMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(false); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(true); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act boolean ret = uut.onTouch(mView, event); // Assert verify(mView, never()).setTranslationX(anyFloat()); verify(mView, never()).setTranslationY(anyFloat()); assertFalse(ret); assertEquals(STATE_IDLE, uut.getCurrentState()); verify(mStateListener, never()).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); verify(mUpdateListener, never()).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
@Test public void onTouchUpAction_eventWhenNotOverscrolled_ignoreTouchEvent() throws Exception { // Arrange MotionEvent event = createDefaultUpActionEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(true); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(true); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); // Act boolean ret = uut.onTouch(mView, event); // Assert verify(mView, never()).setTranslationX(anyFloat()); verify(mView, never()).setTranslationY(anyFloat()); assertFalse(ret); assertEquals(STATE_IDLE, uut.getCurrentState()); verify(mStateListener, never()).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); verify(mUpdateListener, never()).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
@Test public void onTouchMoveAction_2ndLeftDragInRightEnd_overscrollLeftFurther() throws Exception { // Arrange // Bring UUT to a left-overscroll state MotionEvent event1 = createShortLeftMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(false); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(true); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); uut.onTouch(mView, event1); reset(mView); // Create 2nd left-drag event MotionEvent event2 = createLongLeftMoveEvent(); // Act final boolean ret = uut.onTouch(mView, event2); // Assert final float expectedTransX1 = (event1.getX() - event1.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; final float expectedTransX2 = (event2.getX() - event2.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; verify(mView).setTranslationX(expectedTransX2); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_DRAG_END_SIDE, uut.getCurrentState()); // State-change listener called only once? verify(mStateListener).onOverScrollStateChange(eq(uut), eq(STATE_IDLE), eq(STATE_DRAG_END_SIDE)); verify(mStateListener).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); // Update-listener called exactly twice? verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_END_SIDE), eq(expectedTransX1)); verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_END_SIDE), eq(expectedTransX2)); verify(mUpdateListener, times(2)).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
@Test public void onTouchMoveAction_2ndRightDragInLeftEnd_overscrollRightFurther() throws Exception { // Arrange // Bring UUT to a right-overscroll state MotionEvent event1 = createShortRightMoveEvent(); when(mViewAdapter.isInAbsoluteStart()).thenReturn(true); when(mViewAdapter.isInAbsoluteEnd()).thenReturn(false); HorizontalOverScrollBounceEffectDecorator uut = getUUT(); uut.onTouch(mView, event1); reset(mView); // Create 2nd right-drag event MotionEvent event2 = createLongRightMoveEvent(); // Act final boolean ret = uut.onTouch(mView, event2); // Assert final float expectedTransX1 = (event1.getX() - event1.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; final float expectedTransX2 = (event2.getX() - event2.getHistoricalX(0)) / DEFAULT_TOUCH_DRAG_MOVE_RATIO_FWD; verify(mView).setTranslationX(expectedTransX2); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_DRAG_START_SIDE, uut.getCurrentState()); // State-change listener called only once? verify(mStateListener).onOverScrollStateChange(eq(uut), eq(STATE_IDLE), eq(STATE_DRAG_START_SIDE)); verify(mStateListener).onOverScrollStateChange(eq(uut), anyInt(), anyInt()); // Update-listener called exactly twice? verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_START_SIDE), eq(expectedTransX1)); verify(mUpdateListener).onOverScrollUpdate(eq(uut), eq(STATE_DRAG_START_SIDE), eq(expectedTransX2)); verify(mUpdateListener, times(2)).onOverScrollUpdate(eq(uut), anyInt(), anyFloat()); }
uut.onTouch(mView, eventMoveLeft); reset(mView); boolean ret = uut.onTouch(mView, eventMoveRight); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_DRAG_END_SIDE, uut.getCurrentState());
uut.onTouch(mView, eventMoveRight); reset(mView); float startTransX = (eventMoveRight.getX() - eventMoveRight.getHistoricalX(0)) / touchDragRatioFwd; boolean ret = uut.onTouch(mView, eventMoveLeft); verify(mView, never()).setTranslationY(anyFloat()); assertTrue(ret); assertEquals(STATE_IDLE, uut.getCurrentState());