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.app.Dialog 20 import android.app.PendingIntent 21 import android.content.ComponentName 22 import android.content.IIntentSender 23 import android.content.Intent 24 import android.os.UserHandle 25 import android.service.controls.Control 26 import android.service.controls.ControlsProviderService 27 import android.service.controls.DeviceTypes 28 import android.testing.TestableLooper 29 import androidx.lifecycle.Lifecycle 30 import androidx.test.ext.junit.runners.AndroidJUnit4 31 import androidx.test.filters.MediumTest 32 import androidx.test.rule.ActivityTestRule 33 import com.android.systemui.SysuiTestCase 34 import com.android.systemui.activity.SingleActivityFactory 35 import com.android.systemui.controls.controller.ControlInfo 36 import com.android.systemui.controls.controller.ControlsController 37 import com.android.systemui.settings.UserTracker 38 import com.android.systemui.util.mockito.capture 39 import com.android.systemui.util.mockito.eq 40 import org.junit.After 41 import org.junit.Assert.assertEquals 42 import org.junit.Assert.assertNotEquals 43 import org.junit.Before 44 import org.junit.Rule 45 import org.junit.Test 46 import org.junit.runner.RunWith 47 import org.mockito.ArgumentCaptor 48 import org.mockito.Captor 49 import org.mockito.Mock 50 import org.mockito.Mockito.verify 51 import org.mockito.Mockito.`when` 52 import org.mockito.MockitoAnnotations 53 import java.util.concurrent.Executor 54 55 @MediumTest 56 @RunWith(AndroidJUnit4::class) 57 @TestableLooper.RunWithLooper 58 class ControlsRequestDialogTest : SysuiTestCase() { 59 60 companion object { 61 private val CONTROL_COMPONENT = ComponentName.unflattenFromString("TEST_PKG/.TEST_CLS")!! 62 private const val LABEL = "TEST_LABEL" 63 64 private val USER_ID = UserHandle.USER_SYSTEM 65 private const val CONTROL_ID = "id" 66 } 67 68 @Mock 69 private lateinit var controller: ControlsController 70 71 @Mock 72 private lateinit var mainExecutor: Executor 73 @Mock 74 private lateinit var userTracker: UserTracker 75 @Mock 76 private lateinit var listingController: ControlsListingController 77 @Mock 78 private lateinit var iIntentSender: IIntentSender 79 @Captor 80 private lateinit var captor: ArgumentCaptor<ControlInfo> 81 82 @Rule 83 @JvmField 84 var activityRule = ActivityTestRule( <lambda>null85 /* activityFactory= */ SingleActivityFactory { 86 TestControlsRequestDialog( 87 mainExecutor, 88 controller, 89 userTracker, 90 listingController 91 ) 92 }, 93 /* initialTouchMode= */ false, 94 /* launchActivity= */ false, 95 ) 96 97 private lateinit var control: Control 98 99 @Before setUpnull100 fun setUp() { 101 MockitoAnnotations.initMocks(this) 102 103 control = Control.StatelessBuilder(CONTROL_ID, PendingIntent(iIntentSender)) 104 .setTitle("TITLE") 105 .setSubtitle("SUBTITLE") 106 .setDeviceType(DeviceTypes.TYPE_LIGHT) 107 .setStructure("STRUCTURE") 108 .build() 109 110 val intent = Intent(mContext, TestControlsRequestDialog::class.java) 111 intent.putExtra(Intent.EXTRA_USER_ID, USER_ID) 112 intent.putExtra(Intent.EXTRA_COMPONENT_NAME, CONTROL_COMPONENT) 113 intent.putExtra(ControlsProviderService.EXTRA_CONTROL, control) 114 115 `when`(controller.currentUserId).thenReturn(USER_ID) 116 `when`(listingController.getAppLabel(CONTROL_COMPONENT)).thenReturn(LABEL) 117 `when`(controller.getFavoritesForComponent(CONTROL_COMPONENT)).thenReturn(emptyList()) 118 119 activityRule.launchActivity(intent) 120 } 121 122 @After tearDownnull123 fun tearDown() { 124 activityRule.finishActivity() 125 } 126 127 @Test testActivityNotFinishednull128 fun testActivityNotFinished() { 129 assertNotEquals(Lifecycle.State.DESTROYED, 130 activityRule.getActivity().lifecycle.currentState) 131 } 132 133 @Test testDialogAddsCorrectControlnull134 fun testDialogAddsCorrectControl() { 135 activityRule.activity.onClick(null, Dialog.BUTTON_POSITIVE) 136 137 verify(controller) 138 .addFavorite(eq(CONTROL_COMPONENT), eq(control.structure!!), capture(captor)) 139 140 captor.value.let { 141 assertEquals(control.controlId, it.controlId) 142 assertEquals(control.title, it.controlTitle) 143 assertEquals(control.subtitle, it.controlSubtitle) 144 assertEquals(control.deviceType, it.deviceType) 145 } 146 } 147 } 148