1 package com.android.cts.managedprofile; 2 3 import android.annotation.NonNull; 4 import android.app.Notification; 5 import android.os.Bundle; 6 import android.test.AndroidTestCase; 7 import android.text.TextUtils; 8 9 import androidx.test.InstrumentationRegistry; 10 11 import com.android.compatibility.common.util.SystemUtil; 12 import com.android.internal.notification.SystemNotificationChannels; 13 14 import java.util.concurrent.CountDownLatch; 15 import java.util.concurrent.TimeUnit; 16 17 /** 18 * Test wipeDataWithReason() has indeed shown the notification. 19 * The function wipeDataWithReason() is called and executed in another test. 20 */ 21 public class WipeDataNotificationTest extends AndroidTestCase { 22 23 private static final String WIPE_DATA_TITLE = "Work profile deleted"; 24 private static final int NOTIF_TIMEOUT_SECONDS = 5; 25 testWipeDataWithReasonVerification()26 public void testWipeDataWithReasonVerification() throws Exception { 27 final int profileId = getProfileId(); 28 assertTrue("Invalid profileId", profileId > 0); 29 30 CountDownLatch notificationLatch = initWipeNotificationLatch(WIPE_DATA_TITLE, 31 WipeDataReceiver.TEST_WIPE_DATA_REASON); 32 sendWipeProfileBroadcast(WipeDataReceiver.ACTION_WIPE_DATA_WITH_REASON, profileId); 33 34 assertTrue("Did not receive notification for profile wipe", 35 notificationLatch.await(NOTIF_TIMEOUT_SECONDS, TimeUnit.SECONDS)); 36 NotificationListener.getInstance().clearListeners(); 37 } 38 testWipeDataWithEmptyReasonVerification()39 public void testWipeDataWithEmptyReasonVerification() throws Exception { 40 final int profileId = getProfileId(); 41 assertTrue("Invalid profileId", profileId > 0); 42 43 // if reason is empty, DPM will fill it with internal strings: ignore 44 CountDownLatch notificationLatch = initWipeNotificationLatch(WIPE_DATA_TITLE, ""); 45 sendWipeProfileBroadcast(WipeDataReceiver.ACTION_WIPE_DATA, profileId); 46 47 assertTrue("Did not receive notification for profile wipe", 48 notificationLatch.await(NOTIF_TIMEOUT_SECONDS, TimeUnit.SECONDS)); 49 NotificationListener.getInstance().clearListeners(); 50 } 51 testWipeDataWithoutReasonVerification()52 public void testWipeDataWithoutReasonVerification() throws Exception { 53 int profileId = getProfileId(); 54 assertTrue("Invalid profileId", profileId > 0); 55 56 CountDownLatch notificationLatch = initWipeNotificationLatch("", ""); 57 sendWipeProfileBroadcast(WipeDataReceiver.ACTION_WIPE_DATA_WITHOUT_REASON, profileId); 58 59 assertFalse("Wipe notification found", 60 notificationLatch.await(NOTIF_TIMEOUT_SECONDS, TimeUnit.SECONDS)); 61 NotificationListener.getInstance().clearListeners(); 62 } 63 getProfileId()64 private int getProfileId() { 65 int profileId = -1; 66 final Bundle testArguments = InstrumentationRegistry.getArguments(); 67 if (testArguments.containsKey("profileId")) { 68 profileId = Integer.parseInt(testArguments.getString("profileId")); 69 } 70 return profileId; 71 } 72 initWipeNotificationLatch(@onNull String notificationTitle, @NonNull String notificationText)73 private CountDownLatch initWipeNotificationLatch(@NonNull String notificationTitle, 74 @NonNull String notificationText) { 75 CountDownLatch notificationCounterLatch = new CountDownLatch(1); 76 NotificationListener.getInstance().addListener((notification) -> { 77 if (notification.getNotification().getChannelId().equals( 78 SystemNotificationChannels.DEVICE_ADMIN)) { 79 assertEquals("Wipe notification title not found", notificationTitle, 80 notification.getNotification().extras.getString(Notification.EXTRA_TITLE)); 81 // do not check reason text if param is empty == ignore notif. text 82 if (!TextUtils.isEmpty(notificationText)) { 83 assertEquals("Wipe notification content not found", notificationText, 84 notification.getNotification().extras.getString( 85 Notification.EXTRA_TEXT)); 86 } 87 notificationCounterLatch.countDown(); 88 } 89 }); 90 return notificationCounterLatch; 91 } 92 sendWipeProfileBroadcast(String action, int profileId)93 private void sendWipeProfileBroadcast(String action, int profileId) throws Exception { 94 final String cmd = "am broadcast --receiver-foreground --user " + profileId 95 + " -a " + action 96 + " com.android.cts.managedprofile/.WipeDataReceiver"; 97 SystemUtil.runShellCommand(cmd); 98 } 99 } 100