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             {
33                 Assert.assertNull(
34                     "Expected no notification",
35                     getNotification(packageName, notificationId)
36                 )
37             },
38             NOTIFICATION_WAIT_MILLIS
39         )
40     }
41 
42     @JvmStatic
43     fun assertNotificationExist(packageName: String, notificationId: Int) {
44         eventually(
45             {
46                 Assert.assertNotNull(
47                     "Expected notification, none found",
48                     getNotification(packageName, notificationId)
49                 )
50             },
51             NOTIFICATION_WAIT_MILLIS
52         )
53     }
54 
55     @JvmStatic
56     fun cancelNotification(packageName: String, notificationId: Int) {
57         val notificationService = CtsNotificationListenerService.getInstance()
58         val notification = getNotification(packageName, notificationId)
59         if (notification != null) {
60             notificationService.cancelNotification(notification.key)
61             eventually(
62                 { Assert.assertTrue(getNotification(packageName, notificationId) == null) },
63                 NOTIFICATION_CANCELLATION_TIMEOUT_MILLIS
64             )
65         }
66     }
67 
68     @JvmStatic
69     fun cancelNotifications(packageName: String) {
70         val notificationService = CtsNotificationListenerService.getInstance()
71         val notifications = getNotifications(packageName)
72         if (notifications.isNotEmpty()) {
73             notifications.forEach { notification ->
74                 notificationService.cancelNotification(notification.key)
75             }
76             eventually(
77                 { Assert.assertTrue(getNotifications(packageName).isEmpty()) },
78                 NOTIFICATION_CANCELLATION_TIMEOUT_MILLIS
79             )
80         }
81     }
82 
83     @JvmStatic
84     fun getNotification(packageName: String, notificationId: Int): StatusBarNotification? {
85         return getNotifications(packageName).firstOrNull { it.id == notificationId }
86     }
87 
88     @JvmStatic
89     fun getNotifications(packageName: String): List<StatusBarNotification> {
90         val notifications: MutableList<StatusBarNotification> = ArrayList()
91         val notificationService = CtsNotificationListenerService.getInstance()
92         for (notification in notificationService.activeNotifications) {
93             if (notification.packageName == packageName) {
94                 notifications.add(notification)
95             }
96         }
97         return notifications
98     }
99 
100     /**
101      * Get a notification listener notification that is currently visible.
102      *
103      * @param cancelNotification if `true` the notification is canceled inside this method
104      * @return The notification or `null` if there is none
105      */
106     @JvmStatic
107     @Throws(Throwable::class)
108     fun getNotificationForPackageAndId(
109         pkg: String,
110         id: Int,
111         cancelNotification: Boolean
112     ): StatusBarNotification? {
113         val notifications: List<StatusBarNotification> = getNotifications(pkg)
114         if (notifications.isEmpty()) {
115             return null
116         }
117         for (notification in notifications) {
118             if (notification.id == id) {
119                 if (cancelNotification) {
120                     cancelNotification(pkg, id)
121                 }
122                 return notification
123             }
124         }
125         return null
126     }
127 }
128