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