summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Chan <chanm@google.com>2012-05-15 22:44:25 -0700
committerMichael Chan <chanm@google.com>2012-05-17 01:05:19 -0700
commit25b09db869f377b131052447f62ad02e2505cd87 (patch)
tree2e1a675dd4e852e71f331566d21daa38d4cfc500
parente8701d3f3126d378bcee634418037ef2bcf8a679 (diff)
downloadandroid_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.tar.gz
android_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.tar.bz2
android_packages_apps_Calendar-25b09db869f377b131052447f62ad02e2505cd87.zip
Add tests for notification
Bug: 6282451 Change-Id: I53816be140f9f331cf84cd4ebb4c5d0d535df645
-rw-r--r--src/com/android/calendar/alerts/AlertReceiver.java43
-rw-r--r--src/com/android/calendar/alerts/AlertService.java104
-rw-r--r--src/com/android/calendar/alerts/NotificationMgr.java11
-rw-r--r--tests/AndroidManifest.xml6
-rw-r--r--tests/src/com/android/calendar/CalendarLaunchPerformance.java51
-rw-r--r--tests/src/com/android/calendar/alerts/AlertServiceTest.java345
6 files changed, 470 insertions, 90 deletions
diff --git a/src/com/android/calendar/alerts/AlertReceiver.java b/src/com/android/calendar/alerts/AlertReceiver.java
index 59cea57f..97e48c2e 100644
--- a/src/com/android/calendar/alerts/AlertReceiver.java
+++ b/src/com/android/calendar/alerts/AlertReceiver.java
@@ -16,14 +16,10 @@
package com.android.calendar.alerts;
-import com.android.calendar.R;
-import com.android.calendar.Utils;
-
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
-import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
@@ -42,9 +38,12 @@ import android.text.style.RelativeSizeSpan;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
+import com.android.calendar.R;
+import com.android.calendar.Utils;
+import com.android.calendar.alerts.AlertService.NotificationWrapper;
+
import java.util.ArrayList;
import java.util.List;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -216,11 +215,14 @@ public class AlertReceiver extends BroadcastReceiver {
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
- public static Notification makeBasicNotification(Context context, String title,
+ public static NotificationWrapper makeBasicNotification(Context context, String title,
String summaryText, long startMillis, long endMillis, long eventId,
int notificationId, boolean doPopup) {
- return makeBasicNotificationBuilder(context, title, summaryText, startMillis, endMillis,
- eventId, notificationId, doPopup, false, false).build();
+
+ Notification n = makeBasicNotificationBuilder(context, title, summaryText, startMillis,
+ endMillis, eventId, notificationId, doPopup, false, false).build();
+
+ return new NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup);
}
private static Notification.Builder makeBasicNotificationBuilder(Context context, String title,
@@ -282,7 +284,7 @@ public class AlertReceiver extends BroadcastReceiver {
* Creates an expanding notification. The initial expanded state is decided by
* the notification manager based on the priority.
*/
- public static Notification makeExpandingNotification(Context context, String title,
+ public static NotificationWrapper makeExpandingNotification(Context context, String title,
String summaryText, String description, long startMillis, long endMillis, long eventId,
int notificationId, boolean doPopup, boolean highPriority) {
Notification.Builder basicBuilder = makeBasicNotificationBuilder(context, title,
@@ -309,13 +311,15 @@ public class AlertReceiver extends BroadcastReceiver {
text = stringBuilder;
}
expandedBuilder.bigText(text);
- return expandedBuilder.build();
+
+ return new NotificationWrapper(expandedBuilder.build(), notificationId, eventId,
+ startMillis, endMillis, doPopup);
}
/**
* Creates an expanding digest notification for expired events.
*/
- public static Notification makeDigestNotification(Context context,
+ public static NotificationWrapper makeDigestNotification(Context context,
ArrayList<AlertService.NotificationInfo> notificationInfos, String digestTitle,
boolean expandable) {
if (notificationInfos == null || notificationInfos.size() < 1) {
@@ -360,6 +364,8 @@ public class AlertReceiver extends BroadcastReceiver {
// Set to min priority to encourage the notification manager to collapse it.
notificationBuilder.setPriority(Notification.PRIORITY_MIN);
+ Notification n;
+
if (expandable) {
// Multiple reminders. Combine into an expanded digest notification.
Notification.InboxStyle expandedBuilder = new Notification.InboxStyle(
@@ -409,10 +415,19 @@ public class AlertReceiver extends BroadcastReceiver {
// Remove the title in the expanded form (redundant with the listed items).
expandedBuilder.setBigContentTitle("");
- return expandedBuilder.build();
+ n = expandedBuilder.build();
} else {
- return notificationBuilder.build();
+ n = notificationBuilder.build();
+ }
+
+ NotificationWrapper nw = new NotificationWrapper(n);
+ if (AlertService.DEBUG) {
+ for (AlertService.NotificationInfo info : notificationInfos) {
+ nw.add(new NotificationWrapper(null, 0, info.eventId, info.startMillis,
+ info.endMillis, false));
+ }
}
+ return nw;
}
private static final String[] ATTENDEES_PROJECTION = new String[] {
@@ -499,8 +514,6 @@ public class AlertReceiver extends BroadcastReceiver {
* are no emailable attendees.
*/
private static Intent createEmailIntent(Context context, long eventId) {
- ContentResolver resolver = context.getContentResolver();
-
// TODO: Refactor to move query part into Utils.createEmailAttendeeIntent, to
// be shared with EventInfoFragment.
diff --git a/src/com/android/calendar/alerts/AlertService.java b/src/com/android/calendar/alerts/AlertService.java
index fc4b9880..c6c74075 100644
--- a/src/com/android/calendar/alerts/AlertService.java
+++ b/src/com/android/calendar/alerts/AlertService.java
@@ -16,10 +16,6 @@
package com.android.calendar.alerts;
-import com.android.calendar.GeneralPreferences;
-import com.android.calendar.R;
-import com.android.calendar.Utils;
-
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
@@ -48,6 +44,10 @@ import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;
+import com.android.calendar.GeneralPreferences;
+import com.android.calendar.R;
+import com.android.calendar.Utils;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -62,7 +62,7 @@ public class AlertService extends Service {
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
- private static final String[] ALERT_PROJECTION = new String[] {
+ static final String[] ALERT_PROJECTION = new String[] {
CalendarAlerts._ID, // 0
CalendarAlerts.EVENT_ID, // 1
CalendarAlerts.STATE, // 2
@@ -109,7 +109,72 @@ public class AlertService extends Service {
private static final int MIN_DEPRIORITIZE_GRACE_PERIOD_MS = 15 * MINUTE_MS;
// Hard limit to the number of notifications displayed.
- private static final int MAX_NOTIFICATIONS = 20;
+ public static final int MAX_NOTIFICATIONS = 20;
+
+ // Added wrapper for testing
+ public static class NotificationWrapper {
+ Notification mNotification;
+ long mEventId;
+ long mBegin;
+ long mEnd;
+ ArrayList<NotificationWrapper> mNw;
+
+ public NotificationWrapper(Notification n, int notificationId, long eventId,
+ long startMillis, long endMillis, boolean doPopup) {
+ mNotification = n;
+ mEventId = eventId;
+ mBegin = startMillis;
+ mEnd = endMillis;
+
+ // popup?
+ // notification id?
+ }
+
+ public NotificationWrapper(Notification n) {
+ mNotification = n;
+ }
+
+ public void add(NotificationWrapper nw) {
+ if (mNw == null) {
+ mNw = new ArrayList<NotificationWrapper>();
+ }
+ mNw.add(nw);
+ }
+ }
+
+ // Added wrapper for testing
+ public static class NotificationMgrWrapper implements NotificationMgr {
+ NotificationManager mNm;
+
+ public NotificationMgrWrapper(NotificationManager nm) {
+ mNm = nm;
+ }
+
+ @Override
+ public void cancel(int id) {
+ mNm.cancel(id);
+ }
+
+ @Override
+ public void cancel(String tag, int id) {
+ mNm.cancel(tag, id);
+ }
+
+ @Override
+ public void cancelAll() {
+ mNm.cancelAll();
+ }
+
+ @Override
+ public void notify(int id, NotificationWrapper nw) {
+ mNm.notify(id, nw.mNotification);
+ }
+
+ @Override
+ public void notify(String tag, int id, NotificationWrapper nw) {
+ mNm.notify(tag, id, nw.mNotification);
+ }
+ }
void processMessage(Message msg) {
Bundle bundle = (Bundle) msg.obj;
@@ -148,8 +213,8 @@ public class AlertService extends Service {
static boolean updateAlertNotification(Context context) {
ContentResolver cr = context.getContentResolver();
- NotificationManager nm =
- (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
+ NotificationMgr nm = new NotificationMgrWrapper(
+ (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
final long currentTime = System.currentTimeMillis();
SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);
@@ -182,6 +247,11 @@ public class AlertService extends Service {
return false;
}
+ return generateAlerts(context, nm, prefs, alertCursor, currentTime);
+ }
+
+ public static boolean generateAlerts(Context context, NotificationMgr nm,
+ SharedPreferences prefs, Cursor alertCursor, final long currentTime) {
if (DEBUG) {
Log.d(TAG, "alertCursor count:" + alertCursor.getCount());
}
@@ -246,7 +316,7 @@ public class AlertService extends Service {
int numLowPriority = lowPriorityEvents.size();
if (numLowPriority > 0) {
String expiredDigestTitle = getDigestTitle(lowPriorityEvents);
- Notification notification;
+ NotificationWrapper notification;
if (numLowPriority == 1) {
// If only 1 expired event, display an "old-style" basic alert.
NotificationInfo info = lowPriorityEvents.get(0);
@@ -415,7 +485,6 @@ public class AlertService extends Service {
ContentResolver cr = context.getContentResolver();
HashMap<Long, NotificationInfo> eventIds = new HashMap<Long, NotificationInfo>();
int numFired = 0;
- int notificationId = 1;
try {
while (alertCursor.moveToNext()) {
final long alertId = alertCursor.getLong(ALERT_INDEX_ID);
@@ -487,8 +556,7 @@ public class AlertService extends Service {
continue;
}
- // Pick an Event title for the notification panel by the latest
- // alertTime and give prefer accepted events in case of ties.
+ // TODO: Prefer accepted events in case of ties.
int newStatus;
switch (status) {
case Attendees.ATTENDEE_STATUS_ACCEPTED:
@@ -596,9 +664,9 @@ public class AlertService extends Service {
private static void postNotification(NotificationInfo info, String summaryText,
Context context, boolean highPriority, NotificationPrefs prefs,
- NotificationManager notificationMgr, int notificationId) {
+ NotificationMgr notificationMgr, int notificationId) {
String tickerText = getTickerText(info.eventName, info.location);
- Notification notification = AlertReceiver.makeExpandingNotification(context,
+ NotificationWrapper notification = AlertReceiver.makeExpandingNotification(context,
info.eventName, summaryText, info.description, info.startMillis,
info.endMillis, info.eventId, notificationId, prefs.getDoPopup(),
highPriority);
@@ -657,8 +725,9 @@ public class AlertService extends Service {
}
}
- private static void addNotificationOptions(Notification notification, boolean quietUpdate,
+ private static void addNotificationOptions(NotificationWrapper nw, boolean quietUpdate,
String tickerText, boolean defaultVibrate, String reminderRingtone) {
+ Notification notification = nw.mNotification;
notification.defaults |= Notification.DEFAULT_LIGHTS;
// Quietly update notification bar. Nothing new. Maybe something just got deleted.
@@ -683,7 +752,7 @@ public class AlertService extends Service {
}
}
- private static class NotificationPrefs {
+ /* package */ static class NotificationPrefs {
boolean quietUpdate;
private Context context;
private SharedPreferences prefs;
@@ -695,8 +764,7 @@ public class AlertService extends Service {
private static final String EMPTY_RINGTONE = "";
- NotificationPrefs(Context context, SharedPreferences prefs,
- boolean quietUpdate) {
+ NotificationPrefs(Context context, SharedPreferences prefs, boolean quietUpdate) {
this.context = context;
this.prefs = prefs;
this.quietUpdate = quietUpdate;
diff --git a/src/com/android/calendar/alerts/NotificationMgr.java b/src/com/android/calendar/alerts/NotificationMgr.java
new file mode 100644
index 00000000..e0a5baff
--- /dev/null
+++ b/src/com/android/calendar/alerts/NotificationMgr.java
@@ -0,0 +1,11 @@
+package com.android.calendar.alerts;
+
+import com.android.calendar.alerts.AlertService.NotificationWrapper;
+
+public interface NotificationMgr {
+ public void cancel(int id);
+ public void cancel(String tag, int id);
+ public void cancelAll();
+ public void notify(int id, NotificationWrapper notification);
+ public void notify(String tag, int id, NotificationWrapper notification);
+}
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 7612dc2c..59bb98ed 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -32,10 +32,4 @@
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.android.calendar"
android:label="calendar tests"/>
-
- <instrumentation android:name="com.android.calendar.CalendarLaunchPerformance"
- android:targetPackage="com.android.calendar"
- android:label="Calendar Launch Performance">
- </instrumentation>
-
</manifest>
diff --git a/tests/src/com/android/calendar/CalendarLaunchPerformance.java b/tests/src/com/android/calendar/CalendarLaunchPerformance.java
deleted file mode 100644
index a089d86d..00000000
--- a/tests/src/com/android/calendar/CalendarLaunchPerformance.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright (C) 2007 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;
-
-import android.app.Activity;
-import android.test.LaunchPerformanceBase;
-import android.os.Bundle;
-
-/**
- * Instrumentation class for Browser launch performance testing.
- */
-public class CalendarLaunchPerformance extends LaunchPerformanceBase {
-
- public static final String LOG_TAG = "CalendarLaunchPerformance";
-
- public CalendarLaunchPerformance() {
- super();
- }
-
- @Override
- public void onCreate(Bundle arguments) {
- super.onCreate(arguments);
-
- mIntent.setClassName(getTargetContext(), "com.android.calendar.LaunchActivity");
- start();
- }
-
- /**
- * Calls LaunchApp and finish.
- */
- @Override
- public void onStart() {
- super.onStart();
- LaunchApp();
- finish(Activity.RESULT_OK, mResults);
- }
-}
diff --git a/tests/src/com/android/calendar/alerts/AlertServiceTest.java b/tests/src/com/android/calendar/alerts/AlertServiceTest.java
new file mode 100644
index 00000000..ebb965cd
--- /dev/null
+++ b/tests/src/com/android/calendar/alerts/AlertServiceTest.java
@@ -0,0 +1,345 @@
+/*
+ * 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 static android.app.Notification.PRIORITY_DEFAULT;
+import static android.app.Notification.PRIORITY_HIGH;
+import static android.app.Notification.PRIORITY_MIN;
+
+import android.content.SharedPreferences;
+import android.database.MatrixCursor;
+import android.provider.CalendarContract.Attendees;
+import android.provider.CalendarContract.CalendarAlerts;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.test.suitebuilder.annotation.Smoke;
+
+import com.android.calendar.GeneralPreferences;
+import com.android.calendar.alerts.AlertService.NotificationWrapper;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Map;
+import java.util.Set;
+
+public class AlertServiceTest extends AndroidTestCase {
+
+ class MockSharedPreferences implements SharedPreferences {
+
+ /* "always", "silent", depends on ringer mode */
+ private String mVibrateWhen = "always";
+ private String mRingtone = "/some/cool/ringtone";
+ private boolean mPopup = true;
+
+ @Override
+ public boolean contains(String key) {
+ if (GeneralPreferences.KEY_ALERTS_VIBRATE_WHEN.equals(key)) {
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean getBoolean(String key, boolean defValue) {
+ if (GeneralPreferences.KEY_ALERTS_POPUP.equals(key)) {
+ return mPopup;
+ }
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public String getString(String key, String defValue) {
+ if (GeneralPreferences.KEY_ALERTS_VIBRATE_WHEN.equals(key)) {
+ return mVibrateWhen;
+ }
+ if (GeneralPreferences.KEY_ALERTS_RINGTONE.equals(key)) {
+ return mRingtone;
+ }
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public Map<String, ?> getAll() {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public Set<String> getStringSet(String key, Set<String> defValues) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public int getInt(String key, int defValue) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public long getLong(String key, long defValue) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public float getFloat(String key, float defValue) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public Editor edit() {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public void registerOnSharedPreferenceChangeListener(
+ OnSharedPreferenceChangeListener listener) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public void unregisterOnSharedPreferenceChangeListener(
+ OnSharedPreferenceChangeListener listener) {
+ throw new IllegalArgumentException();
+ }
+
+ }
+
+ // Created these constants so the test cases are shorter
+ public static final int SCHEDULED = CalendarAlerts.STATE_SCHEDULED;
+ public static final int FIRED = CalendarAlerts.STATE_FIRED;
+ public static final int DISMISSED = CalendarAlerts.STATE_DISMISSED;
+
+ public static final int ACCEPTED = Attendees.ATTENDEE_STATUS_ACCEPTED;
+ public static final int DECLINED = Attendees.ATTENDEE_STATUS_DECLINED;
+ public static final int INVITED = Attendees.ATTENDEE_STATUS_INVITED;
+ public static final int TENTATIVE = Attendees.ATTENDEE_STATUS_TENTATIVE;
+
+ class NotificationInstance {
+ int mAlertId;
+ int mPriority;
+
+ public NotificationInstance(int alertId, int priority) {
+ mAlertId = alertId;
+ mPriority = priority;
+ }
+ }
+
+ class Alert {
+ long mEventId;
+ int mAlertStatus;
+ int mResponseStatus;
+ int mAllDay;
+ long mBegin;
+ long mEnd;
+ int mMinute;
+ long mAlarmTime;
+
+ public Alert(long eventId, int alertStatus, int responseStatus, int allDay, long begin,
+ long end, int minute, long alarmTime) {
+ mEventId = eventId;
+ mAlertStatus = alertStatus;
+ mResponseStatus = responseStatus;
+ mAllDay = allDay;
+ mBegin = begin;
+ mEnd = end;
+ mMinute = minute;
+ mAlarmTime = alarmTime;
+ }
+
+ }
+
+ class AlertsTable {
+
+ ArrayList<Alert> mAlerts = new ArrayList<Alert>();
+
+ int addAlertRow(long eventId, int alertStatus, int responseStatus, int allDay, long begin,
+ long end, int minute, long alarmTime) {
+ Alert a = new Alert(eventId, alertStatus, responseStatus, allDay, begin, end, minute,
+ alarmTime);
+ int id = mAlerts.size();
+ mAlerts.add(a);
+ return id;
+ }
+
+ public MatrixCursor getAlertCursor() {
+ MatrixCursor alertCursor = new MatrixCursor(AlertService.ALERT_PROJECTION);
+
+ int i = 0;
+ for (Alert a : mAlerts) {
+ Object[] ca = {
+ i++,
+ a.mEventId,
+ a.mAlertStatus,
+ "Title" + a.mEventId + " " + a.mMinute,
+ "Loc" + a.mEventId,
+ a.mResponseStatus,
+ a.mAllDay,
+ a.mAlarmTime > 0 ? a.mAlarmTime : a.mBegin - a.mMinute * 60 * 1000,
+ a.mMinute,
+ a.mBegin,
+ a.mEnd,
+ "Desc: " + a.mAlarmTime
+ };
+ alertCursor.addRow(ca);
+ }
+ return alertCursor;
+ }
+
+ }
+
+ class NotificationTestManager implements NotificationMgr {
+ // Expected notifications
+ NotificationInstance[] mNotifications =
+ new NotificationInstance[AlertService.MAX_NOTIFICATIONS + 1];
+
+ // Flag to know which notification has been posted or canceled
+ boolean[] mDone;
+
+ // CalendarAlerts table
+ private ArrayList<Alert> mAlerts;
+
+ public NotificationTestManager(ArrayList<Alert> alerts) {
+ assertEquals(0, AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID);
+ mAlerts = alerts;
+ }
+
+ public void expectTestNotification(int notificationId, int alertId, int highPriority) {
+ mNotifications[notificationId] = new NotificationInstance(alertId, highPriority);
+ }
+
+ private void verifyNotification(int id, NotificationWrapper nw) {
+ assertEquals(mNotifications[id].mPriority, nw.mNotification.priority);
+ Alert a = mAlerts.get(mNotifications[id].mAlertId);
+ assertEquals(a.mEventId, nw.mEventId);
+ assertEquals(a.mBegin, nw.mBegin);
+ assertEquals(a.mEnd, nw.mEnd);
+ }
+
+ public void validateNotificationsAndReset() {
+ for (int i = 0; i < mDone.length; i++) {
+ assertTrue("Notification id " + i + " has not been posted", mDone[i]);
+ }
+ Arrays.fill(mDone, false);
+ Arrays.fill(mNotifications, null);
+ }
+
+ ///////////////////////////////
+ // NotificationMgr methods
+ @Override
+ public void cancel(int id) {
+ if (mDone == null) {
+ mDone = new boolean[mNotifications.length];
+ }
+ assertTrue("id out of bound: " + id, 0 <= id);
+ assertTrue("id out of bound: " + id, id < mDone.length);
+ assertFalse("id already used", mDone[id]);
+ mDone[id] = true;
+ assertNull("Unexpected cancel for id " + id, mNotifications[id]);
+ }
+
+ @Override
+ public void cancel(String tag, int id) {
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public void cancelAll() {
+ for (int i = 0; i < mNotifications.length; i++) {
+ assertNull("Expecting notification id " + i + ". Got cancelAll", mNotifications[i]);
+
+ if (mDone != null) {
+ assertFalse("Notification id " + i + " is done but got cancelAll", mDone[i]);
+ }
+ }
+
+ assertNull(mDone); // this should have been null since nothing
+ // should have been posted
+ mDone = new boolean[mNotifications.length];
+ Arrays.fill(mDone, true);
+ }
+
+ @Override
+ public void notify(int id, NotificationWrapper nw) {
+ if (mDone == null) {
+ mDone = new boolean[mNotifications.length];
+ }
+ assertTrue("id out of bound: " + id, 0 <= id);
+ assertTrue("id out of bound: " + id, id < mDone.length);
+ assertFalse("id already used", mDone[id]);
+ mDone[id] = true;
+
+ assertNotNull("Unexpected notify for id " + id, mNotifications[id]);
+
+ verifyNotification(id, nw);
+ }
+
+ @Override
+ public void notify(String tag, int id, NotificationWrapper nw) {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ // TODO
+ // Catch updates of new state, notify time, and received time
+ // Test ringer, vibrate,
+ // Test digest notifications
+ // Test intents, action email
+ // Catch alarmmgr calls
+
+ @Smoke
+ @SmallTest
+ public void testNoAlerts() {
+ MockSharedPreferences prefs = new MockSharedPreferences();
+ AlertsTable at = new AlertsTable();
+ NotificationTestManager ntm = new NotificationTestManager(at.mAlerts);
+
+ // Test no alert
+ long currentTime = 1000000;
+ AlertService.generateAlerts(mContext, ntm, prefs, at.getAlertCursor(), currentTime);
+ ntm.validateNotificationsAndReset();
+ }
+
+ @Smoke
+ @SmallTest
+ public void testSingleAlert() {
+ MockSharedPreferences prefs = new MockSharedPreferences();
+ AlertsTable at = new AlertsTable();
+ NotificationTestManager ntm = new NotificationTestManager(at.mAlerts);
+
+ int id = at.addAlertRow(100, SCHEDULED, ACCEPTED, 0 /* all day */, 1300000, 2300000, 5, 0);
+
+ // Test one up coming alert
+ long currentTime = 1000000;
+ ntm.expectTestNotification(1, id, PRIORITY_HIGH);
+
+ AlertService.generateAlerts(mContext, ntm, prefs, at.getAlertCursor(), currentTime);
+ ntm.validateNotificationsAndReset(); // This wipes out notification
+ // tests added so far
+
+ // Test half way into an event
+ currentTime = 2300000;
+ ntm.expectTestNotification(AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID, id, PRIORITY_DEFAULT);
+
+ AlertService.generateAlerts(mContext, ntm, prefs, at.getAlertCursor(), currentTime);
+ ntm.validateNotificationsAndReset();
+
+ // Test event ended
+ currentTime = 4300000;
+ ntm.expectTestNotification(AlertUtils.EXPIRED_GROUP_NOTIFICATION_ID, id, PRIORITY_MIN);
+
+ AlertService.generateAlerts(mContext, ntm, prefs, at.getAlertCursor(), currentTime);
+ ntm.validateNotificationsAndReset();
+ }
+}