1 /* 2 * Copyright (C) 2008 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 17 package android.util.cts; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 24 import android.support.test.filters.SmallTest; 25 import android.support.test.runner.AndroidJUnit4; 26 import android.util.MonthDisplayHelper; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.util.Calendar; 32 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class MonthDisplayHelperTest { 36 @Test 37 public void testConstructor() { 38 new MonthDisplayHelper(2008, Calendar.DECEMBER, Calendar.MONDAY); 39 new MonthDisplayHelper(2008, Calendar.DECEMBER); 40 } 41 42 @Test(expected=IllegalArgumentException.class) 43 public void testConstructorInvalidDay1() { 44 new MonthDisplayHelper(2008, Calendar.DECEMBER, Calendar.SUNDAY - 1); 45 } 46 47 @Test(expected=IllegalArgumentException.class) 48 public void testConstructorInvalidDay2() { 49 new MonthDisplayHelper(2008, Calendar.DECEMBER, Calendar.SATURDAY + 1); 50 } 51 52 @Test(expected=IllegalArgumentException.class) 53 public void testConstructorInvalidYearAndDay() { 54 new MonthDisplayHelper(-1, Calendar.DECEMBER, Calendar.SATURDAY + 1); 55 } 56 57 @Test(expected=IllegalArgumentException.class) 58 public void testConstructorInvalidYearAndMonthAndDay() { 59 new MonthDisplayHelper(-1, Calendar.DECEMBER + 1, Calendar.SATURDAY + 1); 60 } 61 62 @Test 63 public void testNumberOfDaysInCurrentMonth() { 64 assertEquals(30, 65 new MonthDisplayHelper(2007, Calendar.SEPTEMBER).getNumberOfDaysInMonth()); 66 assertEquals(28, 67 new MonthDisplayHelper(2007, Calendar.FEBRUARY).getNumberOfDaysInMonth()); 68 assertEquals(29, 69 new MonthDisplayHelper(2008, Calendar.FEBRUARY).getNumberOfDaysInMonth()); 70 } 71 72 @Test 73 public void testNextMonth() { 74 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.AUGUST, Calendar.SUNDAY); 75 76 assertArrayEquals(new int[] { 29, 30, 31, 1, 2, 3, 4 }, helper.getDigitsForRow(0)); 77 78 helper.nextMonth(); 79 80 assertEquals(Calendar.SEPTEMBER, helper.getMonth()); 81 assertArrayEquals(new int[] { 26, 27, 28, 29, 30, 31, 1 }, helper.getDigitsForRow(0)); 82 } 83 84 @Test 85 public void testGetRowOf() { 86 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.AUGUST, Calendar.SUNDAY); 87 88 assertEquals(0, helper.getRowOf(2)); 89 assertEquals(0, helper.getRowOf(4)); 90 assertEquals(2, helper.getRowOf(12)); 91 assertEquals(2, helper.getRowOf(18)); 92 assertEquals(3, helper.getRowOf(19)); 93 } 94 95 @Test 96 public void testHelperProperties() { 97 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.AUGUST, Calendar.SUNDAY); 98 99 assertEquals(1, helper.getWeekStartDay()); 100 assertEquals(3, helper.getOffset()); 101 helper = new MonthDisplayHelper(2007, Calendar.AUGUST); 102 assertEquals(1, helper.getWeekStartDay()); 103 assertEquals(3, helper.getOffset()); 104 } 105 106 @Test 107 public void testMonthRows() { 108 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.SEPTEMBER); 109 110 assertArrayEquals(new int[] { 26, 27, 28, 29, 30, 31, 1 }, helper 111 .getDigitsForRow(0)); 112 assertArrayEquals(new int[] { 2, 3, 4, 5, 6, 7, 8 }, helper 113 .getDigitsForRow(1)); 114 assertArrayEquals(new int[] { 30, 1, 2, 3, 4, 5, 6 }, helper 115 .getDigitsForRow(5)); 116 117 helper = new MonthDisplayHelper(2007, Calendar.SEPTEMBER, Calendar.MONDAY); 118 119 assertArrayEquals(new int[] { 27, 28, 29, 30, 31, 1, 2 }, helper 120 .getDigitsForRow(0)); 121 assertArrayEquals(new int[] { 3, 4, 5, 6, 7, 8, 9 }, helper 122 .getDigitsForRow(1)); 123 assertArrayEquals(new int[] { 24, 25, 26, 27, 28, 29, 30 }, helper 124 .getDigitsForRow(4)); 125 assertArrayEquals(new int[] { 1, 2, 3, 4, 5, 6, 7 }, helper 126 .getDigitsForRow(5)); 127 } 128 129 @Test 130 public void testFirstDayOfMonth() { 131 assertEquals("august 2007", Calendar.WEDNESDAY, new MonthDisplayHelper( 132 2007, Calendar.AUGUST).getFirstDayOfMonth()); 133 134 assertEquals("september, 2007", Calendar.SATURDAY, 135 new MonthDisplayHelper(2007, Calendar.SEPTEMBER) 136 .getFirstDayOfMonth()); 137 } 138 139 @Test 140 public void testGetColumnOf() { 141 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.AUGUST, Calendar.SUNDAY); 142 143 assertEquals(3, helper.getColumnOf(1)); 144 assertEquals(4, helper.getColumnOf(9)); 145 assertEquals(5, helper.getColumnOf(17)); 146 assertEquals(6, helper.getColumnOf(25)); 147 assertEquals(0, helper.getColumnOf(26)); 148 } 149 150 @Test 151 public void testGetDayAt() { 152 MonthDisplayHelper helper = new MonthDisplayHelper(2007, Calendar.AUGUST, Calendar.SUNDAY); 153 154 assertEquals(30, helper.getDayAt(0, 1)); 155 } 156 157 @Test 158 public void testPrevMonth() { 159 MonthDisplayHelper mHelper = new MonthDisplayHelper(2007, Calendar.SEPTEMBER, 160 Calendar.SUNDAY); 161 162 assertArrayEquals(new int[] { 26, 27, 28, 29, 30, 31, 1 }, mHelper.getDigitsForRow(0)); 163 164 mHelper.previousMonth(); 165 166 assertEquals(Calendar.AUGUST, mHelper.getMonth()); 167 assertArrayEquals(new int[] { 29, 30, 31, 1, 2, 3, 4 }, mHelper.getDigitsForRow(0)); 168 169 mHelper = new MonthDisplayHelper(2007, Calendar.JANUARY); 170 171 mHelper.previousMonth(); 172 173 assertEquals(2006, mHelper.getYear()); 174 assertEquals(Calendar.DECEMBER, mHelper.getMonth()); 175 } 176 177 @Test 178 public void testIsWithinCurrentMonth() { 179 MonthDisplayHelper mHelper = new MonthDisplayHelper(2007, Calendar.SEPTEMBER, 180 Calendar.SUNDAY); 181 182 // out of bounds 183 assertFalse(mHelper.isWithinCurrentMonth(-1, 3)); 184 assertFalse(mHelper.isWithinCurrentMonth(6, 3)); 185 assertFalse(mHelper.isWithinCurrentMonth(2, -1)); 186 assertFalse(mHelper.isWithinCurrentMonth(2, 7)); 187 188 // last day of previous month 189 assertFalse(mHelper.isWithinCurrentMonth(0, 5)); 190 191 // first day of next month 192 assertFalse(mHelper.isWithinCurrentMonth(5, 1)); 193 194 // first day in month 195 assertTrue(mHelper.isWithinCurrentMonth(0, 6)); 196 197 // last day in month 198 assertTrue(mHelper.isWithinCurrentMonth(5, 0)); 199 } 200 } 201 202