Improve SwipeRefreshLayout test
Fixes/improvements in
SwipeRefreshLayoutInHorizontallyScrollingParentTest:
- Make sibling pages non-zero size
- Calculates 'gesture distance' correctly
- Accounts for touch slop when performing gesture
- Asserts that requestDisallowInterceptTouchEvent(true) is called
- Speeds up the gesture
Bug: 138314213
Test: ./gradlew swiperefreshlayout:cC
Change-Id: I00a5f73ef0c54f41034cdacd023b8348e133384c
diff --git a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInHorizontallyScrollingParentTest.java b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInHorizontallyScrollingParentTest.java
index 665fbb4..e5c252e 100644
--- a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInHorizontallyScrollingParentTest.java
+++ b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInHorizontallyScrollingParentTest.java
@@ -73,15 +73,14 @@
assertThat(mSwipeRefreshLayout, notNullValue());
assertThat(isIndicatorVisible(mSwipeRefreshLayout), equalTo(false));
- mGestureDistance = mSwipeRefreshLayout.getProgressViewEndOffset()
- - mSwipeRefreshLayout.getProgressViewStartOffset();
+ mGestureDistance = mSwipeRefreshLayout.getProgressViewEndOffset();
assertThat(mGestureDistance, greaterThanOrEqualTo(2));
}
@Test
public void swipeHorizontallyDuringRefreshGesture() {
recordRvPosition();
- swipeVerticallyThenHorizontally(mGestureDistance, mTouchSlop * 3);
+ swipeVerticallyThenHorizontally(mTouchSlop + mGestureDistance, mTouchSlop * 3);
// Gesture wasn't completed, so SRL should not be refreshing
assertThat(mSwipeRefreshLayout.isRefreshing(), equalTo(false));
@@ -94,7 +93,7 @@
@Test
public void swipeHorizontallyAfterRefreshGesture() {
recordRvPosition();
- swipeVerticallyThenHorizontally(mGestureDistance * 2, mTouchSlop * 3);
+ swipeVerticallyThenHorizontally(mTouchSlop + mGestureDistance * 2 + 1, mTouchSlop * 3);
// Gesture was completed, so SRL should be refreshing
assertThat(mSwipeRefreshLayout.isRefreshing(), equalTo(true));
@@ -107,8 +106,8 @@
private void swipeVerticallyThenHorizontally(int dy, int dx) {
SwipeInjector swiper = new SwipeInjector(InstrumentationRegistry.getInstrumentation());
swiper.startDrag(CENTER, mRecyclerView);
- swiper.dragBy(0, dy, 300);
- swiper.dragBy(dx, 0, 100);
+ swiper.dragBy(0, dy, 150);
+ swiper.dragBy(dx, 0, 50);
swiper.finishDrag();
}
diff --git a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInRecyclerViewActivity.java b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInRecyclerViewActivity.java
index bf2bdb7..bafd8ac 100644
--- a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInRecyclerViewActivity.java
+++ b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeRefreshLayoutInRecyclerViewActivity.java
@@ -20,6 +20,7 @@
import static androidx.recyclerview.widget.RecyclerView.HORIZONTAL;
+import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
@@ -32,13 +33,13 @@
public class SwipeRefreshLayoutInRecyclerViewActivity extends FragmentActivity {
- RecyclerView mRecyclerView;
+ RecyclerViewImpl mRecyclerView;
SwipeRefreshLayout mSwipeRefreshLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mRecyclerView = new RecyclerView(this);
+ mRecyclerView = new RecyclerViewImpl(this);
mRecyclerView.setLayoutParams(matchParent());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, HORIZONTAL, false));
mRecyclerView.setAdapter(new Adapter());
@@ -55,6 +56,25 @@
}
}
+ public static class RecyclerViewImpl extends RecyclerView {
+ public boolean mRequestDisallowInterceptTrueCalled;
+ public boolean mRequestDisallowInterceptFalseCalled;
+
+ public RecyclerViewImpl(@NonNull Context context) {
+ super(context);
+ }
+
+ @Override
+ public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
+ super.requestDisallowInterceptTouchEvent(disallowIntercept);
+ if (disallowIntercept) {
+ mRequestDisallowInterceptTrueCalled = true;
+ } else {
+ mRequestDisallowInterceptFalseCalled = true;
+ }
+ }
+ }
+
private class Adapter extends RecyclerView.Adapter<ViewHolder> {
private static final int SRL_ITEM = 0;
private static final int OTHER_ITEM = 1;
@@ -98,6 +118,7 @@
private ViewHolder createOtherItem(@NonNull ViewGroup parent) {
View view = new View(parent.getContext());
+ view.setLayoutParams(matchParent());
view.setBackgroundColor(0xFFFF0000);
return new ViewHolder(view);
}
diff --git a/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeToRefreshLayoutRequestDisallowInterceptTest.java b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeToRefreshLayoutRequestDisallowInterceptTest.java
new file mode 100644
index 0000000..0a50fbd
--- /dev/null
+++ b/swiperefreshlayout/src/androidTest/java/androidx/swiperefreshlayout/widget/SwipeToRefreshLayoutRequestDisallowInterceptTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2019 The Android Open Source Project
+ *
+ * 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 androidx.swiperefreshlayout.widget;
+
+import static androidx.test.espresso.action.GeneralLocation.CENTER;
+import static androidx.testutils.ActivityTestRuleKt.waitForExecution;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import android.view.ViewConfiguration;
+
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.rule.ActivityTestRule;
+import androidx.testutils.SwipeInjector;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class SwipeToRefreshLayoutRequestDisallowInterceptTest {
+
+ @Rule
+ public final ActivityTestRule<SwipeRefreshLayoutInRecyclerViewActivity> mActivityTestRule =
+ new ActivityTestRule<>(SwipeRefreshLayoutInRecyclerViewActivity.class);
+
+ private SwipeRefreshLayoutInRecyclerViewActivity.RecyclerViewImpl mRecyclerView;
+ private int mTouchSlop;
+
+ @Before
+ public void setUp() throws Throwable {
+ mRecyclerView = mActivityTestRule.getActivity().mRecyclerView;
+ mTouchSlop = ViewConfiguration.get(mRecyclerView.getContext()).getScaledTouchSlop();
+
+ mActivityTestRule.runOnUiThread(() -> mRecyclerView.scrollToPosition(1));
+ waitForExecution(mActivityTestRule, 2);
+ }
+
+ @Test
+ public void swipeLessThanTouchSlop_requestDisallowNotCalled() {
+ assertThat(mRecyclerView.mRequestDisallowInterceptTrueCalled, equalTo(false));
+ assertThat(mRecyclerView.mRequestDisallowInterceptFalseCalled, equalTo(false));
+
+ swipeDown(mTouchSlop / 2);
+
+ assertThat(mRecyclerView.mRequestDisallowInterceptTrueCalled, equalTo(false));
+ assertThat(mRecyclerView.mRequestDisallowInterceptFalseCalled, equalTo(false));
+ }
+
+ @Test
+ public void swipeMoreThanTouchSlop_requestDisallowIsCalled() {
+ assertThat(mRecyclerView.mRequestDisallowInterceptTrueCalled, equalTo(false));
+ assertThat(mRecyclerView.mRequestDisallowInterceptFalseCalled, equalTo(false));
+
+ swipeDown(mTouchSlop + 1);
+
+ assertThat(mRecyclerView.mRequestDisallowInterceptTrueCalled, equalTo(true));
+ assertThat(mRecyclerView.mRequestDisallowInterceptFalseCalled, equalTo(false));
+ }
+
+ private void swipeDown(int dy) {
+ SwipeInjector swiper = new SwipeInjector(InstrumentationRegistry.getInstrumentation());
+ swiper.startDrag(CENTER, mRecyclerView);
+ swiper.dragBy(0, dy);
+ swiper.finishDrag();
+ }
+}