bluetooth: Fix Android unit tests on S+ devices
BT unit tests weren't updated to match S+ permission models, which led to crashes and failures. BUG=374111733 TEST=device_unittests Change-Id: I0384da609bf03f7bfa62ac3eaa2a8295b0898bd3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5949987 Reviewed-by: Matt Reynolds <mattreynolds@chromium.org> Commit-Queue: Garfield Tan <xutan@chromium.org> Reviewed-by: Jack Hsieh <chengweih@chromium.org> Cr-Commit-Position: refs/heads/main@{#1372321}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
89af7371f4
commit
d685286338
device/bluetooth
@ -35,6 +35,7 @@
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
#if BUILDFLAG(IS_ANDROID)
|
||||
#include "base/android/build_info.h"
|
||||
#include "device/bluetooth/test/bluetooth_test_android.h"
|
||||
#elif BUILDFLAG(IS_APPLE)
|
||||
#include "device/bluetooth/test/bluetooth_test_mac.h"
|
||||
@ -742,6 +743,13 @@ TEST_P(BluetoothTestWinrt, ConstructDefaultAdapter) {
|
||||
#else
|
||||
TEST_F(BluetoothTest, MAYBE_ConstructDefaultAdapter) {
|
||||
#endif
|
||||
#if BUILDFLAG(IS_ANDROID)
|
||||
if (base::android::BuildInfo::GetInstance()->sdk_int() >=
|
||||
base::android::SDK_VERSION_S) {
|
||||
GTEST_SKIP() << "Android S+ requires runtime permissions that can't be "
|
||||
"granted automatically, skipping unit test.";
|
||||
}
|
||||
#endif // BUILDFLAG(IS_ANDROID)
|
||||
InitWithDefaultAdapter();
|
||||
if (!adapter_->IsPresent() || !adapter_->IsPowered()) {
|
||||
GTEST_SKIP()
|
||||
@ -931,6 +939,11 @@ TEST_F(BluetoothTest, NoLocationServices) {
|
||||
if (!PlatformSupportsLowEnergy()) {
|
||||
GTEST_SKIP() << "Low Energy Bluetooth unavailable, skipping unit test.";
|
||||
}
|
||||
if (base::android::BuildInfo::GetInstance()->sdk_int() >=
|
||||
base::android::SDK_VERSION_S) {
|
||||
GTEST_SKIP() << "Android S+ doesn't require location services perform "
|
||||
"Bluetooth scanning, skipping unit test.";
|
||||
}
|
||||
InitWithFakeAdapter();
|
||||
TestBluetoothAdapterObserver observer(adapter_);
|
||||
|
||||
|
@ -13,6 +13,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.ParcelUuid;
|
||||
import android.test.mock.MockContext;
|
||||
@ -121,8 +122,12 @@ class Fakes {
|
||||
}
|
||||
|
||||
@CalledByNative("FakeBluetoothAdapter")
|
||||
public void setFakeContextLocationPermission(boolean enabled) {
|
||||
mFakeContext.setLocationPermission(enabled);
|
||||
public void setFakePermission(boolean enabled) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
mFakeContext.setBluetoothPermission(enabled);
|
||||
} else {
|
||||
mFakeContext.setLocationPermission(enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/** Creates and discovers a new device. */
|
||||
@ -369,15 +374,23 @@ class Fakes {
|
||||
|
||||
/** Fakes android.content.Context by extending MockContext. */
|
||||
static class FakeContext extends MockContext {
|
||||
private boolean mLocationPermission;
|
||||
private int mLocationPermission;
|
||||
private int mBluetoothPermission;
|
||||
|
||||
public FakeContext() {
|
||||
super();
|
||||
mLocationPermission = true;
|
||||
mLocationPermission = PackageManager.PERMISSION_GRANTED;
|
||||
mBluetoothPermission = PackageManager.PERMISSION_GRANTED;
|
||||
}
|
||||
|
||||
public void setLocationPermission(boolean enabled) {
|
||||
mLocationPermission = enabled;
|
||||
mLocationPermission = (enabled ? PackageManager.PERMISSION_GRANTED
|
||||
: PackageManager.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
public void setBluetoothPermission(boolean enabled) {
|
||||
mBluetoothPermission = (enabled ? PackageManager.PERMISSION_GRANTED
|
||||
: PackageManager.PERMISSION_DENIED);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -404,11 +417,15 @@ class Fakes {
|
||||
|
||||
@Override
|
||||
public int checkCallingOrSelfPermission(String permission) {
|
||||
final boolean isBluetoothPermissionSOrAbove =
|
||||
permission.equals(Manifest.permission.BLUETOOTH_SCAN)
|
||||
|| permission.equals(Manifest.permission.BLUETOOTH_CONNECT);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && isBluetoothPermissionSOrAbove) {
|
||||
return mBluetoothPermission;
|
||||
}
|
||||
if (permission.equals(Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
|| permission.equals(Manifest.permission.ACCESS_COARSE_LOCATION)) {
|
||||
return mLocationPermission
|
||||
? PackageManager.PERMISSION_GRANTED
|
||||
: PackageManager.PERMISSION_DENIED;
|
||||
return mLocationPermission;
|
||||
}
|
||||
return PackageManager.PERMISSION_DENIED;
|
||||
}
|
||||
|
@ -97,8 +97,8 @@ void BluetoothTestAndroid::InitWithFakeAdapter() {
|
||||
}
|
||||
|
||||
bool BluetoothTestAndroid::DenyPermission() {
|
||||
Java_FakeBluetoothAdapter_setFakeContextLocationPermission(
|
||||
AttachCurrentThread(), j_fake_bluetooth_adapter_, false);
|
||||
Java_FakeBluetoothAdapter_setFakePermission(AttachCurrentThread(),
|
||||
j_fake_bluetooth_adapter_, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user