Skip to content

Commit

Permalink
Fix LocaleManager.setApplicationLocales(LocaleList) in Android U+
Browse files Browse the repository at this point in the history
Android U introduced a new 3-arg variant of
LocaleManager.setApplicationLocales. Add a shadow for this, which will fix
LocaleManager.setApplicationLocales(LocaleList) in U+.

Fixes #8580

PiperOrigin-RevId: 578049631
  • Loading branch information
hoisie committed Oct 31, 2023
1 parent b35b0bf commit 062066c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public void setApplicationLocales_updatesMap() {
.isEqualTo(DEFAULT_LOCALES);
}

@Test
public void setApplicationLocales_defaultPackage_updatesMap() {
// empty map before set is called.
assertThat(localeManager.getApplicationLocales(DEFAULT_PACKAGE_NAME))
.isEqualTo(LocaleList.getEmptyLocaleList());

localeManager.setApplicationLocales(DEFAULT_LOCALES);

shadowLocaleManager.enforceInstallerCheck(false);
assertThat(localeManager.getApplicationLocales()).isEqualTo(DEFAULT_LOCALES);
}

@Test
public void getApplicationLocales_fetchAsInstaller_returnsLocales() {
localeManager.setApplicationLocales(DEFAULT_PACKAGE_NAME, DEFAULT_LOCALES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Set;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.versioning.AndroidVersions.U;

/** Shadow of {@link LocaleManager} */
@Implements(value = LocaleManager.class, minSdk = VERSION_CODES.TIRAMISU, isInAndroidSdk = false)
Expand Down Expand Up @@ -48,13 +49,26 @@ protected LocaleList getApplicationLocales(String packageName) {
/**
* Stores the passed locales for the given package in-memory.
*
* <p>Starting in Android U, this method just invokes the 3-arg version (below).
*
* <p>Use this method in tests to substitute call for {@link LocaleManager#setApplicationLocales}.
*/
@Implementation
@Implementation(maxSdk = VERSION_CODES.TIRAMISU)
protected void setApplicationLocales(String packageName, LocaleList locales) {
appLocales.put(packageName, locales);
}

/**
* Stores the passed locales for the given package in-memory.
*
* <p>Use this method in tests to substitute call for {@link LocaleManager#setApplicationLocales}.
*/
@Implementation(minSdk = U.SDK_INT)
protected void setApplicationLocales(
String packageName, LocaleList locales, boolean fromDelegate) {
setApplicationLocales(packageName, locales);
}

@RequiresApi(api = VERSION_CODES.N)
@Implementation
protected LocaleList getSystemLocales() {
Expand Down

0 comments on commit 062066c

Please sign in to comment.