NotificationTestList.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2007 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 com.android.statusbartest;
18
19import android.app.ListActivity;
20import android.app.PendingIntent;
21import android.widget.ArrayAdapter;
22import android.view.View;
23import android.widget.ListView;
24import android.content.Intent;
25import android.app.Notification;
26import android.app.NotificationManager;
27import android.os.Vibrator;
28import android.os.Bundle;
29import android.os.Handler;
30import android.util.Log;
31import android.net.Uri;
32import android.os.SystemClock;
33import android.widget.RemoteViews;
34import android.widget.TextView;
35import android.os.PowerManager;
36
37public class NotificationTestList extends TestActivity
38{
39    private final static String TAG = "NotificationTestList";
40
41    NotificationManager mNM;
42    Vibrator mVibrator = new Vibrator();
43    Handler mHandler = new Handler();
44
45    long mChronometerBase = 0;
46
47    @Override
48    protected String tag() {
49        return TAG;
50    }
51
52    @Override
53    protected Test[] tests() {
54        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
55
56        return mTests;
57    }
58
59    private Test[] mTests = new Test[] {
60        new Test("Crash") {
61            public void run()
62            {
63                PowerManager.WakeLock wl
64                        = ((PowerManager)NotificationTestList.this.getSystemService("power"))
65                            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
66                wl.acquire();
67                mHandler.postDelayed(new Runnable() {
68                            public void run() {
69                                throw new RuntimeException("Die!");
70                            }
71                        }, 10000);
72
73            }
74        },
75
76        new Test("No view") {
77            public void run() {
78                Notification n = new Notification(R.drawable.icon1, "No view",
79                        System.currentTimeMillis());
80                mNM.notify(1, n);
81            }
82        },
83
84        new Test("No intent") {
85            public void run() {
86                Notification n = new Notification(R.drawable.icon1, "No intent",
87                        System.currentTimeMillis());
88                n.setLatestEventInfo(NotificationTestList.this, "No intent",
89                            "No intent", null);
90                mNM.notify(1, n);
91            }
92        },
93
94        new Test("Layout") {
95            public void run()
96            {
97
98                mNM.notify(1, new Notification(NotificationTestList.this,
99                            R.drawable.ic_statusbar_missedcall,
100                            null, System.currentTimeMillis()-(1000*60*60*24),
101                            "(453) 123-2328",
102                            "", null));
103
104                mNM.notify(2, new Notification(NotificationTestList.this,
105                            R.drawable.ic_statusbar_email,
106                            null, System.currentTimeMillis(),
107                            "Mark Willem, Me (2)",
108                            "Re: Didn't you get the memo?", null));
109
110                mNM.notify(3, new Notification(NotificationTestList.this,
111                            R.drawable.ic_statusbar_chat,
112                            null, System.currentTimeMillis()+(1000*60*60*24),
113                            "Sophia Winterlanden",
114                            "Lorem ipsum dolor sit amet.", null));
115            }
116        },
117
118        new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
119                new Runnable() {
120                    public void run() {
121                        Log.d(TAG, "Stress - Ongoing/Latest 0");
122                        Notification n = new Notification(NotificationTestList.this,
123                                R.drawable.icon3,
124                                null, System.currentTimeMillis(), "Stress - Ongoing",
125                                "Notify me!!!", null);
126                        n.flags |= Notification.FLAG_ONGOING_EVENT;
127                        mNM.notify(1, n);
128                    }
129                },
130                new Runnable() {
131                    public void run() {
132                        Log.d(TAG, "Stress - Ongoing/Latest 1");
133                        Notification n = new Notification(NotificationTestList.this,
134                                R.drawable.icon4,
135                                null, System.currentTimeMillis(), "Stress - Latest",
136                                "Notify me!!!", null);
137                        n.flags |= Notification.FLAG_ONGOING_EVENT;
138                        mNM.notify(1, n);
139                    }
140                }
141            }),
142
143        new Test("Long") {
144            public void run()
145            {
146                Notification n = new Notification();
147                n.defaults |= Notification.DEFAULT_SOUND ;
148                n.vibrate = new long[] {
149                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
150                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
151                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
152                mNM.notify(1, n);
153            }
154        },
155
156        new Test("Default All") {
157            public void run()
158            {
159                Notification n = new Notification();
160                n.defaults |= Notification.DEFAULT_ALL;
161                mNM.notify(1, n);
162            }
163        },
164
165        new Test("Default All, once") {
166            public void run()
167            {
168                Notification n = new Notification();
169                n.defaults |= Notification.DEFAULT_ALL;
170                n.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
171                mNM.notify(1, n);
172            }
173        },
174
175        new Test("Content Sound") {
176            public void run()
177            {
178                Notification n = new Notification();
179                n.sound = Uri.parse(
180                        "content://media/internal/audio/media/7");
181
182                mNM.notify(1, n);
183            }
184        },
185
186        new Test("Resource Sound") {
187            public void run()
188            {
189                Notification n = new Notification();
190                n.sound = Uri.parse(
191                        "android.resource://com.android.notificationtest/raw/ringer");
192                Log.d(TAG, "n.sound=" + n.sound);
193
194                mNM.notify(1, n);
195            }
196        },
197
198        new Test("Sound and Cancel") {
199            public void run()
200            {
201                Notification n = new Notification();
202                n.sound = Uri.parse(
203                            "content://media/internal/audio/media/7");
204
205                mNM.notify(1, n);
206                SystemClock.sleep(200);
207                mNM.cancel(1);
208            }
209        },
210
211        new Test("Vibrate") {
212            public void run()
213            {
214                Notification n = new Notification();
215                    n.vibrate = new long[] { 0, 700, 500, 1000 };
216
217                mNM.notify(1, n);
218            }
219        },
220
221        new Test("Vibrate and cancel") {
222            public void run()
223            {
224                Notification n = new Notification();
225                    n.vibrate = new long[] { 0, 700, 500, 1000 };
226
227                mNM.notify(1, n);
228                SystemClock.sleep(500);
229                mNM.cancel(1);
230            }
231        },
232
233        new Test("Vibrate pattern") {
234            public void run()
235            {
236                mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
237            }
238        },
239
240        new Test("Vibrate pattern repeating") {
241            public void run()
242            {
243                mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
244            }
245        },
246
247        new Test("Vibrate 3s") {
248            public void run()
249            {
250                mVibrator.vibrate(3000);
251            }
252        },
253
254        new Test("Vibrate 100s") {
255            public void run()
256            {
257                mVibrator.vibrate(100000);
258            }
259        },
260
261        new Test("Vibrate off") {
262            public void run()
263            {
264                mVibrator.cancel();
265            }
266        },
267
268        new Test("Cancel #1") {
269            public void run() {
270                mNM.cancel(1);
271            }
272        },
273
274        new Test("Cancel #1 in 3 sec") {
275            public void run() {
276                mHandler.postDelayed(new Runnable() {
277                            public void run() {
278                                Log.d(TAG, "Cancelling now...");
279                                mNM.cancel(1);
280                            }
281                        }, 3000);
282            }
283        },
284
285        new Test("Cancel #2") {
286            public void run() {
287                mNM.cancel(2);
288            }
289        },
290
291        new Test("Persistent #1") {
292            public void run() {
293                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
294                        System.currentTimeMillis());
295                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
296                            "This is a notification!!!", makeIntent());
297                mNM.notify(1, n);
298            }
299        },
300
301        new Test("Persistent #1 in 3 sec") {
302            public void run() {
303                mHandler.postDelayed(new Runnable() {
304                            public void run() {
305                                Notification n = new Notification(R.drawable.icon1,
306                                        "            "
307                                        + "tick tock tick tock\n\nSometimes notifications can "
308                                        + "be really long and wrap to more than one line.\n"
309                                        + "Sometimes."
310                                        + "Ohandwhathappensifwehaveonereallylongstringarewesure"
311                                        + "thatwesegmentitcorrectly?\n",
312                                        System.currentTimeMillis());
313                                n.setLatestEventInfo(NotificationTestList.this,
314                                        "Still Persistent #1",
315                                        "This is still a notification!!!",
316                                        makeIntent());
317                                mNM.notify(1, n);
318                            }
319                        }, 3000);
320            }
321        },
322
323        new Test("Persistent #2") {
324            public void run() {
325                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
326                        System.currentTimeMillis());
327                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
328                            "Notify me!!!", makeIntent());
329                mNM.notify(2, n);
330            }
331        },
332
333        new Test("Persistent #2 Vibrate") {
334            public void run() {
335                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
336                        System.currentTimeMillis());
337                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
338                            "Notify me!!!", makeIntent());
339                n.defaults = Notification.DEFAULT_VIBRATE;
340                mNM.notify(2, n);
341            }
342        },
343
344        new Test("Chronometer Start") {
345            public void run() {
346                Notification n = new Notification(R.drawable.icon2, "me me me me",
347                                                    System.currentTimeMillis());
348                n.contentView = new RemoteViews(getPackageName(), R.layout.chrono_notification);
349                mChronometerBase = SystemClock.elapsedRealtime();
350                n.contentView.setChronometer(R.id.time, mChronometerBase, "Yay! (%s)", true);
351                n.flags |= Notification.FLAG_ONGOING_EVENT;
352                n.contentIntent = makeIntent();
353                mNM.notify(2, n);
354            }
355        },
356
357        new Test("Chronometer Stop") {
358            public void run() {
359                mHandler.postDelayed(new Runnable() {
360                        public void run() {
361                            Log.d(TAG, "Chronometer Stop");
362                            Notification n = new Notification();
363                            n.icon = R.drawable.icon1;
364                            n.contentView = new RemoteViews(getPackageName(),
365                                                             R.layout.chrono_notification);
366                            n.contentView.setChronometer(R.id.time, mChronometerBase, null, false);
367                            n.contentIntent = makeIntent();
368                            mNM.notify(2, n);
369                        }
370                    }, 3000);
371            }
372        },
373
374        new Test("Sequential Persistent") {
375            public void run() {
376                mNM.notify(1, notificationWithNumbers(1));
377                mNM.notify(2, notificationWithNumbers(2));
378            }
379        },
380
381        new Test("Replace Persistent") {
382            public void run() {
383                mNM.notify(1, notificationWithNumbers(1));
384                mNM.notify(1, notificationWithNumbers(1));
385            }
386        },
387
388        new Test("Run and Cancel (n=1)") {
389            public void run() {
390                mNM.notify(1, notificationWithNumbers(1));
391                mNM.cancel(1);
392            }
393        },
394
395        new Test("Run an Cancel (n=2)") {
396            public void run() {
397                mNM.notify(1, notificationWithNumbers(1));
398                mNM.notify(2, notificationWithNumbers(2));
399                mNM.cancel(2);
400            }
401        },
402
403        // Repeatedly notify and cancel -- triggers bug #670627
404        new Test("Bug 670627") {
405            public void run() {
406                for (int i = 0; i < 10; i++) {
407                  Log.d(TAG, "Add two notifications");
408                  mNM.notify(1, notificationWithNumbers(1));
409                  mNM.notify(2, notificationWithNumbers(2));
410                  Log.d(TAG, "Cancel two notifications");
411                  mNM.cancel(1);
412                  mNM.cancel(2);
413                }
414            }
415        },
416
417        new Test("Ten Notifications") {
418            public void run() {
419                for (int i = 0; i < 2; i++) {
420                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
421                            null, System.currentTimeMillis(), "Persistent #" + i,
422                            "Notify me!!!" + i, null);
423                    n.flags |= Notification.FLAG_ONGOING_EVENT;
424                    n.number = i;
425                    mNM.notify((i+1)*10, n);
426                }
427                for (int i = 2; i < 10; i++) {
428                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
429                            null, System.currentTimeMillis(), "Persistent #" + i,
430                            "Notify me!!!" + i, null);
431                    n.number = i;
432                    mNM.notify((i+1)*10, n);
433                }
434            }
435        },
436
437        new Test("Cancel eight notifications") {
438            public void run() {
439                for (int i = 1; i < 9; i++) {
440                    mNM.cancel((i+1)*10);
441                }
442            }
443        },
444
445        new Test("Persistent with numbers 1") {
446            public void run() {
447                mNM.notify(1, notificationWithNumbers(1));
448            }
449        },
450
451        new Test("Persistent with numbers 222") {
452            public void run() {
453                mNM.notify(1, notificationWithNumbers(22));
454            }
455        },
456
457        new Test("Persistent with numbers 333") {
458            public void run() {
459                mNM.notify(1, notificationWithNumbers(333));
460            }
461        },
462
463        new Test("Persistent with numbers 4444") {
464            public void run() {
465                mNM.notify(1, notificationWithNumbers(4444));
466            }
467        },
468
469    };
470
471    private Notification notificationWithNumbers(int num) {
472        Notification n = new Notification(this, R.drawable.icon2, null, System.currentTimeMillis(),
473                "Persistent #2", "Notify me!!!", null);
474        n.number = num;
475        return n;
476    }
477
478    private PendingIntent makeIntent() {
479        Intent intent = new Intent(Intent.ACTION_MAIN);
480        intent.setComponent(new android.content.ComponentName(
481                    "com.android.contacts",
482                    "com.android.contacts.ContactsActivity"));
483        return PendingIntent.getActivity(this, 0, intent, 0);
484    }
485
486    class StateStress extends Test {
487        StateStress(String name, int pause, int iterations, Runnable[] tasks) {
488            super(name);
489            mPause = pause;
490            mTasks = tasks;
491            mIteration = iterations;
492        }
493        Runnable[] mTasks;
494        int mNext;
495        int mIteration;
496        long mPause;
497        Runnable mRunnable = new Runnable() {
498            public void run() {
499                mTasks[mNext].run();
500                mNext++;
501                if (mNext >= mTasks.length) {
502                    mNext = 0;
503                    mIteration--;
504                    if (mIteration <= 0) {
505                        return;
506                    }
507                }
508                mHandler.postDelayed(mRunnable, mPause);
509            }
510        };
511        public void run() {
512            mNext = 0;
513            mHandler.postDelayed(mRunnable, mPause);
514        }
515    }
516}
517
518