Add bluetooth tests to managed profile.
Test whether the basic bluetooth API is callable from
a managed profile.
Those tests correspond to the ones done in
android.bluetooth.cts.BasicAdapterTest for primary users.
Bug: 18466733
Change-Id: Id294ccba45c3ccc7dc87b85483c0d1e58dfd606f
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
index 008ed38..301f90b 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
@@ -18,6 +18,8 @@
package="com.android.cts.managedprofile">
<uses-sdk android:minSdkVersion="20"/>
+ <uses-permission android:name="android.permission.BLUETOOTH" />
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application>
<uses-library android:name="android.test.runner" />
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/BluetoothTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/BluetoothTest.java
new file mode 100644
index 0000000..4d7ddeb
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/BluetoothTest.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright (C) 2014 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.cts.managedprofile;
+
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothServerSocket;
+import android.test.AndroidTestCase;
+
+import java.io.IOException;
+import java.util.Set;
+import java.util.UUID;
+
+/**
+ * Test that the basic bluetooth API is callable in managed profiles.
+ * These tests should only be executed if the device supports bluetooth,
+ * i.e. if it has the {@link android.content.pm.PackageManager#FEATURE_BLUETOOTH} feature.
+ *
+ * This includes tests for the {@link BluetoothAdapter}.
+ * The corresponding CTS tests in the primary profile are in
+ * {@link android.bluetooth.cts.BasicAdapterTest}.
+ * TODO: Merge the primary and managed profile tests into one.
+ */
+public class BluetoothTest extends AndroidTestCase {
+ private static final int DISABLE_TIMEOUT_MS = 8000;
+ private static final int ENABLE_TIMEOUT_MS = 10000;
+ private static final int POLL_TIME_MS = 400;
+ private static final int CHECK_WAIT_TIME_MS = 1000;
+
+ private BluetoothAdapter mAdapter;
+ private boolean mBtWasEnabled;
+
+ public void setUp() throws Exception {
+ super.setUp();
+ mAdapter = BluetoothAdapter.getDefaultAdapter();
+ assertNotNull(mAdapter);
+ mBtWasEnabled = mAdapter.isEnabled();
+ }
+
+ public void tearDown() throws Exception {
+ if (mBtWasEnabled != mAdapter.isEnabled()) {
+ if (mBtWasEnabled) {
+ enable();
+ } else {
+ disable();
+ }
+ }
+ super.tearDown();
+ }
+
+ /**
+ * Checks enable(), disable(), getState(), isEnabled()
+ */
+ public void testEnableDisable() {
+ disable();
+ enable();
+ }
+
+ /**
+ * Test the getAddress() function.
+ */
+ public void testGetAddress() {
+ assertTrue(BluetoothAdapter.checkBluetoothAddress(mAdapter.getAddress()));
+ }
+
+ /**
+ * Tests the listenUsingRfcommWithServiceRecord function.
+ */
+ public void testListenUsingRfcommWithServiceRecord() throws IOException {
+ enable();
+ BluetoothServerSocket socket = mAdapter.listenUsingRfcommWithServiceRecord(
+ "test", UUID.randomUUID());
+ assertNotNull(socket);
+ socket.close();
+ }
+
+ /**
+ * Test the getRemoteDevice() function.
+ */
+ public void testGetRemoteDevice() {
+ // getRemoteDevice() should work even with Bluetooth disabled
+ disable();
+
+ // test bad addresses
+ try {
+ mAdapter.getRemoteDevice((String)null);
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ mAdapter.getRemoteDevice("00:00:00:00:00:00:00:00");
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ mAdapter.getRemoteDevice((byte[])null);
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ mAdapter.getRemoteDevice(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00});
+ fail("IllegalArgumentException not thrown");
+ } catch (IllegalArgumentException e) {
+ }
+
+ // test success
+ BluetoothDevice device = mAdapter.getRemoteDevice("00:11:22:AA:BB:CC");
+ assertNotNull(device);
+ assertEquals("00:11:22:AA:BB:CC", device.getAddress());
+ device = mAdapter.getRemoteDevice(
+ new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06});
+ assertNotNull(device);
+ assertEquals("01:02:03:04:05:06", device.getAddress());
+ }
+
+ /**
+ * Helper to turn BT off.
+ * This method will either fail on an assert, or return with BT turned off.
+ * Behavior of getState() and isEnabled() are validated along the way.
+ */
+ private void disable() {
+ sleep(CHECK_WAIT_TIME_MS);
+ if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) {
+ assertFalse(mAdapter.isEnabled());
+ return;
+ }
+
+ assertEquals(BluetoothAdapter.STATE_ON, mAdapter.getState());
+ assertTrue(mAdapter.isEnabled());
+ assertTrue(mAdapter.disable());
+ boolean turnOff = false;
+ for (int i=0; i<DISABLE_TIMEOUT_MS/POLL_TIME_MS; i++) {
+ sleep(POLL_TIME_MS);
+ int state = mAdapter.getState();
+ switch (state) {
+ case BluetoothAdapter.STATE_OFF:
+ assertFalse(mAdapter.isEnabled());
+ return;
+ default:
+ if (state != BluetoothAdapter.STATE_ON || turnOff) {
+ assertEquals(BluetoothAdapter.STATE_TURNING_OFF, state);
+ turnOff = true;
+ }
+ break;
+ }
+ }
+ fail("disable() timeout");
+ }
+
+ /**
+ * Helper to turn BT on.
+ * This method will either fail on an assert, or return with BT turned on.
+ * Behavior of getState() and isEnabled() are validated along the way.
+ */
+ private void enable() {
+ sleep(CHECK_WAIT_TIME_MS);
+ if (mAdapter.getState() == BluetoothAdapter.STATE_ON) {
+ assertTrue(mAdapter.isEnabled());
+ return;
+ }
+
+ assertEquals(BluetoothAdapter.STATE_OFF, mAdapter.getState());
+ assertFalse(mAdapter.isEnabled());
+ assertTrue(mAdapter.enable());
+ boolean turnOn = false;
+ for (int i=0; i<ENABLE_TIMEOUT_MS/POLL_TIME_MS; i++) {
+ sleep(POLL_TIME_MS);
+ int state = mAdapter.getState();
+ switch (state) {
+ case BluetoothAdapter.STATE_ON:
+ assertTrue(mAdapter.isEnabled());
+ return;
+ default:
+ if (state != BluetoothAdapter.STATE_OFF || turnOn) {
+ assertEquals(BluetoothAdapter.STATE_TURNING_ON, state);
+ turnOn = true;
+ }
+ break;
+ }
+ }
+ fail("enable() timeout");
+ }
+
+ private void sleep(long t) {
+ try {
+ Thread.sleep(t);
+ } catch (InterruptedException e) {}
+ }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
index 544ddff..cd85188 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
@@ -228,7 +228,7 @@
}
}
- private boolean hasDeviceFeatures(String[] requiredFeatures)
+ protected boolean hasDeviceFeatures(String[] requiredFeatures)
throws DeviceNotAvailableException {
// TODO: Move this logic to ITestDevice.
String command = "pm list features";
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index 51b7930..2e2dab8 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -37,6 +37,7 @@
private static final String ADMIN_RECEIVER_TEST_CLASS =
MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
+ private static final String FEATURE_BLUETOOTH = "android.hardware.bluetooth";
private int mUserId;
@Override
@@ -198,6 +199,23 @@
addRestrictionCommandOutput.contains("SecurityException"));
}
+ // Test the bluetooth API from a managed profile.
+ public void testBluetooth() throws Exception {
+ boolean mHasBluetooth = hasDeviceFeatures(new String[] {FEATURE_BLUETOOTH});
+ if (!mHasFeature || !mHasBluetooth) {
+ return ;
+ }
+
+ assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
+ "testEnableDisable", mUserId));
+ assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
+ "testGetAddress", mUserId));
+ assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
+ "testListenUsingRfcommWithServiceRecord", mUserId));
+ assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".BluetoothTest",
+ "testGetRemoteDevice", mUserId));
+ }
+
private void disableActivityForUser(String activityName, int userId)
throws DeviceNotAvailableException {
String command = "am start -W --user " + userId