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