179179import com .android .server .wm .ActivityTaskManagerInternal ;
180180import com .android .server .wm .WindowManagerInternal ;
181181
182+ import com .google .common .collect .ImmutableList ;
183+
182184import org .junit .After ;
183185import org .junit .Before ;
184186import org .junit .Test ;
202204import java .util .List ;
203205import java .util .Map ;
204206import java .util .Set ;
207+ import java .util .concurrent .CountDownLatch ;
205208import java .util .function .Consumer ;
206209
207210
@@ -245,6 +248,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase {
245248 @ Mock
246249 Resources mResources ;
247250 @ Mock
251+ ActivityManagerInternal mAmi ;
252+ @ Mock
248253 RankingHandler mRankingHandler ;
249254 @ Mock
250255 protected PackageManagerInternal mPackageManagerInternal ;
@@ -418,7 +423,6 @@ public void setUp() throws Exception {
418423
419424 DeviceIdleInternal deviceIdleInternal = mock (DeviceIdleInternal .class );
420425 when (deviceIdleInternal .getNotificationWhitelistDuration ()).thenReturn (3000L );
421- ActivityManagerInternal activityManagerInternal = mock (ActivityManagerInternal .class );
422426
423427 LocalServices .removeServiceForTest (UriGrantsManagerInternal .class );
424428 LocalServices .addService (UriGrantsManagerInternal .class , mUgmInternal );
@@ -429,7 +433,7 @@ public void setUp() throws Exception {
429433 LocalServices .removeServiceForTest (DeviceIdleInternal .class );
430434 LocalServices .addService (DeviceIdleInternal .class , deviceIdleInternal );
431435 LocalServices .removeServiceForTest (ActivityManagerInternal .class );
432- LocalServices .addService (ActivityManagerInternal .class , activityManagerInternal );
436+ LocalServices .addService (ActivityManagerInternal .class , mAmi );
433437 mContext .addMockSystemService (Context .ALARM_SERVICE , mAlarmManager );
434438
435439
@@ -506,7 +510,7 @@ null, new ComponentName(PKG, "test_class"),
506510 mock (DevicePolicyManagerInternal .class ), mUgm , mUgmInternal ,
507511 mAppOpsManager , mUm , mHistoryManager , mStatsManager ,
508512 mock (TelephonyManager .class ),
509- mock ( ActivityManagerInternal . class ) );
513+ mAmi );
510514 mService .onBootPhase (SystemService .PHASE_SYSTEM_SERVICES_READY );
511515
512516 mService .setAudioManager (mAudioManager );
@@ -2473,7 +2477,8 @@ public void testDeleteChannelGroupNotifyListener() throws Exception {
24732477 .thenReturn (associations );
24742478 NotificationChannelGroup ncg = new NotificationChannelGroup ("a" , "b/c" );
24752479 mService .setPreferencesHelper (mPreferencesHelper );
2476- when (mPreferencesHelper .getNotificationChannelGroup (eq (ncg .getId ()), eq (PKG ), anyInt ()))
2480+ when (mPreferencesHelper .getNotificationChannelGroupWithChannels (
2481+ eq (PKG ), anyInt (), eq (ncg .getId ()), anyBoolean ()))
24772482 .thenReturn (ncg );
24782483 reset (mListeners );
24792484 mBinderService .deleteNotificationChannelGroup (PKG , ncg .getId ());
@@ -2482,6 +2487,56 @@ public void testDeleteChannelGroupNotifyListener() throws Exception {
24822487 eq (NotificationListenerService .NOTIFICATION_CHANNEL_OR_GROUP_DELETED ));
24832488 }
24842489
2490+ @ Test
2491+ public void testDeleteChannelGroupChecksForFgses () throws Exception {
2492+ List <String > associations = new ArrayList <>();
2493+ associations .add ("a" );
2494+ when (mCompanionMgr .getAssociations (PKG , UserHandle .getUserId (mUid )))
2495+ .thenReturn (associations );
2496+ CountDownLatch latch = new CountDownLatch (2 );
2497+ mService .createNotificationChannelGroup (
2498+ PKG , mUid , new NotificationChannelGroup ("group" , "group" ), true , false );
2499+ new Thread (() -> {
2500+ NotificationChannel notificationChannel = new NotificationChannel ("id" , "id" ,
2501+ NotificationManager .IMPORTANCE_HIGH );
2502+ notificationChannel .setGroup ("group" );
2503+ ParceledListSlice <NotificationChannel > pls =
2504+ new ParceledListSlice (ImmutableList .of (notificationChannel ));
2505+ try {
2506+ mBinderService .createNotificationChannelsForPackage (PKG , mUid , pls );
2507+ } catch (RemoteException e ) {
2508+ throw new RuntimeException (e );
2509+ }
2510+ latch .countDown ();
2511+ }).start ();
2512+ new Thread (() -> {
2513+ try {
2514+ synchronized (this ) {
2515+ wait (5000 );
2516+ }
2517+ mService .createNotificationChannelGroup (PKG , mUid ,
2518+ new NotificationChannelGroup ("new" , "new group" ), true , false );
2519+ NotificationChannel notificationChannel =
2520+ new NotificationChannel ("id" , "id" , NotificationManager .IMPORTANCE_HIGH );
2521+ notificationChannel .setGroup ("new" );
2522+ ParceledListSlice <NotificationChannel > pls =
2523+ new ParceledListSlice (ImmutableList .of (notificationChannel ));
2524+ try {
2525+ mBinderService .createNotificationChannelsForPackage (PKG , mUid , pls );
2526+ mBinderService .deleteNotificationChannelGroup (PKG , "group" );
2527+ } catch (RemoteException e ) {
2528+ throw new RuntimeException (e );
2529+ }
2530+ } catch (Exception e ) {
2531+ e .printStackTrace ();
2532+ }
2533+ latch .countDown ();
2534+ }).start ();
2535+
2536+ latch .await ();
2537+ verify (mAmi ).hasForegroundServiceNotification (anyString (), anyInt (), anyString ());
2538+ }
2539+
24852540 @ Test
24862541 public void testUpdateNotificationChannelFromPrivilegedListener_success () throws Exception {
24872542 mService .setPreferencesHelper (mPreferencesHelper );
0 commit comments