FileDocCategorySizeDatePackage
NotificationStressTest.javaAPI DocAndroid 5.1 API3459Thu Mar 12 22:22:12 GMT 2015android.app

NotificationStressTest

public class NotificationStressTest extends android.test.InstrumentationTestCase
Test which spams notification manager with a large number of notifications, for both stress and performance testing.

Fields Summary
private static final int
NUM_ITERATIONS
private static final int[]
ICONS
private final Random
mRandom
private android.content.Context
mContext
private NotificationManager
mNotificationManager
private int
notifyId
Constructors Summary
Methods Summary
private voidsendNotification(int id, java.lang.CharSequence text)

        // Create "typical" notification with random icon
        Notification notification = new Notification(ICONS[mRandom.nextInt(ICONS.length)], text,
                System.currentTimeMillis());
        // Fill in arbitrary content
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        CharSequence title = text + " " + id;
        CharSequence subtitle = String.valueOf(System.currentTimeMillis());
        notification.setLatestEventInfo(mContext, title, subtitle, pendingIntent);
        mNotificationManager.notify(id, notification);
        SystemClock.sleep(10);
    
protected voidsetUp()


    
         
        super.setUp();
        mContext = getInstrumentation().getContext();
        mNotificationManager = (NotificationManager) mContext.getSystemService(
                Context.NOTIFICATION_SERVICE);
    
protected voidtearDown()

        super.tearDown();
        mNotificationManager.cancelAll();
    
public voidtestNotificationStress()

        // Cancel one of every five notifications to vary load on notification manager
        if (notifyId % 5 == 4) {
            mNotificationManager.cancel(notifyId - 4);
        }
        sendNotification(notifyId++, "testNotificationStressNotify");