1/*
2 * Copyright 2018 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 */
16package androidx.recyclerview.widget;
17
18
19import static androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL;
20import static androidx.recyclerview.widget.LinearLayoutManager.VERTICAL;
21
22import static org.junit.Assert.assertEquals;
23
24import android.support.test.filters.MediumTest;
25import android.view.View;
26
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.Parameterized;
30
31import java.util.ArrayList;
32import java.util.List;
33
34@MediumTest
35@RunWith(Parameterized.class)
36public class GridLayoutManagerCachedBordersTest extends BaseGridLayoutManagerTest {
37
38    @Parameterized.Parameters(name = "{0}")
39    public static List<Config> params() {
40        List<Config> testConfigurations = createBaseVariations();
41        testConfigurations.addAll(cachedBordersTestConfigs());
42        return testConfigurations;
43    }
44
45    private final Config mConfig;
46
47    public GridLayoutManagerCachedBordersTest(Config config) {
48        mConfig = config;
49    }
50
51
52    @Test
53    public void gridCachedBorderstTest() throws Throwable {
54        RecyclerView recyclerView = setupBasic(mConfig);
55        mGlm.expectLayout(1);
56        setRecyclerView(recyclerView);
57        mGlm.waitForLayout(10);
58        final boolean vertical = mConfig.mOrientation == GridLayoutManager.VERTICAL;
59        final int expectedSizeSum = vertical ? recyclerView.getWidth() : recyclerView.getHeight();
60        final int lastVisible = mGlm.findLastVisibleItemPosition();
61        for (int i = 0; i < lastVisible; i += mConfig.mSpanCount) {
62            if ((i + 1) * mConfig.mSpanCount - 1 < lastVisible) {
63                int childrenSizeSum = 0;
64                for (int j = 0; j < mConfig.mSpanCount; j++) {
65                    View child = recyclerView.getChildAt(i * mConfig.mSpanCount + j);
66                    childrenSizeSum += vertical ? child.getWidth() : child.getHeight();
67                }
68                assertEquals(expectedSizeSum, childrenSizeSum);
69            }
70        }
71    }
72
73    private static List<Config> cachedBordersTestConfigs() {
74        ArrayList<Config> configs = new ArrayList<>();
75        final int[] spanCounts = new int[]{88, 279, 741};
76        final int[] spanPerItem = new int[]{11, 9, 13};
77        for (int orientation : new int[]{VERTICAL, HORIZONTAL}) {
78            for (boolean reverseLayout : new boolean[]{false, true}) {
79                for (int i = 0; i < spanCounts.length; i++) {
80                    Config config = new Config(spanCounts[i], orientation, reverseLayout);
81                    config.mSpanPerItem = spanPerItem[i];
82                    configs.add(config);
83                }
84            }
85        }
86        return configs;
87    }
88}
89