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 18import android.test.AndroidTestCase; 19import android.test.suitebuilder.annotation.SmallTest; 20 21/** 22 * Tests for {@link PagingIndicator}. 23 * @hide 24 */ 25@SmallTest 26public class PagingIndicatorTest extends AndroidTestCase { 27 private PagingIndicator mIndicator; 28 29 @Override 30 protected void setUp() throws Exception { 31 super.setUp(); 32 mIndicator = new PagingIndicator(getContext()); 33 } 34 35 public void testDotPosition() { 36 mIndicator.setPageCount(3); 37 assertDotPosition(); 38 mIndicator.setPageCount(6); 39 assertDotPosition(); 40 mIndicator.setPageCount(9); 41 assertDotPosition(); 42 } 43 44 private void assertDotPosition() { 45 assertSymmetry(); 46 assertDistance(); 47 } 48 49 private void assertSymmetry() { 50 int pageCount = mIndicator.getPageCount(); 51 int mid = pageCount / 2; 52 int[] selectedX = mIndicator.getDotSelectedX(); 53 int sum = selectedX[0] + selectedX[pageCount - 1]; 54 for (int i = 1; i <= mid; ++i) { 55 assertEquals("Selected dots are not symmetric", sum, 56 selectedX[i] + selectedX[pageCount - i - 1]); 57 } 58 int[] leftX = mIndicator.getDotSelectedLeftX(); 59 int[] rightX = mIndicator.getDotSelectedRightX(); 60 sum = leftX[0] + rightX[pageCount - 1]; 61 for (int i = 1; i < pageCount - 1; ++i) { 62 assertEquals("Deselected dots are not symmetric", sum, 63 leftX[i] + rightX[pageCount - i - 1]); 64 } 65 } 66 67 private void assertDistance() { 68 int pageCount = mIndicator.getPageCount(); 69 int[] selectedX = mIndicator.getDotSelectedX(); 70 int[] leftX = mIndicator.getDotSelectedLeftX(); 71 int[] rightX = mIndicator.getDotSelectedRightX(); 72 int distance = selectedX[1] - selectedX[0]; 73 for (int i = 2; i < pageCount; ++i) { 74 assertEquals("Gaps between selected dots are not even", distance, 75 selectedX[i] - selectedX[i - 1]); 76 } 77 distance = leftX[1] - leftX[0]; 78 for (int i = 2; i < pageCount - 1; ++i) { 79 assertEquals("Gaps between left dots are not even", distance, 80 leftX[i] - leftX[i - 1]); 81 } 82 distance = rightX[2] - rightX[1]; 83 for (int i = 3; i < pageCount; ++i) { 84 assertEquals("Gaps between right dots are not even", distance, 85 rightX[i] - rightX[i - 1]); 86 } 87 } 88} 89