1 /* 2 * Copyright (C) 2022 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.safetycenter.functional.testing 18 19 import android.app.Notification 20 21 /** The characteristic properties of a notification. */ 22 data class NotificationCharacteristics( 23 val title: String, 24 val text: String, 25 val actions: List<CharSequence> = emptyList(), 26 val importance: Int = IMPORTANCE_ANY, 27 val blockable: Boolean? = null 28 ) { 29 companion object { 30 const val IMPORTANCE_ANY = -1 31 importanceMatchesnull32 private fun importanceMatches( 33 statusBarNotificationWithChannel: StatusBarNotificationWithChannel, 34 characteristicImportance: Int 35 ): Boolean { 36 return characteristicImportance == IMPORTANCE_ANY || 37 statusBarNotificationWithChannel.channel.importance == characteristicImportance 38 } 39 blockableMatchesnull40 private fun blockableMatches( 41 statusBarNotificationWithChannel: StatusBarNotificationWithChannel, 42 characteristicBlockable: Boolean? 43 ): Boolean { 44 return characteristicBlockable == null || 45 statusBarNotificationWithChannel.channel.isBlockable == characteristicBlockable 46 } 47 isMatchnull48 private fun isMatch( 49 statusBarNotificationWithChannel: StatusBarNotificationWithChannel, 50 characteristic: NotificationCharacteristics 51 ): Boolean { 52 val notif = statusBarNotificationWithChannel.statusBarNotification.notification 53 return notif != null && 54 notif.extras.getString(Notification.EXTRA_TITLE) == characteristic.title && 55 notif.extras.getString(Notification.EXTRA_TEXT).orEmpty() == characteristic.text && 56 notif.actions.orEmpty().map { it.title } == characteristic.actions && 57 importanceMatches(statusBarNotificationWithChannel, characteristic.importance) && 58 blockableMatches(statusBarNotificationWithChannel, characteristic.blockable) 59 } 60 areMatchingnull61 fun areMatching( 62 statusBarNotifications: List<StatusBarNotificationWithChannel>, 63 characteristics: List<NotificationCharacteristics> 64 ): Boolean { 65 if (statusBarNotifications.size != characteristics.size) { 66 return false 67 } 68 return statusBarNotifications.zip(characteristics).all { isMatch(it.first, it.second) } 69 } 70 } 71 } 72