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.systemui.controls.ui
18 
19 import android.app.ActivityOptions
20 import android.app.PendingIntent
21 import android.testing.AndroidTestingRunner
22 import android.testing.TestableLooper
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.broadcast.BroadcastSender
26 import com.android.systemui.plugins.ActivityStarter
27 import com.android.systemui.statusbar.policy.KeyguardStateController
28 import com.android.systemui.util.mockito.argumentCaptor
29 import com.android.systemui.util.mockito.capture
30 import com.android.wm.shell.taskview.TaskView
31 import com.google.common.truth.Truth.assertThat
32 import org.junit.Before
33 import org.junit.Test
34 import org.junit.runner.RunWith
35 import org.mockito.ArgumentMatchers.eq
36 import org.mockito.Mock
37 import org.mockito.Mockito.any
38 import org.mockito.Mockito.verify
39 import org.mockito.MockitoAnnotations
40 
41 @SmallTest
42 @RunWith(AndroidTestingRunner::class)
43 @TestableLooper.RunWithLooper
44 class DetailDialogTest : SysuiTestCase() {
45 
46     @Mock
47     private lateinit var taskView: TaskView
48     @Mock
49     private lateinit var broadcastSender: BroadcastSender
50     @Mock
51     private lateinit var controlViewHolder: ControlViewHolder
52     @Mock
53     private lateinit var pendingIntent: PendingIntent
54     @Mock
55     private lateinit var keyguardStateController: KeyguardStateController
56     @Mock
57     private lateinit var activityStarter: ActivityStarter
58 
59     @Before
60     fun setUp() {
61         MockitoAnnotations.initMocks(this)
62     }
63 
64     @Test
65     fun testPendingIntentIsUnModified() {
66         // GIVEN the dialog is created with a PendingIntent
67         val dialog = createDialog(pendingIntent)
68 
69         // WHEN the TaskView is initialized
70         dialog.stateCallback.onInitialized()
71 
72         // THEN the PendingIntent used to call startActivity is unmodified by systemui
73         verify(taskView).startActivity(eq(pendingIntent), any(), any(), any())
74     }
75 
76     @Test
77     fun testActivityOptionsAllowBal() {
78         // GIVEN the dialog is created with a PendingIntent
79         val dialog = createDialog(pendingIntent)
80 
81         // WHEN the TaskView is initialized
82         dialog.stateCallback.onInitialized()
83 
84         val optionsCaptor = argumentCaptor<ActivityOptions>()
85 
86         // THEN the ActivityOptions have the correct flags
87         verify(taskView).startActivity(any(), any(), capture(optionsCaptor), any())
88 
89         assertThat(optionsCaptor.value.pendingIntentBackgroundActivityStartMode)
90             .isEqualTo(ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
91         assertThat(optionsCaptor.value.isPendingIntentBackgroundActivityLaunchAllowedByPermission)
92             .isTrue()
93     }
94 
95     private fun createDialog(pendingIntent: PendingIntent): DetailDialog {
96         return DetailDialog(
97             mContext,
98             broadcastSender,
99             taskView,
100             pendingIntent,
101             controlViewHolder,
102             keyguardStateController,
103             activityStarter
104         )
105     }
106 }
107