From f68eb08dd62435b3d883bd85b5d32caefb86e84f Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 17 Dec 2024 07:21:45 -0500 Subject: [PATCH] Removed WPLocaleTestRule and updateContextLocale --- .../ui/screenshots/JPScreenshotTest.java | 4 +- .../ui/screenshots/WPLocaleTestRule.java | 61 ------------------- .../ui/screenshots/WPScreenshotTest.java | 4 +- .../org/wordpress/android/AppInitializer.kt | 17 ------ .../java/org/wordpress/android/WordPress.kt | 7 --- 5 files changed, 2 insertions(+), 91 deletions(-) delete mode 100644 WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPLocaleTestRule.java diff --git a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java index c7d2ee6d2448..d13b6e416524 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/JPScreenshotTest.java @@ -65,9 +65,7 @@ public class JPScreenshotTest extends BaseTest { @ClassRule public static final RuleChain LOCALE_TEST_RULES = RuleChain // Run fastlane's official LocaleTestRule (which switches device language + sets up screengrab) first - .outerRule(new LocaleTestRule()) - // Run our own rule (which handles our in-app locale switching logic) second - .around(new WPLocaleTestRule()); + .outerRule(new LocaleTestRule()); // Note: running this as a static @ClassRule as part of the above RuleChain doesn't seem to work // (apparently that would make those run too early?), but running it as @Rule does fix the issue. diff --git a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPLocaleTestRule.java b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPLocaleTestRule.java deleted file mode 100644 index a0cb044a1575..000000000000 --- a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPLocaleTestRule.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.wordpress.android.ui.screenshots; - -import android.content.Context; - -import androidx.test.core.app.ApplicationProvider; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; -import org.wordpress.android.WordPress; -import org.wordpress.android.util.LocaleManager; - -import java.util.Locale; - -import tools.fastlane.screengrab.locale.LocaleUtil; - -public class WPLocaleTestRule implements TestRule { - private String mTestLocaleCode; - - public WPLocaleTestRule() { - this(LocaleUtil.getTestLocale()); - } - - public WPLocaleTestRule(String testLocaleCode) { - // Parse as `Locale` then back to `String`, to ensure that when we get `fr-FR` as input - // (which is what is expected to be passed via fastlane's Screengrab and `LocaleUtil.getTestLocale()`), - // we get `fr_FR` (the locale code format expected by `java.util.Locale` and our `LocaleManager`) back. - Locale locale = LocaleUtil.localeFromString(testLocaleCode); - if (locale != null) { - mTestLocaleCode = locale.toString(); - } - } - - @Override - public Statement apply(final Statement base, Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - String original = null; - try { - if (mTestLocaleCode != null) { - original = changeLocale(mTestLocaleCode); - } - base.evaluate(); - } finally { - if (original != null) { - changeLocale(original); - } - } - } - }; - } - - private static String changeLocale(String localeCode) { - Context context = ApplicationProvider.getApplicationContext(); - String original = LocaleManager.getLanguage(context); - LocaleManager.setNewLocale(context, localeCode); - WordPress.updateContextLocale(context); - return original; - } -} diff --git a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java index d19c503815a7..c809d53cd1b9 100644 --- a/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java +++ b/WordPress/src/androidTest/java/org/wordpress/android/ui/screenshots/WPScreenshotTest.java @@ -49,9 +49,7 @@ public class WPScreenshotTest extends BaseTest { @ClassRule public static final RuleChain LOCALE_TEST_RULES = RuleChain // Run fastlane's official LocaleTestRule (which switches device language + sets up screengrab) first - .outerRule(new LocaleTestRule()) - // Run our own rule (which handles our in-app locale switching logic) second - .around(new WPLocaleTestRule()); + .outerRule(new LocaleTestRule()); // Note: running this as a static @ClassRule as part of the above RuleChain doesn't seem to work // (apparently that would make those run too early?), but running it as @Rule does fix the issue. diff --git a/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt b/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt index e3b7fb142355..abbcc8a7ea95 100644 --- a/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt +++ b/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt @@ -97,7 +97,6 @@ import org.wordpress.android.util.BuildConfigWrapper import org.wordpress.android.util.DateTimeUtils import org.wordpress.android.util.EncryptedLogging import org.wordpress.android.util.FluxCUtils -import org.wordpress.android.util.LocaleManager import org.wordpress.android.util.NetworkUtils import org.wordpress.android.util.PackageUtils import org.wordpress.android.util.PerAppLocaleManager @@ -1086,21 +1085,5 @@ class AppInitializer @Inject constructor( } return bitmapCache as BitmapLruCache } - - /** - * Update locale of the static context when language is changed. - * - * When calling this method the application context **must** be already initialized. - * This is already the case in `Activity`, `Fragment` or `View`. - * - * When called from other places (E.g. a `TestRule`) we should provide it in the [appContext] parameter. - */ - fun updateContextLocale(appContext: Context? = null) { - val context = appContext ?: run { - check(context != null) { "Context must be initialized before calling updateContextLocale" } - return@run context - } - this.context = LocaleManager.setLocale(context) - } } } diff --git a/WordPress/src/main/java/org/wordpress/android/WordPress.kt b/WordPress/src/main/java/org/wordpress/android/WordPress.kt index 52975c1294b8..b54862e5a89d 100644 --- a/WordPress/src/main/java/org/wordpress/android/WordPress.kt +++ b/WordPress/src/main/java/org/wordpress/android/WordPress.kt @@ -1,7 +1,6 @@ package org.wordpress.android import android.app.Application -import android.content.Context import coil.decode.VideoFrameDecoder import com.android.volley.RequestQueue import dagger.hilt.EntryPoints @@ -59,12 +58,6 @@ abstract class WordPress : Application(), coil.ImageLoaderFactory { @JvmStatic fun getContext() = AppInitializer.context!! - @JvmStatic - @JvmOverloads - fun updateContextLocale(appContext: Context? = null) { - AppInitializer.updateContextLocale(appContext) - } - @JvmStatic fun getRestClientUtils() = AppInitializer.restClientUtils