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

Commit 9ef128f9 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes I0275a64f,Ib32abd5b into main

* changes:
  SystemServer: test race condition on enable
  SystemServer: Remove un-necessary synchronize
parents 0c8ef807 3bac6704
Loading
Loading
Loading
Loading
+15 −23
Original line number Diff line number Diff line
@@ -991,9 +991,7 @@ class BluetoothManagerService {
            Log.i(TAG, "enableBle: Bluetooth is already in state" + mState);
            return true;
        }
        synchronized (mReceiver) {
        sendEnableMsg(false, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName, true);
        }
        return true;
    }

@@ -1118,11 +1116,9 @@ class BluetoothManagerService {
            return false;
        }

        synchronized (mReceiver) {
        mQuietEnableExternal = true;
        mEnableExternal = true;
        sendEnableMsg(true, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }

@@ -1144,13 +1140,11 @@ class BluetoothManagerService {
            return false;
        }

        synchronized (mReceiver) {
        mQuietEnableExternal = false;
        mEnableExternal = true;
        AirplaneModeListener.notifyUserToggledBluetooth(
                mContentResolver, mCurrentUserContext, true);
        sendEnableMsg(false, ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }

@@ -1167,7 +1161,6 @@ class BluetoothManagerService {
                        + (" isBinding=" + isBinding())
                        + (" mState=" + mState));

        synchronized (mReceiver) {
        AirplaneModeListener.notifyUserToggledBluetooth(
                mContentResolver, mCurrentUserContext, false);

@@ -1176,7 +1169,6 @@ class BluetoothManagerService {
        }
        mEnableExternal = false;
        sendDisableMsg(ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
        }
        return true;
    }

+40 −7
Original line number Diff line number Diff line
@@ -308,14 +308,8 @@ public class BluetoothManagerServiceTest {
        IBluetoothCallback btCallback = captureBluetoothCallback(mAdapterBinder);
        verify(mAdapterBinder).offToBleOn(anyBoolean(), any());

        // AdapterService is sending AdapterState.BLE_TURN_ON that will trigger this callback
        // and in parallel it call its `bringUpBle()`
        btCallback.onBluetoothStateChange(STATE_OFF, STATE_BLE_TURNING_ON);
        syncHandler(MESSAGE_BLUETOOTH_STATE_CHANGE);
        assertThat(mManagerService.getState()).isEqualTo(STATE_BLE_TURNING_ON);

        // assertThat(mManagerService.waitForManagerState(STATE_BLE_TURNING_ON)).isTrue();

        // GattService has been started by AdapterService and it will enable native side then
        // trigger the stateChangeCallback from native
        btCallback.onBluetoothStateChange(STATE_BLE_TURNING_ON, STATE_BLE_ON);
@@ -325,7 +319,7 @@ public class BluetoothManagerServiceTest {

    private IBluetoothCallback transition_offToOn() throws Exception {
        IBluetoothCallback btCallback = transition_offToBleOn();
        verify(mAdapterBinder, times(1)).bleOnToOn(any());
        verify(mAdapterBinder).bleOnToOn(any());

        // AdapterService go to turning_on and start all profile on its own
        btCallback.onBluetoothStateChange(STATE_BLE_ON, STATE_TURNING_ON);
@@ -342,6 +336,45 @@ public class BluetoothManagerServiceTest {
        return btCallback;
    }

    @Test
    public void enable_whileTurningToBleOn_shouldEnable() throws Exception {
        mManagerService.enableBle("enable_whileTurningToBleOn_shouldEnable", mBinder);
        syncHandler(MESSAGE_ENABLE);

        acceptBluetoothBinding(mBinder, "btservice.AdapterService", 1);
        IBluetoothCallback btCallback = captureBluetoothCallback(mAdapterBinder);
        assertThat(mManagerService.getState()).isEqualTo(STATE_BLE_TURNING_ON);

        // receive enable when Bluetooth is in BLE_TURNING_ON
        mManagerService.enable("enable_whileTurningToBleOn_shouldEnable");
        syncHandler(MESSAGE_ENABLE);

        btCallback.onBluetoothStateChange(STATE_BLE_TURNING_ON, STATE_BLE_ON);
        syncHandler(MESSAGE_BLUETOOTH_STATE_CHANGE);

        verify(mAdapterBinder).bleOnToOn(any());
    }

    @Test
    public void enable_whileNotYetBoundToBle_shouldEnable() throws Exception {
        mManagerService.enableBle("enable_whileTurningToBleOn_shouldEnable", mBinder);
        syncHandler(MESSAGE_ENABLE);
        assertThat(mManagerService.getState()).isEqualTo(STATE_OFF);

        // receive enable when Bluetooth is OFF and not yet binded
        mManagerService.enable("enable_whileTurningToBleOn_shouldEnable");
        syncHandler(MESSAGE_ENABLE);

        acceptBluetoothBinding(mBinder, "btservice.AdapterService", 1);
        IBluetoothCallback btCallback = captureBluetoothCallback(mAdapterBinder);
        assertThat(mManagerService.getState()).isEqualTo(STATE_BLE_TURNING_ON);

        btCallback.onBluetoothStateChange(STATE_BLE_TURNING_ON, STATE_BLE_ON);
        syncHandler(MESSAGE_BLUETOOTH_STATE_CHANGE);

        verify(mAdapterBinder).bleOnToOn(any());
    }

    @Test
    public void offToBleOn() throws Exception {
        mManagerService.enableBle("test_offToBleOn", mBinder);