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 package androidx.recyclerview.widget; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.junit.Assert.fail; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 27 class CacheUtils { verifyPositionsPrefetched(RecyclerView view, int dx, int dy, Integer[]... positionData)28 static void verifyPositionsPrefetched(RecyclerView view, int dx, int dy, 29 Integer[]... positionData) { 30 RecyclerView.LayoutManager.LayoutPrefetchRegistry layoutPrefetchRegistry = 31 mock(RecyclerView.LayoutManager.LayoutPrefetchRegistry.class); 32 view.mLayout.collectAdjacentPrefetchPositions( 33 dx, dy, view.mState, layoutPrefetchRegistry); 34 35 verify(layoutPrefetchRegistry, times(positionData.length)).addPosition(anyInt(), anyInt()); 36 for (Integer[] aPositionData : positionData) { 37 verify(layoutPrefetchRegistry).addPosition(aPositionData[0], aPositionData[1]); 38 } 39 } 40 verifyCacheContainsPosition(RecyclerView view, int position)41 private static void verifyCacheContainsPosition(RecyclerView view, int position) { 42 for (int i = 0; i < view.mRecycler.mCachedViews.size(); i++) { 43 if (view.mRecycler.mCachedViews.get(i).mPosition == position) return; 44 } 45 fail("Cache does not contain position " + position); 46 } 47 48 /** 49 * Asserts that the positions passed are all resident in the view's cache. 50 */ verifyCacheContainsPositions(RecyclerView view, Integer... positions)51 static void verifyCacheContainsPositions(RecyclerView view, Integer... positions) { 52 for (Integer position : positions) { 53 verifyCacheContainsPosition(view, position); 54 } 55 } 56 57 /** 58 * Asserts that the position passed is resident in the view's cache, similar to 59 * {@link #verifyCacheContainsPositions}, but additionally requires presence in 60 * PrefetchRegistry. 61 */ verifyCacheContainsPrefetchedPositions(RecyclerView view, Integer... positions)62 static void verifyCacheContainsPrefetchedPositions(RecyclerView view, Integer... positions) { 63 verifyCacheContainsPositions(view, positions); 64 65 for (Integer position : positions) { 66 assertTrue(view.mPrefetchRegistry.lastPrefetchIncludedPosition(position)); 67 } 68 assertEquals(positions.length, view.mRecycler.mCachedViews.size()); 69 } 70 71 /** 72 * Asserts that none of the positions passed are resident in the view's cache. 73 */ verifyCacheDoesNotContainPositions(RecyclerView view, Integer... positions)74 static void verifyCacheDoesNotContainPositions(RecyclerView view, Integer... positions) { 75 for (Integer position : positions) { 76 for (int i = 0; i < view.mRecycler.mCachedViews.size(); i++) { 77 assertNotEquals("Cache must not contain position " + position, 78 (int) position, view.mRecycler.mCachedViews.get(i).mPosition); 79 } 80 } 81 } 82 peekAtCachedViewForPosition(RecyclerView view, int position)83 static RecyclerView.ViewHolder peekAtCachedViewForPosition(RecyclerView view, int position) { 84 for (int i = 0; i < view.mRecycler.mCachedViews.size(); i++) { 85 RecyclerView.ViewHolder holder = view.mRecycler.mCachedViews.get(i); 86 if (holder.mPosition == position) { 87 return holder; 88 } 89 } 90 fail("Unable to find view with position " + position); 91 return null; 92 } 93 } 94