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

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

Merge "Prevent Resources$NotFoundException when loading resources by string" into main

parents f5c769f3 1240447f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -205,7 +205,7 @@ constructor(
        fun getDimen(context: Context, name: String): Int {
            val res = context.packageManager.getResourcesForApplication(context.packageName)
            val id = res.getIdentifier(name, "dimen", context.packageName)
            return res.getDimensionPixelSize(id)
            return if (id == 0) 0 else res.getDimensionPixelSize(id)
        }
    }
}
+55 −33
Original line number Diff line number Diff line
@@ -67,12 +67,15 @@ class ClockSectionTest : SysuiTestCase() {
        context.resources.getDimensionPixelSize(R.dimen.keyguard_clock_top_margin) +
            Utils.getStatusBarHeaderHeightKeyguard(context)

    private val LARGE_CLOCK_TOP =
    private val LARGE_CLOCK_TOP_WITHOUT_SMARTSPACE =
        context.resources.getDimensionPixelSize(R.dimen.status_bar_height) +
            context.resources.getDimensionPixelSize(
                com.android.systemui.customization.R.dimen.small_clock_padding_top
            ) +
            context.resources.getDimensionPixelSize(R.dimen.keyguard_smartspace_top_offset) +
            context.resources.getDimensionPixelSize(R.dimen.keyguard_smartspace_top_offset)

    private val LARGE_CLOCK_TOP =
        LARGE_CLOCK_TOP_WITHOUT_SMARTSPACE +
            SMART_SPACE_DATE_WEATHER_HEIGHT +
            ENHANCED_SMART_SPACE_HEIGHT

@@ -81,35 +84,29 @@ class ClockSectionTest : SysuiTestCase() {
            com.android.systemui.customization.R.dimen.small_clock_height
        )

    private var DIMENSION_BY_IDENTIFIER_NAME: List<Pair<String, Int>> = listOf()

    @Before
    fun setup() {
        DIMENSION_BY_IDENTIFIER_NAME =
            listOf(
                "date_weather_view_height" to SMART_SPACE_DATE_WEATHER_HEIGHT,
                "enhanced_smartspace_height" to ENHANCED_SMART_SPACE_HEIGHT,
            )

        MockitoAnnotations.initMocks(this)
        val remoteResources = mock<Resources>()
        whenever(
                remoteResources.getIdentifier(
                    anyString(),
                    eq("dimen"),
                    anyString(),
                )
            )
            .then { invocation ->
        whenever(remoteResources.getIdentifier(anyString(), eq("dimen"), anyString())).then {
            invocation ->
            val name = invocation.arguments[0] as String
            val index = DIMENSION_BY_IDENTIFIER_NAME.indexOfFirst { (key, _) -> key == name }
                if (index == -1) {
                    error(
                        "No entry for a dimension named \"$name\", please add it to the list above."
                    )
                }
                index
            // increment index so that the not-found sentinel value lines up w/ what is
            // returned by getIdentifier when a resource is not found
            index + 1
        }
        whenever(
                remoteResources.getDimensionPixelSize(
                    anyInt(),
                )
            )
            .then { invocation ->
        whenever(remoteResources.getDimensionPixelSize(anyInt())).then { invocation ->
            val id = invocation.arguments[0] as Int
                DIMENSION_BY_IDENTIFIER_NAME[id].second
            DIMENSION_BY_IDENTIFIER_NAME[id - 1].second
        }
        val packageManager = mock<PackageManager>()
        whenever(packageManager.getResourcesForApplication(anyString())).thenReturn(remoteResources)
@@ -158,6 +155,36 @@ class ClockSectionTest : SysuiTestCase() {
        assertSmallClockTop(cs, expectedSmallClockTopMargin)
    }

    @Test
    fun testApplyDefaultConstraints_LargeClock_MissingSmartspace_SplitShade() {
        DIMENSION_BY_IDENTIFIER_NAME = listOf() // Remove Smartspace from mock
        setLargeClock(true)
        setSplitShade(true)
        val cs = ConstraintSet()
        underTest.applyDefaultConstraints(cs)

        val expectedLargeClockTopMargin = LARGE_CLOCK_TOP_WITHOUT_SMARTSPACE
        assertLargeClockTop(cs, expectedLargeClockTopMargin)

        val expectedSmallClockTopMargin = SMALL_CLOCK_TOP_SPLIT_SHADE
        assertSmallClockTop(cs, expectedSmallClockTopMargin)
    }

    @Test
    fun testApplyDefaultConstraints_LargeClock_MissingSmartspace_NonSplitShade() {
        DIMENSION_BY_IDENTIFIER_NAME = listOf() // Remove Smartspace from mock
        setLargeClock(true)
        setSplitShade(false)
        val cs = ConstraintSet()
        underTest.applyDefaultConstraints(cs)

        val expectedLargeClockTopMargin = LARGE_CLOCK_TOP_WITHOUT_SMARTSPACE
        assertLargeClockTop(cs, expectedLargeClockTopMargin)

        val expectedSmallClockTopMargin = SMALL_CLOCK_TOP_NON_SPLIT_SHADE
        assertSmallClockTop(cs, expectedSmallClockTopMargin)
    }

    @Test
    fun testApplyDefaultConstraints_SmallClock_SplitShade() {
        setLargeClock(false)
@@ -249,10 +276,5 @@ class ClockSectionTest : SysuiTestCase() {
    companion object {
        private val SMART_SPACE_DATE_WEATHER_HEIGHT = 10
        private val ENHANCED_SMART_SPACE_HEIGHT = 11
        private val DIMENSION_BY_IDENTIFIER_NAME =
            listOf(
                "date_weather_view_height" to SMART_SPACE_DATE_WEATHER_HEIGHT,
                "enhanced_smartspace_height" to ENHANCED_SMART_SPACE_HEIGHT,
            )
    }
}