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