Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 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.controls.management |
| 18 | |
| 19 | import android.content.ComponentName |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 20 | import android.content.Context |
| 21 | import android.content.ContextWrapper |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 22 | import android.content.pm.ServiceInfo |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 23 | import android.os.UserHandle |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 24 | import android.testing.AndroidTestingRunner |
| 25 | import androidx.test.filters.SmallTest |
| 26 | import com.android.settingslib.applications.ServiceListing |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 27 | import com.android.systemui.SysuiTestCase |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 28 | import com.android.systemui.controls.ControlsServiceInfo |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 29 | import com.android.systemui.util.concurrency.FakeExecutor |
| 30 | import com.android.systemui.util.time.FakeSystemClock |
| 31 | import org.junit.After |
| 32 | import org.junit.Assert.assertEquals |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 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.`when` |
Fabian Kozynski | 3508673 | 2020-03-10 14:25:32 -0400 | [diff] [blame] | 40 | import org.mockito.Mockito.inOrder |
Fabian Kozynski | 1a47862 | 2020-05-04 14:21:31 -0400 | [diff] [blame] | 41 | import org.mockito.Mockito.mock |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 42 | import org.mockito.Mockito.never |
| 43 | import org.mockito.Mockito.reset |
| 44 | import org.mockito.Mockito.verify |
| 45 | import org.mockito.MockitoAnnotations |
Fabian Kozynski | 1a47862 | 2020-05-04 14:21:31 -0400 | [diff] [blame] | 46 | import java.util.concurrent.Executor |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 47 | |
| 48 | @SmallTest |
| 49 | @RunWith(AndroidTestingRunner::class) |
| 50 | class ControlsListingControllerImplTest : SysuiTestCase() { |
| 51 | |
| 52 | companion object { |
| 53 | private const val TEST_LABEL = "TEST_LABEL" |
| 54 | private const val TEST_PERMISSION = "permission" |
| 55 | fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture() |
| 56 | fun <T> any(): T = Mockito.any<T>() |
| 57 | } |
| 58 | |
| 59 | @Mock |
| 60 | private lateinit var mockSL: ServiceListing |
| 61 | @Mock |
| 62 | private lateinit var mockCallback: ControlsListingController.ControlsListingCallback |
| 63 | @Mock |
| 64 | private lateinit var mockCallbackOther: ControlsListingController.ControlsListingCallback |
| 65 | @Mock |
| 66 | private lateinit var serviceInfo: ServiceInfo |
| 67 | @Mock |
| 68 | private lateinit var componentName: ComponentName |
| 69 | |
| 70 | private val executor = FakeExecutor(FakeSystemClock()) |
| 71 | |
| 72 | private lateinit var controller: ControlsListingControllerImpl |
| 73 | |
| 74 | private var serviceListingCallbackCaptor = |
| 75 | ArgumentCaptor.forClass(ServiceListing.Callback::class.java) |
| 76 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 77 | private val user = mContext.userId |
| 78 | private val otherUser = user + 1 |
| 79 | |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 80 | @Before |
| 81 | fun setUp() { |
| 82 | MockitoAnnotations.initMocks(this) |
| 83 | |
| 84 | `when`(serviceInfo.componentName).thenReturn(componentName) |
| 85 | |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 86 | val wrapper = object : ContextWrapper(mContext) { |
| 87 | override fun createContextAsUser(user: UserHandle, flags: Int): Context { |
| 88 | return baseContext |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | controller = ControlsListingControllerImpl(wrapper, executor, { mockSL }) |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 93 | verify(mockSL).addCallback(capture(serviceListingCallbackCaptor)) |
| 94 | } |
| 95 | |
| 96 | @After |
| 97 | fun tearDown() { |
| 98 | executor.advanceClockToLast() |
| 99 | executor.runAllReady() |
| 100 | } |
| 101 | |
| 102 | @Test |
Fabian Kozynski | 3508673 | 2020-03-10 14:25:32 -0400 | [diff] [blame] | 103 | fun testInitialStateListening() { |
| 104 | verify(mockSL).setListening(true) |
| 105 | verify(mockSL).reload() |
| 106 | } |
| 107 | |
| 108 | @Test |
Fabian Kozynski | 1a47862 | 2020-05-04 14:21:31 -0400 | [diff] [blame] | 109 | fun testImmediateListingReload_doesNotCrash() { |
| 110 | val exec = Executor { it.run() } |
| 111 | val mockServiceListing = mock(ServiceListing::class.java) |
| 112 | var callback: ServiceListing.Callback? = null |
| 113 | `when`(mockServiceListing.addCallback(any<ServiceListing.Callback>())).then { |
| 114 | callback = it.getArgument(0) |
| 115 | Unit |
| 116 | } |
| 117 | `when`(mockServiceListing.reload()).then { |
| 118 | callback?.onServicesReloaded(listOf(serviceInfo)) |
| 119 | } |
| 120 | ControlsListingControllerImpl(mContext, exec, { mockServiceListing }) |
| 121 | } |
| 122 | |
| 123 | @Test |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 124 | fun testStartsOnUser() { |
| 125 | assertEquals(user, controller.currentUserId) |
| 126 | } |
| 127 | |
| 128 | @Test |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 129 | fun testCallbackCalledWhenAdded() { |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 130 | controller.addCallback(mockCallback) |
| 131 | executor.runAllReady() |
| 132 | verify(mockCallback).onServicesUpdated(any()) |
| 133 | reset(mockCallback) |
| 134 | |
| 135 | controller.addCallback(mockCallbackOther) |
| 136 | executor.runAllReady() |
| 137 | verify(mockCallbackOther).onServicesUpdated(any()) |
| 138 | verify(mockCallback, never()).onServicesUpdated(any()) |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | fun testCallbackGetsList() { |
| 143 | val list = listOf(serviceInfo) |
| 144 | controller.addCallback(mockCallback) |
| 145 | controller.addCallback(mockCallbackOther) |
| 146 | |
| 147 | @Suppress("unchecked_cast") |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 148 | val captor: ArgumentCaptor<List<ControlsServiceInfo>> = |
| 149 | ArgumentCaptor.forClass(List::class.java) |
| 150 | as ArgumentCaptor<List<ControlsServiceInfo>> |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 151 | |
| 152 | executor.runAllReady() |
| 153 | reset(mockCallback) |
| 154 | reset(mockCallbackOther) |
| 155 | |
| 156 | serviceListingCallbackCaptor.value.onServicesReloaded(list) |
| 157 | |
| 158 | executor.runAllReady() |
| 159 | verify(mockCallback).onServicesUpdated(capture(captor)) |
| 160 | assertEquals(1, captor.value.size) |
| 161 | assertEquals(componentName.flattenToString(), captor.value[0].key) |
| 162 | |
| 163 | verify(mockCallbackOther).onServicesUpdated(capture(captor)) |
| 164 | assertEquals(1, captor.value.size) |
| 165 | assertEquals(componentName.flattenToString(), captor.value[0].key) |
| 166 | } |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 167 | |
| 168 | @Test |
| 169 | fun testChangeUser() { |
| 170 | controller.changeUser(UserHandle.of(otherUser)) |
| 171 | executor.runAllReady() |
| 172 | assertEquals(otherUser, controller.currentUserId) |
Fabian Kozynski | 3508673 | 2020-03-10 14:25:32 -0400 | [diff] [blame] | 173 | |
| 174 | val inOrder = inOrder(mockSL) |
| 175 | inOrder.verify(mockSL).setListening(false) |
| 176 | inOrder.verify(mockSL).addCallback(any()) // We add a callback because we replaced the SL |
| 177 | inOrder.verify(mockSL).setListening(true) |
| 178 | inOrder.verify(mockSL).reload() |
Fabian Kozynski | 7988bd4 | 2020-01-30 12:21:52 -0500 | [diff] [blame] | 179 | } |
Fabian Kozynski | f10b6ab | 2019-12-27 09:31:04 -0500 | [diff] [blame] | 180 | } |