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