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