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

Commit 1fa54591 authored by Terry Cheong's avatar Terry Cheong
Browse files

Extract logic in notifyWiredAccessoryChanged to make it testable

Extract the calculation logic so that newHeadsetState is returned from a
function. Then we can test the change in between with pre-defined set of
events.

Bug: 411037918
Test: Plug in HDMI and 3.5mm headphone and verify both audio devices exist
Flag: EXEMPT Strict mechanical refactors
Change-Id: Ib1fd68a6ab27705fdaed80529bbe802136ecee79
parent 3f43b564
Loading
Loading
Loading
Loading
+42 −35
Original line number Diff line number Diff line
@@ -154,27 +154,13 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
        }
    }

    @Override
    public void notifyWiredAccessoryChanged(
            long whenNanos, int switchValues, int switchMask) {
        notifyWiredAccessoryChanged(whenNanos, switchValues, switchMask, false /*isSynchronous*/);
    }

    public void notifyWiredAccessoryChanged(
            long whenNanos, int switchValues, int switchMask, boolean isSynchronous) {
        if (LOG) {
            Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos
                    + " bits=" + switchCodeToString(switchValues, switchMask)
                    + " mask=" + Integer.toHexString(switchMask));
        }

        synchronized (mLock) {
    static int calculateHeadsetState(int headsetState, int switchValues, int switchMask) {
        // Assumptions:
        // 1. Events will only be plug 1 device or unplug 1 device.
        //    It would not have plug and unplug in the same time.
        // 2. events for LINEOUT devices won't have a mask that
        //    is SW_AVOUT_INSERT_BITS
            int newHeadsetState = mHeadsetState;
        int newHeadsetState = headsetState;
        if ((switchMask & SW_HEADSET_INSERT_BITS) != 0) {
            int clearMask = BIT_HEADSET | BIT_HEADSET_NO_MIC;

@@ -205,6 +191,27 @@ final class WiredAccessoryManager implements WiredAccessoryCallbacks {
            newHeadsetState = newHeadsetState | device;
        }

        return newHeadsetState;
    }

    @Override
    public void notifyWiredAccessoryChanged(
            long whenNanos, int switchValues, int switchMask) {
        notifyWiredAccessoryChanged(whenNanos, switchValues, switchMask, false /*isSynchronous*/);
    }

    public void notifyWiredAccessoryChanged(
            long whenNanos, int switchValues, int switchMask, boolean isSynchronous) {
        if (LOG) {
            Slog.v(TAG, "notifyWiredAccessoryChanged: when=" + whenNanos
                    + " bits=" + switchCodeToString(switchValues, switchMask)
                    + " mask=" + Integer.toHexString(switchMask));
        }

        synchronized (mLock) {
            // Extracting the logic for unit test
            int newHeadsetState = calculateHeadsetState(mHeadsetState, switchValues, switchMask);

            updateLocked(NAME_H2W, newHeadsetState, isSynchronous);
        }