Fix dialog leak in RequestPermissionActivity

Dialog still show when activity destroyed will cause leak.

Dismiss dialog when activity onDestroy to fix this issue.

Fix: 279522922
Test: Manually with "Don't keep activities"
Test: Robolectric Test
Change-Id: I445f4b160020823a6f6e2883055218c1224e2c48
This commit is contained in:
Chaohui Wang
2023-05-04 19:26:44 +08:00
parent 08b2b68e22
commit 39bf7dd530
4 changed files with 175 additions and 56 deletions

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2023 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.settings.bluetooth
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.android.controller.ActivityController
import org.robolectric.annotation.Config
import org.robolectric.shadow.api.Shadow
import org.robolectric.shadows.ShadowBluetoothAdapter
@RunWith(RobolectricTestRunner::class)
@Config(shadows = [ShadowAlertDialogCompat::class, ShadowBluetoothAdapter::class])
class RequestPermissionActivityTest {
private lateinit var activityController: ActivityController<RequestPermissionActivity>
private lateinit var bluetoothAdapter: ShadowBluetoothAdapter
@Before
fun setUp() {
bluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter())
}
@After
fun tearDown() {
activityController.pause().stop().destroy()
ShadowAlertDialogCompat.reset()
}
@Test
fun requestEnable_whenBluetoothIsOff_showConfirmDialog() {
bluetoothAdapter.setState(BluetoothAdapter.STATE_OFF)
createActivity(action = BluetoothAdapter.ACTION_REQUEST_ENABLE)
val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
val shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog)
assertThat(shadowDialog.message.toString())
.isEqualTo("An app wants to turn on Bluetooth")
}
@Test
fun requestEnable_whenBluetoothIsOn_doNothing() {
bluetoothAdapter.setState(BluetoothAdapter.STATE_ON)
createActivity(action = BluetoothAdapter.ACTION_REQUEST_ENABLE)
val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
assertThat(dialog).isNull()
}
@Test
fun requestDisable_whenBluetoothIsOff_doNothing() {
bluetoothAdapter.setState(BluetoothAdapter.STATE_OFF)
createActivity(action = BluetoothAdapter.ACTION_REQUEST_DISABLE)
val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
assertThat(dialog).isNull()
}
@Test
fun requestDisable_whenBluetoothIsOn_showConfirmDialog() {
bluetoothAdapter.setState(BluetoothAdapter.STATE_ON)
createActivity(action = BluetoothAdapter.ACTION_REQUEST_DISABLE)
val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
val shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog)
assertThat(shadowDialog.message.toString())
.isEqualTo("An app wants to turn off Bluetooth")
}
private fun createActivity(action: String) {
activityController =
ActivityController.of(RequestPermissionActivity(), Intent(action)).apply {
create()
start()
postCreate(null)
resume()
}
}
}

View File

@@ -49,13 +49,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withAppLabelAndNoTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = "App Label",
timeout = -1,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs("App Label wants to turn on Bluetooth")
}
@@ -63,13 +64,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withAppLabelAndZeroTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = "App Label",
timeout = 0,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs(
"App Label wants to turn on Bluetooth and make your phone visible to other devices. " +
@@ -80,13 +82,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withAppLabelAndNormalTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = "App Label",
timeout = 120,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs(
"App Label wants to turn on Bluetooth and make your phone visible to other devices " +
@@ -97,13 +100,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withNoAppLabelAndNoTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = null,
timeout = -1,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs("An app wants to turn on Bluetooth")
}
@@ -111,13 +115,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withNoAppLabelAndZeroTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = null,
timeout = 0,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs(
"An app wants to turn on Bluetooth and make your phone visible to other devices. " +
@@ -128,13 +133,14 @@ class RequestPermissionHelperTest {
@Test
fun requestEnable_withNoAppLabelAndNormalTimeout_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestEnable(
context = activity,
appLabel = null,
timeout = 120,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs(
"An app wants to turn on Bluetooth and make your phone visible to other devices for " +
@@ -177,12 +183,13 @@ class RequestPermissionHelperTest {
@Test
fun requestDisable_withAppLabel_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestDisable(
context = activity,
appLabel = "App Label",
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs("App Label wants to turn off Bluetooth")
}
@@ -190,12 +197,13 @@ class RequestPermissionHelperTest {
@Test
fun requestDisable_withNoAppLabel_hasCorrectMessage() {
val activity = activityController.get()
RequestPermissionHelper.requestDisable(
context = activity,
appLabel = null,
onAllow = {},
onDeny = {},
)
)!!.show()
assertLatestMessageIs("An app wants to turn off Bluetooth")
}