Using qualified resources
As described in the Android developer docs, resource qualifiers allow you to change how your resources are loaded based on such factors as the language on the device, to the screen size, to whether it is day or night. While these changes are often tedious to test rigorously (every string has a translation for all supported languages), you may find yourself wishing to run tests in different resource qualified contexts.
Specifying resources in test
Specifying a resource qualifier is quite simple: simply add the desired qualifiers to the
@Config
annotation on your test case or test class, depending on whether
you would like to change the resource qualifiers for the whole file, or simply one test.
Given the following resources:
this Robolectric test would pass, using the Android resource qualifier resolution rules:
@Test
@Config(qualifiers = "en-port")
public void shouldUseEnglishAndPortraitResources() {
final Context context = RuntimeEnvironment.application;
assertThat(context.getString(R.id.not_overridden)).isEqualTo("Not overridden");
assertThat(context.getString(R.id.overridden)).isEqualTo("Overridden in en");
assertThat(context.getString(R.id.overridden_twice)).isEqualTo("Overridden twice in en-port");
}
@Test
@Config(qualifiers = "en-port")
fun shouldUseEnglishAndPortraitResources() {
val context = RuntimeEnvironment.application
assertThat(context.getString(R.id.not_overridden)).isEqualTo("Not overridden")
assertThat(context.getString(R.id.overridden)).isEqualTo("Overridden in en")
assertThat(context.getString(R.id.overridden_twice)).isEqualTo("Overridden twice in en-port")
}
Multiple qualifiers should be separated by dashes and provided in the order put forth in this list.