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
19e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static android.view.View.MeasureSpec.AT_MOST;
20e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static android.view.View.MeasureSpec.EXACTLY;
21e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static android.view.View.MeasureSpec.UNSPECIFIED;
22e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
23e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
24e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas
25c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikasimport static androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL;
26c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikasimport static androidx.recyclerview.widget.LinearLayoutManager.VERTICAL;
27c95a6f1f125ad3a7e1f9f79bccf4b2603bc40ebaAurimas Liutikas
28e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static org.hamcrest.CoreMatchers.is;
29e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport static org.hamcrest.MatcherAssert.assertThat;
304143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
314143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.graphics.Color;
32754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.MediumTest;
334143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.view.View;
344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
35e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.Before;
36e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.Test;
37e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.runner.RunWith;
38e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikasimport org.junit.runners.Parameterized;
39e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas
404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.ArrayList;
414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.List;
424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar@RunWith(Parameterized.class)
444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar@MediumTest
454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarpublic class GridLayoutManagerWrapContentWithAspectRatioTest
464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        extends BaseWrapContentWithAspectRatioTest {
474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
48e9f9cd8d0e9008340985d17a2541ab24b3adb391Aurimas Liutikas    @Parameterized.Parameters(name = "{0},{1},{2}")
494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public static List<Object[]> params() {
504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        List<Object[]> params = new ArrayList<>();
514143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        for (int orientation : new int[]{VERTICAL, HORIZONTAL}) {
524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            for (boolean reverseLayout : new boolean[]{false, true}) {
534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                for (boolean unlimitedW : new boolean[]{true, false}) {
544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    for (boolean unlimitedH : new boolean[]{true, false}) {
554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        for (int behavior1Size : new int[]{8, 10, 12}) {
564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                            params.add(new Object[]{
57ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                                    new BaseWrapContentTest.WrapContentConfig(unlimitedW, unlimitedH),
58ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                                    new BaseGridLayoutManagerTest.Config(3, orientation, reverseLayout),
594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                                    behavior1Size
604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                            });
614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        }
624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    }
644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                }
654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            }
674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        return params;
694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    private GridLayoutManagerTest.Config mConfig;
724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    private int mBehavior1Size;
744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    int mTestOrientation;
764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    boolean mUnlimited;
784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    RecyclerView.LayoutManager mLayoutManager;
804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
8187b20c0e2004e884422b37fae6202c83cc24a8d9Yigit Boyar    WrappedRecyclerView mRecyclerView;
824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    OrientationHelper mHelper;
844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
85ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas    public GridLayoutManagerWrapContentWithAspectRatioTest(BaseWrapContentTest.WrapContentConfig wrapContentConfig,
864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            GridLayoutManagerTest.Config config, int behavior1Size) {
874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        super(wrapContentConfig);
884143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mConfig = config;
894143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mBehavior1Size = behavior1Size;
904143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
914143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
924143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Before
934143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public final void init() {
944143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        TestedFrameLayout.FullControlLayoutParams lp =
954143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mWrapContentConfig.toLayoutParams(WRAP_CONTENT, WRAP_CONTENT);
964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mConfig.mOrientation == VERTICAL) {
974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mTestOrientation = HORIZONTAL;
984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mUnlimited = lp.wSpec != null;
994143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
1004143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mTestOrientation = VERTICAL;
1014143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            mUnlimited = lp.hSpec != null;
1024143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
1034143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mLayoutManager = createFromConfig();
1044143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
10587b20c0e2004e884422b37fae6202c83cc24a8d9Yigit Boyar        mRecyclerView = new WrappedRecyclerView(getActivity());
1064143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mHelper = OrientationHelper.createOrientationHelper(
1074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mLayoutManager, 1 - mConfig.mOrientation);
1084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setBackgroundColor(Color.rgb(0, 0, 255));
1104143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setLayoutManager(mLayoutManager);
1114143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setLayoutParams(lp);
1124143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
1134143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1144143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Test
1154143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public void testChildWithMultipleSpans() throws Throwable {
1164143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior1;
1174143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        behavior1 = new MeasureBehavior(mBehavior1Size, mBehavior1Size,
1184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == HORIZONTAL ? MATCH_PARENT : WRAP_CONTENT,
1194143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == VERTICAL ? MATCH_PARENT : WRAP_CONTENT);
1204143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1214143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior2 = new MeasureBehavior(
1224143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == HORIZONTAL ? 30 : 10,
1234143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == VERTICAL ? 30 : 10, WRAP_CONTENT, WRAP_CONTENT);
1244143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        WrapContentAdapter adapter = new WrapContentAdapter(behavior1, behavior2);
1254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        ((GridLayoutManager) mLayoutManager).setSpanSizeLookup(
1264143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                new GridLayoutManager.SpanSizeLookup() {
1274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    @Override
1284143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    public int getSpanSize(int position) {
1294143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        return position == 1 ? 2 : 1;
1304143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    }
1314143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                });
1324143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setAdapter(adapter);
1334143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        setRecyclerView(mRecyclerView);
1344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.waitUntilLayout();
1354143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        final int parentSize = getSize((View) mRecyclerView.getParent(), mTestOrientation);
1374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mUnlimited) {
1394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
1404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
1414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
1424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
1434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
1444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            int[] borders = GridLayoutManager.calculateItemBorders(null,
1454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    mConfig.mSpanCount, parentSize);
1464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
1474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[1] - borders[0], AT_MOST));
1484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
1494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[3] - borders[1], AT_MOST));
1504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
1514143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        // child0 should be measured again because it measured its size as 10
1524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(behavior1.getSpec(1, mTestOrientation),
1534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                MeasureSpecMatcher.is(15, EXACTLY));
1544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(behavior1.getSpec(1, mConfig.mOrientation),
1554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                MeasureSpecMatcher.mode(UNSPECIFIED));
1564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        switch (mBehavior1Size) {
1574143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 10:
1584143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(2));
1594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
1604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
1614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 8:
1624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
1634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(3));
1644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(2, mTestOrientation),
1654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(15, EXACTLY));
1664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(2, mConfig.mOrientation),
1674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(10, EXACTLY));
1684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
1694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 12:
1704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(2));
1714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(2));
1724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mTestOrientation),
1734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(30, AT_MOST));
1744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mConfig.mOrientation),
1754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(12, EXACTLY));
1764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
1774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
1784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child0 = mRecyclerView.getChildAt(0);
1804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child0, mTestOrientation), is(15));
1814143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child1 = mRecyclerView.getChildAt(1);
1834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child1, mTestOrientation), is(30));
1844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child0), is(0));
1864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child1), is(15));
1874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1884143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child0), is(15));
1894143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child1), is(45));
1904143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1914143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child0),
1924143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
1934143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child1),
1944143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
1954143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
1964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, mTestOrientation), is(45));
1974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, 1 - mTestOrientation), is(Math.max(10, mBehavior1Size)));
1984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
1994143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2004143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Test
2014143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public void testChildWithMatchParentInOtherDirection() throws Throwable {
2024143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior1;
2034143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        behavior1 = new MeasureBehavior(mBehavior1Size, mBehavior1Size,
2044143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == HORIZONTAL ? MATCH_PARENT : WRAP_CONTENT,
2054143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == VERTICAL ? MATCH_PARENT : WRAP_CONTENT);
2064143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior2 = new MeasureBehavior(
2084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == HORIZONTAL ? 15 : 10,
2094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == VERTICAL ? 15 : 10, WRAP_CONTENT, WRAP_CONTENT);
2104143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        WrapContentAdapter adapter = new WrapContentAdapter(behavior1, behavior2);
2114143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2124143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setAdapter(adapter);
2134143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        setRecyclerView(mRecyclerView);
2144143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.waitUntilLayout();
2154143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        final int parentSize = getSize((View) mRecyclerView.getParent(), mTestOrientation);
2164143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mUnlimited) {
2174143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
2184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
2194143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
2204143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
2214143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
2224143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            int[] borders = GridLayoutManager.calculateItemBorders(null, mConfig.mSpanCount,
2234143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    parentSize);
2244143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
2254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[1] - borders[0], AT_MOST));
2264143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
2274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[2] - borders[1], AT_MOST));
2284143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
2294143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        // child0 should be measured again because it measured its size as 10
2304143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(behavior1.getSpec(1, mTestOrientation),
2314143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                MeasureSpecMatcher.is(15, EXACTLY));
2324143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(behavior1.getSpec(1, mConfig.mOrientation),
2334143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                MeasureSpecMatcher.mode(UNSPECIFIED));
2344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        switch (mBehavior1Size) {
2354143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 10:
2364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(2));
2374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
2384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
2394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 8:
2404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
2414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(3));
2424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(2, mTestOrientation),
2434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(15, EXACTLY));
2444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(2, mConfig.mOrientation),
2454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(10, EXACTLY));
2464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
2474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 12:
2484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(2));
2494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(2));
2504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mTestOrientation),
2514143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(15, AT_MOST));
2524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mConfig.mOrientation),
2534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(12, EXACTLY));
2544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
2554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
2564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2574143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child0 = mRecyclerView.getChildAt(0);
2584143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child0, mTestOrientation), is(15));
2594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child1 = mRecyclerView.getChildAt(1);
2614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child1, mTestOrientation), is(15));
2624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child0), is(0));
2644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child1), is(15));
2654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child0), is(15));
2674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child1), is(30));
2684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child0),
2704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
2714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child1),
2724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
2734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, mTestOrientation), is(45));
2754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, 1 - mTestOrientation), is(Math.max(10, mBehavior1Size)));
2764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
2774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    @Test
2794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    public void testAllChildrenWrapContentInOtherDirection() throws Throwable {
2804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior1;
2814143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        behavior1 = new MeasureBehavior(mBehavior1Size, mBehavior1Size, WRAP_CONTENT, WRAP_CONTENT);
2824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        MeasureBehavior behavior2 = new MeasureBehavior(
2844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == HORIZONTAL ? 15 : 10,
2854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mTestOrientation == VERTICAL ? 15 : 10, WRAP_CONTENT, WRAP_CONTENT);
2864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        WrapContentAdapter adapter = new WrapContentAdapter(behavior1, behavior2);
2874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
2884143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.setAdapter(adapter);
2894143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        setRecyclerView(mRecyclerView);
2904143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        mRecyclerView.waitUntilLayout();
2914143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        final int parentSize = getSize((View) mRecyclerView.getParent(), mTestOrientation);
2924143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        if (mUnlimited) {
2934143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
2944143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
2954143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
2964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.mode(UNSPECIFIED));
2974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        } else {
2984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            int[] borders = GridLayoutManager.calculateItemBorders(null,
2994143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    mConfig.mSpanCount, parentSize);
3004143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior1.getSpec(0, mTestOrientation),
3014143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[1] - borders[0], AT_MOST));
3024143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            assertThat(behavior2.getSpec(0, mTestOrientation),
3034143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                    MeasureSpecMatcher.is(borders[2] - borders[1], AT_MOST));
3044143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
3054143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3064143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        switch (mBehavior1Size) {
3074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 10:
3084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(1));
3094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
3104143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
3114143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 8:
3124143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(1));
3134143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(2));
3144143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(1, mTestOrientation),
3154143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(15, AT_MOST));
3164143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.getSpec(1, mConfig.mOrientation),
3174143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(10, EXACTLY));
3184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
3194143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar            case 12:
3204143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior1.measureSpecs.size(), is(1));
3214143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.measureSpecs.size(), is(2));
3224143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mTestOrientation),
3234143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(15, AT_MOST));
3244143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                assertThat(behavior2.getSpec(1, mConfig.mOrientation),
3254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                        MeasureSpecMatcher.is(12, EXACTLY));
3264143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                break;
3274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        }
3284143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3294143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child0 = mRecyclerView.getChildAt(0);
3304143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child0, mTestOrientation), is(mBehavior1Size));
3314143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3324143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        View child1 = mRecyclerView.getChildAt(1);
3334143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(child1, mTestOrientation), is(15));
3344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3354143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child0), is(0));
3364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedStart(child1), is(15));
3374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child0), is(mBehavior1Size));
3394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedEnd(child1), is(30));
3404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child0),
3424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
3434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(mHelper.getDecoratedMeasurementInOther(child1),
3444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                is(Math.max(10, mBehavior1Size)));
3454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, mTestOrientation), is(45));
3474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        assertThat(getSize(mRecyclerView, 1 - mTestOrientation), is(Math.max(10, mBehavior1Size)));
3484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
3494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar
3504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    private RecyclerView.LayoutManager createFromConfig() {
3514143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar        return new GridLayoutManager(getActivity(), mConfig.mSpanCount,
3524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar                mConfig.mOrientation, mConfig.mReverseLayout);
3534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar    }
3544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar}
355