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

Commit 2dac5b2d authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "CustomTileTest considers HSUM when assessing user id" into main

parents fd92f9b3 81206ac4
Loading
Loading
Loading
Loading
+84 −80
Original line number Diff line number Diff line
@@ -29,12 +29,13 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.Icon
import android.os.Handler
import android.os.Parcel
import android.os.UserManager
import android.service.quicksettings.IQSTileService
import android.service.quicksettings.Tile
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import android.testing.TestableLooper
import android.view.IWindowManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.logging.MetricsLogger
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.ActivityTransitionAnimator
@@ -53,10 +54,12 @@ import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.nullable
import com.android.systemui.util.mockito.whenever
import com.google.common.truth.Truth.assertThat
import java.util.Arrays
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Assume
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -70,8 +73,6 @@ import org.mockito.Mockito.reset
import org.mockito.Mockito.verify
import org.mockito.Mockito.`when`
import org.mockito.MockitoAnnotations
import java.util.Arrays


@SmallTest
@RunWith(AndroidJUnit4::class)
@@ -106,7 +107,8 @@ class CustomTileTest : SysuiTestCase() {
    private val componentName = ComponentName(packageName, className)
    private val TILE_SPEC = CustomTile.toSpec(componentName)

    private val customTileFactory = object : CustomTile.Factory {
    private val customTileFactory =
        object : CustomTile.Factory {
            override fun create(action: String, userContext: Context): CustomTile {
                return CustomTile(
                    { tileHost },
@@ -139,8 +141,7 @@ class CustomTileTest : SysuiTestCase() {
        `when`(tileServices.getTileWrapper(any(CustomTile::class.java)))
            .thenReturn(tileServiceManager)
        `when`(tileServiceManager.tileService).thenReturn(tileService)
        `when`(packageManager.getApplicationInfo(anyString(), anyInt()))
                .thenReturn(applicationInfo)
        `when`(packageManager.getApplicationInfo(anyString(), anyInt())).thenReturn(applicationInfo)

        `when`(packageManager.getServiceInfo(any(ComponentName::class.java), anyInt()))
            .thenReturn(serviceInfo)
@@ -149,25 +150,32 @@ class CustomTileTest : SysuiTestCase() {

        serviceInfo.applicationInfo = applicationInfo


        customTile = CustomTile.create(customTileFactory, TILE_SPEC, mContext)
        customTile.initialize()
        testableLooper.processAllMessages()
    }

    @Test
    fun testCorrectUser() {
    fun testCorrectUser_notHSUM() {
        Assume.assumeFalse(UserManager.isHeadlessSystemUserMode())
        assertEquals(0, customTile.user)

        val userContext = mock(Context::class.java)
        `when`(userContext.packageManager).thenReturn(packageManager)
        `when`(userContext.userId).thenReturn(10)
        `when`(userContext.userId).thenReturn(12)

        val tile = CustomTile.create(customTileFactory, TILE_SPEC, userContext)
        tile.initialize()
        testableLooper.processAllMessages()

        assertEquals(10, tile.user)
        assertEquals(12, tile.user)
    }

    @Test
    fun testCorrectUser_whenHSUM() {
        Assume.assumeTrue(UserManager.isHeadlessSystemUserMode())
        // on HSUM the primary user would be the first real user (10)
        assertEquals(10, customTile.user)
    }

    @Test
@@ -217,8 +225,7 @@ class CustomTileTest : SysuiTestCase() {
    @Test
    fun testNoCrashOnNullDrawable() {
        customTile.qsTile.icon = mock(Icon::class.java)
        `when`(customTile.qsTile.icon.loadDrawable(any(Context::class.java)))
                .thenReturn(null)
        `when`(customTile.qsTile.icon.loadDrawable(any(Context::class.java))).thenReturn(null)
        customTile.handleUpdateState(customTile.newTileState(), null)
    }

@@ -233,9 +240,7 @@ class CustomTileTest : SysuiTestCase() {
    @Test
    fun testNoPersistedStateTileNotActive() {
        // Not active by default
        val t = Tile().apply {
            state = Tile.STATE_INACTIVE
        }
        val t = Tile().apply { state = Tile.STATE_INACTIVE }
        customTile.updateTileState(t, UID)
        testableLooper.processAllMessages()

@@ -250,7 +255,8 @@ class CustomTileTest : SysuiTestCase() {
        val contentDescription = "test_content_description"
        val stateDescription = "test_state_description"

        val t = Tile().apply {
        val t =
            Tile().apply {
                this.state = state
                this.label = label
                this.subtitle = subtitle
@@ -258,8 +264,8 @@ class CustomTileTest : SysuiTestCase() {
                this.stateDescription = stateDescription
            }
        `when`(tileServiceManager.isActiveTile).thenReturn(true)
        `when`(customTileStatePersister
                .readState(TileServiceKey(componentName, customTile.user))).thenReturn(t)
        `when`(customTileStatePersister.readState(TileServiceKey(componentName, customTile.user)))
            .thenReturn(t)
        val tile = CustomTile.create(customTileFactory, TILE_SPEC, mContext)
        tile.initialize()
        testableLooper.processAllMessages()
@@ -289,7 +295,8 @@ class CustomTileTest : SysuiTestCase() {

    @Test
    fun testStoreStateOnChange() {
        val t = Tile().apply {
        val t =
            Tile().apply {
                state = Tile.STATE_INACTIVE
                label = "test_label"
                subtitle = "test_subtitle"
@@ -371,7 +378,10 @@ class CustomTileTest : SysuiTestCase() {

        verify(activityStarter)
            .startPendingIntentMaybeDismissingKeyguard(
                eq(pi), nullable(), nullable<ActivityTransitionAnimator.Controller>())
                eq(pi),
                nullable(),
                nullable<ActivityTransitionAnimator.Controller>(),
            )
    }

    @Test
@@ -452,12 +462,8 @@ class CustomTileTest : SysuiTestCase() {
        val icon = mock(Icon::class.java)
        val drawable = context.getDrawable(R.drawable.cloud)!!
        whenever(icon.loadDrawable(any())).thenReturn(drawable)
        whenever(icon.loadDrawableCheckingUriGrant(
            any(),
            eq(ugm),
            anyInt(),
            anyString())
        ).thenReturn(drawable)
        whenever(icon.loadDrawableCheckingUriGrant(any(), eq(ugm), anyInt(), anyString()))
            .thenReturn(drawable)

        serviceInfo.icon = R.drawable.android

@@ -480,9 +486,10 @@ class CustomTileTest : SysuiTestCase() {
                areDrawablesEqual(
                    customTile.state.iconSupplier.get().getDrawable(context),
                    drawable,
                        size
                    size,
                )
            )
        ).isTrue()
            .isTrue()
    }

    @Test
@@ -491,12 +498,8 @@ class CustomTileTest : SysuiTestCase() {
        val drawable = context.getDrawable(R.drawable.cloud)!!
        val icon = mock(Icon::class.java)
        whenever(icon.loadDrawable(any())).thenReturn(drawable)
        whenever(icon.loadDrawableCheckingUriGrant(
            any(),
            eq(ugm),
            anyInt(),
            anyString())
        ).thenReturn(null)
        whenever(icon.loadDrawableCheckingUriGrant(any(), eq(ugm), anyInt(), anyString()))
            .thenReturn(null)

        // Give it an icon to prevent issues
        serviceInfo.icon = R.drawable.android
@@ -520,9 +523,10 @@ class CustomTileTest : SysuiTestCase() {
                areDrawablesEqual(
                    customTile.state.iconSupplier.get().getDrawable(context),
                    context.getDrawable(R.drawable.android)!!,
                        size
                    size,
                )
            )
        ).isTrue()
            .isTrue()
    }
}