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 */
16
17
18package androidx.recyclerview.widget;
19
20import static org.hamcrest.CoreMatchers.is;
21import static org.hamcrest.CoreMatchers.notNullValue;
22import static org.hamcrest.CoreMatchers.nullValue;
23import static org.hamcrest.CoreMatchers.sameInstance;
24import static org.hamcrest.MatcherAssert.assertThat;
25
26import android.support.test.filters.SmallTest;
27import android.support.test.runner.AndroidJUnit4;
28
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32
33@RunWith(AndroidJUnit4.class)
34@SmallTest
35public class TileListTest {
36    int mTileSize = 3;
37    TileList<Integer> mTileList;
38
39    @Before
40    public void setUp() throws Exception {
41        mTileList = new TileList<>(mTileSize);
42    }
43
44    @Test
45    public void emptyGet() {
46        assertThat(mTileList.getItemAt(3), nullValue());
47        assertThat(mTileList.getItemAt(100), nullValue());
48    }
49
50    @Test
51    public void getItemAt() {
52        assertThat(mTileList.addOrReplace(createTile(0, 1, 2, 3)), nullValue());
53        assertThat(mTileList.getItemAt(0), is(1));
54        assertThat(mTileList.getItemAt(1), is(2));
55        assertThat(mTileList.getItemAt(2), is(3));
56        assertThat(mTileList.getItemAt(3), nullValue());
57    }
58
59    @Test
60    public void size() {
61        assertThat(mTileList.size(), is(0));
62        assertThat(mTileList.addOrReplace(createTile(0, 1, 2, 3)), nullValue());
63        assertThat(mTileList.size(), is(1));
64        assertThat(mTileList.addOrReplace(createTile(0, 3, 4, 5)), notNullValue());
65        assertThat(mTileList.size(), is(1));
66        assertThat(mTileList.addOrReplace(createTile(3, 1, 2, 3)), nullValue());
67        assertThat(mTileList.size(), is(2));
68
69        mTileList.clear();
70        assertThat(mTileList.size(), is(0));
71    }
72
73    @Test
74    public void getAtIndex() {
75        assertThat(mTileList.addOrReplace(createTile(0, 1, 2, 3)), nullValue());
76        assertThat(mTileList.addOrReplace(createTile(3, 1, 2, 3)), nullValue());
77        assertThat(mTileList.addOrReplace(createTile(6, 1, 2, 3)), nullValue());
78
79        assertThat(mTileList.getAtIndex(0).mStartPosition, is(0));
80        assertThat(mTileList.getAtIndex(1).mStartPosition, is(3));
81        assertThat(mTileList.getAtIndex(2).mStartPosition, is(6));
82        assertThat(mTileList.getAtIndex(3), nullValue());
83    }
84
85    @Test
86    public void addShortTileAndGet() {
87        assertThat(mTileList.addOrReplace(createTile(0, 1)), nullValue());
88        assertThat(mTileList.getItemAt(0), is(1));
89        assertThat(mTileList.getItemAt(1), nullValue());
90        assertThat(mTileList.getItemAt(2), nullValue());
91    }
92
93    @Test
94    public void addToReplaceAndGet() {
95        TileList.Tile<Integer> prev = createTile(0, 1, 2, 3);
96        mTileList.addOrReplace(prev);
97        assertThat(mTileList.addOrReplace(createTile(0, 4, 5, 6)), sameInstance(prev));
98        assertThat(mTileList.getItemAt(0), is(4));
99        assertThat(mTileList.getItemAt(1), is(5));
100        assertThat(mTileList.getItemAt(2), is(6));
101        assertThat(mTileList.getItemAt(3), nullValue());
102    }
103
104    @Test
105    public void addRangeWithGapAndGet() {
106        mTileList.addOrReplace(createTile(0, 1, 2, 3));
107        assertThat(mTileList.addOrReplace(createTile(mTileSize * 2, 4, 5, 6)), nullValue());
108        assertThat(mTileList.getItemAt(0), is(1));
109        assertThat(mTileList.getItemAt(1), is(2));
110        assertThat(mTileList.getItemAt(2), is(3));
111        assertThat(mTileList.getItemAt(mTileSize), nullValue());
112        assertThat(mTileList.getItemAt(mTileSize + 1), nullValue());
113        assertThat(mTileList.getItemAt(mTileSize + 2), nullValue());
114        assertThat(mTileList.getItemAt(mTileSize * 2), is(4));
115        assertThat(mTileList.getItemAt(mTileSize * 2 + 1), is(5));
116        assertThat(mTileList.getItemAt(mTileSize * 2 + 2), is(6));
117        assertThat(mTileList.addOrReplace(createTile(mTileSize, 7, 8, 9)), nullValue());
118        assertThat(mTileList.getItemAt(mTileSize), is(7));
119        assertThat(mTileList.getItemAt(mTileSize + 1), is(8));
120        assertThat(mTileList.getItemAt(mTileSize + 2), is(9));
121    }
122
123    @Test
124    public void remove() {
125        mTileList.addOrReplace(createTile(0, 1, 2, 3));
126        mTileList.addOrReplace(createTile(3, 4, 5, 6));
127        mTileList.addOrReplace(createTile(6, 7, 8, 9));
128        mTileList.addOrReplace(createTile(9, 10, 11, 12));
129
130        assertThat(mTileList.removeAtPos(0).mStartPosition, is(0));
131        assertThat(mTileList.size(), is(3));
132
133        assertThat(mTileList.removeAtPos(6).mStartPosition, is(6));
134        assertThat(mTileList.size(), is(2));
135
136        assertThat(mTileList.removeAtPos(9).mStartPosition, is(9));
137        assertThat(mTileList.size(), is(1));
138    }
139
140    private TileList.Tile<Integer> createTile(int startPosition, int... items) {
141        TileList.Tile<Integer> window = new TileList.Tile<>(Integer.class, mTileSize);
142        window.mStartPosition = startPosition;
143        window.mItemCount = items.length;
144        for (int i = 0; i < items.length; i ++) {
145            window.mItems[i] = items[i];
146        }
147        return window;
148    }
149}