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