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 */ 16package android.support.v17.leanback.widget; 17 18/** 19 * Testing SingleRow algorithm 20 * @hide 21 */ 22public class SingleRowTest extends GridTest { 23 24 SingleRow mSingleRow; 25 26 public void testAppendPrependRemove() throws Throwable { 27 mProvider = new Provider(new int[]{80, 80, 30, 100, 40, 10}); 28 29 mSingleRow = new SingleRow(); 30 mSingleRow.setMargin(20); 31 mSingleRow.setProvider(mProvider); 32 mSingleRow.appendVisibleItems(200); 33 assertEquals(dump(mSingleRow) + " Should filled 2 items", 1, mSingleRow.mLastVisibleIndex); 34 35 mSingleRow.appendVisibleItems(201); 36 assertEquals(dump(mSingleRow) + " Should filled 3 items", 37 2, mSingleRow.mLastVisibleIndex); 38 39 mSingleRow.appendVisibleItems(251); 40 assertEquals(dump(mSingleRow) + " Should filled 4 items", 41 3, mSingleRow.mLastVisibleIndex); 42 43 mSingleRow.appendVisibleItems(Integer.MAX_VALUE); 44 assertEquals(dump(mSingleRow) + " Should filled 6 items", 45 5, mSingleRow.mLastVisibleIndex); 46 assertEquals(mProvider.getEdge(0), 0); 47 assertEquals(mProvider.getEdge(1), 100); 48 assertEquals(mProvider.getEdge(2), 200); 49 assertEquals(mProvider.getEdge(3), 250); 50 assertEquals(mProvider.getEdge(4), 370); 51 assertEquals(mProvider.getEdge(5), 430); 52 53 mSingleRow.removeInvisibleItemsAtEnd(0, 200); 54 assertEquals(dump(mSingleRow) + " Should filled 2 items", 1, mSingleRow.mLastVisibleIndex); 55 56 mSingleRow.appendVisibleItems(Integer.MAX_VALUE); 57 assertEquals(dump(mSingleRow) + " Should filled 6 items", 58 5, mSingleRow.mLastVisibleIndex); 59 60 mSingleRow.removeInvisibleItemsAtFront(1000, 80); 61 assertEquals(dump(mSingleRow) + " visible index should start from 1", 62 1, mSingleRow.mFirstVisibleIndex); 63 64 mSingleRow.prependVisibleItems(0); 65 assertEquals(dump(mSingleRow) + " visible index should start from 0", 66 0, mSingleRow.mFirstVisibleIndex); 67 } 68 69 public void testAppendPrependRemoveReversed() throws Throwable { 70 mProvider = new Provider(new int[]{80, 80, 30, 100, 40, 10}); 71 72 mSingleRow = new SingleRow(); 73 mSingleRow.setMargin(20); 74 mSingleRow.setProvider(mProvider); 75 mSingleRow.setReversedFlow(true); 76 mSingleRow.appendVisibleItems(-200); 77 assertEquals(dump(mSingleRow) + " Should filled 2 items", 1, mSingleRow.mLastVisibleIndex); 78 79 mSingleRow.appendVisibleItems(-201); 80 assertEquals(dump(mSingleRow) + " Should filled 3 items", 81 2, mSingleRow.mLastVisibleIndex); 82 83 mSingleRow.appendVisibleItems(-251); 84 assertEquals(dump(mSingleRow) + " Should filled 4 items", 85 3, mSingleRow.mLastVisibleIndex); 86 87 mSingleRow.appendVisibleItems(Integer.MIN_VALUE); 88 assertEquals(dump(mSingleRow) + " Should filled 6 items", 89 5, mSingleRow.mLastVisibleIndex); 90 assertEquals(mProvider.getEdge(0), 0); 91 assertEquals(mProvider.getEdge(1), -100); 92 assertEquals(mProvider.getEdge(2), -200); 93 assertEquals(mProvider.getEdge(3), -250); 94 assertEquals(mProvider.getEdge(4), -370); 95 assertEquals(mProvider.getEdge(5), -430); 96 97 mSingleRow.removeInvisibleItemsAtEnd(0, -200); 98 assertEquals(dump(mSingleRow) + " Should filled 2 items", 1, mSingleRow.mLastVisibleIndex); 99 100 mSingleRow.appendVisibleItems(Integer.MIN_VALUE); 101 assertEquals(dump(mSingleRow) + " Should filled 6 items", 102 5, mSingleRow.mLastVisibleIndex); 103 104 mSingleRow.removeInvisibleItemsAtFront(1000, -80); 105 assertEquals(dump(mSingleRow) + " Should filled 6 items", 106 1, mSingleRow.mFirstVisibleIndex); 107 } 108} 109