1/*
2 * Copyright (C) 2011 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.app;
18
19
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23import android.os.SystemClock;
24import android.test.InstrumentationTestCase;
25import android.test.RepetitiveTest;
26import android.test.TimedTest;
27
28import java.util.Random;
29
30/**
31 * Test which spams notification manager with a large number of notifications, for both stress and
32 * performance testing.
33 */
34public class NotificationStressTest extends InstrumentationTestCase {
35
36    private static final int NUM_ITERATIONS = 200;
37    private static final int[] ICONS = new int[] {
38        android.R.drawable.stat_notify_call_mute,
39        android.R.drawable.stat_notify_chat,
40        android.R.drawable.stat_notify_error,
41        android.R.drawable.stat_notify_missed_call,
42        android.R.drawable.stat_notify_more,
43        android.R.drawable.stat_notify_sdcard,
44        android.R.drawable.stat_notify_sdcard_prepare,
45        android.R.drawable.stat_notify_sdcard_usb,
46        android.R.drawable.stat_notify_sync,
47        android.R.drawable.stat_notify_sync_noanim,
48        android.R.drawable.stat_notify_voicemail,
49    };
50
51    private final Random mRandom = new Random();
52    private Context mContext;
53    private NotificationManager mNotificationManager;
54    private int notifyId = 0;
55
56    @Override
57    protected void setUp() throws Exception {
58        super.setUp();
59        mContext = getInstrumentation().getContext();
60        mNotificationManager = (NotificationManager) mContext.getSystemService(
61                Context.NOTIFICATION_SERVICE);
62    }
63
64    @Override
65    protected void tearDown() throws Exception {
66        super.tearDown();
67        mNotificationManager.cancelAll();
68    }
69
70    @RepetitiveTest(numIterations=NUM_ITERATIONS)
71    public void testNotificationStress() {
72        // Cancel one of every five notifications to vary load on notification manager
73        if (notifyId % 5 == 4) {
74            mNotificationManager.cancel(notifyId - 4);
75        }
76        sendNotification(notifyId++, "testNotificationStressNotify");
77    }
78
79    private void sendNotification(int id, CharSequence text) {
80        // Create "typical" notification with random icon
81        Notification notification = new Notification(ICONS[mRandom.nextInt(ICONS.length)], text,
82                System.currentTimeMillis());
83        // Fill in arbitrary content
84        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
85        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
86        CharSequence title = text + " " + id;
87        CharSequence subtitle = String.valueOf(System.currentTimeMillis());
88        notification.setLatestEventInfo(mContext, title, subtitle, pendingIntent);
89        mNotificationManager.notify(id, notification);
90        SystemClock.sleep(10);
91    }
92}
93