bluetooth CTS test failure.

The bluetooth statemachine task may be delayed to run
 which cause the CTS bluetooth test failed, also
 the bluetooth CTS test check not consistently
 because adapter.getState can be changed at any time,
 you can't suppose the two continuous call
 will return the same result. The first transition from
 ON to TURNING_OFF when disable or from OFF to TURNING_ON
 when enable can be delayed at undetermined time due to
 bluetooth statemachine task may be blocked. Change the CTS
 test code to make it not depend on undetermined time delay.

bug 6948773
Change-Id: I07bc6fb4da64197609a3915faa62005755b46ce2
diff --git a/tests/tests/bluetooth/src/android/bluetooth/cts/BasicAdapterTest.java b/tests/tests/bluetooth/src/android/bluetooth/cts/BasicAdapterTest.java
index e64da89..8616a6d 100644
--- a/tests/tests/bluetooth/src/android/bluetooth/cts/BasicAdapterTest.java
+++ b/tests/tests/bluetooth/src/android/bluetooth/cts/BasicAdapterTest.java
@@ -31,9 +31,10 @@
  * BluetoothAdapter}.
  */
 public class BasicAdapterTest extends AndroidTestCase {
-    private static final int DISABLE_TIMEOUT = 5000;  // ms timeout for BT disable
+    private static final int DISABLE_TIMEOUT = 8000;  // ms timeout for BT disable
     private static final int ENABLE_TIMEOUT = 10000;  // ms timeout for BT enable
     private static final int POLL_TIME = 400;         // ms to poll BT state
+    private static final int CHECK_WAIT_TIME = 1000;  // ms to wait before enable/disable
 
     private boolean mHasBluetooth;
 
@@ -222,6 +223,7 @@
      * Behavior of getState() and isEnabled() are validated along the way.
      */
     private void disable(BluetoothAdapter adapter) {
+        sleep(CHECK_WAIT_TIME);
         if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
             assertFalse(adapter.isEnabled());
             return;
@@ -230,15 +232,19 @@
         assertEquals(BluetoothAdapter.STATE_ON, adapter.getState());
         assertTrue(adapter.isEnabled());
         adapter.disable();
+        boolean turnOff = false;
         for (int i=0; i<DISABLE_TIMEOUT/POLL_TIME; i++) {
             sleep(POLL_TIME);
-            switch (adapter.getState()) {
+            int state = adapter.getState();
+            switch (state) {
             case BluetoothAdapter.STATE_OFF:
                 assertFalse(adapter.isEnabled());
                 return;
             default:
-                assertEquals(BluetoothAdapter.STATE_TURNING_OFF, adapter.getState());
-                assertFalse(adapter.isEnabled());
+                if (state != BluetoothAdapter.STATE_ON || turnOff) {
+                    assertEquals(BluetoothAdapter.STATE_TURNING_OFF, state);
+                    turnOff = true;
+                }
                 break;
             }
         }
@@ -250,6 +256,7 @@
      * Behavior of getState() and isEnabled() are validated along the way.
      */
     private void enable(BluetoothAdapter adapter) {
+        sleep(CHECK_WAIT_TIME);
         if (adapter.getState() == BluetoothAdapter.STATE_ON) {
             assertTrue(adapter.isEnabled());
             return;
@@ -258,15 +265,19 @@
         assertEquals(BluetoothAdapter.STATE_OFF, adapter.getState());
         assertFalse(adapter.isEnabled());
         adapter.enable();
+        boolean turnOn = false;
         for (int i=0; i<ENABLE_TIMEOUT/POLL_TIME; i++) {
             sleep(POLL_TIME);
-            switch (adapter.getState()) {
+            int state = adapter.getState();
+            switch (state) {
             case BluetoothAdapter.STATE_ON:
                 assertTrue(adapter.isEnabled());
                 return;
             default:
-                assertEquals(BluetoothAdapter.STATE_TURNING_ON, adapter.getState());
-                assertFalse(adapter.isEnabled());
+                if (state != BluetoothAdapter.STATE_OFF || turnOn) {
+                    assertEquals(BluetoothAdapter.STATE_TURNING_ON, state);
+                    turnOn = true;
+                }
                 break;
             }
         }