summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2022-04-25 08:25:18 +0300
committerJuha Vuolle <juha.vuolle@insta.fi>2022-05-07 07:51:16 +0300
commitb865d41be6181b309808f432ee825dc84a670e62 (patch)
treef10ddf1073b9fca93c9b3b7ce1a75a8458bf6808 /tests
parent609e95aa7d585f8ca537d3e499e59428768afdb7 (diff)
downloadqtconnectivity-b865d41be6181b309808f432ee825dc84a670e62.tar.gz
Replace deprecated bluetooth disable/enable methods on Android
Starting from Android 13 (API level 33) the BluetoothAdapter enable() and disable() methods have been deprecated. In addition both methods are strongly discouraged as they do not necessarily trigger a permission dialogue to turn the bluetooth ON/OFF. The methods are replaced with preferred 'action requests' which are available since API level 5 and have the benefit of triggering a user dialogue when powering Bluetooth ON/OFF. The calls to these replacing APIs are surrounded by sdkVersion checks with one exception: it appears that the old enable() call does not work well when performing a multi-state transition from Discoverable => PoweredOff => Connectable. The replacing API fairs better there and hence it is replaced unconditionally. Elsewhere the sdkVersion check is for >= 31 in order to be able to test with devices available at the moment (API level 31 corresponds with Android 12). As a drive-by few related code changes: - handle hostmode enum in a switch-case instead of if-elseif - rename the opaque tokens and setConnectable() method in the broadcast receiver to better reflect their role Fixes: QTBUG-102442 Pick-to: 5.15 6.2 6.3 Change-Id: I5d9395ce9e5ecd28b1f8e2f37d13e8aea7cfcdd3 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp60
1 files changed, 34 insertions, 26 deletions
diff --git a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp
index 4052029a..c042f60b 100644
--- a/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp
+++ b/tests/auto/qbluetoothlocaldevice/tst_qbluetoothlocaldevice.cpp
@@ -86,16 +86,6 @@ tst_QBluetoothLocalDevice::tst_QBluetoothLocalDevice()
} else {
qWarning() << "Not using any remote device for testing. Set BT_TEST_DEVICE env to run manual tests involving a remote device";
}
-
- if (numDevices == 0)
- return;
-
- // start with host powered off
- QBluetoothLocalDevice *device = new QBluetoothLocalDevice();
- device->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
- delete device;
- // wait for the device to switch bluetooth mode.
- QTest::qWait(1000);
}
tst_QBluetoothLocalDevice::~tst_QBluetoothLocalDevice()
@@ -122,7 +112,12 @@ void tst_QBluetoothLocalDevice::tst_powerOn()
if (localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff) {
// Ensure device is OFF so we can test switching it ON
localDevice.setHostMode(QBluetoothLocalDevice::HostPoweredOff);
- QTRY_VERIFY(localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff);
+ // On Android user may need to authorize the transition, hence a longer timeout
+ QTRY_VERIFY_WITH_TIMEOUT(localDevice.hostMode()
+ == QBluetoothLocalDevice::HostPoweredOff, 15000);
+ // Allow possible mode-change signal(s) to arrive (QTRY_COMPARE polls the
+ // host mode in a loop, and thus may return before the host mode change signal)
+ QTest::qWait(1000);
}
QSignalSpy hostModeSpy(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)));
@@ -131,12 +126,10 @@ void tst_QBluetoothLocalDevice::tst_powerOn()
QVERIFY(hostModeSpy.isEmpty());
localDevice.powerOn();
- // async, wait for it
- QTRY_VERIFY(!hostModeSpy.isEmpty());
- QBluetoothLocalDevice::HostMode hostMode= localDevice.hostMode();
- // we should not be powered off
- QVERIFY(hostMode == QBluetoothLocalDevice::HostConnectable
- || hostMode == QBluetoothLocalDevice::HostDiscoverable);
+ // On Android user may need to authorize the transition => longer timeout.
+ QTRY_VERIFY_WITH_TIMEOUT(!hostModeSpy.isEmpty(), 15000);
+ QVERIFY(localDevice.hostMode()
+ != QBluetoothLocalDevice::HostPoweredOff);
}
void tst_QBluetoothLocalDevice::tst_powerOff()
@@ -151,7 +144,12 @@ void tst_QBluetoothLocalDevice::tst_powerOff()
if (localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff) {
// Ensure device is ON so we can test switching it OFF
localDevice.powerOn();
- QTRY_VERIFY(localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff);
+ // On Android user may need to authorize the transition => longer timeout.
+ QTRY_VERIFY_WITH_TIMEOUT(localDevice.hostMode()
+ != QBluetoothLocalDevice::HostPoweredOff, 15000);
+ // Allow possible mode-change signal(s) to arrive (QTRY_COMPARE polls the
+ // host mode in a loop, and thus may return before the host mode change signal)
+ QTest::qWait(1000);
}
QSignalSpy hostModeSpy(&localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)));
@@ -160,11 +158,10 @@ void tst_QBluetoothLocalDevice::tst_powerOff()
QVERIFY(hostModeSpy.isEmpty());
localDevice.setHostMode(QBluetoothLocalDevice::HostPoweredOff);
- // async, wait for it
- QTRY_VERIFY(!hostModeSpy.isEmpty());
- // we should not be powered off
- QVERIFY(localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff);
-
+ // On Android user may need to authorize the transition => longer timeout.
+ QTRY_VERIFY_WITH_TIMEOUT(!hostModeSpy.isEmpty(), 15000);
+ QVERIFY(localDevice.hostMode()
+ == QBluetoothLocalDevice::HostPoweredOff);
}
void tst_QBluetoothLocalDevice::tst_hostModes_data()
@@ -213,7 +210,12 @@ void tst_QBluetoothLocalDevice::tst_hostModes()
firstIteration = false;
if (localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff) {
localDevice.setHostMode(QBluetoothLocalDevice::HostPoweredOff);
- QTRY_VERIFY(localDevice.hostMode() == QBluetoothLocalDevice::HostPoweredOff);
+ // On Android user may need to authorize the transition => longer timeout.
+ QTRY_VERIFY_WITH_TIMEOUT(localDevice.hostMode()
+ == QBluetoothLocalDevice::HostPoweredOff, 15000);
+ // Allow possible mode-change signal(s) to arrive (QTRY_COMPARE polls the
+ // host mode in a loop, and thus may return before the host mode change signal).
+ QTest::qWait(1000);
}
}
@@ -226,9 +228,15 @@ void tst_QBluetoothLocalDevice::tst_hostModes()
// Switch the bluetooth mode and verify it changes
localDevice.setHostMode(hostModeExpected);
// Manual interaction may be needed (for example on Android you may
- // need to authorize a permission) => hence a longer timeout
+ // need to authorize a permission) => hence a longer timeout.
+ // Note: it seems that on some Android versions the actual resulting host mode
+ // depends on the device's bluetooth settings. This can cause the test to fail here;
+ // for instance if Bluetooth is set as 'visible to other devices', it may be that the
+ // device enters the 'discoverable' mode when attempting to enter 'connectable' (and
+ // vice versa)
QTRY_COMPARE_WITH_TIMEOUT(localDevice.hostMode(), hostModeExpected, 15000);
- // Allow possible mode-change signal(s) to arrive (QTRY_COMPARE may return immediately)
+ // Allow possible mode-change signal(s) to arrive (QTRY_COMPARE polls the
+ // host mode in a loop, and thus may return before the host mode change signal).
QTest::qWait(1000);
// Verify that signals are as expected