14143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar/*
2ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas * Copyright 2018 The Android Open Source Project
34143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar *
44143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
54143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * you may not use this file except in compliance with the License.
64143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * You may obtain a copy of the License at
74143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar *
84143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
94143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar *
104143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * Unless required by applicable law or agreed to in writing, software
114143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
124143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * See the License for the specific language governing permissions and
144143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * limitations under the License.
154143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar */
164143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.recyclerview.widget;
184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
19ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport static androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL;
20ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikasimport static androidx.recyclerview.widget.LinearLayoutManager.VERTICAL;
21e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas
22e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static org.junit.Assert.assertEquals;
23e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static org.junit.Assert.assertNotNull;
24e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static org.junit.Assert.assertTrue;
254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
26754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.MediumTest;
274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.view.View;
284143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.view.ViewGroup;
294143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
30c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikasimport androidx.annotation.NonNull;
31c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikas
32e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.Test;
33e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.runner.RunWith;
34e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.runners.Parameterized;
35e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas
364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.ArrayList;
374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.List;
384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
39754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas@MediumTest
404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar@RunWith(Parameterized.class)
414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarpublic class GridLayoutManagerRtlTest extends BaseGridLayoutManagerTest {
424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public GridLayoutManagerRtlTest(Config config, boolean changeRtlAfter, boolean oneLine,
444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            boolean itemsWrapContent) {
454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mConfig = config;
464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mChangeRtlAfter = changeRtlAfter;
474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mOneLine = oneLine;
484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mItemsWrapContent = itemsWrapContent;
494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
51e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas    @Parameterized.Parameters(name = "conf:{0},changeRl:{1},oneLine:{2},itemsWrap:{3}")
524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public static List<Object[]> params() {
534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        List<Object[]> result = new ArrayList<>();
544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        for (boolean changeRtlAfter : new boolean[]{false, true}) {
554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            for (boolean oneLine : new boolean[]{false, true}) {
564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                for (boolean itemsWrapContent : new boolean[]{false, true}) {
574143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    for (Config config : createBaseVariations()) {
584143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        result.add(new Object[] {
594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                                config,
604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                                changeRtlAfter,
614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                                oneLine,
624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                                itemsWrapContent
634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        });
644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    }
654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                }
664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            }
674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        return result;
694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    final Config mConfig;
714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    final boolean mChangeRtlAfter;
724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    final boolean mOneLine;
734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    final boolean mItemsWrapContent;
744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Test
774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public void rtlTest() throws Throwable {
784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mOneLine && mConfig.mOrientation != VERTICAL) {
794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            return;// nothing to test
804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
814143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mConfig.mSpanCount == 1) {
824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mConfig.mSpanCount = 2;
834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        String logPrefix = mConfig + ", changeRtlAfterLayout:" + mChangeRtlAfter + ","
854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                + "oneLine:" + mOneLine + " itemsWrap:" + mItemsWrapContent;
864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mConfig.mItemCount = 5;
874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mOneLine) {
884143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mConfig.mSpanCount = mConfig.mItemCount + 1;
894143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
904143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mConfig.mSpanCount = Math.min(mConfig.mItemCount - 1, mConfig.mSpanCount);
914143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
924143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
934143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        RecyclerView rv = setupBasic(mConfig, new GridTestAdapter(mConfig.mItemCount) {
944143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            @Override
958a11e6829c522aa1efcc903afa4c01d337082eabChris Craik            public void onBindViewHolder(@NonNull TestViewHolder holder,
964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    int position) {
974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                super.onBindViewHolder(holder, position);
984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                if (mItemsWrapContent) {
994143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
1004143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    if (lp == null) {
1014143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        lp = mGlm.generateDefaultLayoutParams();
1024143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    }
1034143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    if (mConfig.mOrientation == HORIZONTAL) {
1044143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
1054143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    } else {
1064143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
1074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    }
1084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                }
1094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            }
1104143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        });
1114143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mChangeRtlAfter) {
1124143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            waitForFirstLayout(rv);
1134143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mGlm.expectLayout(1);
1144143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mGlm.setFakeRtl(true);
1154143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mGlm.waitForLayout(2);
1164143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
1174143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mGlm.mFakeRTL = true;
1184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            waitForFirstLayout(rv);
1194143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
1204143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1214143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertEquals("view should become rtl", true, mGlm.isLayoutRTL());
1224143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        OrientationHelper helper = OrientationHelper.createHorizontalHelper(mGlm);
1234143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child0 = mGlm.findViewByPosition(0);
1244143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        final int secondChildPos = mConfig.mOrientation == VERTICAL ? 1
1254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                : mConfig.mSpanCount;
1264143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child1 = mGlm.findViewByPosition(secondChildPos);
1274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertNotNull(logPrefix + " child position 0 should be laid out", child0);
1284143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertNotNull(
1294143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                logPrefix + " second child position " + (secondChildPos) + " should be laid out",
1304143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                child1);
1314143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mConfig.mOrientation == VERTICAL || !mConfig.mReverseLayout) {
1324143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertTrue(logPrefix + " second child should be to the left of first child",
1334143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    helper.getDecoratedStart(child0) >= helper.getDecoratedEnd(child1));
1344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertEquals(logPrefix + " first child should be right aligned",
1354143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    helper.getDecoratedEnd(child0), helper.getEndAfterPadding());
1364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
1374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertTrue(logPrefix + " first child should be to the left of second child",
1384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    helper.getDecoratedStart(child1) >= helper.getDecoratedEnd(child0));
1394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertEquals(logPrefix + " first child should be left aligned",
1404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    helper.getDecoratedStart(child0), helper.getStartAfterPadding());
1414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
1424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        checkForMainThreadException();
1434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
1444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar}
145