1 /*
2  * Copyright (C) 2022 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.broadcast
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.os.Bundle
22 import android.os.UserHandle
23 import android.testing.AndroidTestingRunner
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.util.concurrency.FakeExecutor
27 import com.android.systemui.util.time.FakeSystemClock
28 import com.android.systemui.util.wakelock.WakeLockFake
29 import com.google.common.truth.Truth.assertThat
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.Mock
34 import org.mockito.Mockito.verify
35 import org.mockito.MockitoAnnotations
36 
37 @RunWith(AndroidTestingRunner::class)
38 @SmallTest
39 class BroadcastSenderTest : SysuiTestCase() {
40 
41     @Mock
42     private lateinit var mockContext: Context
43 
44     private lateinit var broadcastSender: BroadcastSender
45     private lateinit var executor: FakeExecutor
46     private lateinit var wakeLock: WakeLockFake
47 
48     @Before
49     fun setUp() {
50         MockitoAnnotations.initMocks(this)
51         executor = FakeExecutor(FakeSystemClock())
52         wakeLock = WakeLockFake()
53         val wakeLockBuilder = WakeLockFake.Builder(mContext)
54         wakeLockBuilder.setWakeLock(wakeLock)
55         broadcastSender = BroadcastSender(mockContext, wakeLockBuilder, executor)
56     }
57 
58     @Test
59     fun sendBroadcast_dispatchesWithWakelock() {
60         val intent = Intent(Intent.ACTION_VIEW)
61         broadcastSender.sendBroadcast(intent)
62 
63         runExecutorAssertingWakelock {
64             verify(mockContext).sendBroadcast(intent)
65         }
66     }
67 
68     @Test
69     fun sendBroadcastWithPermission_dispatchesWithWakelock() {
70         val intent = Intent(Intent.ACTION_VIEW)
71         val permission = "Permission"
72         broadcastSender.sendBroadcast(intent, permission)
73 
74         runExecutorAssertingWakelock {
75             verify(mockContext).sendBroadcast(intent, permission)
76         }
77     }
78 
79     @Test
80     fun sendBroadcastAsUser_dispatchesWithWakelock() {
81         val intent = Intent(Intent.ACTION_VIEW)
82         broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL)
83 
84         runExecutorAssertingWakelock {
85             verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL)
86         }
87     }
88 
89     @Test
90     fun sendBroadcastAsUserWithPermission_dispatchesWithWakelock() {
91         val intent = Intent(Intent.ACTION_VIEW)
92         val permission = "Permission"
93         broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission)
94 
95         runExecutorAssertingWakelock {
96             verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission)
97         }
98     }
99 
100     @Test
101     fun sendBroadcastAsUserWithPermissionAndOptions_dispatchesWithWakelock() {
102         val intent = Intent(Intent.ACTION_VIEW)
103         val permission = "Permission"
104         val options = Bundle()
105         options.putString("key", "value")
106 
107         broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission, options)
108 
109         runExecutorAssertingWakelock {
110             verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission, options)
111         }
112     }
113 
114     @Test
115     fun sendBroadcastAsUserWithPermissionAndAppOp_dispatchesWithWakelock() {
116         val intent = Intent(Intent.ACTION_VIEW)
117         val permission = "Permission"
118 
119         broadcastSender.sendBroadcastAsUser(intent, UserHandle.ALL, permission, 12)
120 
121         runExecutorAssertingWakelock {
122             verify(mockContext).sendBroadcastAsUser(intent, UserHandle.ALL, permission, 12)
123         }
124     }
125 
126     @Test
127     fun sendCloseSystemDialogs_dispatchesWithWakelock() {
128         broadcastSender.closeSystemDialogs()
129 
130         runExecutorAssertingWakelock {
131             verify(mockContext).closeSystemDialogs()
132         }
133     }
134 
135     private fun runExecutorAssertingWakelock(verification: () -> Unit) {
136         assertThat(wakeLock.isHeld).isTrue()
137         executor.runAllReady()
138         verification.invoke()
139         assertThat(wakeLock.isHeld).isFalse()
140     }
141 }