1 /*
2  * Copyright (C) 2024 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.systemui.screenshot
18 
19 import android.app.PendingIntent
20 import android.content.Intent
21 import android.os.Bundle
22 import android.os.UserHandle
23 import android.testing.AndroidTestingRunner
24 import android.view.Window
25 import androidx.test.filters.SmallTest
26 import com.android.systemui.SysuiTestCase
27 import com.android.systemui.util.mockito.argumentCaptor
28 import com.android.systemui.util.mockito.mock
29 import com.google.common.truth.Truth.assertThat
30 import kotlin.test.Test
31 import kotlinx.coroutines.test.StandardTestDispatcher
32 import kotlinx.coroutines.test.TestCoroutineScheduler
33 import kotlinx.coroutines.test.TestScope
34 import kotlinx.coroutines.test.runTest
35 import org.junit.runner.RunWith
36 import org.mockito.ArgumentMatchers.any
37 import org.mockito.kotlin.capture
38 import org.mockito.kotlin.eq
39 import org.mockito.kotlin.verify
40 import org.mockito.kotlin.verifyBlocking
41 
42 @RunWith(AndroidTestingRunner::class)
43 @SmallTest
44 class ActionExecutorTest : SysuiTestCase() {
45     private val scheduler = TestCoroutineScheduler()
46     private val mainDispatcher = StandardTestDispatcher(scheduler)
47     private val testScope = TestScope(mainDispatcher)
48 
49     private val intentExecutor = mock<ActionIntentExecutor>()
50     private val window = mock<Window>()
51     private val viewProxy = mock<ScreenshotShelfViewProxy>()
52     private val onDismiss = mock<(() -> Unit)>()
53     private val pendingIntent = mock<PendingIntent>()
54 
55     private lateinit var actionExecutor: ActionExecutor
56 
57     @Test
<lambda>null58     fun startSharedTransition_callsLaunchIntent() = runTest {
59         actionExecutor = createActionExecutor()
60 
61         actionExecutor.startSharedTransition(Intent(Intent.ACTION_EDIT), UserHandle.CURRENT, true)
62         scheduler.advanceUntilIdle()
63 
64         val intentCaptor = argumentCaptor<Intent>()
65         verifyBlocking(intentExecutor) {
66             launchIntent(capture(intentCaptor), eq(UserHandle.CURRENT), eq(true), any(), any())
67         }
68         assertThat(intentCaptor.value.action).isEqualTo(Intent.ACTION_EDIT)
69     }
70 
71     @Test
<lambda>null72     fun sendPendingIntent_requestsDismissal() = runTest {
73         actionExecutor = createActionExecutor()
74 
75         actionExecutor.sendPendingIntent(pendingIntent)
76 
77         verify(pendingIntent).send(any(Bundle::class.java))
78         verify(viewProxy).requestDismissal(null)
79     }
80 
createActionExecutornull81     private fun createActionExecutor(): ActionExecutor {
82         return ActionExecutor(intentExecutor, testScope, window, viewProxy, onDismiss)
83     }
84 }
85