Skip to content

Commit

Permalink
Add TouchInterceptionFrameLayout's test.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksoichiro committed Mar 1, 2015
1 parent 9cbc1fd commit 3c65189
Show file tree
Hide file tree
Showing 16 changed files with 852 additions and 6 deletions.
6 changes: 5 additions & 1 deletion observablescrollview/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
<activity android:name=".RecyclerViewActivity" />
<activity android:name=".RecyclerViewScrollFromBottomActivity" />
<activity android:name=".ScrollViewActivity" />
<activity android:name=".TouchIntereceptionActivity" />
<activity android:name=".TouchInterceptionGridViewActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".TouchInterceptionListViewActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".TouchInterceptionRecyclerViewActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".TouchInterceptionScrollViewActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".TouchInterceptionWebViewActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".ViewPagerTabActivity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".ViewPagerTab2Activity" android:theme="@style/AppTheme.Toolbar" />
<activity android:name=".WebViewActivity" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.github.ksoichiro.android.observablescrollview.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableGridView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.Scrollable;
import com.github.ksoichiro.android.observablescrollview.TouchInterceptionFrameLayout;
import com.nineoldandroids.view.ViewHelper;

public class TouchInterceptionGridViewActivity extends Activity implements ObservableScrollViewCallbacks {

private TouchInterceptionFrameLayout mInterceptionLayout;
private Scrollable mScrollable;

private int mIntersectionHeight;
private int mHeaderBarHeight;

private float mScrollYOnDownMotion;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchinterception_gridview);
((TextView) findViewById(R.id.title)).setText(getClass().getSimpleName());
mScrollable = (Scrollable) findViewById(R.id.scrollable);
mScrollable.setScrollViewCallbacks(this);
UiTestUtils.setDummyData(this, (ObservableGridView) mScrollable);

mIntersectionHeight = getResources().getDimensionPixelSize(R.dimen.intersection_height);
mHeaderBarHeight = getResources().getDimensionPixelSize(R.dimen.header_bar_height);

mInterceptionLayout = (TouchInterceptionFrameLayout) findViewById(R.id.scroll_wrapper);
mInterceptionLayout.setScrollInterceptionListener(mInterceptionListener);
}

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
}

@Override
public void onDownMotionEvent() {
}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
}

private TouchInterceptionFrameLayout.TouchInterceptionListener mInterceptionListener = new TouchInterceptionFrameLayout.TouchInterceptionListener() {
@Override
public boolean shouldInterceptTouchEvent(MotionEvent ev, boolean moving, float diffX, float diffY) {
final int minInterceptionLayoutY = -mIntersectionHeight;
return minInterceptionLayoutY < (int) ViewHelper.getY(mInterceptionLayout)
|| (moving && mScrollable.getCurrentScrollY() - diffY < 0);
}

@Override
public void onDownMotionEvent(MotionEvent ev) {
mScrollYOnDownMotion = mScrollable.getCurrentScrollY();
}

@Override
public void onMoveMotionEvent(MotionEvent ev, float diffX, float diffY) {
float translationY = ViewHelper.getTranslationY(mInterceptionLayout) - mScrollYOnDownMotion + diffY;
if (translationY < -mIntersectionHeight) {
translationY = -mIntersectionHeight;
} else if (getScreenHeight() - mHeaderBarHeight < translationY) {
translationY = getScreenHeight() - mHeaderBarHeight;
}

slideTo(translationY, true);
}

@Override
public void onUpOrCancelMotionEvent(MotionEvent ev) {
}
};

private void slideTo(float translationY, final boolean animated) {
ViewHelper.setTranslationY(mInterceptionLayout, translationY);

if (translationY < 0) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mInterceptionLayout.getLayoutParams();
lp.height = (int) -translationY + getScreenHeight();
mInterceptionLayout.requestLayout();
}
}

private int getScreenHeight() {
return findViewById(android.R.id.content).getHeight();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.ksoichiro.android.observablescrollview.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;

import com.github.ksoichiro.android.observablescrollview.ObservableGridView;

public class TouchInterceptionGridViewActivityTest extends ActivityInstrumentationTestCase2<TouchInterceptionGridViewActivity> {

private Activity activity;
private ObservableGridView scrollable;

public TouchInterceptionGridViewActivityTest() {
super(TouchInterceptionGridViewActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
activity = getActivity();
scrollable = (ObservableGridView) activity.findViewById(R.id.scrollable);
}

public void testScroll() throws Throwable {
TouchUtils.touchAndCancelView(this, scrollable);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.UP);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.DOWN);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.DOWN);
getInstrumentation().waitForIdleSync();
}

