1 /* 2 * Copyright (C) 2021 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.car.notification; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.when; 24 25 import android.app.Notification; 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.PackageInfo; 28 import android.content.pm.PackageManager; 29 import android.os.Bundle; 30 import android.service.notification.StatusBarNotification; 31 import android.testing.TestableContext; 32 33 import androidx.test.ext.junit.runners.AndroidJUnit4; 34 import androidx.test.platform.app.InstrumentationRegistry; 35 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 43 import java.util.HashMap; 44 import java.util.Map; 45 46 @RunWith(AndroidJUnit4.class) 47 public class NotificationUtilsTest { 48 @Rule 49 public final TestableContext mContext = new TestableContext( 50 InstrumentationRegistry.getInstrumentation().getTargetContext()); 51 52 private AlertEntry mAlertEntry; 53 54 @Mock 55 private StatusBarNotification mStatusBarNotification; 56 @Mock 57 private PackageManager mPackageManager; 58 59 @Before setup()60 public void setup() { 61 MockitoAnnotations.initMocks(this); 62 when(mStatusBarNotification.getKey()).thenReturn("TEST_KEY"); 63 when(mStatusBarNotification.getPackageName()).thenReturn("TEST_PACKAGE_NAME"); 64 mAlertEntry = new AlertEntry(mStatusBarNotification); 65 mContext.setMockPackageManager(mPackageManager); 66 } 67 68 @Test onIsSystemPrivilegedOrPlatformKey_isPlatformKey_returnsTrue()69 public void onIsSystemPrivilegedOrPlatformKey_isPlatformKey_returnsTrue() 70 throws PackageManager.NameNotFoundException { 71 setApplicationInfo(/* signedWithPlatformKey= */ true, /* isSystemApp= */ 72 false, /* isPrivilegedApp= */ false); 73 74 assertThat(NotificationUtils.isSystemPrivilegedOrPlatformKey(mContext, mAlertEntry)) 75 .isTrue(); 76 } 77 78 @Test onIsSystemPrivilegedOrPlatformKey_isSystemPrivileged_returnsTrue()79 public void onIsSystemPrivilegedOrPlatformKey_isSystemPrivileged_returnsTrue() 80 throws PackageManager.NameNotFoundException { 81 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 82 true, /* isPrivilegedApp= */ true); 83 84 assertThat(NotificationUtils.isSystemPrivilegedOrPlatformKey(mContext, mAlertEntry)) 85 .isTrue(); 86 } 87 88 @Test onIsSystemPrivilegedOrPlatformKey_isSystemNotPrivileged_returnsFalse()89 public void onIsSystemPrivilegedOrPlatformKey_isSystemNotPrivileged_returnsFalse() 90 throws PackageManager.NameNotFoundException { 91 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 92 true, /* isPrivilegedApp= */ false); 93 94 assertThat(NotificationUtils.isSystemPrivilegedOrPlatformKey(mContext, mAlertEntry)) 95 .isFalse(); 96 } 97 98 @Test onIsSystemPrivilegedOrPlatformKey_isNeither_returnsFalse()99 public void onIsSystemPrivilegedOrPlatformKey_isNeither_returnsFalse() 100 throws PackageManager.NameNotFoundException { 101 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 102 false, /* isPrivilegedApp= */ false); 103 104 assertThat(NotificationUtils.isSystemPrivilegedOrPlatformKey(mContext, mAlertEntry)) 105 .isFalse(); 106 } 107 108 @Test onIsSystemOrPlatformKey_isPlatformKey_returnsTrue()109 public void onIsSystemOrPlatformKey_isPlatformKey_returnsTrue() 110 throws PackageManager.NameNotFoundException { 111 setApplicationInfo(/* signedWithPlatformKey= */ true, /* isSystemApp= */ 112 false, /* isPrivilegedApp= */ false); 113 114 assertThat(NotificationUtils.isSystemOrPlatformKey(mContext, mAlertEntry)) 115 .isTrue(); 116 } 117 118 @Test onIsSystemOrPlatformKey_isSystemPrivileged_returnsTrue()119 public void onIsSystemOrPlatformKey_isSystemPrivileged_returnsTrue() 120 throws PackageManager.NameNotFoundException { 121 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 122 true, /* isPrivilegedApp= */ true); 123 124 assertThat(NotificationUtils.isSystemOrPlatformKey(mContext, mAlertEntry)) 125 .isTrue(); 126 } 127 128 @Test onIsSystemOrPlatformKey_isSystemNotPrivileged_returnsTrue()129 public void onIsSystemOrPlatformKey_isSystemNotPrivileged_returnsTrue() 130 throws PackageManager.NameNotFoundException { 131 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 132 true, /* isPrivilegedApp= */ false); 133 134 assertThat(NotificationUtils.isSystemOrPlatformKey(mContext, mAlertEntry)) 135 .isTrue(); 136 } 137 138 @Test onIsSystemOrPlatformKey_isNeither_returnsFalse()139 public void onIsSystemOrPlatformKey_isNeither_returnsFalse() 140 throws PackageManager.NameNotFoundException { 141 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 142 false, /* isPrivilegedApp= */ false); 143 144 assertThat(NotificationUtils.isSystemOrPlatformKey(mContext, mAlertEntry)) 145 .isFalse(); 146 } 147 148 @Test onIsSystemApp_isPlatformKey_returnsTrue()149 public void onIsSystemApp_isPlatformKey_returnsTrue() 150 throws PackageManager.NameNotFoundException { 151 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 152 true, /* isPrivilegedApp= */ false); 153 154 assertThat(NotificationUtils.isSystemApp(mContext, mAlertEntry.getStatusBarNotification())) 155 .isTrue(); 156 } 157 158 @Test onIsSystemApp_isNotPlatformKey_returnsFalse()159 public void onIsSystemApp_isNotPlatformKey_returnsFalse() 160 throws PackageManager.NameNotFoundException { 161 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 162 false, /* isPrivilegedApp= */ false); 163 164 assertThat(NotificationUtils.isSystemApp(mContext, mAlertEntry.getStatusBarNotification())) 165 .isFalse(); 166 } 167 168 @Test onIsSignedWithPlatformKey_isSignedWithPlatformKey_returnsTrue()169 public void onIsSignedWithPlatformKey_isSignedWithPlatformKey_returnsTrue() 170 throws PackageManager.NameNotFoundException { 171 setApplicationInfo(/* signedWithPlatformKey= */ true, /* isSystemApp= */ 172 false, /* isPrivilegedApp= */ false); 173 174 assertThat(NotificationUtils.isSignedWithPlatformKey(mContext, 175 mAlertEntry.getStatusBarNotification())) 176 .isTrue(); 177 } 178 179 @Test onIsSignedWithPlatformKey_isNotSignedWithPlatformKey_returnsFalse()180 public void onIsSignedWithPlatformKey_isNotSignedWithPlatformKey_returnsFalse() 181 throws PackageManager.NameNotFoundException { 182 setApplicationInfo(/* signedWithPlatformKey= */ false, /* isSystemApp= */ 183 false, /* isPrivilegedApp= */ false); 184 185 assertThat(NotificationUtils.isSignedWithPlatformKey(mContext, 186 mAlertEntry.getStatusBarNotification())) 187 .isFalse(); 188 } 189 190 @Test onGetNotificationViewType_notificationIsARecognizedType_returnsCorrectType()191 public void onGetNotificationViewType_notificationIsARecognizedType_returnsCorrectType() { 192 Map<String, CarNotificationTypeItem> typeMap = new HashMap<>(); 193 typeMap.put(Notification.CATEGORY_CAR_EMERGENCY, CarNotificationTypeItem.EMERGENCY); 194 typeMap.put(Notification.CATEGORY_NAVIGATION, CarNotificationTypeItem.NAVIGATION); 195 typeMap.put(Notification.CATEGORY_CALL, CarNotificationTypeItem.CALL); 196 typeMap.put(Notification.CATEGORY_CAR_WARNING, CarNotificationTypeItem.WARNING); 197 typeMap.put(Notification.CATEGORY_CAR_INFORMATION, CarNotificationTypeItem.INFORMATION); 198 typeMap.put(Notification.CATEGORY_MESSAGE, CarNotificationTypeItem.MESSAGE); 199 200 typeMap.forEach((category, typeItem) -> { 201 Notification notification = new Notification(); 202 notification.category = category; 203 when(mStatusBarNotification.getNotification()).thenReturn(notification); 204 AlertEntry alertEntry = new AlertEntry(mStatusBarNotification); 205 assertThat(NotificationUtils.getNotificationViewType(alertEntry)).isEqualTo(typeItem); 206 }); 207 } 208 209 @Test onGetNotificationViewType_notificationHasBigTextAndSummaryText_returnsInbox()210 public void onGetNotificationViewType_notificationHasBigTextAndSummaryText_returnsInbox() { 211 Bundle extras = new Bundle(); 212 extras.putBoolean(Notification.EXTRA_BIG_TEXT, true); 213 extras.putBoolean(Notification.EXTRA_SUMMARY_TEXT, true); 214 215 Notification notification = new Notification(); 216 notification.extras = extras; 217 when(mStatusBarNotification.getNotification()).thenReturn(notification); 218 AlertEntry alertEntry = new AlertEntry(mStatusBarNotification); 219 220 assertThat(NotificationUtils.getNotificationViewType(alertEntry)).isEqualTo( 221 CarNotificationTypeItem.INBOX); 222 } 223 224 @Test onGetNotificationViewType_unrecognizedTypeWithoutBigTextOrSummary_returnsBasic()225 public void onGetNotificationViewType_unrecognizedTypeWithoutBigTextOrSummary_returnsBasic() { 226 Notification notification = new Notification(); 227 when(mStatusBarNotification.getNotification()).thenReturn(notification); 228 AlertEntry alertEntry = new AlertEntry(mStatusBarNotification); 229 230 assertThat(NotificationUtils.getNotificationViewType(alertEntry)).isEqualTo( 231 CarNotificationTypeItem.BASIC); 232 } 233 setApplicationInfo(boolean signedWithPlatformKey, boolean isSystemApp, boolean isPrivilegedApp)234 private void setApplicationInfo(boolean signedWithPlatformKey, boolean isSystemApp, 235 boolean isPrivilegedApp) throws PackageManager.NameNotFoundException { 236 ApplicationInfo applicationInfo = new ApplicationInfo(); 237 238 if (signedWithPlatformKey) { 239 applicationInfo.privateFlags = applicationInfo.privateFlags 240 | ApplicationInfo.PRIVATE_FLAG_SIGNED_WITH_PLATFORM_KEY; 241 } 242 243 if (isSystemApp) { 244 applicationInfo.flags = applicationInfo.flags 245 | ApplicationInfo.FLAG_SYSTEM; 246 } 247 248 if (isPrivilegedApp) { 249 applicationInfo.privateFlags = applicationInfo.privateFlags 250 | ApplicationInfo.PRIVATE_FLAG_PRIVILEGED; 251 } 252 253 PackageInfo packageInfo = new PackageInfo(); 254 packageInfo.applicationInfo = applicationInfo; 255 when(mPackageManager.getPackageInfoAsUser(anyString(), anyInt(), anyInt())).thenReturn( 256 packageInfo); 257 } 258 } 259