-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle color selector in DeferredColor.resolve (#32)
* Improve attribute resolution error messages * Add KDoc warnings about attribute color selectors
- Loading branch information
1 parent
57a43d9
commit 8eeb433
Showing
7 changed files
with
110 additions
and
22 deletions.
There are no files selected for viewing
43 changes: 36 additions & 7 deletions
43
deferred-resources/src/androidTest/java/com/backbase/deferredresources/DeferredColorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,73 @@ | ||
package com.backbase.deferredresources | ||
|
||
import android.graphics.Color | ||
import androidx.test.filters.SdkSuppress | ||
import com.backbase.deferredresources.test.R | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
class DeferredColorTest { | ||
|
||
@Test fun constant_withIntValue_returnsSameValue() { | ||
//region Constant | ||
@Test fun constantResolve_withIntValue_returnsSameValue() { | ||
val deferred = DeferredColor.Constant(Color.MAGENTA) | ||
assertThat(deferred.resolve(context)).isEqualTo(Color.MAGENTA) | ||
} | ||
|
||
@Test fun constant_withStringValue_returnsParsedValue() { | ||
@Test fun constantResolve_withStringValue_returnsParsedValue() { | ||
val deferred = DeferredColor.Constant("#00FF00") | ||
assertThat(deferred.resolve(context)).isEqualTo(Color.GREEN) | ||
} | ||
//endregion | ||
|
||
@Test fun resource_resolvesWithContext() { | ||
//region Resource | ||
@Test fun resourceResolve_withStandardColor_resolvesColor() { | ||
val deferred = DeferredColor.Resource(R.color.blue) | ||
assertThat(deferred.resolve(context)).isEqualTo(Color.BLUE) | ||
} | ||
|
||
@Test fun attribute_resolvesWithContext() { | ||
@Test fun resourceResolve_withSelectorColor_resolvesDefaultColor() { | ||
val deferred = DeferredColor.Resource(R.color.stateful_color_without_attr) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#aaaaaa")) | ||
} | ||
|
||
@SdkSuppress(minSdkVersion = 23) | ||
@Test fun resourceResolve_withSelectorColorWithAttribute_resolvesDefaultColor() { | ||
val deferred = DeferredColor.Resource(R.color.stateful_color_with_attr) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#987654")) | ||
} | ||
//endregion | ||
|
||
//region Attribute | ||
@Test fun attributeResolve_withStandardColor_resolvesColor() { | ||
val deferred = DeferredColor.Attribute(R.attr.colorPrimary) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#212121")) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#987654")) | ||
} | ||
|
||
@Test fun attributeResolve_withSelectorColor_resolvesDefaultColor() { | ||
val deferred = DeferredColor.Attribute(R.attr.subtitleTextColor) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#aaaaaa")) | ||
} | ||
|
||
@SdkSuppress(minSdkVersion = 23) | ||
@Test fun attributeResolve_withSelectorColorWithAttributeDefault_resolvesDefaultColor() { | ||
val deferred = DeferredColor.Attribute(R.attr.titleTextColor) | ||
assertThat(deferred.resolve(AppCompatContext())).isEqualTo(Color.parseColor("#987654")) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun attribute_withUnknownAttribute_throwsException() { | ||
fun attributeResolve_withUnknownAttribute_throwsException() { | ||
val deferred = DeferredColor.Attribute(R.attr.colorPrimary) | ||
|
||
// Default-theme context does not have <colorPrimary> attribute: | ||
deferred.resolve(context) | ||
} | ||
|
||
@Test(expected = IllegalArgumentException::class) | ||
fun attribute_withWrongAttributeType_throwsException() { | ||
fun attributeResolve_withWrongAttributeType_throwsException() { | ||
val deferred = DeferredColor.Attribute(R.attr.isLightTheme) | ||
|
||
deferred.resolve(AppCompatContext()) | ||
} | ||
//endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
deferred-resources/src/androidTest/res/color/stateful_color_with_attr.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item android:color="#dbdbdb" android:state_enabled="false" /> | ||
|
||
<item android:color="@android:color/darker_gray" android:state_checked="true" /> | ||
|
||
<item android:color="?colorPrimary" /> | ||
|
||
</selector> |
8 changes: 8 additions & 0 deletions
8
deferred-resources/src/androidTest/res/color/stateful_color_without_attr.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<selector xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item android:color="#dbdbdb" android:state_enabled="false" /> | ||
|
||
<item android:color="@android:color/darker_gray" /> | ||
|
||
</selector> |
14 changes: 14 additions & 0 deletions
14
deferred-resources/src/androidTest/res/values/test_theme.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<style name="TestTheme" parent="Theme.AppCompat"> | ||
<item name="colorPrimary">#987654</item> | ||
<item name="titleTextColor">@color/stateful_color_with_attr</item> | ||
<item name="subtitleTextColor">@color/stateful_color_without_attr</item> | ||
</style> | ||
|
||
<style name="TestTheme.Light" parent="Theme.AppCompat.Light"> | ||
<item name="colorPrimary">#987654</item> | ||
<item name="titleTextColor">@color/stateful_color_with_attr</item> | ||
<item name="subtitleTextColor">@color/stateful_color_without_attr</item> | ||
</style> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters