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