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