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

Commit 1551a5bf authored by Priyanka Advani (xWF)'s avatar Priyanka Advani (xWF) Committed by Android (Google) Code Review
Browse files

Merge "Revert "Initialize UserTracker with boot user"" into main

parents ca02dae8 837ef799
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -67,7 +67,7 @@ public abstract class MultiUserUtilsModule {
            @Background CoroutineDispatcher backgroundDispatcher,
            @Background Handler handler
    ) {
        int startingUser = userManager.getBootUser().getIdentifier();
        int startingUser = ActivityManager.getCurrentUser();
        UserTrackerImpl tracker = new UserTrackerImpl(context, featureFlagsProvider, userManager,
                iActivityManager, dumpManager, appScope, backgroundDispatcher, handler);
        tracker.initialize(startingUser);
+4 −4
Original line number Diff line number Diff line
@@ -14,7 +14,6 @@ import com.android.systemui.dump.DumpManager
import com.android.systemui.flags.FakeFeatureFlagsClassic
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import java.util.concurrent.Executor
import kotlinx.coroutines.test.StandardTestDispatcher
@@ -30,6 +29,7 @@ import org.mockito.Captor
import org.mockito.Mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations

@SmallTest
@@ -73,8 +73,8 @@ class UserTrackerImplReceiveTest : SysuiTestCase() {
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        whenever(context.user).thenReturn(UserHandle.SYSTEM)
        whenever(context.createContextAsUser(ArgumentMatchers.any(), anyInt())).thenReturn(context)
        `when`(context.user).thenReturn(UserHandle.SYSTEM)
        `when`(context.createContextAsUser(ArgumentMatchers.any(), anyInt())).thenReturn(context)
    }

    @Test
@@ -94,7 +94,7 @@ class UserTrackerImplReceiveTest : SysuiTestCase() {
        tracker.addCallback(callback, executor)
        val profileID = tracker.userId + 10

        whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
            val infoProfile =
+316 −321
Original line number Diff line number Diff line
@@ -33,11 +33,9 @@ import com.android.systemui.flags.FakeFeatureFlagsClassic
import com.android.systemui.flags.Flags
import com.android.systemui.util.concurrency.FakeExecutor
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.whenever
import com.android.systemui.util.time.FakeSystemClock
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.TruthJUnit.assume
import java.util.concurrent.Executor
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestScope
@@ -56,7 +54,10 @@ import org.mockito.ArgumentMatchers.isNull
import org.mockito.Mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import java.util.concurrent.Executor


@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@@ -70,19 +71,27 @@ class UserTrackerImplTest : SysuiTestCase() {
        fun isBackgroundUserTrackerEnabled(): Iterable<Boolean> = listOf(true, false)
    }

    @Mock private lateinit var context: Context
    @Mock
    private lateinit var context: Context

    @Mock private lateinit var userManager: UserManager
    @Mock
    private lateinit var userManager: UserManager

    @Mock private lateinit var iActivityManager: IActivityManager
    @Mock
    private lateinit var iActivityManager: IActivityManager

    @Mock private lateinit var userSwitchingReply: IRemoteCallback
    @Mock
    private lateinit var userSwitchingReply: IRemoteCallback

    @Mock(stubOnly = true) private lateinit var dumpManager: DumpManager
    @Mock(stubOnly = true)
    private lateinit var dumpManager: DumpManager

    @Mock(stubOnly = true) private lateinit var handler: Handler
    @Mock(stubOnly = true)
    private lateinit var handler: Handler

    @Parameterized.Parameter @JvmField var isBackgroundUserTrackerEnabled: Boolean = false
    @Parameterized.Parameter
    @JvmField
    var isBackgroundUserTrackerEnabled: Boolean = false

    private val testScope = TestScope()
    private val testDispatcher = StandardTestDispatcher(testScope.testScheduler)
@@ -95,15 +104,15 @@ class UserTrackerImplTest : SysuiTestCase() {
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        whenever(context.userId).thenReturn(UserHandle.USER_SYSTEM)
        whenever(context.user).thenReturn(UserHandle.SYSTEM)
        whenever(context.createContextAsUser(any(), anyInt())).thenAnswer { invocation ->
        `when`(context.userId).thenReturn(UserHandle.USER_SYSTEM)
        `when`(context.user).thenReturn(UserHandle.SYSTEM)
        `when`(context.createContextAsUser(any(), anyInt())).thenAnswer { invocation ->
            val user = invocation.getArgument<UserHandle>(0)
            whenever(context.user).thenReturn(user)
            whenever(context.userId).thenReturn(user.identifier)
            `when`(context.user).thenReturn(user)
            `when`(context.userId).thenReturn(user.identifier)
            context
        }
        whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val info = UserInfo(invocation.getArgument<Int>(0), "", UserInfo.FLAG_FULL)
            listOf(info)
        }
@@ -122,35 +131,45 @@ class UserTrackerImplTest : SysuiTestCase() {
                )
    }

    @Test fun testNotInitialized() = testScope.runTest { assertThat(tracker.initialized).isFalse() }
    @Test
    fun testNotInitialized() = testScope.runTest {
        assertThat(tracker.initialized).isFalse()
    }

    @Test(expected = IllegalStateException::class)
    fun testGetUserIdBeforeInitThrowsException() = testScope.runTest { tracker.userId }
    fun testGetUserIdBeforeInitThrowsException() = testScope.runTest {
        tracker.userId
    }

    @Test(expected = IllegalStateException::class)
    fun testGetUserHandleBeforeInitThrowsException() = testScope.runTest { tracker.userHandle }
    fun testGetUserHandleBeforeInitThrowsException() = testScope.runTest {
        tracker.userHandle
    }

    @Test(expected = IllegalStateException::class)
    fun testGetUserContextBeforeInitThrowsException() = testScope.runTest { tracker.userContext }
    fun testGetUserContextBeforeInitThrowsException() = testScope.runTest {
        tracker.userContext
    }

    @Test(expected = IllegalStateException::class)
    fun testGetUserContentResolverBeforeInitThrowsException() =
        testScope.runTest { tracker.userContentResolver }
    fun testGetUserContentResolverBeforeInitThrowsException() = testScope.runTest {
        tracker.userContentResolver
    }

    @Test(expected = IllegalStateException::class)
    fun testGetUserProfilesBeforeInitThrowsException() = testScope.runTest { tracker.userProfiles }
    fun testGetUserProfilesBeforeInitThrowsException() = testScope.runTest {
        tracker.userProfiles
    }

    @Test
    fun testInitialize() =
        testScope.runTest {
    fun testInitialize() = testScope.runTest {
        tracker.initialize(0)

        assertThat(tracker.initialized).isTrue()
    }

    @Test
    fun testReceiverRegisteredOnInitialize() =
        testScope.runTest {
    fun testReceiverRegisteredOnInitialize() = testScope.runTest {
        tracker.initialize(0)

        val captor = ArgumentCaptor.forClass(IntentFilter::class.java)
@@ -174,8 +193,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testInitialValuesSet() =
        testScope.runTest {
    fun testInitialValuesSet() = testScope.runTest {
        val testID = 4
        tracker.initialize(testID)

@@ -192,8 +210,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testUserSwitch() =
        testScope.runTest {
    fun testUserSwitch() = testScope.runTest {
        tracker.initialize(0)
        val newID = 5

@@ -217,16 +234,14 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testManagedProfileAvailable() =
        testScope.runTest {
    fun testManagedProfileAvailable() = testScope.runTest {
        tracker.initialize(0)
        val profileID = tracker.userId + 10

            whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
                val infoProfile =
                    UserInfo(
            val infoProfile = UserInfo(
                    id + 10,
                    "",
                    "",
@@ -237,26 +252,22 @@ class UserTrackerImplTest : SysuiTestCase() {
            listOf(info, infoProfile)
        }

            val intent =
                Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
        val intent = Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))
        tracker.onReceive(context, intent)

            assertThat(tracker.userProfiles.map { it.id })
                .containsExactly(tracker.userId, profileID)
        assertThat(tracker.userProfiles.map { it.id }).containsExactly(tracker.userId, profileID)
    }

    @Test
    fun testManagedProfileUnavailable() =
        testScope.runTest {
    fun testManagedProfileUnavailable() = testScope.runTest {
        tracker.initialize(0)
        val profileID = tracker.userId + 10

            whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
                val infoProfile =
                    UserInfo(
            val infoProfile = UserInfo(
                    id + 10,
                    "",
                    "",
@@ -267,26 +278,22 @@ class UserTrackerImplTest : SysuiTestCase() {
            listOf(info, infoProfile)
        }

            val intent =
                Intent(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)
        val intent = Intent(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))
        tracker.onReceive(context, intent)

            assertThat(tracker.userProfiles.map { it.id })
                .containsExactly(tracker.userId, profileID)
        assertThat(tracker.userProfiles.map { it.id }).containsExactly(tracker.userId, profileID)
    }

    @Test
    fun testManagedProfileStartedAndRemoved() =
        testScope.runTest {
    fun testManagedProfileStartedAndRemoved() = testScope.runTest {
        tracker.initialize(0)
        val profileID = tracker.userId + 10

            whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
                val infoProfile =
                    UserInfo(
            val infoProfile = UserInfo(
                    id + 10,
                    "",
                    "",
@@ -298,20 +305,17 @@ class UserTrackerImplTest : SysuiTestCase() {
        }

        // Managed profile started
            val intent =
                Intent(Intent.ACTION_MANAGED_PROFILE_UNLOCKED)
        val intent = Intent(Intent.ACTION_MANAGED_PROFILE_UNLOCKED)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))
        tracker.onReceive(context, intent)

            assertThat(tracker.userProfiles.map { it.id })
                .containsExactly(tracker.userId, profileID)
        assertThat(tracker.userProfiles.map { it.id }).containsExactly(tracker.userId, profileID)

            whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            listOf(UserInfo(invocation.getArgument(0), "", UserInfo.FLAG_FULL))
        }

            val intent2 =
                Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED)
        val intent2 = Intent(Intent.ACTION_MANAGED_PROFILE_REMOVED)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))
        tracker.onReceive(context, intent2)

@@ -319,8 +323,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testCallbackNotCalledOnAdd() =
        testScope.runTest {
    fun testCallbackNotCalledOnAdd() = testScope.runTest {
        tracker.initialize(0)
        val callback = TestCallback()

@@ -331,8 +334,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testCallbackCalledOnUserChanging() =
        testScope.runTest {
    fun testCallbackCalledOnUserChanging() = testScope.runTest {
        tracker.initialize(0)
        val callback = TestCallback()
        tracker.addCallback(callback, executor)
@@ -352,8 +354,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testAsyncCallbackWaitsUserToChange() =
        testScope.runTest {
    fun testAsyncCallbackWaitsUserToChange() = testScope.runTest {
        // Skip this test for CountDownLatch variation. The problem is that there would be a
        // deadlock if the callbacks processing runs on the same thread as the callback (which
        // is blocked by the latch). Before the change it works because the callbacks are
@@ -385,8 +386,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testCallbackCalledOnUserChanged() =
        testScope.runTest {
    fun testCallbackCalledOnUserChanged() = testScope.runTest {
        tracker.initialize(0)
        val callback = TestCallback()
        tracker.addCallback(callback, executor)
@@ -407,18 +407,16 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testCallbackCalledOnUserInfoChanged() =
        testScope.runTest {
    fun testCallbackCalledOnUserInfoChanged() = testScope.runTest {
        tracker.initialize(0)
        val callback = TestCallback()
        tracker.addCallback(callback, executor)
        val profileID = tracker.userId + 10

            whenever(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
        `when`(userManager.getProfiles(anyInt())).thenAnswer { invocation ->
            val id = invocation.getArgument<Int>(0)
            val info = UserInfo(id, "", UserInfo.FLAG_FULL)
                val infoProfile =
                    UserInfo(
            val infoProfile = UserInfo(
                    id + 10,
                    "",
                    "",
@@ -429,8 +427,7 @@ class UserTrackerImplTest : SysuiTestCase() {
            listOf(info, infoProfile)
        }

            val intent =
                Intent(Intent.ACTION_USER_INFO_CHANGED)
        val intent = Intent(Intent.ACTION_USER_INFO_CHANGED)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))

        tracker.onReceive(context, intent)
@@ -441,8 +438,7 @@ class UserTrackerImplTest : SysuiTestCase() {
    }

    @Test
    fun testCallbackRemoved() =
        testScope.runTest {
    fun testCallbackRemoved() = testScope.runTest {
        tracker.initialize(0)
        val newID = 5
        val profileID = newID + 10
@@ -458,8 +454,7 @@ class UserTrackerImplTest : SysuiTestCase() {
        verify(userSwitchingReply).sendResult(any())
        captor.value.onUserSwitchComplete(newID)

            val intentProfiles =
                Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
        val intentProfiles = Intent(Intent.ACTION_MANAGED_PROFILE_AVAILABLE)
                .putExtra(Intent.EXTRA_USER, UserHandle.of(profileID))

        tracker.onReceive(context, intentProfiles)