Loading android/app/tests/unit/src/com/android/bluetooth/TestUtils.java +25 −0 Original line number Diff line number Diff line Loading @@ -28,7 +28,9 @@ import android.content.pm.PackageManager; import android.content.res.Resources; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.MessageQueue; import android.os.test.TestLooper; import android.service.media.MediaBrowserService; import android.util.Log; Loading Loading @@ -56,6 +58,7 @@ import java.util.HashMap; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; /** * A set of methods useful in Bluetooth instrumentation tests Loading Loading @@ -284,6 +287,28 @@ public class TestUtils { }); } /** * Dispatch all the message on the Loopper and check that the `what` is expected * * @param looper looper to execute the message from * @param what list of Messages.what that are expected to be run by the handler */ public static void syncHandler(TestLooper looper, int... what) { IntStream.of(what) .forEach( w -> { Message msg = looper.nextMessage(); assertWithMessage("Expecting [" + w + "] instead of null Msg") .that(msg) .isNotNull(); assertWithMessage("Not the expected Message:\n" + msg) .that(msg.what) .isEqualTo(w); Log.d(TAG, "Processing message: " + msg); msg.getTarget().dispatchMessage(msg); }); } /** * Wait for looper to become idle * Loading android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +17 −37 Original line number Diff line number Diff line Loading @@ -119,7 +119,6 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.List; import java.util.stream.IntStream; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; Loading Loading @@ -366,13 +365,8 @@ public class AdapterServiceTest { PeriodicScanNativeInterface.setInstance(null); } /** * Dispatch all the message on the Loopper and check that the `what` is expected * * @param what list of message that are expected to be run by the handler */ private void syncHandler(int... what) { syncHandler(mLooper, what); TestUtils.syncHandler(mLooper, what); } private void dropNextMessage(int what) { Loading @@ -382,20 +376,6 @@ public class AdapterServiceTest { Log.d(TAG, "Message dropped on purpose: " + msg); } private static void syncHandler(TestLooper looper, int... what) { IntStream.of(what) .forEach( w -> { Message msg = looper.nextMessage(); assertThat(msg).isNotNull(); assertWithMessage("Not the expected Message:\n" + msg) .that(msg.what) .isEqualTo(w); Log.d(TAG, "Processing message: " + msg); msg.getTarget().dispatchMessage(msg); }); } private void verifyStateChange(int prevState, int currState) { try { verify(mIBluetoothCallback).onBluetoothStateChange(prevState, currState); Loading Loading @@ -438,16 +418,16 @@ public class AdapterServiceTest { IBluetoothCallback callback, AdapterNativeInterface nativeInterface) { adapter.enable(false); syncHandler(looper, AdapterState.BLE_TURN_ON); TestUtils.syncHandler(looper, AdapterState.BLE_TURN_ON); verifyStateChange(callback, STATE_OFF, STATE_BLE_TURNING_ON); syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); verify(nativeInterface).enable(); adapter.stateChangeCallback(AbstractionLayer.BT_STATE_ON); syncHandler(looper, AdapterState.BLE_STARTED); TestUtils.syncHandler(looper, AdapterState.BLE_STARTED); verifyStateChange(callback, STATE_BLE_TURNING_ON, STATE_BLE_ON); assertThat(adapter.getState()).isEqualTo(STATE_BLE_ON); } Loading @@ -460,7 +440,7 @@ public class AdapterServiceTest { boolean onlyGatt, List<ProfileService> services) { adapter.disable(); syncHandler(looper, AdapterState.USER_TURN_OFF); TestUtils.syncHandler(looper, AdapterState.USER_TURN_OFF); verifyStateChange(callback, STATE_ON, STATE_TURNING_OFF); if (!onlyGatt) { Loading @@ -469,11 +449,11 @@ public class AdapterServiceTest { for (ProfileService service : services) { adapter.onProfileServiceStateChanged(service, STATE_OFF); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); } } syncHandler(looper, AdapterState.BREDR_STOPPED); TestUtils.syncHandler(looper, AdapterState.BREDR_STOPPED); verifyStateChange(callback, STATE_TURNING_OFF, STATE_BLE_ON); assertThat(adapter.getState()).isEqualTo(STATE_BLE_ON); Loading Loading @@ -510,7 +490,7 @@ public class AdapterServiceTest { offToBleOn(looper, gattService, adapter, ctx, callback, nativeInterface); adapter.startBrEdr(); syncHandler(looper, AdapterState.USER_TURN_ON); TestUtils.syncHandler(looper, AdapterState.USER_TURN_ON); verifyStateChange(callback, STATE_BLE_ON, STATE_TURNING_ON); if (!onlyGatt) { Loading @@ -519,16 +499,16 @@ public class AdapterServiceTest { for (ProfileService service : services) { adapter.addProfile(service); syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); } // Keep in 2 separate loop to first add the services and then eventually trigger the // ON transition during the callback for (ProfileService service : services) { adapter.onProfileServiceStateChanged(service, STATE_ON); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); } } syncHandler(looper, AdapterState.BREDR_STARTED); TestUtils.syncHandler(looper, AdapterState.BREDR_STARTED); verifyStateChange(callback, STATE_TURNING_ON, STATE_ON); assertThat(adapter.getState()).isEqualTo(STATE_ON); Loading Loading @@ -566,15 +546,15 @@ public class AdapterServiceTest { onToBleOn(looper, adapter, ctx, callback, onlyGatt, services); adapter.stopBle(); syncHandler(looper, AdapterState.BLE_TURN_OFF); TestUtils.syncHandler(looper, AdapterState.BLE_TURN_OFF); verifyStateChange(callback, STATE_BLE_ON, STATE_BLE_TURNING_OFF); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); syncHandler(looper, MESSAGE_PROFILE_SERVICE_UNREGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_UNREGISTERED); verify(nativeInterface).disable(); adapter.stateChangeCallback(AbstractionLayer.BT_STATE_OFF); syncHandler(looper, AdapterState.BLE_STOPPED); TestUtils.syncHandler(looper, AdapterState.BLE_STOPPED); verifyStateChange(callback, STATE_BLE_TURNING_OFF, STATE_OFF); assertThat(adapter.getState()).isEqualTo(STATE_OFF); Loading Loading @@ -818,7 +798,7 @@ public class AdapterServiceTest { verify(mNativeInterface).disable(); mAdapterService.stateChangeCallback(AbstractionLayer.BT_STATE_OFF); syncHandler(mLooper, AdapterState.BLE_STOPPED); syncHandler(AdapterState.BLE_STOPPED); verifyStateChange(STATE_BLE_TURNING_OFF, STATE_OFF); assertThat(mAdapterService.getState()).isEqualTo(STATE_OFF); Loading Loading
android/app/tests/unit/src/com/android/bluetooth/TestUtils.java +25 −0 Original line number Diff line number Diff line Loading @@ -28,7 +28,9 @@ import android.content.pm.PackageManager; import android.content.res.Resources; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.MessageQueue; import android.os.test.TestLooper; import android.service.media.MediaBrowserService; import android.util.Log; Loading Loading @@ -56,6 +58,7 @@ import java.util.HashMap; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.stream.IntStream; /** * A set of methods useful in Bluetooth instrumentation tests Loading Loading @@ -284,6 +287,28 @@ public class TestUtils { }); } /** * Dispatch all the message on the Loopper and check that the `what` is expected * * @param looper looper to execute the message from * @param what list of Messages.what that are expected to be run by the handler */ public static void syncHandler(TestLooper looper, int... what) { IntStream.of(what) .forEach( w -> { Message msg = looper.nextMessage(); assertWithMessage("Expecting [" + w + "] instead of null Msg") .that(msg) .isNotNull(); assertWithMessage("Not the expected Message:\n" + msg) .that(msg.what) .isEqualTo(w); Log.d(TAG, "Processing message: " + msg); msg.getTarget().dispatchMessage(msg); }); } /** * Wait for looper to become idle * Loading
android/app/tests/unit/src/com/android/bluetooth/btservice/AdapterServiceTest.java +17 −37 Original line number Diff line number Diff line Loading @@ -119,7 +119,6 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.List; import java.util.stream.IntStream; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; Loading Loading @@ -366,13 +365,8 @@ public class AdapterServiceTest { PeriodicScanNativeInterface.setInstance(null); } /** * Dispatch all the message on the Loopper and check that the `what` is expected * * @param what list of message that are expected to be run by the handler */ private void syncHandler(int... what) { syncHandler(mLooper, what); TestUtils.syncHandler(mLooper, what); } private void dropNextMessage(int what) { Loading @@ -382,20 +376,6 @@ public class AdapterServiceTest { Log.d(TAG, "Message dropped on purpose: " + msg); } private static void syncHandler(TestLooper looper, int... what) { IntStream.of(what) .forEach( w -> { Message msg = looper.nextMessage(); assertThat(msg).isNotNull(); assertWithMessage("Not the expected Message:\n" + msg) .that(msg.what) .isEqualTo(w); Log.d(TAG, "Processing message: " + msg); msg.getTarget().dispatchMessage(msg); }); } private void verifyStateChange(int prevState, int currState) { try { verify(mIBluetoothCallback).onBluetoothStateChange(prevState, currState); Loading Loading @@ -438,16 +418,16 @@ public class AdapterServiceTest { IBluetoothCallback callback, AdapterNativeInterface nativeInterface) { adapter.enable(false); syncHandler(looper, AdapterState.BLE_TURN_ON); TestUtils.syncHandler(looper, AdapterState.BLE_TURN_ON); verifyStateChange(callback, STATE_OFF, STATE_BLE_TURNING_ON); syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); verify(nativeInterface).enable(); adapter.stateChangeCallback(AbstractionLayer.BT_STATE_ON); syncHandler(looper, AdapterState.BLE_STARTED); TestUtils.syncHandler(looper, AdapterState.BLE_STARTED); verifyStateChange(callback, STATE_BLE_TURNING_ON, STATE_BLE_ON); assertThat(adapter.getState()).isEqualTo(STATE_BLE_ON); } Loading @@ -460,7 +440,7 @@ public class AdapterServiceTest { boolean onlyGatt, List<ProfileService> services) { adapter.disable(); syncHandler(looper, AdapterState.USER_TURN_OFF); TestUtils.syncHandler(looper, AdapterState.USER_TURN_OFF); verifyStateChange(callback, STATE_ON, STATE_TURNING_OFF); if (!onlyGatt) { Loading @@ -469,11 +449,11 @@ public class AdapterServiceTest { for (ProfileService service : services) { adapter.onProfileServiceStateChanged(service, STATE_OFF); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); } } syncHandler(looper, AdapterState.BREDR_STOPPED); TestUtils.syncHandler(looper, AdapterState.BREDR_STOPPED); verifyStateChange(callback, STATE_TURNING_OFF, STATE_BLE_ON); assertThat(adapter.getState()).isEqualTo(STATE_BLE_ON); Loading Loading @@ -510,7 +490,7 @@ public class AdapterServiceTest { offToBleOn(looper, gattService, adapter, ctx, callback, nativeInterface); adapter.startBrEdr(); syncHandler(looper, AdapterState.USER_TURN_ON); TestUtils.syncHandler(looper, AdapterState.USER_TURN_ON); verifyStateChange(callback, STATE_BLE_ON, STATE_TURNING_ON); if (!onlyGatt) { Loading @@ -519,16 +499,16 @@ public class AdapterServiceTest { for (ProfileService service : services) { adapter.addProfile(service); syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_REGISTERED); } // Keep in 2 separate loop to first add the services and then eventually trigger the // ON transition during the callback for (ProfileService service : services) { adapter.onProfileServiceStateChanged(service, STATE_ON); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); } } syncHandler(looper, AdapterState.BREDR_STARTED); TestUtils.syncHandler(looper, AdapterState.BREDR_STARTED); verifyStateChange(callback, STATE_TURNING_ON, STATE_ON); assertThat(adapter.getState()).isEqualTo(STATE_ON); Loading Loading @@ -566,15 +546,15 @@ public class AdapterServiceTest { onToBleOn(looper, adapter, ctx, callback, onlyGatt, services); adapter.stopBle(); syncHandler(looper, AdapterState.BLE_TURN_OFF); TestUtils.syncHandler(looper, AdapterState.BLE_TURN_OFF); verifyStateChange(callback, STATE_BLE_ON, STATE_BLE_TURNING_OFF); syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); syncHandler(looper, MESSAGE_PROFILE_SERVICE_UNREGISTERED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_STATE_CHANGED); TestUtils.syncHandler(looper, MESSAGE_PROFILE_SERVICE_UNREGISTERED); verify(nativeInterface).disable(); adapter.stateChangeCallback(AbstractionLayer.BT_STATE_OFF); syncHandler(looper, AdapterState.BLE_STOPPED); TestUtils.syncHandler(looper, AdapterState.BLE_STOPPED); verifyStateChange(callback, STATE_BLE_TURNING_OFF, STATE_OFF); assertThat(adapter.getState()).isEqualTo(STATE_OFF); Loading Loading @@ -818,7 +798,7 @@ public class AdapterServiceTest { verify(mNativeInterface).disable(); mAdapterService.stateChangeCallback(AbstractionLayer.BT_STATE_OFF); syncHandler(mLooper, AdapterState.BLE_STOPPED); syncHandler(AdapterState.BLE_STOPPED); verifyStateChange(STATE_BLE_TURNING_OFF, STATE_OFF); assertThat(mAdapterService.getState()).isEqualTo(STATE_OFF); Loading