14143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar/* 24143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar * Copyright (C) 2015 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 174143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarpackage android.support.v7.widget; 184143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 196b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport static android.support.v7.widget.BaseLinearLayoutManagerTest.Config; 206b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport static android.support.v7.widget.LinearLayoutManager.HORIZONTAL; 216b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport static android.support.v7.widget.LinearLayoutManager.VERTICAL; 224143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 236b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport android.graphics.Rect; 244143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.support.v4.view.ViewCompat; 254143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.test.suitebuilder.annotation.MediumTest; 264143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport android.view.Gravity; 274143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 286b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport org.junit.Test; 296b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport org.junit.runner.RunWith; 306b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport org.junit.runners.Parameterized; 316b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar 324143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.ArrayList; 336b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyarimport java.util.Arrays; 344143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarimport java.util.List; 354143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar@RunWith(Parameterized.class) 374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar@MediumTest 384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyarpublic class LinearLayoutManagerWrapContentTest extends BaseWrapContentTest { 394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar Config mConfig; 414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar public LinearLayoutManagerWrapContentTest(Config config, 434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar WrapContentConfig wrapContentConfig) { 444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar super(wrapContentConfig); 454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mConfig = config; 464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Test 49f3844451301cb14ca885e125cb27f108a834c386Yigit Boyar public void testUnspecifiedWithHint() throws Throwable { 50f3844451301cb14ca885e125cb27f108a834c386Yigit Boyar unspecifiedWithHintTest(mConfig.mOrientation == StaggeredGridLayoutManager.HORIZONTAL); 51f3844451301cb14ca885e125cb27f108a834c386Yigit Boyar } 52f3844451301cb14ca885e125cb27f108a834c386Yigit Boyar 53f3844451301cb14ca885e125cb27f108a834c386Yigit Boyar @Test 544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar public void deletion() throws Throwable { 554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar testScenerio(new Scenario( 564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 574143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 584143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.deleteAndNotify(3, 3); 604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }, 624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.deleteAndNotify(3, 3); 664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }, 684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.deleteAndNotify(1, 2); 724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }) { 744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }); 754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Test 784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar public void addition() throws Throwable { 794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar testScenerio(new Scenario( 804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 814143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 824143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 834143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.addAndNotify(1, 2); 844143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 854143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 864143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar , 874143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 884143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 894143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 904143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.addAndNotify(0, 2); 914143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 924143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }, 934143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar new Step() { 944143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 954143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar void onRun() throws Throwable { 964143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mTestAdapter.addAndNotify(6, 3); 974143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 984143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 994143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar ) { 1004143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 1014143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar public int getSeedAdapterSize() { 1024143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return 2; 1034143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1044143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar }); 1054143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1064143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 1074143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Parameterized.Parameters(name = "{0} {1}") 1084143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar public static Iterable<Object[]> data() { 1094143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar List<Object[]> params = new ArrayList<>(); 1106b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar List<Rect> paddings = Arrays.asList( 1116b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(0, 0, 0, 0), 1126b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(5, 0, 0, 0), 1136b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(0, 6, 0, 0), 1146b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(0, 0, 7, 0), 1156b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(0, 0, 0, 8), 1166b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Rect(3, 5, 7, 11) 1176b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar ); 1186b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar for (Rect padding : paddings) { 1196b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar for (int orientation : new int[]{VERTICAL, HORIZONTAL}) { 1206b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar for (boolean reverseLayout : new boolean[]{false, true}) { 1216b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar for (boolean stackFromBottom : new boolean[]{false, true}) { 1226b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar params.add( 1236b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Object[]{ 1246b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Config(orientation, reverseLayout, stackFromBottom), 1256b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new WrapContentConfig(false, false, new Rect(padding)) 1266b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar } 1276b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar ); 1286b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar params.add( 1296b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Object[]{ 1306b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new Config(orientation, reverseLayout, stackFromBottom), 1316b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar new WrapContentConfig(HORIZONTAL == orientation, 1326b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar VERTICAL == orientation, new Rect(padding)) 1336b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar } 1346b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar ); 1356b6a29eea7f6a212447b3cc7b45a081b609ca4b1Yigit Boyar } 1364143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1374143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1384143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1394143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return params; 1404143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1414143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 1424143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 1434143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar RecyclerView.LayoutManager createLayoutManager() { 1444143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return createFromConfig(); 1454143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1464143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 1474143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar private LinearLayoutManager createFromConfig() { 1484143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar LinearLayoutManager llm = new LinearLayoutManager(getActivity(), mConfig.mOrientation, 1494143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar mConfig.mReverseLayout); 1504143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar llm.setStackFromEnd(mConfig.mStackFromEnd); 1514143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return llm; 1524143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1534143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 1544143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 1554143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar protected int getVerticalGravity(RecyclerView.LayoutManager layoutManager) { 1564143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar if (mConfig.mOrientation == HORIZONTAL) { 1574143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.TOP; 1584143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1594143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar if (mConfig.mReverseLayout ^ mConfig.mStackFromEnd) { 1604143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.BOTTOM; 1614143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1624143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.TOP; 1634143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1644143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar 1654143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar @Override 1664143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar protected int getHorizontalGravity(RecyclerView.LayoutManager layoutManager) { 1674143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar boolean rtl = layoutManager.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL; 1684143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar if (mConfig.mOrientation == VERTICAL) { 1694143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar if (rtl) { 1704143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.RIGHT; 1714143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1724143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.LEFT; 1734143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1744143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar boolean end = mConfig.mReverseLayout ^ mConfig.mStackFromEnd; 1754143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar if (rtl ^ end) { 1764143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.RIGHT; 1774143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1784143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar return Gravity.LEFT; 1794143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar } 1804143554adb9b31b700b6876a251a64419e6111e2Yigit Boyar} 181