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 android.view.ViewGroup.LayoutParams.MATCH_PARENT; 20import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 21 22import static androidx.recyclerview.widget.StaggeredGridLayoutManager.HORIZONTAL; 23 24import android.graphics.Rect; 25import android.os.Build; 26import android.support.test.filters.MediumTest; 27import android.support.test.filters.SdkSuppress; 28import android.view.Gravity; 29import android.view.View; 30 31import org.junit.Test; 32import org.junit.runner.RunWith; 33import org.junit.runners.Parameterized; 34 35import java.util.Arrays; 36import java.util.List; 37 38@MediumTest 39@RunWith(Parameterized.class) 40public class StaggeredGridLayoutManagerWrapContentTest extends BaseWrapContentTest { 41 int mOrientation = StaggeredGridLayoutManager.VERTICAL; 42 public StaggeredGridLayoutManagerWrapContentTest(Rect padding) { 43 super(new WrapContentConfig(false, false, padding)); 44 } 45 46 @Parameterized.Parameters(name = "paddingRect={0}") 47 public static List<Rect> params() { 48 return Arrays.asList( 49 new Rect(0, 0, 0, 0), 50 new Rect(5, 0, 0, 0), 51 new Rect(0, 3, 0, 0), 52 new Rect(0, 0, 2, 0), 53 new Rect(0, 0, 0, 7), 54 new Rect(3, 5, 7, 11) 55 ); 56 } 57 58 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.M) 59 @Test 60 public void testUnspecifiedWithHint() throws Throwable { 61 unspecifiedWithHintTest(mOrientation == StaggeredGridLayoutManager.HORIZONTAL); 62 } 63 64 @Test 65 public void testSimple() throws Throwable { 66 TestedFrameLayout.FullControlLayoutParams lp = 67 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 68 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 69 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 70 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 71 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 15, WRAP_CONTENT, WRAP_CONTENT), 72 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 20, WRAP_CONTENT, WRAP_CONTENT), 73 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 74 ); 75 Rect[] expected = new Rect[] { 76 new Rect(0, 0, 10, 10), 77 new Rect(20, 0, 30, 15), 78 new Rect(40, 0, 50, 20), 79 new Rect(0, 10, 20, 20) 80 }; 81 layoutAndCheck(lp, adapter, expected, 60, 20); 82 } 83 84 @Test 85 public void testSimpleHorizontal() throws Throwable { 86 mOrientation = HORIZONTAL; 87 TestedFrameLayout.FullControlLayoutParams lp = 88 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 89 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 90 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 91 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 92 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(15, 10, WRAP_CONTENT, WRAP_CONTENT), 93 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT), 94 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 20, WRAP_CONTENT, WRAP_CONTENT) 95 ); 96 Rect[] expected = new Rect[] { 97 new Rect(0, 0, 10, 10), 98 new Rect(0, 20, 15, 30), 99 new Rect(0, 40, 20, 50), 100 new Rect(10, 0, 20, 20) 101 }; 102 layoutAndCheck(lp, adapter, expected, 20, 60); 103 } 104 105 @Test 106 public void testUnspecifiedWidth() throws Throwable { 107 TestedFrameLayout.FullControlLayoutParams lp = 108 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 109 lp.wSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 110 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 111 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 112 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(2000, 10, WRAP_CONTENT, WRAP_CONTENT), 113 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(500, 15, WRAP_CONTENT, WRAP_CONTENT), 114 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(400, 20, WRAP_CONTENT, WRAP_CONTENT), 115 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(50, 10, MATCH_PARENT, WRAP_CONTENT) 116 ); 117 Rect[] expected = new Rect[] { 118 new Rect(0, 0, 2000, 10), 119 new Rect(2000, 0, 2500, 15), 120 new Rect(4000, 0, 4400, 20), 121 new Rect(0, 10, 2000, 20) 122 }; 123 layoutAndCheck(lp, adapter, expected, 6000, 20); 124 } 125 126 @Test 127 public void testUnspecifiedHeight() throws Throwable { 128 TestedFrameLayout.FullControlLayoutParams lp = 129 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 130 lp.hSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 131 BaseWrapContentWithAspectRatioTest.WrapContentAdapter 132 adapter = new BaseWrapContentWithAspectRatioTest.WrapContentAdapter( 133 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 4000, WRAP_CONTENT, WRAP_CONTENT), 134 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 5500, WRAP_CONTENT, WRAP_CONTENT), 135 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(10, 3000, WRAP_CONTENT, WRAP_CONTENT), 136 new BaseWrapContentWithAspectRatioTest.MeasureBehavior(20, 100, WRAP_CONTENT, WRAP_CONTENT) 137 ); 138 Rect[] expected = new Rect[] { 139 new Rect(0, 0, 10, 4000), 140 new Rect(20, 0, 30, 5500), 141 new Rect(40, 0, 50, 3000), 142 new Rect(40, 3000, 60, 3100) 143 }; 144 layoutAndCheck(lp, adapter, expected, 60, 5500); 145 } 146 147 @Override 148 RecyclerView.LayoutManager createLayoutManager() { 149 return new StaggeredGridLayoutManager(3, mOrientation); 150 } 151 152 @Override 153 protected int getVerticalGravity(RecyclerView.LayoutManager layoutManager) { 154 return Gravity.TOP; 155 } 156 157 @Override 158 protected int getHorizontalGravity(RecyclerView.LayoutManager layoutManager) { 159 return Gravity.LEFT; 160 } 161} 162