Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
| 21 | |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 22 | import static com.android.server.notification.NotificationIntrusivenessExtractor.HANG_TIME_MS; |
| 23 | |
| 24 | import static junit.framework.Assert.assertFalse; |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 25 | import static junit.framework.Assert.assertNotNull; |
| 26 | import static junit.framework.Assert.assertNull; |
| 27 | |
| 28 | import android.app.Notification; |
| 29 | import android.app.NotificationChannel; |
| 30 | import android.app.PendingIntent; |
| 31 | import android.content.Intent; |
| 32 | import android.os.UserHandle; |
| 33 | import android.service.notification.StatusBarNotification; |
| 34 | |
| 35 | import org.junit.Test; |
| 36 | |
| 37 | public class NotificationIntrusivenessExtractorTest extends NotificationTestCase { |
| 38 | |
| 39 | @Test |
| 40 | public void testNonIntrusive() { |
| 41 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW); |
| 42 | final Notification.Builder builder = new Notification.Builder(getContext()) |
| 43 | .setContentTitle("foo") |
| 44 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 45 | |
| 46 | Notification n = builder.build(); |
| 47 | StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0, |
| 48 | 0, n, UserHandle.ALL, null, System.currentTimeMillis()); |
| 49 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
| 50 | |
| 51 | assertNull(new NotificationIntrusivenessExtractor().process(r)); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testIntrusive_fillScreen() { |
| 56 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 57 | final Notification.Builder builder = new Notification.Builder(getContext()) |
| 58 | .setContentTitle("foo") |
| 59 | .setFullScreenIntent(PendingIntent.getActivity( |
| 60 | getContext(), 0, new Intent(""), 0), true) |
| 61 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 62 | |
| 63 | Notification n = builder.build(); |
| 64 | StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0, |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 65 | 0, n, UserHandle.ALL, null, |
| 66 | System.currentTimeMillis()); |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 67 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
| 68 | |
| 69 | assertNotNull(new NotificationIntrusivenessExtractor().process(r)); |
| 70 | } |
Julia Reynolds | bbc469c | 2017-07-19 13:48:07 -0400 | [diff] [blame] | 71 | |
| 72 | @Test |
| 73 | public void testOldNotificationsNotIntrusive() { |
| 74 | NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_DEFAULT); |
| 75 | final Notification.Builder builder = new Notification.Builder(getContext()) |
| 76 | .setContentTitle("foo") |
| 77 | .setFullScreenIntent(PendingIntent.getActivity( |
| 78 | getContext(), 0, new Intent(""), 0), true) |
| 79 | .setSmallIcon(android.R.drawable.sym_def_app_icon); |
| 80 | |
| 81 | Notification n = builder.build(); |
| 82 | StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0, |
| 83 | 0, n, UserHandle.ALL, null, |
| 84 | System.currentTimeMillis() - HANG_TIME_MS); |
| 85 | |
| 86 | NotificationRecord r = new NotificationRecord(getContext(), sbn, channel); |
| 87 | assertNull(new NotificationIntrusivenessExtractor().process(r)); |
| 88 | assertFalse(r.isRecentlyIntrusive()); |
| 89 | } |
Julia Reynolds | eb3dca7 | 2017-07-11 10:39:58 -0400 | [diff] [blame] | 90 | } |