NotificationTestList.java revision 6cf5eb0606ecdc22e7c431971a0c621413564840
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.PendingIntent;
20import android.content.Context;
21import android.content.ContentResolver;
22import android.content.Intent;
23import android.app.Notification;
24import android.app.NotificationManager;
25import android.os.Environment;
26import android.os.Vibrator;
27import android.os.Handler;
28import android.util.Log;
29import android.net.Uri;
30import android.os.SystemClock;
31import android.widget.RemoteViews;
32import android.os.PowerManager;
33
34public class NotificationTestList extends TestActivity
35{
36    private final static String TAG = "NotificationTestList";
37
38    NotificationManager mNM;
39    Vibrator mVibrator = new Vibrator();
40    Handler mHandler = new Handler();
41
42    long mActivityCreateTime = System.currentTimeMillis();
43    long mChronometerBase = 0;
44
45    @Override
46    protected String tag() {
47        return TAG;
48    }
49
50    @Override
51    protected Test[] tests() {
52        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
53
54        return mTests;
55    }
56
57    private Test[] mTests = new Test[] {
58        new Test("Off and sound") {
59            public void run() {
60                PowerManager pm = (PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE);
61                PowerManager.WakeLock wl =
62                            pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sound");
63                wl.acquire();
64
65                pm.goToSleep(SystemClock.uptimeMillis());
66
67                Notification n = new Notification();
68                n.sound = Uri.parse("file://" + Environment.getExternalStorageDirectory() +
69                        "/virtual-void.mp3");
70                Log.d(TAG, "n.sound=" + n.sound);
71
72                mNM.notify(1, n);
73
74                Log.d(TAG, "releasing wake lock");
75                wl.release();
76                Log.d(TAG, "released wake lock");
77            }
78        },
79
80        new Test("No view") {
81            public void run() {
82                Notification n = new Notification(R.drawable.icon1, "No view",
83                        System.currentTimeMillis());
84                mNM.notify(1, n);
85            }
86        },
87
88        new Test("No intent") {
89            public void run() {
90                Notification n = new Notification(R.drawable.icon1, "No intent",
91                        System.currentTimeMillis());
92                n.setLatestEventInfo(NotificationTestList.this, "No intent",
93                            "No intent", null);
94                mNM.notify(1, n);
95            }
96        },
97
98        new Test("Layout") {
99            public void run()
100            {
101
102                mNM.notify(1, new Notification(NotificationTestList.this,
103                            R.drawable.ic_statusbar_missedcall,
104                            null, System.currentTimeMillis()-(1000*60*60*24),
105                            "(453) 123-2328",
106                            "", null));
107
108                mNM.notify(2, new Notification(NotificationTestList.this,
109                            R.drawable.ic_statusbar_email,
110                            null, System.currentTimeMillis(),
111                            "Mark Willem, Me (2)",
112                            "Re: Didn't you get the memo?", null));
113
114                mNM.notify(3, new Notification(NotificationTestList.this,
115                            R.drawable.ic_statusbar_chat,
116                            null, System.currentTimeMillis()+(1000*60*60*24),
117                            "Sophia Winterlanden",
118                            "Lorem ipsum dolor sit amet.", null));
119            }
120        },
121
122        new Test("Bad Icon #1 (when=create)") {
123            public void run() {
124                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
125                        null, mActivityCreateTime);
126                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
127                            "This is the same notification!!!", makeIntent());
128                mNM.notify(1, n);
129            }
130        },
131
132        new Test("Bad Icon #1 (when=now)") {
133            public void run() {
134                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
135                        null, System.currentTimeMillis());
136                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
137                            "This is the same notification!!!", makeIntent());
138                mNM.notify(1, n);
139            }
140        },
141
142        new Test("Bad resource #1 (when=create)") {
143            public void run() {
144                Notification n = new Notification(R.drawable.icon2,
145                        null, mActivityCreateTime);
146                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
147                            "This is the same notification!!!", makeIntent());
148                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
149                mNM.notify(1, n);
150            }
151        },
152
153        new Test("Bad resource #1 (when=now)") {
154            public void run() {
155                Notification n = new Notification(R.drawable.icon2,
156                        null, System.currentTimeMillis());
157                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
158                            "This is the same notification!!!", makeIntent());
159                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
160                mNM.notify(1, n);
161            }
162        },
163
164
165        new Test("Bad resource #3") {
166            public void run()
167            {
168                Notification n = new Notification(NotificationTestList.this,
169                            R.drawable.ic_statusbar_missedcall,
170                            null, System.currentTimeMillis()-(1000*60*60*24),
171                            "(453) 123-2328",
172                            "", null);
173                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
174                mNM.notify(3, n);
175            }
176        },
177
178        new Test("Times") {
179            public void run()
180            {
181                long now = System.currentTimeMillis();
182
183                timeNotification(7, "24 hours from now", now+(1000*60*60*24));
184                timeNotification(6, "12:01:00 from now", now+(1000*60*60*12)+(60*1000));
185                timeNotification(5, "12 hours from now", now+(1000*60*60*12));
186                timeNotification(4, "now", now);
187                timeNotification(3, "11:59:00 ago", now-((1000*60*60*12)-(60*1000)));
188                timeNotification(2, "12 hours ago", now-(1000*60*60*12));
189                timeNotification(1, "24 hours ago", now-(1000*60*60*24));
190            }
191        },
192        new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
193                new Runnable() {
194                    public void run() {
195                        Log.d(TAG, "Stress - Ongoing/Latest 0");
196                        Notification n = new Notification(NotificationTestList.this,
197                                R.drawable.icon3,
198                                null, System.currentTimeMillis(), "Stress - Ongoing",
199                                "Notify me!!!", null);
200                        n.flags |= Notification.FLAG_ONGOING_EVENT;
201                        mNM.notify(1, n);
202                    }
203                },
204                new Runnable() {
205                    public void run() {
206                        Log.d(TAG, "Stress - Ongoing/Latest 1");
207                        Notification n = new Notification(NotificationTestList.this,
208                                R.drawable.icon4,
209                                null, System.currentTimeMillis(), "Stress - Latest",
210                                "Notify me!!!", null);
211                        //n.flags |= Notification.FLAG_ONGOING_EVENT;
212                        mNM.notify(1, n);
213                    }
214                }
215            }),
216
217        new Test("Long") {
218            public void run()
219            {
220                Notification n = new Notification();
221                n.defaults |= Notification.DEFAULT_SOUND ;
222                n.vibrate = new long[] {
223                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
224                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
225                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
226                mNM.notify(1, n);
227            }
228        },
229
230        new Test("Blue Lights") {
231            public void run()
232            {
233                Notification n = new Notification();
234                n.flags |= Notification.FLAG_SHOW_LIGHTS;
235                n.ledARGB = 0xff0000ff;
236                n.ledOnMS = 1;
237                n.ledOffMS = 0;
238                mNM.notify(1, n);
239            }
240        },
241
242        new Test("Red Lights") {
243            public void run()
244            {
245                Notification n = new Notification();
246                n.flags |= Notification.FLAG_SHOW_LIGHTS;
247                n.ledARGB = 0xffff0000;
248                n.ledOnMS = 1;
249                n.ledOffMS = 0;
250                mNM.notify(1, n);
251            }
252        },
253
254        new Test("Yellow Lights") {
255            public void run()
256            {
257                Notification n = new Notification();
258                n.flags |= Notification.FLAG_SHOW_LIGHTS;
259                n.ledARGB = 0xffffff00;
260                n.ledOnMS = 1;
261                n.ledOffMS = 0;
262                mNM.notify(1, n);
263            }
264        },
265
266        new Test("Lights off") {
267            public void run()
268            {
269                Notification n = new Notification();
270                n.flags |= Notification.FLAG_SHOW_LIGHTS;
271                n.ledARGB = 0x00000000;
272                n.ledOnMS = 0;
273                n.ledOffMS = 0;
274                mNM.notify(1, n);
275            }
276        },
277
278        new Test("Blue Blinking Slow") {
279            public void run()
280            {
281                Notification n = new Notification();
282                n.flags |= Notification.FLAG_SHOW_LIGHTS;
283                n.ledARGB = 0xff0000ff;
284                n.ledOnMS = 1300;
285                n.ledOffMS = 1300;
286                mNM.notify(1, n);
287            }
288        },
289
290        new Test("Blue Blinking Fast") {
291            public void run()
292            {
293                Notification n = new Notification();
294                n.flags |= Notification.FLAG_SHOW_LIGHTS;
295                n.ledARGB = 0xff0000ff;
296                n.ledOnMS = 300;
297                n.ledOffMS = 300;
298                mNM.notify(1, n);
299            }
300        },
301
302        new Test("Default All") {
303            public void run()
304            {
305                Notification n = new Notification();
306                n.defaults |= Notification.DEFAULT_ALL;
307                mNM.notify(1, n);
308            }
309        },
310
311        new Test("Default All, once") {
312            public void run()
313            {
314                Notification n = new Notification();
315                n.defaults |= Notification.DEFAULT_ALL;
316                n.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
317                mNM.notify(1, n);
318            }
319        },
320
321        new Test("Content Sound") {
322            public void run()
323            {
324                Notification n = new Notification();
325                n.sound = Uri.parse(
326                        "content://media/internal/audio/media/7");
327
328                mNM.notify(1, n);
329            }
330        },
331
332        new Test("Resource Sound") {
333            public void run()
334            {
335                Notification n = new Notification();
336                n.sound = Uri.parse(
337                        ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
338                        getPackageName() + "/raw/ringer");
339                Log.d(TAG, "n.sound=" + n.sound);
340
341                mNM.notify(1, n);
342            }
343        },
344
345        new Test("Sound and Cancel") {
346            public void run()
347            {
348                Notification n = new Notification();
349                n.sound = Uri.parse(
350                            "content://media/internal/audio/media/7");
351
352                mNM.notify(1, n);
353                SystemClock.sleep(200);
354                mNM.cancel(1);
355            }
356        },
357
358        new Test("Vibrate") {
359            public void run()
360            {
361                Notification n = new Notification();
362                    n.vibrate = new long[] { 0, 700, 500, 1000 };
363
364                mNM.notify(1, n);
365            }
366        },
367
368        new Test("Vibrate and cancel") {
369            public void run()
370            {
371                Notification n = new Notification();
372                    n.vibrate = new long[] { 0, 700, 500, 1000 };
373
374                mNM.notify(1, n);
375                SystemClock.sleep(500);
376                mNM.cancel(1);
377            }
378        },
379
380        new Test("Vibrate pattern") {
381            public void run()
382            {
383                mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
384            }
385        },
386
387        new Test("Vibrate pattern repeating") {
388            public void run()
389            {
390                mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
391            }
392        },
393
394        new Test("Vibrate 3s") {
395            public void run()
396            {
397                mVibrator.vibrate(3000);
398            }
399        },
400
401        new Test("Vibrate 100s") {
402            public void run()
403            {
404                mVibrator.vibrate(100000);
405            }
406        },
407
408        new Test("Vibrate off") {
409            public void run()
410            {
411                mVibrator.cancel();
412            }
413        },
414
415        new Test("Cancel #1") {
416            public void run() {
417                mNM.cancel(1);
418            }
419        },
420
421        new Test("Cancel #1 in 3 sec") {
422            public void run() {
423                mHandler.postDelayed(new Runnable() {
424                            public void run() {
425                                Log.d(TAG, "Cancelling now...");
426                                mNM.cancel(1);
427                            }
428                        }, 3000);
429            }
430        },
431
432        new Test("Cancel #2") {
433            public void run() {
434                mNM.cancel(2);
435            }
436        },
437
438        new Test("Persistent #1") {
439            public void run() {
440                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
441                        mActivityCreateTime);
442                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
443                            "This is a notification!!!", makeIntent());
444                mNM.notify(1, n);
445            }
446        },
447
448        new Test("Persistent #1 in 3 sec") {
449            public void run() {
450                mHandler.postDelayed(new Runnable() {
451                            public void run() {
452                                Notification n = new Notification(R.drawable.icon1,
453                                        "            "
454                                        + "tick tock tick tock\n\nSometimes notifications can "
455                                        + "be really long and wrap to more than one line.\n"
456                                        + "Sometimes."
457                                        + "Ohandwhathappensifwehaveonereallylongstringarewesure"
458                                        + "thatwesegmentitcorrectly?\n",
459                                        System.currentTimeMillis());
460                                n.setLatestEventInfo(NotificationTestList.this,
461                                        "Still Persistent #1",
462                                        "This is still a notification!!!",
463                                        makeIntent());
464                                mNM.notify(1, n);
465                            }
466                        }, 3000);
467            }
468        },
469
470        new Test("Persistent #2") {
471            public void run() {
472                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
473                        System.currentTimeMillis());
474                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
475                            "Notify me!!!", makeIntent());
476                mNM.notify(2, n);
477            }
478        },
479
480        new Test("Persistent #3") {
481            public void run() {
482                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
483                        System.currentTimeMillis());
484                n.setLatestEventInfo(NotificationTestList.this, "Persistent #3",
485                            "Notify me!!!", makeIntent());
486                mNM.notify(3, n);
487            }
488        },
489
490        new Test("Persistent #2 Vibrate") {
491            public void run() {
492                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
493                        System.currentTimeMillis());
494                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
495                            "Notify me!!!", makeIntent());
496                n.defaults = Notification.DEFAULT_VIBRATE;
497                mNM.notify(2, n);
498            }
499        },
500
501        new Test("Persistent #1 - different icon") {
502            public void run() {
503                Notification n = new Notification(R.drawable.icon2, null,
504                        mActivityCreateTime);
505                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
506                            "This is the same notification!!!", makeIntent());
507                mNM.notify(1, n);
508            }
509        },
510
511        new Test("Chronometer Start") {
512            public void run() {
513                Notification n = new Notification(R.drawable.icon2, "me me me me",
514                                                    System.currentTimeMillis());
515                n.contentView = new RemoteViews(getPackageName(), R.layout.chrono_notification);
516                mChronometerBase = SystemClock.elapsedRealtime();
517                n.contentView.setChronometer(R.id.time, mChronometerBase, "Yay! (%s)", true);
518                n.flags |= Notification.FLAG_ONGOING_EVENT;
519                n.contentIntent = makeIntent();
520                mNM.notify(2, n);
521            }
522        },
523
524        new Test("Chronometer Stop") {
525            public void run() {
526                mHandler.postDelayed(new Runnable() {
527                        public void run() {
528                            Log.d(TAG, "Chronometer Stop");
529                            Notification n = new Notification();
530                            n.icon = R.drawable.icon1;
531                            n.contentView = new RemoteViews(getPackageName(),
532                                                             R.layout.chrono_notification);
533                            n.contentView.setChronometer(R.id.time, mChronometerBase, null, false);
534                            n.contentIntent = makeIntent();
535                            mNM.notify(2, n);
536                        }
537                    }, 3000);
538            }
539        },
540
541        new Test("Sequential Persistent") {
542            public void run() {
543                mNM.notify(1, notificationWithNumbers(1));
544                mNM.notify(2, notificationWithNumbers(2));
545            }
546        },
547
548        new Test("Replace Persistent") {
549            public void run() {
550                mNM.notify(1, notificationWithNumbers(1));
551                mNM.notify(1, notificationWithNumbers(1));
552            }
553        },
554
555        new Test("Run and Cancel (n=1)") {
556            public void run() {
557                mNM.notify(1, notificationWithNumbers(1));
558                mNM.cancel(1);
559            }
560        },
561
562        new Test("Run an Cancel (n=2)") {
563            public void run() {
564                mNM.notify(1, notificationWithNumbers(1));
565                mNM.notify(2, notificationWithNumbers(2));
566                mNM.cancel(2);
567            }
568        },
569
570        // Repeatedly notify and cancel -- triggers bug #670627
571        new Test("Bug 670627") {
572            public void run() {
573                for (int i = 0; i < 10; i++) {
574                  Log.d(TAG, "Add two notifications");
575                  mNM.notify(1, notificationWithNumbers(1));
576                  mNM.notify(2, notificationWithNumbers(2));
577                  Log.d(TAG, "Cancel two notifications");
578                  mNM.cancel(1);
579                  mNM.cancel(2);
580                }
581            }
582        },
583
584        new Test("Ten Notifications") {
585            public void run() {
586                for (int i = 0; i < 2; i++) {
587                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
588                            null, System.currentTimeMillis(), "Persistent #" + i,
589                            "Notify me!!!" + i, null);
590                    n.flags |= Notification.FLAG_ONGOING_EVENT;
591                    n.number = i;
592                    mNM.notify((i+1)*10, n);
593                }
594                for (int i = 2; i < 10; i++) {
595                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
596                            null, System.currentTimeMillis(), "Persistent #" + i,
597                            "Notify me!!!" + i, null);
598                    n.number = i;
599                    mNM.notify((i+1)*10, n);
600                }
601            }
602        },
603
604        new Test("Cancel eight notifications") {
605            public void run() {
606                for (int i = 1; i < 9; i++) {
607                    mNM.cancel((i+1)*10);
608                }
609            }
610        },
611
612        new Test("Persistent with numbers 1") {
613            public void run() {
614                mNM.notify(1, notificationWithNumbers(1));
615            }
616        },
617
618        new Test("Persistent with numbers 222") {
619            public void run() {
620                mNM.notify(1, notificationWithNumbers(22));
621            }
622        },
623
624        new Test("Persistent with numbers 333") {
625            public void run() {
626                mNM.notify(1, notificationWithNumbers(333));
627            }
628        },
629
630        new Test("Persistent with numbers 4444") {
631            public void run() {
632                mNM.notify(1, notificationWithNumbers(4444));
633            }
634        },
635
636        new Test("Ticker") {
637            public void run() {
638                Notification not = new Notification(
639                    R.drawable.app_gmail,
640                    "New mail from joeo@example.com, on the topic of very long ticker texts",
641                    System.currentTimeMillis());
642                not.setLatestEventInfo(NotificationTestList.this,
643                    "A new message awaits",
644                    "The contents are very interesting and important",
645                    makeIntent());
646                mNM.notify(1, not);
647            }
648        },
649
650        new Test("Crash") {
651            public void run()
652            {
653                PowerManager.WakeLock wl
654                        = ((PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE))
655                            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
656                wl.acquire();
657                mHandler.postDelayed(new Runnable() {
658                            public void run() {
659                                throw new RuntimeException("Die!");
660                            }
661                        }, 10000);
662
663            }
664        },
665
666    };
667
668    private Notification notificationWithNumbers(int num) {
669        Notification n = new Notification(this, R.drawable.icon2, null, System.currentTimeMillis(),
670                "Persistent #2", "Notify me!!!", null);
671        n.number = num;
672        return n;
673    }
674
675    private PendingIntent makeIntent() {
676        Intent intent = new Intent(Intent.ACTION_MAIN);
677        intent.addCategory(Intent.CATEGORY_HOME);
678        return PendingIntent.getActivity(this, 0, intent, 0);
679    }
680
681    class StateStress extends Test {
682        StateStress(String name, int pause, int iterations, Runnable[] tasks) {
683            super(name);
684            mPause = pause;
685            mTasks = tasks;
686            mIteration = iterations;
687        }
688        Runnable[] mTasks;
689        int mNext;
690        int mIteration;
691        long mPause;
692        Runnable mRunnable = new Runnable() {
693            public void run() {
694                mTasks[mNext].run();
695                mNext++;
696                if (mNext >= mTasks.length) {
697                    mNext = 0;
698                    mIteration--;
699                    if (mIteration <= 0) {
700                        return;
701                    }
702                }
703                mHandler.postDelayed(mRunnable, mPause);
704            }
705        };
706        public void run() {
707            mNext = 0;
708            mHandler.postDelayed(mRunnable, mPause);
709        }
710    }
711
712    void timeNotification(int n, String label, long time) {
713        mNM.notify(n, new Notification(NotificationTestList.this,
714                    R.drawable.ic_statusbar_missedcall, null,
715                    time, label, "" + new java.util.Date(time), null));
716
717    }
718}
719
720