1 /*
2  * Copyright (C) 2023 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 package com.android.settings.bluetooth
17 
18 import android.bluetooth.BluetoothAdapter
19 import android.content.Intent
20 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat
21 import com.google.common.truth.Truth.assertThat
22 import org.junit.After
23 import org.junit.Before
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.robolectric.RobolectricTestRunner
27 import org.robolectric.android.controller.ActivityController
28 import org.robolectric.annotation.Config
29 import org.robolectric.shadow.api.Shadow
30 import org.robolectric.shadows.ShadowBluetoothAdapter
31 
32 @RunWith(RobolectricTestRunner::class)
33 @Config(shadows = [ShadowAlertDialogCompat::class, ShadowBluetoothAdapter::class])
34 class RequestPermissionActivityTest {
35     private lateinit var activityController: ActivityController<RequestPermissionActivity>
36     private lateinit var bluetoothAdapter: ShadowBluetoothAdapter
37 
38     @Before
setUpnull39     fun setUp() {
40         bluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter())
41     }
42 
43     @After
tearDownnull44     fun tearDown() {
45         activityController.pause().stop().destroy()
46         ShadowAlertDialogCompat.reset()
47     }
48 
49     @Test
requestEnable_whenBluetoothIsOff_showConfirmDialognull50     fun requestEnable_whenBluetoothIsOff_showConfirmDialog() {
51         bluetoothAdapter.setState(BluetoothAdapter.STATE_OFF)
52 
53         createActivity(action = BluetoothAdapter.ACTION_REQUEST_ENABLE)
54 
55         val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
56         val shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog)
57         assertThat(shadowDialog.message.toString())
58             .isEqualTo("An app wants to turn on Bluetooth")
59     }
60 
61     @Test
requestEnable_whenBluetoothIsOn_doNothingnull62     fun requestEnable_whenBluetoothIsOn_doNothing() {
63         bluetoothAdapter.setState(BluetoothAdapter.STATE_ON)
64 
65         createActivity(action = BluetoothAdapter.ACTION_REQUEST_ENABLE)
66 
67         val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
68         assertThat(dialog).isNull()
69     }
70 
71     @Test
requestDisable_whenBluetoothIsOff_doNothingnull72     fun requestDisable_whenBluetoothIsOff_doNothing() {
73         bluetoothAdapter.setState(BluetoothAdapter.STATE_OFF)
74 
75         createActivity(action = BluetoothAdapter.ACTION_REQUEST_DISABLE)
76 
77         val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
78         assertThat(dialog).isNull()
79     }
80 
81     @Test
requestDisable_whenBluetoothIsOn_showConfirmDialognull82     fun requestDisable_whenBluetoothIsOn_showConfirmDialog() {
83         bluetoothAdapter.setState(BluetoothAdapter.STATE_ON)
84 
85         createActivity(action = BluetoothAdapter.ACTION_REQUEST_DISABLE)
86 
87         val dialog = ShadowAlertDialogCompat.getLatestAlertDialog()
88         val shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog)
89         assertThat(shadowDialog.message.toString())
90             .isEqualTo("An app wants to turn off Bluetooth")
91     }
92 
createActivitynull93     private fun createActivity(action: String) {
94         activityController =
95             ActivityController.of(RequestPermissionActivity(), Intent(action)).apply {
96                 create()
97                 start()
98                 postCreate(null)
99                 resume()
100             }
101     }
102 }