1 /* 2 * Copyright (C) 2010 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 com.android.calendar; 18 19 20 import android.database.MatrixCursor; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.test.suitebuilder.annotation.Smoke; 23 import android.text.format.Time; 24 import android.util.Log; 25 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 29 import junit.framework.TestCase; 30 31 /** 32 * Test class for verifying helper functions in Calendar's Utils 33 * 34 * You can run these tests with the following command: 35 * "adb shell am instrument -w -e class com.android.calendar.UtilsTests 36 * com.android.calendar.tests/android.test.InstrumentationTestRunner" 37 */ 38 public class UtilsTests extends TestCase { 39 HashMap<String, Boolean> mIsDuplicateName; 40 HashMap<String, Boolean> mIsDuplicateNameExpected; 41 MatrixCursor mDuplicateNameCursor; 42 43 private static final int NAME_COLUMN = 0; 44 private static final String[] DUPLICATE_NAME_COLUMNS = new String[] { "name" }; 45 private static final String[][] DUPLICATE_NAMES = new String[][] { 46 {"Pepper Pots"}, 47 {"Green Goblin"}, 48 {"Pepper Pots"}, 49 {"Peter Parker"}, 50 {"Silver Surfer"}, 51 {"John Jameson"}, 52 {"John Jameson"}, 53 {"Pepper Pots"} 54 }; 55 // First date is Thursday, Jan 1st, 1970. 56 private static final int[] JULIAN_DAYS = {2440588, 2440589, 2440590, 2440591, 2440592, 2440593, 57 2440594, 2440595, 2440596, 2440597, 2440598, 2440599, 2440600, 2440601 58 }; 59 private static final int[] EXPECTED_WEEK_MONDAY_START = { 60 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2 }; 61 private static final int[] EXPECTED_WEEK_SUNDAY_START = { 62 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2 }; 63 private static final int[] EXPECTED_WEEK_SATURDAY_START = { 64 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2 }; 65 private static final int[] WEEKS_FOR_JULIAN_MONDAYS = {1, 2}; 66 private static final int[] EXPECTED_JULIAN_MONDAYS = {2440592, 2440599}; 67 68 @Override 69 public void setUp() { 70 mIsDuplicateName = new HashMap<String, Boolean> (); 71 mDuplicateNameCursor = new MatrixCursor(DUPLICATE_NAME_COLUMNS); 72 for (int i = 0; i < DUPLICATE_NAMES.length; i++) { 73 mDuplicateNameCursor.addRow(DUPLICATE_NAMES[i]); 74 } 75 76 mIsDuplicateNameExpected = new HashMap<String, Boolean> (); 77 mIsDuplicateNameExpected.put("Pepper Pots", true); 78 mIsDuplicateNameExpected.put("Green Goblin", false); 79 mIsDuplicateNameExpected.put("Peter Parker", false); 80 mIsDuplicateNameExpected.put("Silver Surfer", false); 81 mIsDuplicateNameExpected.put("John Jameson", true); 82 } 83 84 @Override 85 public void tearDown() { 86 mDuplicateNameCursor.close(); 87 } 88 89 @Smoke 90 @SmallTest 91 public void testCheckForDuplicateNames() { 92 Utils.checkForDuplicateNames(mIsDuplicateName, mDuplicateNameCursor, NAME_COLUMN); 93 assertEquals(mIsDuplicateName, mIsDuplicateNameExpected); 94 } 95 96 @Smoke 97 @SmallTest 98 public void testGetWeeksSinceEpochFromJulianDay() { 99 for (int i = 0; i < JULIAN_DAYS.length; i++) { 100 assertEquals(EXPECTED_WEEK_MONDAY_START[i], 101 Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.MONDAY)); 102 assertEquals(EXPECTED_WEEK_SUNDAY_START[i], 103 Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.SUNDAY)); 104 assertEquals(EXPECTED_WEEK_SATURDAY_START[i], 105 Utils.getWeeksSinceEpochFromJulianDay(JULIAN_DAYS[i], Time.SATURDAY)); 106 } 107 } 108 109 @Smoke 110 @SmallTest 111 public void testGetJulianMondayFromWeeksSinceEpoch() { 112 for (int i = 0; i < WEEKS_FOR_JULIAN_MONDAYS.length; i++) { 113 assertEquals(EXPECTED_JULIAN_MONDAYS[i], 114 Utils.getJulianMondayFromWeeksSinceEpoch(WEEKS_FOR_JULIAN_MONDAYS[i])); 115 } 116 } 117 118 @Smoke 119 @SmallTest 120 public void testEquals() { 121 assertTrue(Utils.equals(null, null)); 122 assertFalse(Utils.equals("", null)); 123 assertFalse(Utils.equals(null, "")); 124 assertTrue(Utils.equals("","")); 125 126 Integer int1 = new Integer(1); 127 Integer int2 = new Integer(1); 128 assertTrue(Utils.equals(int1, int2)); 129 } 130 131 132 // Helper function to create test events for BusyBits testing 133 Event buildTestEvent(int startTime, int endTime, boolean allDay, int startDay, int endDay) { 134 Event e = new Event(); 135 e.startTime = startTime; 136 e.endTime = endTime; 137 e.allDay = allDay; 138 e.startDay = startDay; 139 e.endDay = endDay; 140 e.startMillis = e.startDay * 1000L * 3600L * 24L + e.startTime * 60L * 1000L; 141 e.endMillis = e.endDay * 1000L * 3600L * 24L + e.endTime * 60L * 1000L; 142 return e; 143 } 144 145 @Smoke 146 @SmallTest 147 public void testCreateBusyBitSegments() { 148 149 /* ArrayList<Event> events = new ArrayList<Event>(); 150 151 // Test cases that should return null 152 // Empty events list 153 assertEquals(null, Utils.createBusyBitSegments(10, 30, 100, 200, 0, events)); 154 // No events list 155 assertEquals(null, Utils.createBusyBitSegments(10, 30, 100, 200, 0, null)); 156 157 events.add(buildTestEvent(100, 130, false, 1, 1)); 158 events.add(buildTestEvent(1000, 1030, false, 1, 1)); 159 // Illegal pixel positions 160 assertEquals(null, Utils.createBusyBitSegments(30, 10, 100, 200, 1, events)); 161 // Illegal start and end times 162 assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, 100, 1, events)); 163 assertEquals(null, Utils.createBusyBitSegments(10, 30, -10, 100, 1, events)); 164 assertEquals(null, Utils.createBusyBitSegments(10, 30, 24 * 60 + 100, 24 * 60 + 200, 1, 165 events)); 166 assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, 24 * 60 + 100, 1, events)); 167 assertEquals(null, Utils.createBusyBitSegments(10, 30, 200, -100, 1, events)); 168 // No Events in time frame 169 assertEquals(null, Utils.createBusyBitSegments(10, 30, 500, 900, 1, events)); 170 171 // Test event that spans over the day 172 events.clear(); 173 events.add(buildTestEvent(100, 300, false, 1, 5)); 174 ArrayList<BusyBitsSegment> segments = new ArrayList<BusyBitsSegment>(); 175 assertEquals(null, Utils.createBusyBitSegments(0, 250, 200, 1200, 3, events)); 176 177 // test zero times events, events that are partially in the time span 178 // and all day events 179 events.clear(); 180 events.add(buildTestEvent(100, 300, false, 1, 1)); 181 events.add(buildTestEvent(1100, 1300, false, 1, 1)); 182 events.add(buildTestEvent(500, 600, true, 1, 1)); 183 events.add(buildTestEvent(700, 700, false, 1, 1)); 184 segments.clear(); 185 segments.add(new BusyBitsSegment(0, 10, false)); 186 segments.add(new BusyBitsSegment(90, 100, false)); 187 assertEquals(segments, Utils.createBusyBitSegments(0, 100, 200, 1200, 1, events)); 188 189 // Test event that spans over 2 days but start and end time do not 190 // overlap fully with tested time span 191 192 events.clear(); 193 events.add(buildTestEvent(23 * 60, 120, false, 1, 2)); 194 segments.clear(); 195 segments.add(new BusyBitsSegment(0, 120, false)); 196 assertEquals(segments, Utils.createBusyBitSegments(0, 240, 60, 180, 2, events)); 197 198 // Test overlapped events (two draw sizes) 199 events.clear(); 200 events.add(buildTestEvent(10, 200, false, 1, 1)); 201 events.add(buildTestEvent(150, 250, false, 1, 1)); 202 events.add(buildTestEvent(150, 250, false, 1, 1)); 203 events.add(buildTestEvent(200, 400, false, 1, 1)); 204 events.add(buildTestEvent(500, 700, false, 1, 1)); 205 events.add(buildTestEvent(550, 600, false, 1, 1)); 206 events.add(buildTestEvent(550, 580, false, 1, 1)); 207 events.add(buildTestEvent(560, 570, false, 1, 1)); 208 events.add(buildTestEvent(600, 700, false, 1, 1)); 209 events.add(buildTestEvent(620, 700, false, 1, 1)); 210 events.add(buildTestEvent(650, 700, false, 1, 1)); 211 events.add(buildTestEvent(800, 900, false, 1, 1)); 212 events.add(buildTestEvent(800, 900, false, 1, 1)); 213 events.add(buildTestEvent(800, 850, false, 1, 1)); 214 events.add(buildTestEvent(1000, 1200, false, 1, 1)); 215 events.add(buildTestEvent(1000, 1200, false, 1, 1)); 216 segments.clear(); 217 segments.add(new BusyBitsSegment(100, 149, false)); 218 segments.add(new BusyBitsSegment(150, 250, true)); 219 segments.add(new BusyBitsSegment(251, 400, false)); 220 segments.add(new BusyBitsSegment(500, 549, false)); 221 segments.add(new BusyBitsSegment(550, 700, true)); 222 segments.add(new BusyBitsSegment(800, 900, true)); 223 segments.add(new BusyBitsSegment(1000, 1100, true)); 224 assertEquals(segments, Utils.createBusyBitSegments(100, 1100, 100, 1100, 1, events)); 225 segments.clear(); 226 segments.add(new BusyBitsSegment(100, 111, false)); 227 segments.add(new BusyBitsSegment(112, 137, true)); 228 segments.add(new BusyBitsSegment(138, 175, false)); 229 segments.add(new BusyBitsSegment(200, 211, false)); 230 segments.add(new BusyBitsSegment(212, 250, true)); 231 segments.add(new BusyBitsSegment(275, 300, true)); 232 segments.add(new BusyBitsSegment(325, 350, true)); 233 assertEquals(segments, Utils.createBusyBitSegments(100, 350, 100, 1100, 1, events)); 234 */ 235 } 236 } 237