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 */ 16package android.support.v7.widget; 17 18import static android.support.v7.widget.BaseWrapContentWithAspectRatioTest.AspectRatioMeasureBehavior; 19import static android.support.v7.widget.BaseWrapContentWithAspectRatioTest.MeasureBehavior; 20import static android.support.v7.widget.BaseWrapContentWithAspectRatioTest.WrapContentAdapter; 21import static android.support.v7.widget.LinearLayoutManager.HORIZONTAL; 22import static android.support.v7.widget.LinearLayoutManager.VERTICAL; 23import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; 24import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 25 26import android.app.Activity; 27import android.graphics.Rect; 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@RunWith(Parameterized.class) 39public class GridLayoutManagerWrapContentTest extends BaseWrapContentTest { 40 private boolean mHorizontal = false; 41 private int mSpanCount = 3; 42 private RecyclerView.ItemDecoration mItemDecoration; 43 public GridLayoutManagerWrapContentTest(Rect padding) { 44 super(new WrapContentConfig(false, false, padding)); 45 } 46 47 @Parameterized.Parameters(name = "paddingRect={0}") 48 public static List<Rect> params() { 49 return Arrays.asList( 50 new Rect(0, 0, 0, 0), 51 new Rect(5, 0, 0, 0), 52 new Rect(0, 3, 0, 0), 53 new Rect(0, 0, 2, 0), 54 new Rect(0, 0, 0, 7), 55 new Rect(3, 5, 7, 11) 56 ); 57 } 58 59 @Override 60 RecyclerView.LayoutManager createLayoutManager() { 61 GridLayoutManager lm = new GridLayoutManager(getActivity(), mSpanCount); 62 lm.setOrientation(mHorizontal ? HORIZONTAL : VERTICAL); 63 return lm; 64 } 65 66 @Override 67 protected WrappedRecyclerView createRecyclerView(Activity activity) { 68 WrappedRecyclerView recyclerView = super.createRecyclerView(activity); 69 if (mItemDecoration != null) { 70 recyclerView.addItemDecoration(mItemDecoration); 71 } 72 return recyclerView; 73 } 74 75 @Test 76 public void testUnspecifiedWithHint() throws Throwable { 77 unspecifiedWithHintTest(mHorizontal); 78 } 79 80 @Test 81 public void testVerticalWithItemDecors() throws Throwable { 82 mItemDecoration = new RecyclerView.ItemDecoration() { 83 @Override 84 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 85 RecyclerView.State state) { 86 outRect.set(0, 5, 0, 10); 87 } 88 }; 89 TestedFrameLayout.FullControlLayoutParams lp = 90 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 91 WrapContentAdapter adapter = new WrapContentAdapter( 92 new MeasureBehavior(10, 10, WRAP_CONTENT, MATCH_PARENT) 93 ); 94 Rect[] expected = new Rect[] { 95 new Rect(0, 0, 10, 25) 96 }; 97 layoutAndCheck(lp, adapter, expected, 30, 25); 98 } 99 100 @Test 101 public void testHorizontalWithItemDecors() throws Throwable { 102 mItemDecoration = new RecyclerView.ItemDecoration() { 103 @Override 104 public void getItemOffsets(Rect outRect, View view, RecyclerView parent, 105 RecyclerView.State state) { 106 outRect.set(5, 0, 10, 0); 107 } 108 }; 109 TestedFrameLayout.FullControlLayoutParams lp = 110 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 111 WrapContentAdapter adapter = new WrapContentAdapter( 112 new MeasureBehavior(10, 10, MATCH_PARENT, WRAP_CONTENT) 113 ); 114 Rect[] expected = new Rect[] { 115 new Rect(0, 0, 25, 10) 116 }; 117 layoutAndCheck(lp, adapter, expected, 75, 10); 118 } 119 120 @Test 121 public void testHorizontal() throws Throwable { 122 mHorizontal = true; 123 mSpanCount = 2; 124 TestedFrameLayout.FullControlLayoutParams lp = 125 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 126 WrapContentAdapter adapter = new WrapContentAdapter( 127 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 128 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 129 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 130 new MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 131 ); 132 Rect[] expected = new Rect[] { 133 new Rect(0, 0, 10, 10), 134 new Rect(0, 10, 10, 20), 135 new Rect(10, 0, 30, 10), 136 new Rect(10, 10, 30, 20) 137 }; 138 layoutAndCheck(lp, adapter, expected, 30, 20); 139 } 140 141 @Test 142 public void testHandleSecondLineChangingBorders() throws Throwable { 143 TestedFrameLayout.FullControlLayoutParams lp = 144 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 145 WrapContentAdapter adapter = new WrapContentAdapter( 146 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 147 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 148 new MeasureBehavior(10, 10, WRAP_CONTENT, WRAP_CONTENT), 149 new MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 150 ); 151 Rect[] expected = new Rect[] { 152 new Rect(0, 0, 10, 10), 153 new Rect(20, 0, 30, 10), 154 new Rect(40, 0, 50, 10), 155 new Rect(0, 10, 20, 20) 156 }; 157 layoutAndCheck(lp, adapter, expected, 60, 20); 158 } 159 160 @Test 161 public void testSecondLineAffectingBordersWithAspectRatio() throws Throwable { 162 TestedFrameLayout.FullControlLayoutParams lp = 163 mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT); 164 WrapContentAdapter adapter = new WrapContentAdapter( 165 new AspectRatioMeasureBehavior(10, 5, MATCH_PARENT, WRAP_CONTENT) 166 .aspectRatio(HORIZONTAL, .5f), 167 new MeasureBehavior(10, 5, WRAP_CONTENT, WRAP_CONTENT), 168 new MeasureBehavior(10, 5, MATCH_PARENT, WRAP_CONTENT), 169 new MeasureBehavior(20, 10, WRAP_CONTENT, WRAP_CONTENT) 170 ); 171 Rect[] expected = new Rect[] { 172 new Rect(0, 0, 20, 10), 173 new Rect(20, 0, 30, 10), 174 new Rect(40, 0, 60, 10), 175 new Rect(0, 10, 20, 20) 176 }; 177 layoutAndCheck(lp, adapter, expected, 60, 20); 178 } 179 180 @Override 181 protected int getVerticalGravity(RecyclerView.LayoutManager layoutManager) { 182 return Gravity.TOP; 183 } 184 185 @Override 186 protected int getHorizontalGravity(RecyclerView.LayoutManager layoutManager) { 187 return Gravity.LEFT; 188 } 189} 190