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 android.view.ViewGroup.LayoutParams.MATCH_PARENT; 19 import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 20 21 import static androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL; 22 import static androidx.recyclerview.widget.LinearLayoutManager.VERTICAL; 23 24 import android.app.Activity; 25 import android.graphics.Rect; 26 import android.os.Build; 27 import android.support.test.filters.MediumTest; 28 import android.support.test.filters.SdkSuppress; 29 import android.view.Gravity; 30 import android.view.View; 31 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.Parameterized; 35 36 import java.util.Arrays; 37 import java.util.List; 38 39 @MediumTest 40 @RunWith(Parameterized.class) 41 public class GridLayoutManagerWrapContentTest extends BaseWrapContentTest { 42 private boolean mHorizontal = false; 43 private int mSpanCount = 3; 44 private RecyclerView.ItemDecoration mItemDecoration; 45 46 public GridLayoutManagerWrapContentTest(Rect padding) { 47 super(new WrapContentConfig(false, false, padding)); 48 } 49 50 @Parameterized.Parameters(name = "paddingRect={0}") 51 public static List<Rect> params() { 52 return Arrays.asList( 53 new Rect(0, 0, 0, 0), 54 new Rect(5, 0, 0, 0), 55 new Rect(0, 3, 0, 0), 56 new Rect(0, 0, 2, 0), 57 new Rect(0, 0, 0, 7), 58 new Rect(3, 5, 7, 11) 59 ); 60 } 61 62 @Override 63 RecyclerView.LayoutManager createLayoutManager() { 64 GridLayoutManager lm = new GridLayoutManager(getActivity(), mSpanCount); 65 lm.setOrientation(mHorizontal ? HORIZONTAL : VERTICAL); 66 return lm; 67 } 68 69 @Override 70 protected WrappedRecyclerView createRecyclerView(Activity activity) { 71 WrappedRecyclerView recyclerView = super.createRecyclerView(activity); 72 if (mItemDecoration != null) { 73 recyclerView.addItemDecoration(mItemDecoration); 74 } 75 return recyclerView; 76 } 77 78 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M) 79 @Test 80 public void testUnspecifiedWithHint() throws Throwable { 81 unspecifiedWithHintTest(mHorizontal); 82 } 83 84 @Test 85 public void testVerticalWithItemDecors() throws Throwable { 86 mItemDecoration = new RecyclerView.ItemDecoration() { 87 @Override 88 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 89 RecyclerView.State state) { 90 outRect.set(0, 5, 0, 10); 91 } 92 }; 93 TestedFrameLayout.FullControlLayoutParams lp = 94 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 95 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 96 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 97 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, MATCH_PARENT) 98 ); 99 Rect[] expected = new Rect[] { 100 new Rect(0, 0, 10, 25) 101 }; 102 layoutAndCheck(lp, adapter, expected, 30, 25); 103 } 104 105 @Test 106 public void testHorizontalWithItemDecors() throws Throwable { 107 mItemDecoration = new RecyclerView.ItemDecoration() { 108 @Override 109 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 110 RecyclerView.State state) { 111 outRect.set(5, 0, 10, 0); 112 } 113 }; 114 TestedFrameLayout.FullControlLayoutParams lp = 115 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 116 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 117 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 118 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, MATCH_PARENT, WRAP_CONTENT) 119 ); 120 Rect[] expected = new Rect[] { 121 new Rect(0, 0, 25, 10) 122 }; 123 layoutAndCheck(lp, adapter, expected, 75, 10); 124 } 125 126 @Test 127 public void testHorizontal() throws Throwable { 128 mHorizontal = true; 129 mSpanCount = 2; 130 TestedFrameLayout.FullControlLayoutParams lp = 131 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 132 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 133 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 134 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 135 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 136 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 137 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 138 ); 139 Rect[] expected = new Rect[] { 140 new Rect(0, 0, 10, 10), 141 new Rect(0, 10, 10, 20), 142 new Rect(10, 0, 30, 10), 143 new Rect(10, 10, 30, 20) 144 }; 145 layoutAndCheck(lp, adapter, expected, 30, 20); 146 } 147 148 @Test 149 public void testHandleSecondLineChangingBorders() throws Throwable { 150 TestedFrameLayout.FullControlLayoutParams lp = 151 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 152 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 153 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 154 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 155 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 156 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 157 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 158 ); 159 Rect[] expected = new Rect[] { 160 new Rect(0, 0, 10, 10), 161 new Rect(20, 0, 30, 10), 162 new Rect(40, 0, 50, 10), 163 new Rect(0, 10, 20, 20) 164 }; 165 layoutAndCheck(lp, adapter, expected, 60, 20); 166 } 167 168 @Test 169 public void testSecondLineAffectingBordersWithAspectRatio() throws Throwable { 170 TestedFrameLayout.FullControlLayoutParams lp = 171 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 172 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 173 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 174 new BaseWrapContentWithAspectRatioTest.AspectRatioMeasureBehavior(10, 5, MATCH_PARENT, WRAP_CONTENT) 175 .aspectRatio(HORIZONTAL, .5f), 176 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 5, WRAP_CONTENT, WRAP_CONTENT), 177 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 5, MATCH_PARENT, WRAP_CONTENT), 178 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 179 ); 180 Rect[] expected = new Rect[] { 181 new Rect(0, 0, 20, 10), 182 new Rect(20, 0, 30, 10), 183 new Rect(40, 0, 60, 10), 184 new Rect(0, 10, 20, 20) 185 }; 186 layoutAndCheck(lp, adapter, expected, 60, 20); 187 } 188 189 @Test 190 public void testVerticalWithHorizontalMargins() throws Throwable { 191 TestedFrameLayout.FullControlLayoutParams lp = 192 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 193 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 194 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 195 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(100, 50, 100, WRAP_CONTENT).withMargins(10, 0, 5, 0) 196 ); 197 Rect[] expected = new Rect[] { 198 new Rect(0, 0, 115, 50) 199 }; 200 layoutAndCheck(lp, adapter, expected, 345, 50); 201 } 202 203 @Test 204 public void testHorizontalWithHorizontalMargins() throws Throwable { 205 mHorizontal = true; 206 mSpanCount = 1; 207 TestedFrameLayout.FullControlLayoutParams lp = 208 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 209 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 210 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 211 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(100, 50, 100, WRAP_CONTENT).withMargins(10, 0, 5, 0), 212 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(100, 50, 100, WRAP_CONTENT).withMargins(3, 4, 5, 6) 213 ); 214 Rect[] expected = new Rect[] { 215 new Rect(0, 0, 115, 50), 216 new Rect(115, 0, 223, 60) 217 }; 218 layoutAndCheck(lp, adapter, expected, 223, 60); 219 } 220 221 @Override 222 protected int getVerticalGravity(RecyclerView.LayoutManager layoutManager) { 223 return Gravity.TOP; 224 } 225 226 @Override 227 protected int getHorizontalGravity(RecyclerView.LayoutManager layoutManager) { 228 return Gravity.LEFT; 229 } 230 } 231