Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c6fdd4b authored by William Escande's avatar William Escande
Browse files

Force bluetooth to stay off during instrumentation

Bluetooth is already turned off at the start of the instrumentation, but
nothing prevent it for being turned back on before the end of the test.
This CL is enabling satellite mode to prevent the bluetooth to be turned
on by external factor during the whole test.

Bug: 292141273
Test: atest BluetoothInstrumentationTests + cmd bluetooth enable
Change-Id: I9c4e3530d0b632201e1787d964fc1871f94d0d3b
parent ac19a64d
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -284,17 +284,29 @@ public class AdapterService extends Service {
        return sAdapterService;
    }

    private static synchronized void setAdapterService(AdapterService instance) {
        Log.d(TAG, "setAdapterService() - trying to set service to " + instance);
    /** Allow test to set an AdapterService to be return by AdapterService.getAdapterService() */
    @VisibleForTesting
    public static synchronized void setAdapterService(AdapterService instance) {
        if (instance == null) {
            Log.e(TAG, "setAdapterService() - instance is null");
            return;
        }
        Log.d(TAG, "setAdapterService() - set service to " + instance);
        sAdapterService = instance;
    }

    private static synchronized void clearAdapterService(AdapterService current) {
        if (sAdapterService == current) {
    /** Clear test Adapter service. See {@code setAdapterService} */
    @VisibleForTesting
    public static synchronized void clearAdapterService(AdapterService instance) {
        if (sAdapterService == instance) {
            Log.d(TAG, "clearAdapterService() - This adapter was cleared " + instance);
            sAdapterService = null;
        } else {
            Log.d(
                    TAG,
                    "clearAdapterService() - incorrect cleared adapter."
                            + (" Instance=" + instance)
                            + (" vs sAdapterService=" + sAdapterService));
        }
    }

+4 −0
Original line number Diff line number Diff line
@@ -30,6 +30,10 @@
        <option name="run-command" value="settings put global ble_scan_always_enabled 0" />
        <option name="run-command" value="cmd bluetooth_manager disable" />
        <option name="run-command" value="cmd bluetooth_manager wait-for-state:STATE_OFF" />
        <option name="run-command" value="settings put global satellite_mode_radios bluetooth" />
        <option name="run-command" value="settings put global satellite_mode_enabled 1" />
        <option name="teardown-command" value="settings delete global satellite_mode_radios" />
        <option name="teardown-command" value="settings put global satellite_mode_enabled 0" />
        <option name="teardown-command" value="cmd bluetooth_manager enable" />
        <option name="teardown-command" value="cmd bluetooth_manager wait-for-state:STATE_ON" />
        <option name="teardown-command" value="settings put global ble_scan_always_enabled 1" />
+9 −30
Original line number Diff line number Diff line
@@ -51,8 +51,6 @@ import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
@@ -93,49 +91,30 @@ public class TestUtils {
    /**
     * Set the return value of {@link AdapterService#getAdapterService()} to a test specified value
     *
     * @param adapterService the designated {@link AdapterService} in test, must not be null, can
     *                       be mocked or spied
     * @throws NoSuchMethodException     when setAdapterService method is not found
     * @throws IllegalAccessException    when setAdapterService method cannot be accessed
     * @throws InvocationTargetException when setAdapterService method cannot be invoked, which
     *                                   should never happen since setAdapterService is a static
     *                                   method
     * @param adapterService the designated {@link AdapterService} in test, must not be null, can be
     *     mocked or spied
     */
    public static void setAdapterService(AdapterService adapterService)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    public static void setAdapterService(AdapterService adapterService) {
        Assert.assertNull("AdapterService.getAdapterService() must be null before setting another"
                + " AdapterService", AdapterService.getAdapterService());
        Assert.assertNotNull("Adapter service should not be null", adapterService);
        // We cannot mock AdapterService.getAdapterService() with Mockito.
        // Hence we need to use reflection to call a private method to
        // initialize properly the AdapterService.sAdapterService field.
        Method method =
                AdapterService.class.getDeclaredMethod("setAdapterService", AdapterService.class);
        method.setAccessible(true);
        method.invoke(null, adapterService);
        // Hence we need to set AdapterService.sAdapterService field.
        AdapterService.setAdapterService(adapterService);
    }

    /**
     * Clear the return value of {@link AdapterService#getAdapterService()} to null
     *
     * @param adapterService the {@link AdapterService} used when calling
     *                       {@link TestUtils#setAdapterService(AdapterService)}
     * @throws NoSuchMethodException     when clearAdapterService method is not found
     * @throws IllegalAccessException    when clearAdapterService method cannot be accessed
     * @throws InvocationTargetException when clearAdappterService method cannot be invoked,
     *                                   which should never happen since clearAdapterService is a
     *                                   static method
     * @param adapterService the {@link AdapterService} used when calling {@link
     *     TestUtils#setAdapterService(AdapterService)}
     */
    public static void clearAdapterService(AdapterService adapterService)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    public static void clearAdapterService(AdapterService adapterService) {
        Assert.assertSame("AdapterService.getAdapterService() must return the same object as the"
                        + " supplied adapterService in this method", adapterService,
                AdapterService.getAdapterService());
        Assert.assertNotNull("Adapter service should not be null", adapterService);
        Method method =
                AdapterService.class.getDeclaredMethod("clearAdapterService", AdapterService.class);
        method.setAccessible(true);
        method.invoke(null, adapterService);
        AdapterService.clearAdapterService(adapterService);
    }

    /**