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.BaseLinearLayoutManagerTest.Config;
20import static android.support.v7.widget.LinearLayoutManager.HORIZONTAL;
21import static android.support.v7.widget.LinearLayoutManager.VERTICAL;
22
23import android.graphics.Rect;
24import android.support.v4.view.ViewCompat;
25import android.test.suitebuilder.annotation.MediumTest;
26import android.view.Gravity;
27
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.Parameterized;
31
32import java.util.ArrayList;
33import java.util.Arrays;
34import java.util.List;
35
36@RunWith(Parameterized.class)
37@MediumTest
38public class LinearLayoutManagerWrapContentTest extends BaseWrapContentTest {
39
40    Config mConfig;
41
42    public LinearLayoutManagerWrapContentTest(Config config,
43            WrapContentConfig wrapContentConfig) {
44        super(wrapContentConfig);
45        mConfig = config;
46    }
47
48    @Test
49    public void testUnspecifiedWithHint() throws Throwable {
50        unspecifiedWithHintTest(mConfig.mOrientation == StaggeredGridLayoutManager.HORIZONTAL);
51    }
52
53    @Test
54    public void deletion() throws Throwable {
55        testScenerio(new Scenario(
56                new Step() {
57                    @Override
58                    void onRun() throws Throwable {
59                        mTestAdapter.deleteAndNotify(3, 3);
60                    }
61                },
62                new Step() {
63                    @Override
64                    void onRun() throws Throwable {
65                        mTestAdapter.deleteAndNotify(3, 3);
66                    }
67                },
68                new Step() {
69                    @Override
70                    void onRun() throws Throwable {
71                        mTestAdapter.deleteAndNotify(1, 2);
72                    }
73                }) {
74        });
75    }
76
77    @Test
78    public void addition() throws Throwable {
79        testScenerio(new Scenario(
80                new Step() {
81                    @Override
82                    void onRun() throws Throwable {
83                        mTestAdapter.addAndNotify(1, 2);
84                    }
85                }
86                ,
87                new Step() {
88                    @Override
89                    void onRun() throws Throwable {
90                        mTestAdapter.addAndNotify(0, 2);
91                    }
92                },
93                new Step() {
94                    @Override
95                    void onRun() throws Throwable {
96                        mTestAdapter.addAndNotify(6, 3);
97                    }
98                }
99        ) {
100            @Override
101            public int getSeedAdapterSize() {
102                return 2;
103            }
104        });
105    }
106
107    @Parameterized.Parameters(name = "{0} {1}")
108    public static Iterable<Object[]> data() {
109        List<Object[]> params = new ArrayList<>();
110        List<Rect> paddings = Arrays.asList(
111                new Rect(0, 0, 0, 0),
112                new Rect(5, 0, 0, 0),
113                new Rect(0, 6, 0, 0),
114                new Rect(0, 0, 7, 0),
115                new Rect(0, 0, 0, 8),
116                new Rect(3, 5, 7, 11)
117        );
118        for (Rect padding : paddings) {
119            for (int orientation : new int[]{VERTICAL, HORIZONTAL}) {
120                for (boolean reverseLayout : new boolean[]{false, true}) {
121                    for (boolean stackFromBottom : new boolean[]{false, true}) {
122                        params.add(
123                                new Object[]{
124                                        new Config(orientation, reverseLayout, stackFromBottom),
125                                        new WrapContentConfig(false, false, new Rect(padding))
126                                }
127                        );
128                        params.add(
129                                new Object[]{
130                                        new Config(orientation, reverseLayout, stackFromBottom),
131                                        new WrapContentConfig(HORIZONTAL == orientation,
132                                                VERTICAL == orientation, new Rect(padding))
133                                }
134                        );
135                    }
136                }
137            }
138        }
139        return params;
140    }
141
142    @Override
143    RecyclerView.LayoutManager createLayoutManager() {
144        return createFromConfig();
145    }
146
147    private LinearLayoutManager createFromConfig() {
148        LinearLayoutManager llm = new LinearLayoutManager(getActivity(), mConfig.mOrientation,
149                mConfig.mReverseLayout);
150        llm.setStackFromEnd(mConfig.mStackFromEnd);
151        return llm;
152    }
153
154    @Override
155    protected int getVerticalGravity(RecyclerView.LayoutManager layoutManager) {
156        if (mConfig.mOrientation == HORIZONTAL) {
157            return Gravity.TOP;
158        }
159        if (mConfig.mReverseLayout ^ mConfig.mStackFromEnd) {
160            return Gravity.BOTTOM;
161        }
162        return Gravity.TOP;
163    }
164
165    @Override
166    protected int getHorizontalGravity(RecyclerView.LayoutManager layoutManager) {
167        boolean rtl = layoutManager.getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
168        if (mConfig.mOrientation == VERTICAL) {
169            if (rtl) {
170                return Gravity.RIGHT;
171            }
172            return Gravity.LEFT;
173        }
174        boolean end = mConfig.mReverseLayout ^ mConfig.mStackFromEnd;
175        if (rtl ^ end) {
176            return Gravity.RIGHT;
177        }
178        return Gravity.LEFT;
179    }
180}
181