1999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar/*
2999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * Copyright (C) 2015 The Android Open Source Project
3999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar *
4999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * Licensed under the Apache License, Version 2.0 (the "License");
5999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * you may not use this file except in compliance with the License.
6999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * You may obtain a copy of the License at
7999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar *
8999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar *      http://www.apache.org/licenses/LICENSE-2.0
9999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar *
10999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * Unless required by applicable law or agreed to in writing, software
11999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * distributed under the License is distributed on an "AS IS" BASIS,
12999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * See the License for the specific language governing permissions and
14999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar * limitations under the License.
15999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar */
16999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
17999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarpackage android.support.v7.widget;
18999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
19754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static android.support.v7.widget.LinearLayoutManager.HORIZONTAL;
20754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas
21754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport static org.junit.Assert.assertEquals;
22754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas
23754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.support.test.filters.MediumTest;
24754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikasimport android.widget.FrameLayout;
25754cb29c50f09a83251dd4bb633ba445b2411adbAurimas Liutikas
26999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarimport org.junit.Test;
27999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarimport org.junit.runner.RunWith;
28999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarimport org.junit.runners.Parameterized;
29999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
30999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarimport java.util.ArrayList;
31999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarimport java.util.List;
32999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
33999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar@RunWith(Parameterized.class)
34999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyarpublic class LinearLayoutManagerResizeTest extends BaseLinearLayoutManagerTest {
35999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
36999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    final Config mConfig;
37999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
38999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    public LinearLayoutManagerResizeTest(Config config) {
39999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        mConfig = config;
40999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    }
41999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
42999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    @Parameterized.Parameters(name = "{0}")
43999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    public static List<Config> testResize() throws Throwable {
44999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        List<Config> configs = new ArrayList<>();
45999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        for (Config config : addConfigVariation(createBaseVariations(), "mItemCount", 5
46999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                , Config.DEFAULT_ITEM_COUNT)) {
47999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            configs.add(config);
48999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        }
49999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        return configs;
50999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    }
51999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
52999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    @MediumTest
53999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    @Test
54999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    public void resize() throws Throwable {
55999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        final Config config = (Config) mConfig.clone();
56999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        final FrameLayout container = getRecyclerViewContainer();
5742e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas        mActivityRule.runOnUiThread(new Runnable() {
58999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            @Override
59999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            public void run() {
60999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                container.setPadding(0, 0, 0, 0);
61999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            }
62999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        });
63999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar
64999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        setupByConfig(config, true);
65999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        int lastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
66999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
67999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        int lastCompletelyVisibleItemPosition = mLayoutManager
68999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                .findLastCompletelyVisibleItemPosition();
69999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        int firstCompletelyVisibleItemPosition = mLayoutManager
70999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                .findFirstCompletelyVisibleItemPosition();
71999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        mLayoutManager.expectLayouts(1);
72999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        // resize the recycler view to half
7342e7d6fafcde7bfe261dd7d8d75ee53ca0cd6790Aurimas Liutikas        mActivityRule.runOnUiThread(new Runnable() {
74999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            @Override
75999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            public void run() {
76999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                if (config.mOrientation == HORIZONTAL) {
77999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    container.setPadding(0, 0, container.getWidth() / 2, 0);
78999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                } else {
7986d05261a6618fe1c3114ca40cd4077b29cd7790Aurimas Liutikas                    container.setPadding(0, 0, 0, container.getHeight() / 2);
80999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                }
81999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            }
82999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        });
83999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        mLayoutManager.waitForLayout(1);
84999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        if (config.mStackFromEnd) {
85999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            assertEquals("[" + config + "]: last visible position should not change.",
86999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    lastVisibleItemPosition, mLayoutManager.findLastVisibleItemPosition());
87999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            assertEquals("[" + config + "]: last completely visible position should not change",
88999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    lastCompletelyVisibleItemPosition,
89999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    mLayoutManager.findLastCompletelyVisibleItemPosition());
90999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        } else {
91999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar            assertEquals("[" + config + "]: first visible position should not change.",
92999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    firstVisibleItemPosition, mLayoutManager.findFirstVisibleItemPosition());
9386d05261a6618fe1c3114ca40cd4077b29cd7790Aurimas Liutikas            assertEquals("[" + config + "]: first completely visible position should not change",
94999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    firstCompletelyVisibleItemPosition,
95999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar                    mLayoutManager.findFirstCompletelyVisibleItemPosition());
96999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar        }
97999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar    }
98999c3976674d20b0de5425490bdfe7415b9c6af2Yigit Boyar}
99