1 /*
2  * Copyright (C) 2019 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.BroadcastReceiver
20 import android.content.Context
21 import android.content.IntentFilter
22 import android.os.Handler
23 import android.os.UserHandle
24 import android.test.suitebuilder.annotation.SmallTest
25 import android.testing.AndroidTestingRunner
26 import android.testing.TestableLooper
27 import com.android.systemui.SysuiTestCase
28 import com.android.systemui.broadcast.logging.BroadcastDispatcherLogger
29 import com.android.systemui.util.concurrency.FakeExecutor
30 import com.android.systemui.util.time.FakeSystemClock
31 import junit.framework.Assert.assertFalse
32 import junit.framework.Assert.assertNotNull
33 import org.junit.Before
34 import org.junit.Test
35 import org.junit.runner.RunWith
36 import org.mockito.ArgumentCaptor
37 import org.mockito.Mock
38 import org.mockito.Mockito
39 import org.mockito.Mockito.mock
40 import org.mockito.Mockito.verify
41 import org.mockito.MockitoAnnotations
42 import java.util.concurrent.Executor
43 
44 @RunWith(AndroidTestingRunner::class)
45 @TestableLooper.RunWithLooper
46 @SmallTest
47 class UserBroadcastDispatcherTest : SysuiTestCase() {
48 
49     companion object {
50         private const val ACTION_1 = "com.android.systemui.tests.ACTION_1"
51         private const val ACTION_2 = "com.android.systemui.tests.ACTION_2"
52         private const val USER_ID = 0
53         private val USER_HANDLE = UserHandle.of(USER_ID)
54 
capturenull55         fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()
56         fun <T> any(): T = Mockito.any()
57         fun <T> eq(v: T) = Mockito.eq(v) ?: v
58     }
59 
60     @Mock
61     private lateinit var broadcastReceiver: BroadcastReceiver
62     @Mock
63     private lateinit var broadcastReceiverOther: BroadcastReceiver
64     @Mock
65     private lateinit var mockContext: Context
66     @Mock
67     private lateinit var logger: BroadcastDispatcherLogger
68 
69     private lateinit var testableLooper: TestableLooper
70     private lateinit var userBroadcastDispatcher: UserBroadcastDispatcher
71     private lateinit var intentFilter: IntentFilter
72     private lateinit var intentFilterOther: IntentFilter
73     private lateinit var handler: Handler
74     private lateinit var fakeExecutor: FakeExecutor
75 
76     @Before
77     fun setUp() {
78         MockitoAnnotations.initMocks(this)
79         testableLooper = TestableLooper.get(this)
80         handler = Handler(testableLooper.looper)
81         fakeExecutor = FakeExecutor(FakeSystemClock())
82 
83         userBroadcastDispatcher = object : UserBroadcastDispatcher(
84                 mockContext, USER_ID, testableLooper.looper, mock(Executor::class.java), logger) {
85             override fun createActionReceiver(action: String): ActionReceiver {
86                 return mock(ActionReceiver::class.java)
87             }
88         }
89     }
90 
91     @Test
testSingleReceiverRegisterednull92     fun testSingleReceiverRegistered() {
93         intentFilter = IntentFilter(ACTION_1)
94         val receiverData = ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE)
95 
96         userBroadcastDispatcher.registerReceiver(receiverData)
97         testableLooper.processAllMessages()
98 
99         val actionReceiver = userBroadcastDispatcher.getActionReceiver(ACTION_1)
100         assertNotNull(actionReceiver)
101         verify(actionReceiver)?.addReceiverData(receiverData)
102     }
103 
104     @Test
testSingleReceiverRegistered_loggingnull105     fun testSingleReceiverRegistered_logging() {
106         intentFilter = IntentFilter(ACTION_1)
107 
108         userBroadcastDispatcher.registerReceiver(
109                 ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE))
110         testableLooper.processAllMessages()
111 
112         verify(logger).logReceiverRegistered(USER_HANDLE.identifier, broadcastReceiver)
113     }
114 
115     @Test
testSingleReceiverUnregisterednull116     fun testSingleReceiverUnregistered() {
117         intentFilter = IntentFilter(ACTION_1)
118 
119         userBroadcastDispatcher.registerReceiver(
120                 ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE))
121         testableLooper.processAllMessages()
122 
123         userBroadcastDispatcher.unregisterReceiver(broadcastReceiver)
124         testableLooper.processAllMessages()
125 
126         val actionReceiver = userBroadcastDispatcher.getActionReceiver(ACTION_1)
127         assertNotNull(actionReceiver)
128         verify(actionReceiver)?.removeReceiver(broadcastReceiver)
129     }
130 
131     @Test
testSingleReceiverUnregistered_loggernull132     fun testSingleReceiverUnregistered_logger() {
133         intentFilter = IntentFilter(ACTION_1)
134 
135         userBroadcastDispatcher.registerReceiver(
136                 ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE))
137         testableLooper.processAllMessages()
138 
139         userBroadcastDispatcher.unregisterReceiver(broadcastReceiver)
140         testableLooper.processAllMessages()
141 
142         verify(logger).logReceiverUnregistered(USER_HANDLE.identifier, broadcastReceiver)
143     }
144 
145     @Test
testRemoveReceiverReferencesnull146     fun testRemoveReceiverReferences() {
147         intentFilter = IntentFilter(ACTION_1)
148         userBroadcastDispatcher.registerReceiver(
149                 ReceiverData(broadcastReceiver, intentFilter, fakeExecutor, USER_HANDLE))
150 
151         intentFilterOther = IntentFilter(ACTION_1)
152         intentFilterOther.addAction(ACTION_2)
153         userBroadcastDispatcher.registerReceiver(
154                 ReceiverData(broadcastReceiverOther, intentFilterOther, fakeExecutor, USER_HANDLE))
155 
156         userBroadcastDispatcher.unregisterReceiver(broadcastReceiver)
157         testableLooper.processAllMessages()
158         fakeExecutor.runAllReady()
159 
160         assertFalse(userBroadcastDispatcher.isReceiverReferenceHeld(broadcastReceiver))
161     }
162 
UserBroadcastDispatchernull163     private fun UserBroadcastDispatcher.getActionReceiver(action: String): ActionReceiver? {
164         return actionsToActionsReceivers.get(action)
165     }
166 }
167