GridLayoutManagerRtlTest.java revision 754cb29c50f09a83251dd4bb633ba445b2411adb
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.LinearLayoutManager.HORIZONTAL; 20import static android.support.v7.widget.LinearLayoutManager.VERTICAL; 21 22import static org.junit.Assert.assertEquals; 23import static org.junit.Assert.assertNotNull; 24import static org.junit.Assert.assertTrue; 25 26import android.support.test.filters.MediumTest; 27import android.view.View; 28import android.view.ViewGroup; 29 30import org.junit.Test; 31import org.junit.runner.RunWith; 32import org.junit.runners.Parameterized; 33 34import java.util.ArrayList; 35import java.util.List; 36 37@MediumTest 38@RunWith(Parameterized.class) 39public class GridLayoutManagerRtlTest extends BaseGridLayoutManagerTest { 40 41 public GridLayoutManagerRtlTest(Config config, boolean changeRtlAfter, boolean oneLine, 42 boolean itemsWrapContent) { 43 mConfig = config; 44 mChangeRtlAfter = changeRtlAfter; 45 mOneLine = oneLine; 46 mItemsWrapContent = itemsWrapContent; 47 } 48 49 @Parameterized.Parameters(name = "conf:{0},changeRl:{1},oneLine:{2},itemsWrap:{3}") 50 public static List<Object[]> params() { 51 List<Object[]> result = new ArrayList<>(); 52 for (boolean changeRtlAfter : new boolean[]{false, true}) { 53 for (boolean oneLine : new boolean[]{false, true}) { 54 for (boolean itemsWrapContent : new boolean[]{false, true}) { 55 for (Config config : createBaseVariations()) { 56 result.add(new Object[] { 57 config, 58 changeRtlAfter, 59 oneLine, 60 itemsWrapContent 61 }); 62 } 63 } 64 } 65 } 66 return result; 67 } 68 final Config mConfig; 69 final boolean mChangeRtlAfter; 70 final boolean mOneLine; 71 final boolean mItemsWrapContent; 72 73 74 @Test 75 public void rtlTest() throws Throwable { 76 if (mOneLine && mConfig.mOrientation != VERTICAL) { 77 return;// nothing to test 78 } 79 if (mConfig.mSpanCount == 1) { 80 mConfig.mSpanCount = 2; 81 } 82 String logPrefix = mConfig + ", changeRtlAfterLayout:" + mChangeRtlAfter + "," 83 + "oneLine:" + mOneLine + " itemsWrap:" + mItemsWrapContent; 84 mConfig.mItemCount = 5; 85 if (mOneLine) { 86 mConfig.mSpanCount = mConfig.mItemCount + 1; 87 } else { 88 mConfig.mSpanCount = Math.min(mConfig.mItemCount - 1, mConfig.mSpanCount); 89 } 90 91 RecyclerView rv = setupBasic(mConfig, new GridTestAdapter(mConfig.mItemCount) { 92 @Override 93 public void onBindViewHolder(TestViewHolder holder, 94 int position) { 95 super.onBindViewHolder(holder, position); 96 if (mItemsWrapContent) { 97 ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); 98 if (lp == null) { 99 lp = mGlm.generateDefaultLayoutParams(); 100 } 101 if (mConfig.mOrientation == HORIZONTAL) { 102 lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; 103 } else { 104 lp.width = ViewGroup.LayoutParams.WRAP_CONTENT; 105 } 106 } 107 } 108 }); 109 if (mChangeRtlAfter) { 110 waitForFirstLayout(rv); 111 mGlm.expectLayout(1); 112 mGlm.setFakeRtl(true); 113 mGlm.waitForLayout(2); 114 } else { 115 mGlm.mFakeRTL = true; 116 waitForFirstLayout(rv); 117 } 118 119 assertEquals("view should become rtl", true, mGlm.isLayoutRTL()); 120 OrientationHelper helper = OrientationHelper.createHorizontalHelper(mGlm); 121 View child0 = mGlm.findViewByPosition(0); 122 final int secondChildPos = mConfig.mOrientation == VERTICAL ? 1 123 : mConfig.mSpanCount; 124 View child1 = mGlm.findViewByPosition(secondChildPos); 125 assertNotNull(logPrefix + " child position 0 should be laid out", child0); 126 assertNotNull( 127 logPrefix + " second child position " + (secondChildPos) + " should be laid out", 128 child1); 129 if (mConfig.mOrientation == VERTICAL || !mConfig.mReverseLayout) { 130 assertTrue(logPrefix + " second child should be to the left of first child", 131 helper.getDecoratedStart(child0) >= helper.getDecoratedEnd(child1)); 132 assertEquals(logPrefix + " first child should be right aligned", 133 helper.getDecoratedEnd(child0), helper.getEndAfterPadding()); 134 } else { 135 assertTrue(logPrefix + " first child should be to the left of second child", 136 helper.getDecoratedStart(child1) >= helper.getDecoratedEnd(child0)); 137 assertEquals(logPrefix + " first child should be left aligned", 138 helper.getDecoratedStart(child0), helper.getStartAfterPadding()); 139 } 140 checkForMainThreadException(); 141 } 142} 143