Add more tests for SwipeRefreshLayout.
Bug: 29518514
Change-Id: I6c5df04e9cf9622d44d5e7bcdf99f24384a6a843
diff --git a/core-ui/tests/java/android/support/v4/testutils/PollingCheck.java b/core-ui/tests/java/android/support/v4/testutils/PollingCheck.java
new file mode 100644
index 0000000..b4271f4
--- /dev/null
+++ b/core-ui/tests/java/android/support/v4/testutils/PollingCheck.java
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2016 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 android.support.v4.testutils;
+
+import java.util.concurrent.Callable;
+
+import junit.framework.Assert;
+
+public abstract class PollingCheck {
+ private static final long TIME_SLICE = 50;
+ private long mTimeout = 3000;
+
+ public static interface PollingCheckCondition {
+ boolean canProceed();
+ }
+
+ public PollingCheck() {
+ }
+
+ public PollingCheck(long timeout) {
+ mTimeout = timeout;
+ }
+
+ protected abstract boolean check();
+
+ public void run() {
+ if (check()) {
+ return;
+ }
+
+ long timeout = mTimeout;
+ while (timeout > 0) {
+ try {
+ Thread.sleep(TIME_SLICE);
+ } catch (InterruptedException e) {
+ Assert.fail("unexpected InterruptedException");
+ }
+
+ if (check()) {
+ return;
+ }
+
+ timeout -= TIME_SLICE;
+ }
+
+ Assert.fail("unexpected timeout");
+ }
+
+ public static void check(CharSequence message, long timeout, Callable<Boolean> condition)
+ throws Exception {
+ while (timeout > 0) {
+ if (condition.call()) {
+ return;
+ }
+
+ Thread.sleep(TIME_SLICE);
+ timeout -= TIME_SLICE;
+ }
+
+ Assert.fail(message.toString());
+ }
+
+ public static void waitFor(final PollingCheckCondition condition) {
+ new PollingCheck() {
+ @Override
+ protected boolean check() {
+ return condition.canProceed();
+ }
+ }.run();
+ }
+
+ public static void waitFor(long timeout, final PollingCheckCondition condition) {
+ new PollingCheck(timeout) {
+ @Override
+ protected boolean check() {
+ return condition.canProceed();
+ }
+ }.run();
+ }
+}
\ No newline at end of file
diff --git a/core-ui/tests/java/android/support/v4/widget/SwipeRefreshLayoutTest.java b/core-ui/tests/java/android/support/v4/widget/SwipeRefreshLayoutTest.java
index 3efd652..3f9ed9c 100644
--- a/core-ui/tests/java/android/support/v4/widget/SwipeRefreshLayoutTest.java
+++ b/core-ui/tests/java/android/support/v4/widget/SwipeRefreshLayoutTest.java
@@ -24,20 +24,29 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.*;
+import android.support.test.espresso.action.ViewActions;
import android.support.coreui.test.R;
import android.support.v4.BaseInstrumentationTestCase;
+import android.support.v4.testutils.PollingCheck;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
+import android.view.View;
import org.junit.Before;
import org.junit.Test;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
/**
* Tests SwipeRefreshLayout widget.
*/
public class SwipeRefreshLayoutTest
extends BaseInstrumentationTestCase<SwipeRefreshLayoutActivity> {
+ private static final long TIMEOUT = 1000;
+ private static final int INVALID_SIZE = 1000;
private SwipeRefreshLayout mSwipeRefresh;
@@ -54,6 +63,10 @@
@Test
@MediumTest
public void testStartAndStopRefreshing() throws Throwable {
+ SwipeRefreshLayout.OnRefreshListener mockListener =
+ mock(SwipeRefreshLayout.OnRefreshListener.class);
+ mSwipeRefresh.setOnRefreshListener(mockListener);
+
assertFalse(mSwipeRefresh.isRefreshing());
for (int i = 0; i < 5; i++) {
onView(withId(R.id.swipe_refresh)).perform(setRefreshing());
@@ -67,14 +80,35 @@
mSwipeRefresh.setRefreshing(false);
}
});
- long waitTime = 1000;
- while (mSwipeRefresh.isRefreshing()) {
- Thread.sleep(20);
- waitTime -= 20;
- assertTrue("Timed out while waiting for SwipeRefreshLayout to stop refreshing",
- waitTime > 0);
- }
+
+ new PollingCheck(TIMEOUT) {
+ @Override
+ protected boolean check() {
+ return mSwipeRefresh.isRefreshing();
+ }
+ }.run();
}
+ verify(mockListener, times(0)).onRefresh();
+ }
+
+ @Test
+ @MediumTest
+ public void testSwipeDownToRefresh() throws Throwable {
+ assertFalse(mSwipeRefresh.isRefreshing());
+
+ final CountDownLatch latch = new CountDownLatch(1);
+ SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() {
+ @Override
+ public void onRefresh() {
+ latch.countDown();
+ assertTrue(mSwipeRefresh.isRefreshing());
+ mSwipeRefresh.setRefreshing(false);
+ }
+ };
+ mSwipeRefresh.setOnRefreshListener(listener);
+ onView(withId(R.id.content)).perform(ViewActions.swipeDown());
+ assertTrue("SwipeRefreshLayout never started refreshing",
+ latch.await(500, TimeUnit.MILLISECONDS));
}
@Test
@@ -90,5 +124,25 @@
assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density),
mSwipeRefresh.getProgressCircleDiameter());
onView(withId(R.id.swipe_refresh)).perform(setSize(SwipeRefreshLayout.DEFAULT));
+ onView(withId(R.id.swipe_refresh)).perform(setSize(INVALID_SIZE));
+ assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density),
+ mSwipeRefresh.getProgressCircleDiameter());
+ }
+
+ @Test
+ @SmallTest
+ public void testSetOnChildScrollUpCallback() throws Throwable {
+ SwipeRefreshLayout.OnChildScrollUpCallback mockCallback =
+ mock(SwipeRefreshLayout.OnChildScrollUpCallback.class);
+ when(mockCallback.canChildScrollUp(eq(mSwipeRefresh), any(View.class)))
+ .thenReturn(true)
+ .thenReturn(true)
+ .thenReturn(false)
+ .thenReturn(false);
+ mSwipeRefresh.setOnChildScrollUpCallback(mockCallback);
+ assertTrue(mSwipeRefresh.canChildScrollUp());
+ assertTrue(mSwipeRefresh.canChildScrollUp());
+ assertFalse(mSwipeRefresh.canChildScrollUp());
+ assertFalse(mSwipeRefresh.canChildScrollUp());
}
}