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