diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsRequestDialog.kt b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsRequestDialog.kt index 15d15e8ffbc73..4ed6106750937 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsRequestDialog.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsRequestDialog.kt @@ -41,7 +41,7 @@ import com.android.systemui.statusbar.phone.SystemUIDialog import com.android.systemui.util.LifecycleActivity import javax.inject.Inject -class ControlsRequestDialog @Inject constructor( +open class ControlsRequestDialog @Inject constructor( private val controller: ControlsController, private val broadcastDispatcher: BroadcastDispatcher, private val controlsListingController: ControlsListingController @@ -51,7 +51,7 @@ class ControlsRequestDialog @Inject constructor( private const val TAG = "ControlsRequestDialog" } - private lateinit var component: ComponentName + private lateinit var controlComponent: ComponentName private lateinit var control: Control private var dialog: Dialog? = null private val callback = object : ControlsListingController.ControlsListingCallback { @@ -86,7 +86,7 @@ class ControlsRequestDialog @Inject constructor( finish() } - component = intent.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME) ?: run { + controlComponent = intent.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME) ?: run { Log.e(TAG, "Request did not contain componentName") finish() return @@ -103,7 +103,7 @@ class ControlsRequestDialog @Inject constructor( super.onResume() val label = verifyComponentAndGetLabel() if (label == null) { - Log.e(TAG, "The component specified (${component.flattenToString()} " + + Log.e(TAG, "The component specified (${controlComponent.flattenToString()} " + "is not a valid ControlsProviderService") finish() return @@ -127,16 +127,16 @@ class ControlsRequestDialog @Inject constructor( } private fun verifyComponentAndGetLabel(): CharSequence? { - return controlsListingController.getAppLabel(component) + return controlsListingController.getAppLabel(controlComponent) } private fun isCurrentFavorite(): Boolean { - val favorites = controller.getFavoritesForComponent(component) + val favorites = controller.getFavoritesForComponent(controlComponent) return favorites.any { it.controls.any { it.controlId == control.controlId } } } fun createDialog(label: CharSequence): Dialog { - val renderInfo = RenderInfo.lookup(this, component, control.deviceType) + val renderInfo = RenderInfo.lookup(this, controlComponent, control.deviceType) val frame = LayoutInflater.from(this).inflate(R.layout.controls_dialog, null).apply { requireViewById(R.id.icon).apply { setImageDrawable(renderInfo.icon) @@ -170,7 +170,7 @@ class ControlsRequestDialog @Inject constructor( override fun onClick(dialog: DialogInterface?, which: Int) { if (which == Dialog.BUTTON_POSITIVE) { controller.addFavorite( - componentName, + controlComponent, control.structure ?: "", ControlInfo(control.controlId, control.title, control.subtitle, control.deviceType) ) diff --git a/packages/SystemUI/tests/AndroidManifest.xml b/packages/SystemUI/tests/AndroidManifest.xml index 9c94b8693b9ff..65e5baa7926a5 100644 --- a/packages/SystemUI/tests/AndroidManifest.xml +++ b/packages/SystemUI/tests/AndroidManifest.xml @@ -74,6 +74,11 @@ android:excludeFromRecents="true" android:exported="false" /> + + + + @Rule + @JvmField + var activityRule = ActivityTestRule( + object : SingleActivityFactory( + TestControlsRequestDialog::class.java + ) { + override fun create(intent: Intent?): TestControlsRequestDialog { + return TestControlsRequestDialog( + controller, + broadcastDispatcher, + listingController + ) + } + }, false, false) + + private lateinit var control: Control + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + + control = Control.StatelessBuilder(CONTROL_ID, PendingIntent(iIntentSender)) + .setTitle("TITLE") + .setSubtitle("SUBTITLE") + .setDeviceType(DeviceTypes.TYPE_LIGHT) + .setStructure("STRUCTURE") + .build() + + val intent = Intent(mContext, TestControlsRequestDialog::class.java) + intent.putExtra(Intent.EXTRA_USER_ID, USER_ID) + intent.putExtra(Intent.EXTRA_COMPONENT_NAME, CONTROL_COMPONENT) + intent.putExtra(ControlsProviderService.EXTRA_CONTROL, control) + + `when`(controller.currentUserId).thenReturn(USER_ID) + `when`(controller.available).thenReturn(true) + `when`(listingController.getAppLabel(CONTROL_COMPONENT)).thenReturn(LABEL) + `when`(controller.getFavoritesForComponent(CONTROL_COMPONENT)).thenReturn(emptyList()) + + activityRule.launchActivity(intent) + } + + @After + fun tearDown() { + activityRule.finishActivity() + } + + @Test + fun testActivityNotFinished() { + assertNotEquals(Lifecycle.State.DESTROYED, + activityRule.getActivity().lifecycle.currentState) + } + + @Test + fun testDialogAddsCorrectControl() { + activityRule.activity.onClick(null, Dialog.BUTTON_POSITIVE) + + verify(controller) + .addFavorite(eq(CONTROL_COMPONENT), eq(control.structure!!), capture(captor)) + + captor.value.let { + assertEquals(control.controlId, it.controlId) + assertEquals(control.title, it.controlTitle) + assertEquals(control.subtitle, it.controlSubtitle) + assertEquals(control.deviceType, it.deviceType) + } + } +} \ No newline at end of file diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/management/TestControlsRequestDialog.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/management/TestControlsRequestDialog.kt new file mode 100644 index 0000000000000..3f6308b18d032 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/controls/management/TestControlsRequestDialog.kt @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.controls.management + +import com.android.systemui.broadcast.BroadcastDispatcher +import com.android.systemui.controls.controller.ControlsController + +class TestControlsRequestDialog( + controller: ControlsController, + dispatcher: BroadcastDispatcher, + listingController: ControlsListingController +) : ControlsRequestDialog(controller, dispatcher, listingController) \ No newline at end of file