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