1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.support.v4.widget; 18 19 import static android.support.test.espresso.Espresso.onView; 20 import static android.support.test.espresso.matcher.ViewMatchers.withId; 21 import static android.support.v4.widget.SwipeRefreshLayoutActions.setEnabled; 22 import static android.support.v4.widget.SwipeRefreshLayoutActions.setRefreshing; 23 import static android.support.v4.widget.SwipeRefreshLayoutActions.setSize; 24 25 import static org.junit.Assert.assertEquals; 26 import static org.junit.Assert.assertFalse; 27 import static org.junit.Assert.assertTrue; 28 import static org.mockito.Mockito.*; 29 30 import android.app.Activity; 31 import android.support.test.espresso.action.ViewActions; 32 import android.support.coreui.test.R; 33 import android.support.v4.BaseInstrumentationTestCase; 34 import android.support.v4.testutils.PollingCheck; 35 import android.test.suitebuilder.annotation.MediumTest; 36 import android.test.suitebuilder.annotation.SmallTest; 37 import android.view.View; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 42 import java.util.concurrent.CountDownLatch; 43 import java.util.concurrent.TimeUnit; 44 45 /** 46 * Tests SwipeRefreshLayout widget. 47 */ 48 public class SwipeRefreshLayoutTest 49 extends BaseInstrumentationTestCase<SwipeRefreshLayoutActivity> { 50 private static final long TIMEOUT = 1000; 51 private static final int INVALID_SIZE = 1000; 52 53 private SwipeRefreshLayout mSwipeRefresh; 54 55 public SwipeRefreshLayoutTest() { 56 super(SwipeRefreshLayoutActivity.class); 57 } 58 59 @Before 60 public void setUp() { 61 mSwipeRefresh = (SwipeRefreshLayout) mActivityTestRule.getActivity().findViewById( 62 R.id.swipe_refresh); 63 } 64 65 @Test 66 @MediumTest 67 public void testStartAndStopRefreshing() throws Throwable { 68 SwipeRefreshLayout.OnRefreshListener mockListener = 69 mock(SwipeRefreshLayout.OnRefreshListener.class); 70 mSwipeRefresh.setOnRefreshListener(mockListener); 71 72 assertFalse(mSwipeRefresh.isRefreshing()); 73 for (int i = 0; i < 5; i++) { 74 onView(withId(R.id.swipe_refresh)).perform(setRefreshing()); 75 assertTrue(mSwipeRefresh.isRefreshing()); 76 77 // onView(..).perform(..) does not work when views are animated. 78 // Therefore this is using a posted task to turn off refreshing. 79 mSwipeRefresh.getHandler().post(new Runnable() { 80 @Override 81 public void run() { 82 mSwipeRefresh.setRefreshing(false); 83 } 84 }); 85 86 new PollingCheck(TIMEOUT) { 87 @Override 88 protected boolean check() { 89 return mSwipeRefresh.isRefreshing(); 90 } 91 }.run(); 92 } 93 verify(mockListener, times(0)).onRefresh(); 94 } 95 96 @Test 97 @MediumTest 98 public void testSwipeDownToRefresh() throws Throwable { 99 assertFalse(mSwipeRefresh.isRefreshing()); 100 101 swipeToRefreshVerifyThenStopRefreshing(true); 102 } 103 104 @Test 105 @SmallTest 106 public void testSetSize() throws Throwable { 107 float density = mSwipeRefresh.getResources().getDisplayMetrics().density; 108 assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), 109 mSwipeRefresh.getProgressCircleDiameter()); 110 onView(withId(R.id.swipe_refresh)).perform(setSize(SwipeRefreshLayout.LARGE)); 111 assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER_LARGE * density), 112 mSwipeRefresh.getProgressCircleDiameter()); 113 onView(withId(R.id.swipe_refresh)).perform(setSize(SwipeRefreshLayout.DEFAULT)); 114 assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), 115 mSwipeRefresh.getProgressCircleDiameter()); 116 onView(withId(R.id.swipe_refresh)).perform(setSize(SwipeRefreshLayout.DEFAULT)); 117 onView(withId(R.id.swipe_refresh)).perform(setSize(INVALID_SIZE)); 118 assertEquals((int) (SwipeRefreshLayout.CIRCLE_DIAMETER * density), 119 mSwipeRefresh.getProgressCircleDiameter()); 120 } 121 122 @Test 123 @SmallTest 124 public void testSetOnChildScrollUpCallback() throws Throwable { 125 SwipeRefreshLayout.OnChildScrollUpCallback mockCallback = 126 mock(SwipeRefreshLayout.OnChildScrollUpCallback.class); 127 when(mockCallback.canChildScrollUp(eq(mSwipeRefresh), any(View.class))) 128 .thenReturn(true) 129 .thenReturn(true) 130 .thenReturn(false) 131 .thenReturn(false); 132 mSwipeRefresh.setOnChildScrollUpCallback(mockCallback); 133 assertTrue(mSwipeRefresh.canChildScrollUp()); 134 assertTrue(mSwipeRefresh.canChildScrollUp()); 135 assertFalse(mSwipeRefresh.canChildScrollUp()); 136 assertFalse(mSwipeRefresh.canChildScrollUp()); 137 } 138 139 @Test 140 @SmallTest 141 public void testSwipeDownToRefreshInitiallyDisabled() throws Throwable { 142 mActivityTestRule.runOnUiThread(new Runnable() { 143 @Override 144 public void run() { 145 mActivityTestRule.getActivity().setContentView( 146 R.layout.swipe_refresh_layout_disabled_activity); 147 } 148 }); 149 mSwipeRefresh = (SwipeRefreshLayout) mActivityTestRule.getActivity().findViewById( 150 R.id.swipe_refresh); 151 152 assertFalse(mSwipeRefresh.isRefreshing()); 153 154 swipeToRefreshVerifyThenStopRefreshing(false); 155 156 onView(withId(R.id.swipe_refresh)).perform(setEnabled(true)); 157 158 swipeToRefreshVerifyThenStopRefreshing(true); 159 } 160 161 private void swipeToRefreshVerifyThenStopRefreshing(boolean expectRefreshing) throws Throwable { 162 final CountDownLatch latch = new CountDownLatch(1); 163 SwipeRefreshLayout.OnRefreshListener listener = new SwipeRefreshLayout.OnRefreshListener() { 164 @Override 165 public void onRefresh() { 166 latch.countDown(); 167 assertTrue(mSwipeRefresh.isRefreshing()); 168 mSwipeRefresh.setRefreshing(false); 169 } 170 }; 171 mSwipeRefresh.setOnRefreshListener(listener); 172 onView(withId(R.id.content)).perform(ViewActions.swipeDown()); 173 if (expectRefreshing) { 174 assertTrue("SwipeRefreshLayout never started refreshing", 175 latch.await(500, TimeUnit.MILLISECONDS)); 176 } else { 177 assertFalse("SwipeRefreshLayout unexpectedly started refreshing", 178 latch.await(500, TimeUnit.MILLISECONDS)); 179 } 180 } 181 } 182