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

Commit d6a4a88c authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Android (Google) Code Review
Browse files

Merge "Fixed threading issue in LocationControllerImpl" into rvc-dev

parents c86de6f9 8764413d
Loading
Loading
Loading
Loading
+23 −7
Original line number Original line Diff line number Diff line
@@ -38,6 +38,7 @@ import androidx.annotation.VisibleForTesting;
import com.android.systemui.BootCompleteCache;
import com.android.systemui.BootCompleteCache;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Background;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.util.Utils;
import com.android.systemui.util.Utils;


import java.util.ArrayList;
import java.util.ArrayList;
@@ -64,16 +65,16 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio


    private boolean mAreActiveLocationRequests;
    private boolean mAreActiveLocationRequests;


    private ArrayList<LocationChangeCallback> mSettingsChangeCallbacks =
    private final H mHandler;
            new ArrayList<LocationChangeCallback>();
    private final H mHandler = new H();


    @Inject
    @Inject
    public LocationControllerImpl(Context context, @Background Looper bgLooper,
    public LocationControllerImpl(Context context, @Main Looper mainLooper,
            BroadcastDispatcher broadcastDispatcher, BootCompleteCache bootCompleteCache) {
            @Background Looper bgLooper, BroadcastDispatcher broadcastDispatcher,
            BootCompleteCache bootCompleteCache) {
        mContext = context;
        mContext = context;
        mBroadcastDispatcher = broadcastDispatcher;
        mBroadcastDispatcher = broadcastDispatcher;
        mBootCompleteCache = bootCompleteCache;
        mBootCompleteCache = bootCompleteCache;
        mHandler = new H(mainLooper);


        // Register to listen for changes in location settings.
        // Register to listen for changes in location settings.
        IntentFilter filter = new IntentFilter();
        IntentFilter filter = new IntentFilter();
@@ -94,12 +95,12 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio
     * Add a callback to listen for changes in location settings.
     * Add a callback to listen for changes in location settings.
     */
     */
    public void addCallback(LocationChangeCallback cb) {
    public void addCallback(LocationChangeCallback cb) {
        mSettingsChangeCallbacks.add(cb);
        mHandler.obtainMessage(H.MSG_ADD_CALLBACK, cb).sendToTarget();
        mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED);
        mHandler.sendEmptyMessage(H.MSG_LOCATION_SETTINGS_CHANGED);
    }
    }


    public void removeCallback(LocationChangeCallback cb) {
    public void removeCallback(LocationChangeCallback cb) {
        mSettingsChangeCallbacks.remove(cb);
        mHandler.obtainMessage(H.MSG_REMOVE_CALLBACK, cb).sendToTarget();
    }
    }


    /**
    /**
@@ -208,6 +209,14 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio
    private final class H extends Handler {
    private final class H extends Handler {
        private static final int MSG_LOCATION_SETTINGS_CHANGED = 1;
        private static final int MSG_LOCATION_SETTINGS_CHANGED = 1;
        private static final int MSG_LOCATION_ACTIVE_CHANGED = 2;
        private static final int MSG_LOCATION_ACTIVE_CHANGED = 2;
        private static final int MSG_ADD_CALLBACK = 3;
        private static final int MSG_REMOVE_CALLBACK = 4;

        private ArrayList<LocationChangeCallback> mSettingsChangeCallbacks = new ArrayList<>();

        H(Looper looper) {
            super(looper);
        }


        @Override
        @Override
        public void handleMessage(Message msg) {
        public void handleMessage(Message msg) {
@@ -218,6 +227,13 @@ public class LocationControllerImpl extends BroadcastReceiver implements Locatio
                case MSG_LOCATION_ACTIVE_CHANGED:
                case MSG_LOCATION_ACTIVE_CHANGED:
                    locationActiveChanged();
                    locationActiveChanged();
                    break;
                    break;
                case MSG_ADD_CALLBACK:
                    mSettingsChangeCallbacks.add((LocationChangeCallback) msg.obj);
                    break;
                case MSG_REMOVE_CALLBACK:
                    mSettingsChangeCallbacks.remove((LocationChangeCallback) msg.obj);
                    break;

            }
            }
        }
        }


+50 −6
Original line number Original line Diff line number Diff line
@@ -14,8 +14,11 @@


package com.android.systemui.statusbar.policy;
package com.android.systemui.statusbar.policy;


import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.when;


import android.content.Intent;
import android.content.Intent;
@@ -32,7 +35,6 @@ import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback;
import com.android.systemui.statusbar.policy.LocationController.LocationChangeCallback;


import org.junit.Before;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;


@@ -42,17 +44,19 @@ import org.junit.runner.RunWith;
public class LocationControllerImplTest extends SysuiTestCase {
public class LocationControllerImplTest extends SysuiTestCase {


    private LocationControllerImpl mLocationController;
    private LocationControllerImpl mLocationController;
    private TestableLooper mTestableLooper;


    @Before
    @Before
    public void setup() {
    public void setup() {
        mTestableLooper = TestableLooper.get(this);
        mLocationController = spy(new LocationControllerImpl(mContext,
        mLocationController = spy(new LocationControllerImpl(mContext,
                TestableLooper.get(this).getLooper(),
                mTestableLooper.getLooper(),
                mTestableLooper.getLooper(),
                mock(BroadcastDispatcher.class),
                mock(BroadcastDispatcher.class),
                mock(BootCompleteCache.class)));
                mock(BootCompleteCache.class)));
    }
    }


    @Test
    @Test
    @Ignore("flaky")
    public void testRemoveSelfActive_DoesNotCrash() {
    public void testRemoveSelfActive_DoesNotCrash() {
        LocationController.LocationChangeCallback callback = new LocationChangeCallback() {
        LocationController.LocationChangeCallback callback = new LocationChangeCallback() {
            @Override
            @Override
@@ -70,11 +74,10 @@ public class LocationControllerImplTest extends SysuiTestCase {
        mLocationController.onReceive(mContext, new Intent(
        mLocationController.onReceive(mContext, new Intent(
                LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION));
                LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION));


        TestableLooper.get(this).processAllMessages();
        mTestableLooper.processAllMessages();
    }
    }


    @Test
    @Test
    @Ignore("flaky")
    public void testRemoveSelfSettings_DoesNotCrash() {
    public void testRemoveSelfSettings_DoesNotCrash() {
        LocationController.LocationChangeCallback callback = new LocationChangeCallback() {
        LocationController.LocationChangeCallback callback = new LocationChangeCallback() {
            @Override
            @Override
@@ -85,6 +88,47 @@ public class LocationControllerImplTest extends SysuiTestCase {
        mLocationController.addCallback(callback);
        mLocationController.addCallback(callback);
        mLocationController.addCallback(mock(LocationChangeCallback.class));
        mLocationController.addCallback(mock(LocationChangeCallback.class));


        TestableLooper.get(this).processAllMessages();
        mTestableLooper.processAllMessages();
    }

    @Test
    public void testAddCallback_notifiedImmediately() {
        LocationChangeCallback callback = mock(LocationChangeCallback.class);

        mLocationController.addCallback(callback);

        mTestableLooper.processAllMessages();

        verify(callback).onLocationSettingsChanged(anyBoolean());
    }

    @Test
    public void testCallbackNotified() {
        LocationChangeCallback callback = mock(LocationChangeCallback.class);

        mLocationController.addCallback(callback);
        mLocationController.onReceive(mContext, new Intent(LocationManager.MODE_CHANGED_ACTION));

        mTestableLooper.processAllMessages();

        verify(callback, times(2)).onLocationSettingsChanged(anyBoolean());
    }

    @Test
    public void testCallbackRemoved() {
        LocationChangeCallback callback = mock(LocationChangeCallback.class);

        mLocationController.addCallback(callback);
        mTestableLooper.processAllMessages();

        verify(callback).onLocationSettingsChanged(anyBoolean());
        mLocationController.removeCallback(callback);

        mLocationController.onReceive(mContext, new Intent(LocationManager.MODE_CHANGED_ACTION));

        mTestableLooper.processAllMessages();

        // No new callbacks
        verify(callback).onLocationSettingsChanged(anyBoolean());
    }
    }
}
}