1 /*
2  * Copyright (C) 2020 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.util
18 
19 import android.content.BroadcastReceiver
20 import android.content.IntentFilter
21 import android.os.UserHandle
22 import android.testing.AndroidTestingRunner
23 import android.testing.TestableLooper
24 import androidx.lifecycle.Observer
25 import androidx.test.filters.SmallTest
26 import com.android.systemui.SysuiTestCase
27 import com.android.systemui.broadcast.BroadcastDispatcher
28 import org.junit.After
29 import org.junit.Assert.assertTrue
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 import org.mockito.ArgumentCaptor
34 import org.mockito.Captor
35 import org.mockito.Mock
36 import org.mockito.Mockito
37 import org.mockito.Mockito.anyInt
38 import org.mockito.Mockito.verify
39 import org.mockito.Mockito.verifyNoMoreInteractions
40 import org.mockito.MockitoAnnotations
41 import java.util.concurrent.Executor
42 
43 @SmallTest
44 @RunWith(AndroidTestingRunner::class)
45 @TestableLooper.RunWithLooper(setAsMainLooper = true)
46 class RingerModeLiveDataTest : SysuiTestCase() {
47 
48     companion object {
49         private fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()
50         private fun <T> any(): T = Mockito.any()
51         private fun <T> eq(value: T): T = Mockito.eq(value) ?: value
52         private val INTENT = "INTENT"
53     }
54 
55     @Mock
56     private lateinit var broadcastDispatcher: BroadcastDispatcher
57     @Mock
58     private lateinit var valueSupplier: () -> Int
59     @Mock
60     private lateinit var observer: Observer<Int>
61     @Captor
62     private lateinit var broadcastReceiverCaptor: ArgumentCaptor<BroadcastReceiver>
63     @Captor
64     private lateinit var intentFilterCaptor: ArgumentCaptor<IntentFilter>
65 
66     // Run everything immediately
67     private val executor = Executor { it.run() }
68     private lateinit var liveData: RingerModeLiveData
69 
70     @Before
71     fun setUp() {
72         MockitoAnnotations.initMocks(this)
73 
74         liveData = RingerModeLiveData(broadcastDispatcher, executor, INTENT, valueSupplier)
75     }
76 
77     @After
78     fun tearDown() {
79         liveData.removeObserver(observer)
80     }
81 
82     @Test
83     fun testInit_broadcastNotRegistered() {
84         verifyNoMoreInteractions(broadcastDispatcher)
85     }
86 
87     @Test
88     fun testOnActive_broadcastRegistered() {
89         liveData.observeForever(observer)
90         verify(broadcastDispatcher)
91                 .registerReceiver(any(), any(), eq(executor), eq(UserHandle.ALL), anyInt(), any())
92     }
93 
94     @Test
95     fun testOnActive_intentFilterHasIntent() {
96         liveData.observeForever(observer)
97         verify(broadcastDispatcher).registerReceiver(any(), capture(intentFilterCaptor), any(),
98                 any(), anyInt(), any())
99         assertTrue(intentFilterCaptor.value.hasAction(INTENT))
100     }
101 
102     @Test
103     fun testOnActive_valueObtained() {
104         liveData.observeForever(observer)
105         verify(valueSupplier).invoke()
106     }
107 
108     @Test
109     fun testOnInactive_broadcastUnregistered() {
110         liveData.observeForever(observer)
111         liveData.removeObserver(observer)
112         verify(broadcastDispatcher).unregisterReceiver(any())
113     }
114 }