diff options
author | Sara Ting <sarating@google.com> | 2012-10-31 13:19:38 -0700 |
---|---|---|
committer | Sara Ting <sarating@google.com> | 2012-11-16 14:37:34 -0800 |
commit | 3a07a68da6460c36a5dbec5b8828baa4355dbe04 (patch) | |
tree | 5bedb69a0e53650b847a97c1ab3876d88037b945 /tests | |
parent | ddc1b123074b7243f7d39071c8c39788e44d9079 (diff) | |
download | android_packages_apps_Calendar-3a07a68da6460c36a5dbec5b8828baa4355dbe04.tar.gz android_packages_apps_Calendar-3a07a68da6460c36a5dbec5b8828baa4355dbe04.tar.bz2 android_packages_apps_Calendar-3a07a68da6460c36a5dbec5b8828baa4355dbe04.zip |
Adding alert scheduling to app, to allow unbundled app's alerts to work on more devices.
Bug:7383861
Change-Id: I5dcffb8ac586966b21e938728be0393e6776f704
Diffstat (limited to 'tests')
4 files changed, 439 insertions, 56 deletions
diff --git a/tests/src/com/android/calendar/alerts/AlarmSchedulerTest.java b/tests/src/com/android/calendar/alerts/AlarmSchedulerTest.java new file mode 100644 index 00000000..1202e102 --- /dev/null +++ b/tests/src/com/android/calendar/alerts/AlarmSchedulerTest.java @@ -0,0 +1,319 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.calendar.alerts; + +import android.app.AlarmManager; +import android.database.Cursor; +import android.database.MatrixCursor; +import android.net.Uri; +import android.provider.CalendarContract; +import android.provider.CalendarContract.Instances; +import android.provider.CalendarContract.Reminders; +import android.test.AndroidTestCase; +import android.test.IsolatedContext; +import android.test.mock.MockContentProvider; +import android.test.mock.MockContentResolver; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.format.DateUtils; +import android.text.format.Time; +import android.util.Log; + +import junit.framework.Assert; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; + +@SmallTest +public class AlarmSchedulerTest extends AndroidTestCase { + private static final int BATCH_SIZE = 50; + private MockProvider mMockProvider; + private MockAlarmManager mMockAlarmManager; + private IsolatedContext mIsolatedContext; + + /** + * A helper class to mock query results from the test data. + */ + private static class MockProvider extends MockContentProvider { + private ArrayList<EventInfo> mEvents = new ArrayList<EventInfo>(); + private ArrayList<String> mExpectedRemindersQueries = new ArrayList<String>(); + private int mCurrentReminderQueryIndex = 0; + + /** + * Contains info for a test event and its reminder. + */ + private static class EventInfo { + long mEventId; + long mBegin; + boolean mAllDay; + int mReminderMinutes; + + public EventInfo(long eventId, boolean allDay, long begin, int reminderMinutes) { + mEventId = eventId; + mAllDay = allDay; + mBegin = begin; + mReminderMinutes = reminderMinutes; + } + + } + + /** + * Adds event/reminder data for testing. These will always be returned in the mocked + * query result cursors. + */ + void addEventInfo(long eventId, boolean allDay, long begin, int reminderMinutes) { + mEvents.add(new EventInfo(eventId, allDay, begin, reminderMinutes)); + } + + private MatrixCursor getInstancesCursor() { + MatrixCursor instancesCursor = new MatrixCursor(AlarmScheduler.INSTANCES_PROJECTION); + int i = 0; + HashSet<Long> eventIds = new HashSet<Long>(); + for (EventInfo event : mEvents) { + if (!eventIds.contains(event.mEventId)) { + Object[] ca = { + event.mEventId, + event.mBegin, + event.mAllDay ? 1 : 0, + }; + instancesCursor.addRow(ca); + eventIds.add(event.mEventId); + } + } + return instancesCursor; + } + + private MatrixCursor getRemindersCursor() { + MatrixCursor remindersCursor = new MatrixCursor(AlarmScheduler.REMINDERS_PROJECTION); + int i = 0; + for (EventInfo event : mEvents) { + Object[] ca = { + event.mEventId, + event.mReminderMinutes, + 1, + }; + remindersCursor.addRow(ca); + } + return remindersCursor; + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + if (uri.toString().startsWith(Instances.CONTENT_URI.toString())) { + return getInstancesCursor(); + } else if (Reminders.CONTENT_URI.equals(uri)) { + if (mExpectedRemindersQueries.size() > 0) { + if (mExpectedRemindersQueries.size() <= mCurrentReminderQueryIndex || + !mExpectedRemindersQueries.get(mCurrentReminderQueryIndex).equals( + selection)) { + String msg = "Reminders query not as expected.\n"; + msg += " Expected:"; + msg += Arrays.deepToString(mExpectedRemindersQueries.toArray()); + msg += "\n Got in position " + mCurrentReminderQueryIndex + ": "; + msg += selection; + fail(msg); + } + mCurrentReminderQueryIndex++; + } + return getRemindersCursor(); + } else { + return super.query(uri, projection, selection, selectionArgs, sortOrder); + } + } + + /** + * Optionally set up expectation for the reminders query selection. + */ + public void addExpectedRemindersQuery(String expectedRemindersQuery) { + this.mExpectedRemindersQueries.add(expectedRemindersQuery); + } + } + + /** + * Expect an alarm for the specified time. + */ + private void expectAlarmAt(long millis) { + // AlarmScheduler adds a slight delay to the alarm so account for that here. + mMockAlarmManager.expectAlarmTime(AlarmManager.RTC_WAKEUP, + millis + AlarmScheduler.ALARM_DELAY_MS); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + + mMockProvider = new MockProvider(); + mMockAlarmManager = new MockAlarmManager(mContext); + MockContentResolver mockResolver = new MockContentResolver(); + mockResolver.addProvider(CalendarContract.AUTHORITY, mMockProvider); + mIsolatedContext = new IsolatedContext(mockResolver, mContext); + } + + public void testNoEvents() { + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, + BATCH_SIZE, System.currentTimeMillis()); + assertFalse(mMockAlarmManager.isAlarmSet()); + } + + public void testNonAllDayEvent() { + // Set up mock test data. + long currentMillis = System.currentTimeMillis(); + long startMillis = currentMillis + DateUtils.HOUR_IN_MILLIS; + int reminderMin = 10; + mMockProvider.addEventInfo(1, false, startMillis, reminderMin); + expectAlarmAt(startMillis - reminderMin * DateUtils.MINUTE_IN_MILLIS); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testAllDayEvent() { + // Set up mock allday data. + long startMillisUtc = Utils.createTimeInMillis(0, 0, 0, 1, 5, 2012, Time.TIMEZONE_UTC); + long startMillisLocal = Utils.createTimeInMillis(0, 0, 0, 1, 5, 2012, + Time.getCurrentTimezone()); + long currentMillis = startMillisLocal - DateUtils.DAY_IN_MILLIS; + int reminderMin = 15; + mMockProvider.addEventInfo(1, true, startMillisUtc, reminderMin); + expectAlarmAt(startMillisLocal - reminderMin * DateUtils.MINUTE_IN_MILLIS); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testAllDayAndNonAllDayEvents() { + // Set up mock test data. + long startMillisUtc = Utils.createTimeInMillis(0, 0, 0, 1, 5, 2012, Time.TIMEZONE_UTC); + long startMillisLocal = Utils.createTimeInMillis(0, 0, 0, 1, 5, 2012, + Time.getCurrentTimezone()); + long currentMillis = startMillisLocal - DateUtils.DAY_IN_MILLIS; + mMockProvider.addEventInfo(1, true, startMillisUtc, 15); + mMockProvider.addEventInfo(1, false, startMillisLocal, 10); + expectAlarmAt(startMillisLocal - 15 * DateUtils.MINUTE_IN_MILLIS); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testExpiredReminder() { + // Set up mock test data. + long currentMillis = System.currentTimeMillis(); + long startMillis = currentMillis + DateUtils.HOUR_IN_MILLIS; + int reminderMin = 61; + mMockProvider.addEventInfo(1, false, startMillis, reminderMin); + + // Invoke scheduleNextAlarm and verify no alarm was set. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertFalse(mMockAlarmManager.isAlarmSet()); + } + + public void testAlarmMax() { + // Set up mock test data for a reminder greater than 1 day in the future. + // This will be maxed out to 1 day out. + long currentMillis = System.currentTimeMillis(); + long startMillis = currentMillis + DateUtils.DAY_IN_MILLIS * 3; + int reminderMin = (int) DateUtils.DAY_IN_MILLIS / (1000 * 60); + mMockProvider.addEventInfo(1, false, startMillis, reminderMin); + expectAlarmAt(currentMillis + DateUtils.DAY_IN_MILLIS); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testMultipleEvents() { + // Set up multiple events where a later event time has an earlier reminder time. + long currentMillis = System.currentTimeMillis(); + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS, 0); + mMockProvider.addEventInfo(2, false, currentMillis + DateUtils.MINUTE_IN_MILLIS * 60, 45); + mMockProvider.addEventInfo(3, false, currentMillis + DateUtils.MINUTE_IN_MILLIS * 30, 10); + + // Expect event 2's reminder. + expectAlarmAt(currentMillis + DateUtils.MINUTE_IN_MILLIS * 15); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testRecurringEvents() { + long currentMillis = System.currentTimeMillis(); + + // Event in 3 days, with a 2 day reminder + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS * 3, + (int) DateUtils.DAY_IN_MILLIS * 2 / (1000 * 60) /* 2 day reminder */); + // Event for tomorrow, with a 2 day reminder + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS, + (int) DateUtils.DAY_IN_MILLIS * 2 / (1000 * 60) /* 2 day reminder */); + + // Expect the reminder for the top event because the reminder time for the bottom + // one already passed. + expectAlarmAt(currentMillis + DateUtils.DAY_IN_MILLIS); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testMultipleRemindersForEvent() { + // Set up mock test data. + long currentMillis = System.currentTimeMillis(); + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS, 10); + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS, 20); + mMockProvider.addEventInfo(1, false, currentMillis + DateUtils.DAY_IN_MILLIS, 15); + + // Expect earliest reminder. + expectAlarmAt(currentMillis + DateUtils.DAY_IN_MILLIS - DateUtils.MINUTE_IN_MILLIS * 20); + + // Invoke scheduleNextAlarm and verify alarm was set at the expected time. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, BATCH_SIZE, + currentMillis); + assertTrue(mMockAlarmManager.isAlarmSet()); + } + + public void testLargeBatch() { + // Add enough events to require several batches. + long currentMillis = System.currentTimeMillis(); + int batchSize = 5; + for (int i = 19; i > 0; i--) { + mMockProvider.addEventInfo(i, false, currentMillis + DateUtils.HOUR_IN_MILLIS * i, + 10); + } + + // Set up expectations for the batch queries. + expectAlarmAt(currentMillis + DateUtils.MINUTE_IN_MILLIS * 50); + mMockProvider.addExpectedRemindersQuery("method=1 AND event_id IN (19,18,17,16,15)"); + mMockProvider.addExpectedRemindersQuery("method=1 AND event_id IN (14,13,12,11,10)"); + mMockProvider.addExpectedRemindersQuery("method=1 AND event_id IN (9,8,7,6,5)"); + mMockProvider.addExpectedRemindersQuery("method=1 AND event_id IN (4,3,2,1)"); + + // Invoke scheduleNextAlarm and verify alarm and reminder query batches. + AlarmScheduler.scheduleNextAlarm(mIsolatedContext, mMockAlarmManager, batchSize, + currentMillis); + } +} diff --git a/tests/src/com/android/calendar/alerts/AlertServiceTest.java b/tests/src/com/android/calendar/alerts/AlertServiceTest.java index ef472866..6cb65151 100644 --- a/tests/src/com/android/calendar/alerts/AlertServiceTest.java +++ b/tests/src/com/android/calendar/alerts/AlertServiceTest.java @@ -385,28 +385,6 @@ public class AlertServiceTest extends AndroidTestCase { } } - private class MockAlarmManager implements AlarmManagerInterface { - private int expectedAlarmType = -1; - private long expectedAlarmTime = -1; - - public void expectAlarmTime(int type, long millis) { - this.expectedAlarmType = type; - this.expectedAlarmTime = millis; - } - - @Override - public void set(int actualAlarmType, long actualAlarmTime, PendingIntent operation) { - assertNotNull(operation); - if (expectedAlarmType != -1) { - assertEquals("Alarm type not expected.", expectedAlarmType, actualAlarmType); - assertEquals("Alarm time not expected. Expected:" + DateUtils.formatDateTime( - mContext, expectedAlarmTime, DateUtils.FORMAT_SHOW_TIME) + ", actual:" - + DateUtils.formatDateTime(mContext, actualAlarmTime, - DateUtils.FORMAT_SHOW_TIME), expectedAlarmTime, actualAlarmTime); - } - } - } - // TODO // Catch updates of new state, notify time, and received time // Test ringer, vibrate, @@ -422,7 +400,7 @@ public class AlertServiceTest extends AndroidTestCase { // Test no alert long currentTime = 1000000; - AlertService.generateAlerts(mContext, ntm, new MockAlarmManager(), prefs, + AlertService.generateAlerts(mContext, ntm, new MockAlarmManager(mContext), prefs, at.getAlertCursor(), currentTime, AlertService.MAX_NOTIFICATIONS); ntm.validateNotificationsAndReset(); } @@ -431,7 +409,7 @@ public class AlertServiceTest extends AndroidTestCase { @SmallTest public void testGenerateAlerts_single() { MockSharedPreferences prefs = new MockSharedPreferences(); - MockAlarmManager alarmMgr = new MockAlarmManager(); + MockAlarmManager alarmMgr = new MockAlarmManager(mContext); AlertsTable at = new AlertsTable(); NotificationTestManager ntm = new NotificationTestManager(at.mAlerts, AlertService.MAX_NOTIFICATIONS); @@ -468,7 +446,7 @@ public class AlertServiceTest extends AndroidTestCase { public void testGenerateAlerts_multiple() { int maxNotifications = 10; MockSharedPreferences prefs = new MockSharedPreferences(); - MockAlarmManager alarmMgr = new MockAlarmManager(); + MockAlarmManager alarmMgr = new MockAlarmManager(mContext); AlertsTable at = new AlertsTable(); NotificationTestManager ntm = new NotificationTestManager(at.mAlerts, maxNotifications); @@ -541,7 +519,7 @@ public class AlertServiceTest extends AndroidTestCase { @SmallTest public void testGenerateAlerts_maxAlerts() { MockSharedPreferences prefs = new MockSharedPreferences(); - MockAlarmManager alarmMgr = new MockAlarmManager(); + MockAlarmManager alarmMgr = new MockAlarmManager(mContext); AlertsTable at = new AlertsTable(); // Current time - 5:00 @@ -625,14 +603,14 @@ public class AlertServiceTest extends AndroidTestCase { // If this does not result in a failure (MockSharedPreferences fails for duplicate // queries), then test passes. - AlertService.generateAlerts(mContext, ntm, new MockAlarmManager(), prefs, + AlertService.generateAlerts(mContext, ntm, new MockAlarmManager(mContext), prefs, at.getAlertCursor(), currentTime, AlertService.MAX_NOTIFICATIONS); } public void testGenerateAlerts_refreshTime() { AlertsTable at = new AlertsTable(); MockSharedPreferences prefs = new MockSharedPreferences(); - MockAlarmManager alarmMgr = new MockAlarmManager(); + MockAlarmManager alarmMgr = new MockAlarmManager(mContext); NotificationTestManager ntm = new NotificationTestManager(at.mAlerts, AlertService.MAX_NOTIFICATIONS); @@ -648,28 +626,30 @@ public class AlertServiceTest extends AndroidTestCase { yesterday.set(System.currentTimeMillis() - DateUtils.DAY_IN_MILLIS); Time tomorrow = new Time(); tomorrow.set(System.currentTimeMillis() + DateUtils.DAY_IN_MILLIS); - long allDayStart = createTimeInMillis(0, 0, 0, day, month, year, Time.TIMEZONE_UTC); + long allDayStart = Utils.createTimeInMillis(0, 0, 0, day, month, year, Time.TIMEZONE_UTC); /* today 10am - 10:30am */ int id4 = at.addAlertRow(4, SCHEDULED, ACCEPTED, 0, - createTimeInMillis(0, 0, 10, day, month, year, Time.getCurrentTimezone()), - createTimeInMillis(0, 30, 10, day, month, year, Time.getCurrentTimezone()), 0); + Utils.createTimeInMillis(0, 0, 10, day, month, year, Time.getCurrentTimezone()), + Utils.createTimeInMillis(0, 30, 10, day, month, year, Time.getCurrentTimezone()), + 0); /* today 6am - 6am (0 duration event) */ int id3 = at.addAlertRow(3, SCHEDULED, ACCEPTED, 0, - createTimeInMillis(0, 0, 6, day, month, year, Time.getCurrentTimezone()), - createTimeInMillis(0, 0, 6, day, month, year, Time.getCurrentTimezone()), 0); + Utils.createTimeInMillis(0, 0, 6, day, month, year, Time.getCurrentTimezone()), + Utils.createTimeInMillis(0, 0, 6, day, month, year, Time.getCurrentTimezone()), 0); /* today allDay */ int id2 = at.addAlertRow(2, SCHEDULED, ACCEPTED, 1, allDayStart, allDayStart + DateUtils.HOUR_IN_MILLIS * 24, 0); /* yesterday 11pm - today 7am (multiday event) */ int id1 = at.addAlertRow(1, SCHEDULED, ACCEPTED, 0, - createTimeInMillis(0, 0, 23, yesterday.monthDay, yesterday.month, yesterday.year, - Time.getCurrentTimezone()), - createTimeInMillis(0, 0, 7, day, month, year, Time.getCurrentTimezone()), 0); + Utils.createTimeInMillis(0, 0, 23, yesterday.monthDay, yesterday.month, + yesterday.year, Time.getCurrentTimezone()), + Utils.createTimeInMillis(0, 0, 7, day, month, year, Time.getCurrentTimezone()), 0); // Test at midnight - next refresh should be 15 min later (15 min into the all // day event). - long currentTime = createTimeInMillis(0, 0, 0, day, month, year, Time.getCurrentTimezone()); + long currentTime = Utils.createTimeInMillis(0, 0, 0, day, month, year, + Time.getCurrentTimezone()); alarmMgr.expectAlarmTime(AlarmManager.RTC, currentTime + 15 * DateUtils.MINUTE_IN_MILLIS); ntm.expectTestNotification(4, id1, PRIORITY_HIGH); ntm.expectTestNotification(3, id2, PRIORITY_HIGH); @@ -680,7 +660,8 @@ public class AlertServiceTest extends AndroidTestCase { ntm.validateNotificationsAndReset(); // Test at 12:30am - next refresh should be 30 min later (1/4 into event 'id1'). - currentTime = createTimeInMillis(0, 30, 0, day, month, year, Time.getCurrentTimezone()); + currentTime = Utils.createTimeInMillis(0, 30, 0, day, month, year, + Time.getCurrentTimezone()); alarmMgr.expectAlarmTime(AlarmManager.RTC, currentTime + 30 * DateUtils.MINUTE_IN_MILLIS); ntm.expectTestNotification(3, id1, PRIORITY_HIGH); ntm.expectTestNotification(2, id3, PRIORITY_HIGH); @@ -691,7 +672,8 @@ public class AlertServiceTest extends AndroidTestCase { ntm.validateNotificationsAndReset(); // Test at 5:55am - next refresh should be 20 min later (15 min after 'id3'). - currentTime = createTimeInMillis(0, 55, 5, day, month, year, Time.getCurrentTimezone()); + currentTime = Utils.createTimeInMillis(0, 55, 5, day, month, year, + Time.getCurrentTimezone()); alarmMgr.expectAlarmTime(AlarmManager.RTC, currentTime + 20 * DateUtils.MINUTE_IN_MILLIS); ntm.expectTestNotification(2, id3, PRIORITY_HIGH); ntm.expectTestNotification(1, id4, PRIORITY_HIGH); @@ -702,7 +684,8 @@ public class AlertServiceTest extends AndroidTestCase { ntm.validateNotificationsAndReset(); // Test at 10:14am - next refresh should be 1 min later (15 min into event 'id4'). - currentTime = createTimeInMillis(0, 14, 10, day, month, year, Time.getCurrentTimezone()); + currentTime = Utils.createTimeInMillis(0, 14, 10, day, month, year, + Time.getCurrentTimezone()); alarmMgr.expectAlarmTime(AlarmManager.RTC, currentTime + 1 * DateUtils.MINUTE_IN_MILLIS); ntm.expectTestNotification(1, id4, PRIORITY_HIGH); ntm.expectTestNotification(2, id2, PRIORITY_DEFAULT); @@ -713,9 +696,10 @@ public class AlertServiceTest extends AndroidTestCase { ntm.validateNotificationsAndReset(); // Test at 10:15am - next refresh should be tomorrow midnight (end of all day event 'id2'). - currentTime = createTimeInMillis(0, 15, 10, day, month, year, Time.getCurrentTimezone()); - alarmMgr.expectAlarmTime(AlarmManager.RTC, createTimeInMillis(0, 0, 23, tomorrow.monthDay, - tomorrow.month, tomorrow.year, Time.getCurrentTimezone())); + currentTime = Utils.createTimeInMillis(0, 15, 10, day, month, year, + Time.getCurrentTimezone()); + alarmMgr.expectAlarmTime(AlarmManager.RTC, Utils.createTimeInMillis(0, 0, 23, + tomorrow.monthDay, tomorrow.month, tomorrow.year, Time.getCurrentTimezone())); ntm.expectTestNotification(1, id2, PRIORITY_DEFAULT); ntm.expectTestNotification(AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID, new int[] {id4, id3, id1}, PRIORITY_MIN); @@ -730,18 +714,10 @@ public class AlertServiceTest extends AndroidTestCase { } private static long createTimeInMillis(int hour, int minute) { - return createTimeInMillis(0 /* second */, minute, hour, 1 /* day */, 1 /* month */, + return Utils.createTimeInMillis(0 /* second */, minute, hour, 1 /* day */, 1 /* month */, 2012 /* year */, Time.getCurrentTimezone()); } - private static long createTimeInMillis(int second, int minute, int hour, int monthDay, - int month, int year, String timezone) { - Time t = new Time(timezone); - t.set(second, minute, hour, monthDay, month, year); - t.normalize(false); - return t.toMillis(false); - } - @SmallTest public void testProcessQuery_skipDeclinedDismissed() { int declinedEventId = 1; @@ -864,8 +840,8 @@ public class AlertServiceTest extends AndroidTestCase { @SmallTest public void testProcessQuery_recurringAllDayEvent() { int eventId = 1; - long day1 = createTimeInMillis(0, 0, 0, 1, 5, 2012, Time.TIMEZONE_UTC); - long day2 = createTimeInMillis(0, 0, 0, 2, 5, 2012, Time.TIMEZONE_UTC); + long day1 = Utils.createTimeInMillis(0, 0, 0, 1, 5, 2012, Time.TIMEZONE_UTC); + long day2 = Utils.createTimeInMillis(0, 0, 0, 2, 5, 2012, Time.TIMEZONE_UTC); ArrayList<NotificationInfo> highPriority = new ArrayList<NotificationInfo>(); ArrayList<NotificationInfo> mediumPriority = new ArrayList<NotificationInfo>(); @@ -890,7 +866,7 @@ public class AlertServiceTest extends AndroidTestCase { // Increment time just past the earlier event (to 12:10am). The earlier one should // be chosen. highPriority.clear(); - currentTime = createTimeInMillis(0, 10, 0, 1, 5, 2012, Time.getCurrentTimezone()); + currentTime = Utils.createTimeInMillis(0, 10, 0, 1, 5, 2012, Time.getCurrentTimezone()); AlertService.processQuery(at.getAlertCursor(), mContext, currentTime, highPriority, mediumPriority, lowPriority); assertEquals(0, lowPriority.size()); @@ -901,7 +877,7 @@ public class AlertServiceTest extends AndroidTestCase { // Increment time to 15 min past the earlier event: the later one should be chosen. highPriority.clear(); - currentTime = createTimeInMillis(0, 15, 0, 1, 5, 2012, Time.getCurrentTimezone()); + currentTime = Utils.createTimeInMillis(0, 15, 0, 1, 5, 2012, Time.getCurrentTimezone()); AlertService.processQuery(at.getAlertCursor(), mContext, currentTime, highPriority, mediumPriority, lowPriority); assertEquals(0, lowPriority.size()); diff --git a/tests/src/com/android/calendar/alerts/MockAlarmManager.java b/tests/src/com/android/calendar/alerts/MockAlarmManager.java new file mode 100644 index 00000000..067a7865 --- /dev/null +++ b/tests/src/com/android/calendar/alerts/MockAlarmManager.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.calendar.alerts; + +import android.app.PendingIntent; +import android.content.Context; +import android.text.format.DateUtils; + +import junit.framework.Assert; + +public class MockAlarmManager implements AlarmManagerInterface { + private Context context; + private int expectedAlarmType = -1; + private long expectedAlarmTime = -1; + private boolean alarmSet = false; + + MockAlarmManager(Context context) { + this.context = context; + } + + public void expectAlarmTime(int type, long millis) { + this.expectedAlarmType = type; + this.expectedAlarmTime = millis; + } + + @Override + public void set(int actualAlarmType, long actualAlarmTime, PendingIntent operation) { + Assert.assertNotNull(operation); + alarmSet = true; + if (expectedAlarmType != -1) { + Assert.assertEquals("Alarm type not expected.", expectedAlarmType, actualAlarmType); + Assert.assertEquals("Alarm time not expected. Expected:" + DateUtils.formatDateTime( + context, expectedAlarmTime, DateUtils.FORMAT_SHOW_TIME) + ", actual:" + + DateUtils.formatDateTime(context, actualAlarmTime, + DateUtils.FORMAT_SHOW_TIME), expectedAlarmTime, actualAlarmTime); + } + } + + /** + * Returns whether set() was invoked. + */ + public boolean isAlarmSet() { + return alarmSet; + } +} diff --git a/tests/src/com/android/calendar/alerts/Utils.java b/tests/src/com/android/calendar/alerts/Utils.java new file mode 100644 index 00000000..b887c4b9 --- /dev/null +++ b/tests/src/com/android/calendar/alerts/Utils.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.calendar.alerts; + +import android.text.format.Time; + +class Utils { + public static long createTimeInMillis(int second, int minute, int hour, int monthDay, + int month, int year, String timezone) { + Time t = new Time(timezone); + t.set(second, minute, hour, monthDay, month, year); + t.normalize(false); + return t.toMillis(false); + } +} |