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
102 lines
3.5 KiB
Kotlin
102 lines
3.5 KiB
Kotlin
/*
|
|
* 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()
|
|
}
|
|
}
|
|
} |