diff --git a/app/src/main/kotlin/io/homeassistant/companion/android/widgets/entity/EntityWidgetConfigureActivity.kt b/app/src/main/kotlin/io/homeassistant/companion/android/widgets/entity/EntityWidgetConfigureActivity.kt index 70edc6fbf19..a1d1ee85d3b 100644 --- a/app/src/main/kotlin/io/homeassistant/companion/android/widgets/entity/EntityWidgetConfigureActivity.kt +++ b/app/src/main/kotlin/io/homeassistant/companion/android/widgets/entity/EntityWidgetConfigureActivity.kt @@ -342,6 +342,9 @@ class EntityWidgetConfigureActivity : BaseWidgetConfigureActivity WidgetTapAction.TOGGLE else -> WidgetTapAction.REFRESH }, - backgroundType = when (binding.backgroundType.selectedItem as String?) { - getString(commonR.string.widget_background_type_dynamiccolor) -> WidgetBackgroundType.DYNAMICCOLOR - getString(commonR.string.widget_background_type_transparent) -> WidgetBackgroundType.TRANSPARENT - else -> WidgetBackgroundType.DAYNIGHT - }, - textColor = if (binding.backgroundType.selectedItem as String? == - getString(commonR.string.widget_background_type_transparent) - ) { + backgroundType = selectedBackgroundType, + textColor = if (selectedBackgroundType == WidgetBackgroundType.TRANSPARENT) { getHexForColor( if (binding.textColorWhite.isChecked) { android.R.color.white diff --git a/app/src/test/kotlin/io/homeassistant/companion/android/widgets/common/WidgetUtilsTest.kt b/app/src/test/kotlin/io/homeassistant/companion/android/widgets/common/WidgetUtilsTest.kt new file mode 100644 index 00000000000..f36375fb03a --- /dev/null +++ b/app/src/test/kotlin/io/homeassistant/companion/android/widgets/common/WidgetUtilsTest.kt @@ -0,0 +1,55 @@ +package io.homeassistant.companion.android.widgets.common + +import androidx.test.core.app.ApplicationProvider +import dagger.hilt.android.testing.HiltTestApplication +import io.homeassistant.companion.android.common.R +import io.homeassistant.companion.android.database.widget.WidgetBackgroundType +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(application = HiltTestApplication::class) +class WidgetUtilsTest { + private val context = ApplicationProvider.getApplicationContext() + + @Test + fun `Given dynamic color background option when getting widget background type then returns dynamic color`() { + assertWidgetBackgroundType( + context.getString(R.string.widget_background_type_dynamiccolor), + WidgetBackgroundType.DYNAMICCOLOR, + ) + } + + @Test + fun `Given transparent background option when getting widget background type then returns transparent`() { + assertWidgetBackgroundType( + context.getString(R.string.widget_background_type_transparent), + WidgetBackgroundType.TRANSPARENT, + ) + } + + @Test + fun `Given day night background option when getting widget background type then returns day night`() { + assertWidgetBackgroundType( + context.getString(R.string.widget_background_type_daynight), + WidgetBackgroundType.DAYNIGHT, + ) + } + + @Test + fun `Given unknown background option when getting widget background type then returns day night`() { + assertWidgetBackgroundType("unknown", WidgetBackgroundType.DAYNIGHT) + } + + private fun assertWidgetBackgroundType( + selectedOption: String, + expectedType: WidgetBackgroundType, + ) { + val result = WidgetUtils.getWidgetBackgroundType(context, selectedOption) + + assertEquals(expectedType, result) + } +}