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

Commit f08622ca authored by Alex Kingsborough's avatar Alex Kingsborough
Browse files

Call onStopped before creating new WifiPickerTracker

Bug: 379023973
Test: Manually connect to hotspot via Quick Settings
Test: atest passing for all affected unittest files
Flag: com.android.systemui.multiuser_wifi_picker_tracker_support
Change-Id: Ife19116928a958b1b8e247796044da25d3f73a37
parent 3802103d
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.systemui.statusbar.connectivity
import android.content.Context
import android.os.UserHandle
import android.os.UserManager
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.testing.TestableLooper.RunWithLooper
import androidx.lifecycle.Lifecycle
@@ -251,9 +252,25 @@ class AccessPointControllerImplTest : SysuiTestCase() {
        val primaryUserMockContext = mock<Context>()
        mContext.prepareCreateContextAsUser(UserHandle.of(PRIMARY_USER_ID), primaryUserMockContext)
        controller.onUserSwitched(PRIMARY_USER_ID)

        // Create is expected to be called once when the test starts and a second time when the user
        // is switched.
        // is switched. The first WifiPickerTracker should have its onStop() method called prior to
        // the new WifiPickerTracker being created.
        verify(wifiPickerTrackerFactory, times(2)).create(any(), any(), any(), any())
        verify(wifiPickerTracker).onStop()
    }

    @Test
    @DisableFlags(FLAG_MULTIUSER_WIFI_PICKER_TRACKER_SUPPORT)
    fun switchUsers_flagDisabled() {
        val primaryUserMockContext = mock<Context>()
        mContext.prepareCreateContextAsUser(UserHandle.of(PRIMARY_USER_ID), primaryUserMockContext)
        controller.onUserSwitched(PRIMARY_USER_ID)

        // Create is expected to only be called once when the test starts, switching users should
        // have no effects.
        verify(wifiPickerTrackerFactory, times(1)).create(any(), any(), any(), any())
        verify(wifiPickerTracker, never()).onStop()
    }

    private companion object {
+3 −0
Original line number Diff line number Diff line
@@ -1248,10 +1248,12 @@ class WifiRepositoryImplTest : SysuiTestCase() {
                otherUserMockContext,
            )
            userRepository.setSelectedUserInfo(ANOTHER_USER)
            verify(wifiPickerTracker).onStop()

            // THEN we use the different user's context to create WifiPickerTracker
            val newCaptor = argumentCaptor<Context>()
            verify(wifiPickerTrackerFactory).create(newCaptor.capture(), any(), any(), any())
            verify(wifiPickerTracker).onStop()
            assertThat(newCaptor.firstValue).isEqualTo(otherUserMockContext)
        }

@@ -1288,6 +1290,7 @@ class WifiRepositoryImplTest : SysuiTestCase() {

            // THEN we do NOT re-create WifiPickerTracker because the multiuser flag is off
            verify(wifiPickerTrackerFactory, never()).create(any(), any(), any(), any())
            verify(wifiPickerTracker, never()).onStop()
        }

    private fun getCallback(): WifiPickerTracker.WifiPickerTrackerCallback {
+11 −6
Original line number Diff line number Diff line
@@ -132,13 +132,10 @@ public class AccessPointControllerImpl implements AccessPointController,
        }

        if (mWifiPickerTracker != null) {
            mMainExecutor.execute(() -> mLifecycle.setCurrentState(Lifecycle.State.CREATED));
            mWifiPickerTracker.onStop();
        }
        Context context = mContext.createContextAsUser(UserHandle.of(newUserId), /* flags= */ 0);
        mWifiPickerTracker = mWifiPickerTrackerFactory.create(context, mLifecycle, this, TAG);
        if (!mCallbacks.isEmpty()) {
            mMainExecutor.execute(() -> mLifecycle.setCurrentState(Lifecycle.State.STARTED));
        }
    }

    @Override
@@ -147,9 +144,13 @@ public class AccessPointControllerImpl implements AccessPointController,
        if (DEBUG) Log.d(TAG, "addCallback " + callback);
        mCallbacks.add(callback);
        if (mCallbacks.size() == 1) {
            if (mWifiPickerTrackerFactory.isSupported()) {
                mWifiPickerTracker.onStart();
            } else {
                mMainExecutor.execute(() -> mLifecycle.setCurrentState(Lifecycle.State.STARTED));
            }
        }
    }

    @Override
    public void removeAccessPointCallback(AccessPointCallback callback) {
@@ -157,9 +158,13 @@ public class AccessPointControllerImpl implements AccessPointController,
        if (DEBUG) Log.d(TAG, "removeCallback " + callback);
        mCallbacks.remove(callback);
        if (mCallbacks.isEmpty()) {
            if (mWifiPickerTrackerFactory.isSupported()) {
                mWifiPickerTracker.onStop();
            } else {
                mMainExecutor.execute(() -> mLifecycle.setCurrentState(Lifecycle.State.CREATED));
            }
        }
    }

    @Override
    public void scanForAccessPoints() {
+10 −13
Original line number Diff line number Diff line
@@ -87,8 +87,12 @@ constructor(

    override val lifecycle =
        LifecycleRegistry(this).also {
            if (multiuserWifiPickerTrackerSupport()) {
                mainExecutor.execute { it.currentState = Lifecycle.State.STARTED }
            } else {
                mainExecutor.execute { it.currentState = Lifecycle.State.CREATED }
            }
        }

    private var wifiPickerTracker: WifiPickerTracker? = null

@@ -178,6 +182,10 @@ constructor(
                                        trySend(new)
                                    }
                                }

                            // If a WifiPicker already exists, call onStop to begin its shutdown
                            // process in preparation for a new one to be created.
                            wifiPickerTracker?.onStop()
                            wifiPickerTracker =
                                wifiPickerTrackerFactory
                                    .create(currentContext, lifecycle, callback, "WifiRepository")
@@ -189,17 +197,7 @@ constructor(
                                        // costly and should be avoided whenever possible).
                                        this?.disableScanning()
                                    }

                            // The lifecycle must be STARTED in order for the callback to receive
                            // events.
                            mainExecutor.execute {
                                lifecycle.currentState = Lifecycle.State.STARTED
                            }
                            awaitClose {
                                mainExecutor.execute {
                                    lifecycle.currentState = Lifecycle.State.CREATED
                                }
                            }
                            awaitClose { mainExecutor.execute { wifiPickerTracker?.onStop() } }
                        }
                        .stateIn(scope, SharingStarted.Eagerly, current)
                }
@@ -275,7 +273,6 @@ constructor(
                                    trySend(new)
                                }
                            }

                        wifiPickerTracker =
                            wifiPickerTrackerFactory
                                .create(applicationContext, lifecycle, callback, "WifiRepository")