LinearLayoutManagerResizeTest.java revision 999c3976674d20b0de5425490bdfe7415b9c6af2
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;
33
34@RunWith(Parameterized.class)
35public class LinearLayoutManagerResizeTest extends BaseLinearLayoutManagerTest {
36
37    final Config mConfig;
38
39    public LinearLayoutManagerResizeTest(Config config) {
40        mConfig = config;
41    }
42
43    @Before
44    @Override
45    public void setUp() throws Exception {
46        super.setUp();
47        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
48    }
49
50    @After
51    @Override
52    public void tearDown() throws Exception {
53        super.tearDown();
54    }
55
56    @Parameterized.Parameters(name = "{0}")
57    public static List<Config> testResize() throws Throwable {
58        List<Config> configs = new ArrayList<>();
59        for (Config config : addConfigVariation(createBaseVariations(), "mItemCount", 5
60                , Config.DEFAULT_ITEM_COUNT)) {
61            configs.add(config);
62        }
63        return configs;
64    }
65
66    @MediumTest
67    @Test
68    public void resize() throws Throwable {
69        final Config config = (Config) mConfig.clone();
70        final FrameLayout container = getRecyclerViewContainer();
71        runTestOnUiThread(new Runnable() {
72            @Override
73            public void run() {
74                container.setPadding(0, 0, 0, 0);
75            }
76        });
77
78        setupByConfig(config, true);
79        int lastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
80        int firstVisibleItemPosition = mLayoutManager.findFirstVisibleItemPosition();
81        int lastCompletelyVisibleItemPosition = mLayoutManager
82                .findLastCompletelyVisibleItemPosition();
83        int firstCompletelyVisibleItemPosition = mLayoutManager
84                .findFirstCompletelyVisibleItemPosition();
85        mLayoutManager.expectLayouts(1);
86        // resize the recycler view to half
87        runTestOnUiThread(new Runnable() {
88            @Override
89            public void run() {
90                if (config.mOrientation == HORIZONTAL) {
91                    container.setPadding(0, 0, container.getWidth() / 2, 0);
92                } else {
93                    container.setPadding(0, 0, 0, container.getWidth() / 2);
94                }
95            }
96        });
97        mLayoutManager.waitForLayout(1);
98        if (config.mStackFromEnd) {
99            assertEquals("[" + config + "]: last visible position should not change.",
100                    lastVisibleItemPosition, mLayoutManager.findLastVisibleItemPosition());
101            assertEquals("[" + config + "]: last completely visible position should not change",
102                    lastCompletelyVisibleItemPosition,
103                    mLayoutManager.findLastCompletelyVisibleItemPosition());
104        } else {
105            assertEquals("[" + config + "]: first visible position should not change.",
106                    firstVisibleItemPosition, mLayoutManager.findFirstVisibleItemPosition());
107            assertEquals("[" + config + "]: last completely visible position should not change",
108                    firstCompletelyVisibleItemPosition,
109                    mLayoutManager.findFirstCompletelyVisibleItemPosition());
110        }
111    }
112}
113