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

Commit dd9556ec authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Force bluetooth to stay off during instrumentation" into main am: 4c73b789

parents c31f76ab 4c73b789
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -285,17 +285,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
@@ -52,8 +52,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;
@@ -94,49 +92,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);
    }

    /** Helper function to mock getSystemService calls */