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