1 /* <lambda>null2 * Copyright (C) 2023 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 android.permission.cts 18 19 import android.permission.cts.TestUtils.ensure 20 import android.permission.cts.TestUtils.eventually 21 import android.service.notification.StatusBarNotification 22 import org.junit.Assert 23 24 object CtsNotificationListenerServiceUtils { 25 26 private const val NOTIFICATION_CANCELLATION_TIMEOUT_MILLIS = 5000L 27 private const val NOTIFICATION_WAIT_MILLIS = 2000L 28 29 @JvmStatic 30 fun assertEmptyNotification(packageName: String, notificationId: Int) { 31 ensure({ 32 Assert.assertNull( 33 "Expected no notification", 34 getNotification(packageName, notificationId)) 35 }, NOTIFICATION_WAIT_MILLIS) 36 } 37 38 @JvmStatic 39 fun assertNotificationExist(packageName: String, notificationId: Int) { 40 eventually({ 41 Assert.assertNotNull( 42 "Expected notification, none found", 43 getNotification(packageName, notificationId)) 44 }, NOTIFICATION_WAIT_MILLIS) 45 } 46 47 @JvmStatic 48 fun cancelNotification(packageName: String, notificationId: Int) { 49 val notificationService = CtsNotificationListenerService.getInstance() 50 val notification = getNotification(packageName, notificationId) 51 if (notification != null) { 52 notificationService.cancelNotification(notification.key) 53 eventually({ 54 Assert.assertTrue(getNotification(packageName, notificationId) == null) 55 }, NOTIFICATION_CANCELLATION_TIMEOUT_MILLIS) 56 } 57 } 58 59 @JvmStatic 60 fun cancelNotifications(packageName: String) { 61 val notificationService = CtsNotificationListenerService.getInstance() 62 val notifications = getNotifications(packageName) 63 if (notifications.isNotEmpty()) { 64 notifications.forEach { notification -> 65 notificationService.cancelNotification(notification.key) 66 } 67 eventually({ 68 Assert.assertTrue(getNotifications(packageName).isEmpty()) 69 }, NOTIFICATION_CANCELLATION_TIMEOUT_MILLIS) 70 } 71 } 72 73 @JvmStatic 74 fun getNotification(packageName: String, notificationId: Int): StatusBarNotification? { 75 return getNotifications(packageName).firstOrNull { 76 it.id == notificationId 77 } 78 } 79 80 @JvmStatic 81 fun getNotifications(packageName: String): List<StatusBarNotification> { 82 val notifications: MutableList<StatusBarNotification> = ArrayList() 83 val notificationService = CtsNotificationListenerService.getInstance() 84 for (notification in notificationService.activeNotifications) { 85 if (notification.packageName == packageName) { 86 notifications.add(notification) 87 } 88 } 89 return notifications 90 } 91 92 /** 93 * Get a notification listener notification that is currently visible. 94 * 95 * @param cancelNotification if `true` the notification is canceled inside this method 96 * @return The notification or `null` if there is none 97 */ 98 @JvmStatic 99 @Throws(Throwable::class) 100 fun getNotificationForPackageAndId( 101 pkg: String, 102 id: Int, 103 cancelNotification: Boolean 104 ): StatusBarNotification? { 105 val notifications: List<StatusBarNotification> = getNotifications(pkg) 106 if (notifications.isEmpty()) { 107 return null 108 } 109 for (notification in notifications) { 110 if (notification.id == id) { 111 if (cancelNotification) { 112 cancelNotification(pkg, id) 113 } 114 return notification 115 } 116 } 117 return null 118 } 119 } 120