Skip to content

Commit

Permalink
Stop returning the resources sdk level in RuntimeEnvironment.getQuali…
Browse files Browse the repository at this point in the history
…fiers

Previously, the Build.VERSION.RESOURCES_SDK_INT constant was returned as a
suffix in the call to RuntimeEnvironment.getQualifiers. For example, it could
contain the suffix 'v24'.

There was not much value in returning the SDK level. It was unused, except for
RuntimeEnvironment.setQualifiers checking that the sdk level matched
RuntimeEnvironment.getApiLevel(). It is not possible to set the SDK level using
RuntimeEnvironment.setQualifiers.

RESOURCES_SDK_INT causes problems in the current Android V SDK, as it is higher
than the SDK level. This causes
RuntimeEnvironment.setQualifiers(RuntimeEnvironment.getQualifiers) to throw an
Exception.

Because this API level is unused, it can be removed from the result of
RuntimeEnvironment.getQualifiers.

PiperOrigin-RevId: 632580756
  • Loading branch information
hoisie authored and Copybara-Service committed May 10, 2024
1 parent 006211d commit 8e8e283
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 80 deletions.
18 changes: 0 additions & 18 deletions resources/src/main/java/org/robolectric/res/Qualifiers.java
Expand Up @@ -98,24 +98,6 @@ public static int getSmallestScreenWidth(String qualifiers) {
return -1;
}

/**
* If the Config already has a version qualifier, do nothing. Otherwise, add a version
* qualifier for the target api level (which comes from the manifest or Config.sdk()).
*
* @deprecated Figure something else out.
*/
@Deprecated
public static String addPlatformVersion(String qualifiers, int apiLevel) {
int versionQualifierApiLevel = Qualifiers.getPlatformVersion(qualifiers);
if (versionQualifierApiLevel == -1) {
if (qualifiers.length() > 0) {
qualifiers += "-";
}
qualifiers += "v" + apiLevel;
}
return qualifiers;
}