public void testSaveAndRestoreInstanceState() throws Throwable {
UiTestUtils.saveAndRestoreInstanceState(this, activity);
testScroll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.github.ksoichiro.android.observablescrollview.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.github.ksoichiro.android.observablescrollview.ObservableListView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.Scrollable;
import com.github.ksoichiro.android.observablescrollview.TouchInterceptionFrameLayout;
import com.nineoldandroids.view.ViewHelper;

public class TouchInterceptionListViewActivity extends Activity implements ObservableScrollViewCallbacks {

private TouchInterceptionFrameLayout mInterceptionLayout;
private Scrollable mScrollable;

private int mIntersectionHeight;
private int mHeaderBarHeight;

private float mScrollYOnDownMotion;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touchinterception_listview);
((TextView) findViewById(R.id.title)).setText(getClass().getSimpleName());
mScrollable = (Scrollable) findViewById(R.id.scrollable);
mScrollable.setScrollViewCallbacks(this);
UiTestUtils.setDummyData(this, (ObservableListView) mScrollable);

mIntersectionHeight = getResources().getDimensionPixelSize(R.dimen.intersection_height);
mHeaderBarHeight = getResources().getDimensionPixelSize(R.dimen.header_bar_height);

mInterceptionLayout = (TouchInterceptionFrameLayout) findViewById(R.id.scroll_wrapper);
mInterceptionLayout.setScrollInterceptionListener(mInterceptionListener);
}

@Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
}

@Override
public void onDownMotionEvent() {
}

@Override
public void onUpOrCancelMotionEvent(ScrollState scrollState) {
}

private TouchInterceptionFrameLayout.TouchInterceptionListener mInterceptionListener = new TouchInterceptionFrameLayout.TouchInterceptionListener() {
@Override
public boolean shouldInterceptTouchEvent(MotionEvent ev, boolean moving, float diffX, float diffY) {
final int minInterceptionLayoutY = -mIntersectionHeight;
return minInterceptionLayoutY < (int) ViewHelper.getY(mInterceptionLayout)
|| (moving && mScrollable.getCurrentScrollY() - diffY < 0);
}

@Override
public void onDownMotionEvent(MotionEvent ev) {
mScrollYOnDownMotion = mScrollable.getCurrentScrollY();
}

@Override
public void onMoveMotionEvent(MotionEvent ev, float diffX, float diffY) {
float translationY = ViewHelper.getTranslationY(mInterceptionLayout) - mScrollYOnDownMotion + diffY;
if (translationY < -mIntersectionHeight) {
translationY = -mIntersectionHeight;
} else if (getScreenHeight() - mHeaderBarHeight < translationY) {
translationY = getScreenHeight() - mHeaderBarHeight;
}

slideTo(translationY, true);
}

@Override
public void onUpOrCancelMotionEvent(MotionEvent ev) {
}
};

private void slideTo(float translationY, final boolean animated) {
ViewHelper.setTranslationY(mInterceptionLayout, translationY);

if (translationY < 0) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mInterceptionLayout.getLayoutParams();
lp.height = (int) -translationY + getScreenHeight();
mInterceptionLayout.requestLayout();
}
}

private int getScreenHeight() {
return findViewById(android.R.id.content).getHeight();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.ksoichiro.android.observablescrollview.test;

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;

import com.github.ksoichiro.android.observablescrollview.ObservableListView;

public class TouchInterceptionListViewActivityTest extends ActivityInstrumentationTestCase2<TouchInterceptionListViewActivity> {

private Activity activity;
private ObservableListView scrollable;

public TouchInterceptionListViewActivityTest() {
super(TouchInterceptionListViewActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(true);
activity = getActivity();
scrollable = (ObservableListView) activity.findViewById(R.id.scrollable);
}

public void testScroll() throws Throwable {
TouchUtils.touchAndCancelView(this, scrollable);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.UP);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.DOWN);
getInstrumentation().waitForIdleSync();

UiTestUtils.swipeVertically(this, scrollable, UiTestUtils.Direction.DOWN);
getInstrumentation().waitForIdleSync();
}

public void testSaveAndRestoreInstanceState() throws Throwable {
UiTestUtils.saveAndRestoreInstanceState(this, activity);
testScroll();
}
}
Loading

0 comments on commit 3c65189

Please sign in to comment.