Loading core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java +62 −46 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Set; import java.util.Set; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; public class BluetoothTestUtils extends Assert { public class BluetoothTestUtils extends Assert { Loading Loading @@ -505,40 +507,47 @@ public class BluetoothTestUtils extends Assert { * @param adapter The BT adapter. * @param adapter The BT adapter. */ */ public void discoverable(BluetoothAdapter adapter) { public void discoverable(BluetoothAdapter adapter) { int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_DISCOVERABLE_FLAG; if (!adapter.isEnabled()) { if (!adapter.isEnabled()) { fail("discoverable() bluetooth not enabled"); fail("discoverable() bluetooth not enabled"); } } int scanMode = adapter.getScanMode(); int scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE) { return; return; } } BluetoothReceiver receiver = getBluetoothReceiver(mask); final Semaphore completionSemaphore = new Semaphore(0); final BroadcastReceiver receiver = new BroadcastReceiver() { assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE, scanMode); @Override long start = System.currentTimeMillis(); public void onReceive(Context context, Intent intent) { assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); final String action = intent.getAction(); if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) { while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) { scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE && (receiver.getFiredFlags() & mask) == mask) { writeOutput(String.format("discoverable() completed in %d ms", (receiver.getCompletedTime() - start))); removeReceiver(receiver); return; return; } } sleep(POLL_TIME); final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { completionSemaphore.release(); } } } }; int firedFlags = receiver.getFiredFlags(); final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); removeReceiver(receiver); mContext.registerReceiver(receiver, filter); fail(String.format("discoverable() timeout: scanMode=%d (expected %d), flags=0x%x " assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, boolean success = false; firedFlags, mask)); try { success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT, TimeUnit.MILLISECONDS); writeOutput(String.format("discoverable() completed in 0 ms"); } catch (final InterruptedException e) { // This should never happen but just in case it does, the test will fail anyway. } mContext.unregisterReceiver(receiver); if (!success) { fail(String.format("discoverable() timeout: scanMode=%d (expected %d)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); } } } /** /** Loading @@ -548,40 +557,47 @@ public class BluetoothTestUtils extends Assert { * @param adapter The BT adapter. * @param adapter The BT adapter. */ */ public void undiscoverable(BluetoothAdapter adapter) { public void undiscoverable(BluetoothAdapter adapter) { int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_FLAG; if (!adapter.isEnabled()) { if (!adapter.isEnabled()) { fail("undiscoverable() bluetooth not enabled"); fail("undiscoverable() bluetooth not enabled"); } } int scanMode = adapter.getScanMode(); int scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) { if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { return; return; } } BluetoothReceiver receiver = getBluetoothReceiver(mask); final Semaphore completionSemaphore = new Semaphore(0); final BroadcastReceiver receiver = new BroadcastReceiver() { assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, scanMode); @Override long start = System.currentTimeMillis(); public void onReceive(Context context, Intent intent) { assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE)); final String action = intent.getAction(); if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) { while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) { scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE && (receiver.getFiredFlags() & mask) == mask) { writeOutput(String.format("undiscoverable() completed in %d ms", (receiver.getCompletedTime() - start))); removeReceiver(receiver); return; return; } } sleep(POLL_TIME); final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) { completionSemaphore.release(); } } } }; int firedFlags = receiver.getFiredFlags(); final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); removeReceiver(receiver); mContext.registerReceiver(receiver, filter); fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d), flags=0x%x " assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE)); + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE, firedFlags, boolean success = false; mask)); try { success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT, TimeUnit.MILLISECONDS); writeOutput(String.format("undiscoverable() completed in 0 ms"); } catch (InterruptedException e) { // This should never happen but just in case it does, the test will fail anyway. } mContext.unregisterReceiver(receiver); if (!success) { fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE)); } } } /** /** Loading Loading
core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java +62 −46 Original line number Original line Diff line number Diff line Loading @@ -35,6 +35,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.ArrayList; import java.util.List; import java.util.List; import java.util.Set; import java.util.Set; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; public class BluetoothTestUtils extends Assert { public class BluetoothTestUtils extends Assert { Loading Loading @@ -505,40 +507,47 @@ public class BluetoothTestUtils extends Assert { * @param adapter The BT adapter. * @param adapter The BT adapter. */ */ public void discoverable(BluetoothAdapter adapter) { public void discoverable(BluetoothAdapter adapter) { int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_DISCOVERABLE_FLAG; if (!adapter.isEnabled()) { if (!adapter.isEnabled()) { fail("discoverable() bluetooth not enabled"); fail("discoverable() bluetooth not enabled"); } } int scanMode = adapter.getScanMode(); int scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE) { return; return; } } BluetoothReceiver receiver = getBluetoothReceiver(mask); final Semaphore completionSemaphore = new Semaphore(0); final BroadcastReceiver receiver = new BroadcastReceiver() { assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE, scanMode); @Override long start = System.currentTimeMillis(); public void onReceive(Context context, Intent intent) { assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); final String action = intent.getAction(); if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) { while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) { scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE && (receiver.getFiredFlags() & mask) == mask) { writeOutput(String.format("discoverable() completed in %d ms", (receiver.getCompletedTime() - start))); removeReceiver(receiver); return; return; } } sleep(POLL_TIME); final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { completionSemaphore.release(); } } } }; int firedFlags = receiver.getFiredFlags(); final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); removeReceiver(receiver); mContext.registerReceiver(receiver, filter); fail(String.format("discoverable() timeout: scanMode=%d (expected %d), flags=0x%x " assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, boolean success = false; firedFlags, mask)); try { success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT, TimeUnit.MILLISECONDS); writeOutput(String.format("discoverable() completed in 0 ms"); } catch (final InterruptedException e) { // This should never happen but just in case it does, the test will fail anyway. } mContext.unregisterReceiver(receiver); if (!success) { fail(String.format("discoverable() timeout: scanMode=%d (expected %d)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)); } } } /** /** Loading @@ -548,40 +557,47 @@ public class BluetoothTestUtils extends Assert { * @param adapter The BT adapter. * @param adapter The BT adapter. */ */ public void undiscoverable(BluetoothAdapter adapter) { public void undiscoverable(BluetoothAdapter adapter) { int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_FLAG; if (!adapter.isEnabled()) { if (!adapter.isEnabled()) { fail("undiscoverable() bluetooth not enabled"); fail("undiscoverable() bluetooth not enabled"); } } int scanMode = adapter.getScanMode(); int scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) { if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { return; return; } } BluetoothReceiver receiver = getBluetoothReceiver(mask); final Semaphore completionSemaphore = new Semaphore(0); final BroadcastReceiver receiver = new BroadcastReceiver() { assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, scanMode); @Override long start = System.currentTimeMillis(); public void onReceive(Context context, Intent intent) { assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE)); final String action = intent.getAction(); if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) { while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) { scanMode = adapter.getScanMode(); if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE && (receiver.getFiredFlags() & mask) == mask) { writeOutput(String.format("undiscoverable() completed in %d ms", (receiver.getCompletedTime() - start))); removeReceiver(receiver); return; return; } } sleep(POLL_TIME); final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE); if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) { completionSemaphore.release(); } } } }; int firedFlags = receiver.getFiredFlags(); final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); removeReceiver(receiver); mContext.registerReceiver(receiver, filter); fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d), flags=0x%x " assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE)); + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE, firedFlags, boolean success = false; mask)); try { success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT, TimeUnit.MILLISECONDS); writeOutput(String.format("undiscoverable() completed in 0 ms"); } catch (InterruptedException e) { // This should never happen but just in case it does, the test will fail anyway. } mContext.unregisterReceiver(receiver); if (!success) { fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE)); } } } /** /** Loading