1 package com.android.systemui.screenshot 2 3 import android.graphics.drawable.Drawable 4 import android.os.UserHandle 5 import android.platform.test.annotations.DisableFlags 6 import android.platform.test.annotations.EnableFlags 7 import android.testing.AndroidTestingRunner 8 import android.view.View 9 import android.view.ViewGroup 10 import android.widget.FrameLayout 11 import androidx.constraintlayout.widget.ConstraintLayout 12 import androidx.constraintlayout.widget.Guideline 13 import androidx.test.filters.SmallTest 14 import com.android.systemui.SysuiTestCase 15 import com.android.systemui.screenshot.message.LabeledIcon 16 import com.android.systemui.screenshot.message.ProfileMessageController 17 import com.android.systemui.util.mockito.any 18 import com.android.systemui.util.mockito.eq 19 import com.android.systemui.util.mockito.whenever 20 import junit.framework.Assert.assertEquals 21 import kotlinx.coroutines.ExperimentalCoroutinesApi 22 import kotlinx.coroutines.test.TestScope 23 import kotlinx.coroutines.test.UnconfinedTestDispatcher 24 import kotlinx.coroutines.test.runTest 25 import org.junit.Before 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 import org.mockito.Mock 29 import org.mockito.Mockito.never 30 import org.mockito.Mockito.verify 31 import org.mockito.MockitoAnnotations 32 33 @SmallTest 34 @RunWith(AndroidTestingRunner::class) 35 class MessageContainerControllerTest : SysuiTestCase() { 36 lateinit var messageContainer: MessageContainerController 37 38 @Mock lateinit var workProfileMessageController: WorkProfileMessageController 39 @Mock lateinit var profileMessageController: ProfileMessageController 40 41 @Mock lateinit var screenshotDetectionController: ScreenshotDetectionController 42 43 @Mock lateinit var icon: Drawable 44 45 lateinit var workProfileFirstRunView: ViewGroup 46 lateinit var detectionNoticeView: ViewGroup 47 lateinit var container: FrameLayout 48 49 lateinit var screenshotView: ViewGroup 50 51 val userHandle = UserHandle.of(5) 52 val screenshotData = ScreenshotData.forTesting() 53 54 val appName = "app name" 55 lateinit var workProfileData: WorkProfileMessageController.WorkProfileFirstRunData 56 57 @Before 58 @ExperimentalCoroutinesApi setupnull59 fun setup() { 60 MockitoAnnotations.initMocks(this) 61 messageContainer = 62 MessageContainerController( 63 workProfileMessageController, 64 profileMessageController, 65 screenshotDetectionController, 66 TestScope(UnconfinedTestDispatcher()) 67 ) 68 screenshotView = ConstraintLayout(mContext) 69 workProfileData = WorkProfileMessageController.WorkProfileFirstRunData(appName, icon) 70 71 val guideline = Guideline(mContext) 72 guideline.id = com.android.systemui.res.R.id.guideline 73 screenshotView.addView(guideline) 74 75 container = FrameLayout(mContext) 76 container.id = com.android.systemui.res.R.id.screenshot_message_container 77 screenshotView.addView(container) 78 79 workProfileFirstRunView = FrameLayout(mContext) 80 workProfileFirstRunView.id = com.android.systemui.res.R.id.work_profile_first_run 81 container.addView(workProfileFirstRunView) 82 83 detectionNoticeView = FrameLayout(mContext) 84 detectionNoticeView.id = com.android.systemui.res.R.id.screenshot_detection_notice 85 container.addView(detectionNoticeView) 86 87 messageContainer.setView(screenshotView) 88 89 screenshotData.userHandle = userHandle 90 } 91 92 @Test 93 @DisableFlags(com.android.systemui.Flags.FLAG_SCREENSHOT_PRIVATE_PROFILE_BEHAVIOR_FIX) testOnScreenshotTakenUserHandle_withWorkProfileFirstRunnull94 fun testOnScreenshotTakenUserHandle_withWorkProfileFirstRun() { 95 whenever(workProfileMessageController.onScreenshotTaken(eq(userHandle))) 96 .thenReturn(workProfileData) 97 messageContainer.onScreenshotTaken(screenshotData) 98 99 verify(workProfileMessageController) 100 .populateView(eq(workProfileFirstRunView), eq(workProfileData), any()) 101 assertEquals(View.VISIBLE, workProfileFirstRunView.visibility) 102 assertEquals(View.GONE, detectionNoticeView.visibility) 103 } 104 105 @Test 106 @EnableFlags(com.android.systemui.Flags.FLAG_SCREENSHOT_PRIVATE_PROFILE_BEHAVIOR_FIX) <lambda>null107 fun testOnScreenshotTakenUserHandle_withProfileProfileFirstRun() = runTest { 108 val profileData = 109 ProfileMessageController.ProfileFirstRunData( 110 LabeledIcon(appName, icon), 111 ProfileMessageController.FirstRunProfile.PRIVATE 112 ) 113 whenever(profileMessageController.onScreenshotTaken(eq(userHandle))).thenReturn(profileData) 114 messageContainer.onScreenshotTaken(screenshotData) 115 116 verify(profileMessageController) 117 .bindView(eq(workProfileFirstRunView), eq(profileData), any()) 118 assertEquals(View.VISIBLE, workProfileFirstRunView.visibility) 119 assertEquals(View.GONE, detectionNoticeView.visibility) 120 } 121 122 @Test testOnScreenshotTakenScreenshotData_nothingToShownull123 fun testOnScreenshotTakenScreenshotData_nothingToShow() { 124 messageContainer.onScreenshotTaken(screenshotData) 125 126 verify(workProfileMessageController, never()).populateView(any(), any(), any()) 127 verify(profileMessageController, never()).bindView(any(), any(), any()) 128 verify(screenshotDetectionController, never()).populateView(any(), any()) 129 130 assertEquals(View.GONE, container.visibility) 131 } 132 } 133