RecyclerViewSmoothScrollerTest.java revision bf134220bd242d4228e78ddd849d2a92ae7dfe4d
1/* 2 * Copyright 2018 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 17package androidx.recyclerview.widget; 18 19import static org.hamcrest.CoreMatchers.is; 20import static org.hamcrest.MatcherAssert.assertThat; 21import static org.mockito.Mockito.mock; 22import static org.mockito.Mockito.never; 23import static org.mockito.Mockito.spy; 24import static org.mockito.Mockito.verify; 25 26import android.support.test.InstrumentationRegistry; 27import android.support.test.filters.SmallTest; 28import android.support.test.runner.AndroidJUnit4; 29import android.view.View; 30 31import org.junit.Test; 32import org.junit.runner.RunWith; 33 34@RunWith(AndroidJUnit4.class) 35@SmallTest 36public class RecyclerViewSmoothScrollerTest { 37 38 @Test 39 public void stop_whileRunning_isRunningIsFalseInOnStop() { 40 RecyclerView recyclerView = new RecyclerView(InstrumentationRegistry.getContext()); 41 RecyclerView.LayoutManager layoutManager = mock(RecyclerView.LayoutManager.class); 42 recyclerView.setLayoutManager(layoutManager); 43 MockSmoothScroller mockSmoothScroller = spy(new MockSmoothScroller()); 44 mockSmoothScroller.setTargetPosition(0); 45 mockSmoothScroller.start(recyclerView, layoutManager); 46 47 mockSmoothScroller.stop(); 48 49 verify(mockSmoothScroller).onStop(); 50 assertThat(mockSmoothScroller.mWasRunningInOnStop, is(false)); 51 } 52 53 @Test 54 public void stop_whileNotRunning_doesNotCallOnStop() { 55 RecyclerView.SmoothScroller mockSmoothScroller = spy(new MockSmoothScroller()); 56 mockSmoothScroller.stop(); 57 verify(mockSmoothScroller, never()).onStop(); 58 } 59 60 public static class MockSmoothScroller extends RecyclerView.SmoothScroller { 61 62 boolean mWasRunningInOnStop; 63 64 @Override 65 protected void onStart() { 66 67 } 68 69 @Override 70 protected void onStop() { 71 mWasRunningInOnStop = isRunning(); 72 } 73 74 @Override 75 protected void onSeekTargetStep(int dx, int dy, RecyclerView.State state, 76 Action action) { 77 78 } 79 80 @Override 81 protected void onTargetFound(View targetView, RecyclerView.State state, 82 Action action) { 83 84 } 85 } 86 87} 88