【问题标题】:Android Testing method that returns color resource id from R.attr reference从 R.attr 参考返回颜色资源 id 的 Android 测试方法
【发布时间】:2019-05-22 14:42:07
【问题描述】:

我第一次使用 Android Studio 3.2 实现 AndroidInstrumentationTest,尝试检查方法是否根据字符串从属性(R.attr 和样式中设置的颜色)返回颜色资源 ID,但返回的资源 ID 始终为 0的预期。

代码在我的应用程序中正常工作,颜色设置如下:

textView.setTextColor(fetchCorrectColor(myContext))

问题是测试中的 fetchColor 返回 0

mContext.getString() 等其他资源完美运行

使用@RunWith(AndroidJunit4::class) 注释测试类并在模拟的Android Pie (28) 和设备上运行

我尝试了不同的上下文,结果相同:

InstrumentationRegistry.getInstrumentation().targetContext
InstrumentationRegistry.getInstrumentation().context
ApplicationProvider.getApplicationContext()

测试方法

fun getTextColor(status: String?, mContext: Context): Int{
    return when(status){
        "A", "B" ->{
            fetchCorrectColor(mContext)
        }
        "C", "D"->{
            fetchWarningColor(mContext)
        }
        else -> {
            fetchDisabledColor(mContext)
        }
    }
}

从属性中获取颜色资源的方法

fun fetchCorrectColor(context: Context): Int{
    return fetchColor(context, R.attr.correct)
}

private fun fetchColor(context: Context, colorId: Int): Int{
    val typedValue = TypedValue()
    context.theme.resolveAttribute(colorId, typedValue, true)
    return typedValue.data
}

测试

@Test fun getTextColor_isCorrect(){
    Assert.assertEquals(R.attr.correct, getTextColor("A", mContext))
    Assert.assertEquals(R.attr.correct, getTextColor("B", mContext))
    Assert.assertEquals(R.attr.warning, getTextColor("C", mContext))
    Assert.assertEquals(R.attr.warning, getTextColor("D", mContext))
    Assert.assertEquals(R.attr.disabled, getTextColor(null, mContext))
}

这是我一直遇到的错误:

java.lang.AssertionError: expected:<2130968760> but was:<0>
at org.junit.Assert.fail(Assert.java:88)

【问题讨论】:

    标签: android kotlin android-testing android-styles android-attributes


    【解决方案1】:

    属性是Theme 感知的。确保context 使用与您的应用相同的theme

    appContext.setTheme(R.style.AppTheme)
    

    解析仅在AppCompat 主题中可用的R.attr.colorPrimary 属性的示例测试代码:

    @Test
    fun testColorPrimary() {
        // Context of the app under test.
        val appContext = InstrumentationRegistry.getTargetContext()
    
        // actual R.color.colorPrimary value
        val actualPrimaryColor = appContext.getColor(R.color.colorPrimary)
    
        // R.attr.colorPrimary resolved with invalid theme
        val colorPrimary1 = TypedValue().also {
            appContext.theme.resolveAttribute(R.attr.colorPrimary, it, true)
        }.data
    
        // provided context has invalid theme so attribute resolution fails (returns 0)
        assertEquals(0, colorPrimary1)
    
        // be sure test context uses same theme as app
        appContext.setTheme(R.style.AppTheme)
    
        // R.attr.colorPrimary resolved from valid theme
        val colorPrimary2 = TypedValue().also {
            appContext.theme.resolveAttribute(R.attr.colorPrimary, it, true)
        }.data
    
        // valid theme returns proper color
        assertEquals(actualPrimaryColor, colorPrimary2)
    }
    

    【讨论】:

    • 非常感谢,主要问题是获取应用程序主题的上下文(mContext.setTheme(R.style.AppTheme) 成功了),对于测试我已经认为预期值应该是上下文中的颜色,我可以在 fetchColor() 返回后检查这个!= 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    相关资源
    最近更新 更多