1/*
2 * Copyright (C) 2015 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;
20
21import static org.junit.Assert.assertEquals;
22
23import android.support.test.filters.MediumTest;
24import android.widget.FrameLayout;
25
26import org.junit.Test;
27import org.junit.runner.RunWith;
28import org.junit.runners.Parameterized;
29
30import java.util.ArrayList;
31import java.util.List;
32
33@RunWith(Parameterized.class)
34public class LinearLayoutManagerResizeTest extends BaseLinearLayoutManagerTest {
35
36    final Config mConfig;
37
38    public LinearLayoutManagerResizeTest(Config config) {
39        mConfig = config;
40    }
41
42    @Parameterized.Parameters(name = "{0}")
43    public static List<Config> testResize() throws Throwable {
44        List<Config> configs = new ArrayList<>();
45        for (Config config : addConfigVariation(createBaseVariations(), "mItemCount", 5
46                , Config.DEFAULT_ITEM_COUNT)) {
47            configs.add(config);
48        }
49        return configs;
50    }
51
52    @MediumTest
53    @Test
54    public void resize() throws Throwable {
55        final Config config = (Config) mConfig.clone();
56        final FrameLayout container = getRecyclerViewContainer();
57        mActivityRule.runOnUiThread(new Runnable() {
58            @Override
59            public void run() {
60                container.setPadding(0, 0, 0, 0);
61            }
62        });
63
64        setupByConfig(config, true);
65        int lastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
66        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
67        int lastCompletelyVisibleItemPosition = mLayoutManager
68                .findLastCompletelyVisibleItemPosition();
69        int firstCompletelyVisibleItemPosition = mLayoutManager
70                .findFirstCompletelyVisibleItemPosition();
71        mLayoutManager.expectLayouts(1);
72        // resize the recycler view to half
73        mActivityRule.runOnUiThread(new Runnable() {
74            @Override
75            public void run() {
76                if (config.mOrientation == HORIZONTAL) {
77                    container.setPadding(0, 0, container.getWidth() / 2, 0);
78                } else {
79                    container.setPadding(0, 0, 0, container.getHeight() / 2);
80                }
81            }
82        });
83        mLayoutManager.waitForLayout(1);
84        if (config.mStackFromEnd) {
85            assertEquals("[" + config + "]: last visible position should not change.",
86                    lastVisibleItemPosition, mLayoutManager.findLastVisibleItemPosition());
87            assertEquals("[" + config + "]: last completely visible position should not change",
88                    lastCompletelyVisibleItemPosition,
89                    mLayoutManager.findLastCompletelyVisibleItemPosition());
90        } else {
91            assertEquals("[" + config + "]: first visible position should not change.",
92                    firstVisibleItemPosition, mLayoutManager.findFirstVisibleItemPosition());
93            assertEquals("[" + config + "]: first completely visible position should not change",
94                    firstCompletelyVisibleItemPosition,
95                    mLayoutManager.findFirstCompletelyVisibleItemPosition());
96        }
97    }
98}
99