/**
* If the Config already has a {@code sw} qualifier, do nothing. Otherwise, add a {@code sw}
* qualifier for the given width.
Expand Down
21 changes: 10 additions & 11 deletions resources/src/test/java/org/robolectric/res/QualifiersTest.java
Expand Up @@ -38,26 +38,25 @@ private String configFrom(String path) {

///////// deprecated stuff...

@Test public void addPlatformVersion() throws Exception {
assertThat(Qualifiers.addPlatformVersion("", 21)).isEqualTo("v21");
assertThat(Qualifiers.addPlatformVersion("v23", 21)).isEqualTo("v23");
assertThat(Qualifiers.addPlatformVersion("foo-v14", 21)).isEqualTo("foo-v14");
}

@Test public void addSmallestScreenWidth() throws Exception {
@Test
public void addSmallestScreenWidth() throws Exception {
assertThat(Qualifiers.addSmallestScreenWidth("", 320)).isEqualTo("sw320dp");
assertThat(Qualifiers.addSmallestScreenWidth("sw160dp", 320)).isEqualTo("sw160dp");
assertThat(Qualifiers.addSmallestScreenWidth("sw480dp", 320)).isEqualTo("sw480dp");
assertThat(Qualifiers.addSmallestScreenWidth("en-v23", 320)).isEqualTo("en-v23-sw320dp"); // todo: order is wrong here
assertThat(Qualifiers.addSmallestScreenWidth("en-sw160dp-v23", 320)).isEqualTo("en-sw160dp-v23");
assertThat(Qualifiers.addSmallestScreenWidth("en-sw480dp-v23", 320)).isEqualTo("en-sw480dp-v23");
assertThat(Qualifiers.addSmallestScreenWidth("en-v23", 320))
.isEqualTo("en-v23-sw320dp"); // todo: order is wrong here
assertThat(Qualifiers.addSmallestScreenWidth("en-sw160dp-v23", 320))
.isEqualTo("en-sw160dp-v23");
assertThat(Qualifiers.addSmallestScreenWidth("en-sw480dp-v23", 320))
.isEqualTo("en-sw480dp-v23");
}

@Test public void addScreenWidth() throws Exception {
assertThat(Qualifiers.addScreenWidth("", 320)).isEqualTo("w320dp");
assertThat(Qualifiers.addScreenWidth("w160dp", 320)).isEqualTo("w160dp");
assertThat(Qualifiers.addScreenWidth("w480dp", 320)).isEqualTo("w480dp");
assertThat(Qualifiers.addScreenWidth("en-v23", 320)).isEqualTo("en-v23-w320dp"); // todo: order is wrong here
assertThat(Qualifiers.addScreenWidth("en-v23", 320))
.isEqualTo("en-v23-w320dp"); // todo: order is wrong here
assertThat(Qualifiers.addScreenWidth("en-w160dp-v23", 320)).isEqualTo("en-w160dp-v23");
assertThat(Qualifiers.addScreenWidth("en-w480dp-v23", 320)).isEqualTo("en-w480dp-v23");
}
Expand Down
4 changes: 2 additions & 2 deletions robolectric/src/test/java/org/robolectric/QualifiersTest.java
Expand Up @@ -32,15 +32,15 @@ public void setUp() throws Exception {
public void testDefaultQualifiers() throws Exception {
assertThat(RuntimeEnvironment.getQualifiers())
.isEqualTo(
"en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v26");
"en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav");
}

@Test
@Config(qualifiers = "en", sdk = 26)
public void testDefaultQualifiers_withoutRegion() throws Exception {
assertThat(RuntimeEnvironment.getQualifiers())
.isEqualTo(
"en-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav-v26");
"en-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-nowidecg-lowdr-port-notnight-mdpi-finger-keyssoft-nokeys-navhidden-nonav");
}

@Test
Expand Down
Expand Up @@ -144,4 +144,10 @@ public void setQualifiers_resetsDateUtilsFormatCache() {
RuntimeEnvironment.setQualifiers("en-rUS");
assertThat(DateUtils.formatElapsedTime(120)).isEqualTo("02:00");
}

@Test
public void setQualifiers_withResultFromGetQualifiers() {
// Calling this should not cause an exception, e.g. API level mismatch.
RuntimeEnvironment.setQualifiers(RuntimeEnvironment.getQualifiers());
}
}
Expand Up @@ -147,12 +147,11 @@ public void applyQualifiers_shouldAddDefaults() {
"en-rUS-ldltr-sw320dp-w320dp-h470dp-normal-notlong-notround-"
+ optsForO
+ "port-notnight-mdpi"
+ "-finger-keyssoft-nokeys-navhidden-nonav-v"
+ Build.VERSION.RESOURCES_SDK_INT);
+ "-finger-keyssoft-nokeys-navhidden-nonav");

assertThat(configuration.mcc).isEqualTo(0);
assertThat(configuration.mnc).isEqualTo(0);
assertThat(configuration.locale).isEqualTo(new Locale("en", "US"));
assertThat(configuration.locale).isEqualTo(Locale.US);
assertThat(configuration.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK).isEqualTo(SCREENLAYOUT_LAYOUTDIR_LTR);
assertThat(configuration.smallestScreenWidthDp).isEqualTo(320);
assertThat(configuration.screenWidthDp).isEqualTo(320);
Expand Down Expand Up @@ -187,9 +186,11 @@ public void applyQualifiers_shouldHonorSpecifiedQualifiers() {

Bootstrap.applyQualifiers(
"mcc310-mnc004-fr-rFR-ldrtl-sw400dp-w480dp-h456dp-"
+ "xlarge-long-round" + altOptsForO + "-land-appliance-night-hdpi-notouch-"
+ "xlarge-long-round"
+ altOptsForO
+ "-land-appliance-night-hdpi-notouch-"
+ "keyshidden-12key-navhidden-dpad",
Build.VERSION.RESOURCES_SDK_INT,
RuntimeEnvironment.getApiLevel(),
configuration,
displayMetrics);
String outQualifiers = ConfigurationV25.resourceQualifierString(configuration, displayMetrics);
Expand All @@ -201,12 +202,11 @@ public void applyQualifiers_shouldHonorSpecifiedQualifiers() {
+ "-xlarge-long-round"
+ altOptsForO
+ "-land-appliance-night-hdpi-notouch-"
+ "keyshidden-12key-navhidden-dpad-v"
+ Build.VERSION.RESOURCES_SDK_INT);
+ "keyshidden-12key-navhidden-dpad");

assertThat(configuration.mcc).isEqualTo(310);
assertThat(configuration.mnc).isEqualTo(4);
assertThat(configuration.locale).isEqualTo(new Locale("fr", "FR"));
assertThat(configuration.locale).isEqualTo(Locale.FRANCE);
assertThat(configuration.screenLayout & SCREENLAYOUT_LAYOUTDIR_MASK)
.isEqualTo(SCREENLAYOUT_LAYOUTDIR_LTR);
assertThat(configuration.smallestScreenWidthDp).isEqualTo(400);
Expand Down Expand Up @@ -350,7 +350,7 @@ public void whenAllPrefixedWithPlus_applyQualifiers_shouldOverlayQualifiers() th
@Test
@Config(minSdk = N)
public void testUpdateDisplayResourcesWithDifferentLocale() {
Locale locale = new Locale("en", "IN");
Locale locale = Locale.forLanguageTag("en-IN");
RuntimeEnvironment.setQualifiers("ar");
LocaleList originalDefault = LocaleList.getDefault();
try {
Expand Down
Expand Up @@ -101,15 +101,7 @@ public void applyRules_rtlScript() {
String language = "he";
applyQualifiers(language);
DeviceConfig.applyRules(configuration, displayMetrics, apiLevel);
// Locale's constructor has always converted three language codes to their earlier, obsoleted
// forms: he maps to iw, yi maps to ji, and id maps to in. Since Java SE 17, this is no longer
// the case. Each language maps to its new form; iw maps to he, ji maps to yi, and in maps to
// id.
// See
// https://stackoverflow.com/questions/8202406/locale-code-for-hebrew-reference-to-other-locale-codes/70882234#70882234,
// and https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Locale.html.
// To make sure this test can work with different JDK versions, using the following workaround.
Locale locale = new Locale(language);
Locale locale = Locale.forLanguageTag(language);
assertThat(asQualifierString())
.isEqualTo(
locale.getLanguage()
Expand Down Expand Up @@ -230,6 +222,6 @@ private void applyQualifiers(String qualifiers) {
}

private String asQualifierString() {
return ConfigurationV25.resourceQualifierString(configuration, displayMetrics, false);
return ConfigurationV25.resourceQualifierString(configuration, displayMetrics);
}
}
Expand Up @@ -161,17 +161,6 @@ public void testWhenConscryptModeOff_BouncyCastleInstalled() throws GeneralSecur
assertThat(aesCipher.getProvider().getName()).isEqualTo(BouncyCastleProvider.PROVIDER_NAME);
}

@Test
public void setUpApplicationState_setsVersionQualifierFromSdk() {
String givenQualifiers = "";
ConfigurationImpl config = new ConfigurationImpl();
config.put(Config.class, new Config.Builder().setQualifiers(givenQualifiers).build());
config.put(LooperMode.Mode.class, LEGACY);
bootstrapWrapper.changeConfig(config);
bootstrapWrapper.callSetUpApplicationState();
assertThat(RuntimeEnvironment.getQualifiers()).contains("v" + Build.VERSION.RESOURCES_SDK_INT);
}

@Test
public void setUpApplicationState_setsVersionQualifierFromSdkWithOtherQualifiers() {
String givenQualifiers = "large-land";
Expand All @@ -186,9 +175,11 @@ public void setUpApplicationState_setsVersionQualifierFromSdkWithOtherQualifiers
? "nowidecg-lowdr-"
: "";
assertThat(RuntimeEnvironment.getQualifiers())
.contains("large-notlong-notround-" + optsForO + "land-notnight-mdpi-finger-keyssoft"
+ "-nokeys-navhidden-nonav-v"
+ Build.VERSION.RESOURCES_SDK_INT);
.contains(
"large-notlong-notround-"
+ optsForO
+ "land-notnight-mdpi-finger-keyssoft"
+ "-nokeys-navhidden-nonav");
}

@Test
Expand Down
Expand Up @@ -60,18 +60,8 @@ private static String localesToResourceQualifier(List<Locale> locs) {
return sb.toString();
}


/**
* Returns a string representation of the configuration that can be parsed
* by build tools (like AAPT).
*
* @hide
*/
public static String resourceQualifierString(Configuration config, DisplayMetrics displayMetrics) {
return resourceQualifierString(config, displayMetrics, true);
}

public static String resourceQualifierString(Configuration config, DisplayMetrics displayMetrics, boolean includeSdk) {
public static String resourceQualifierString(
Configuration config, DisplayMetrics displayMetrics) {
ArrayList<String> parts = new ArrayList<String>();

if (config.mcc != 0) {
Expand Down Expand Up @@ -325,10 +315,6 @@ public static String resourceQualifierString(Configuration config, DisplayMetric
break;
}

if (includeSdk) {
parts.add("v" + Build.VERSION.RESOURCES_SDK_INT);
}

return TextUtils.join("-", parts);
}

Expand Down

0 comments on commit 8e8e283

Please sign in to comment.