diff --git a/android/build.gradle b/android/build.gradle index 25bd9d6a21e..109a4b79d6b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -39,7 +39,7 @@ buildscript { System.getenv("HOME") + "/fbsource" minSdkVersion = 16 - compileSdkVersion = 28 + compileSdkVersion = 31 abis = project.hasProperty('abis') ? project.getProperty("abis").split(",") : ["arm64-v8a", "armeabi-v7a", "x86_64", "x86"] diff --git a/android/intltest/build.gradle b/android/intltest/build.gradle index 1f2cda51d89..7ea4bd805b2 100644 --- a/android/intltest/build.gradle +++ b/android/intltest/build.gradle @@ -56,7 +56,7 @@ task prepareTests() { // TODO: Figure out how to deduplicate this file and intl/build.gradle android { - compileSdkVersion 24 + compileSdkVersion 31 defaultConfig { minSdkVersion 16 diff --git a/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java b/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java index 2a4cd66b7ee..42d0faf64a2 100644 --- a/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java +++ b/android/intltest/java/com/facebook/hermes/test/HermesInstrumentationTest.java @@ -9,12 +9,15 @@ import static org.assertj.core.api.Java6Assertions.assertThat; -import android.test.InstrumentationTestCase; import java.util.Locale; import java.util.TimeZone; import org.junit.Test; +import org.junit.runner.RunWith; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; -public class HermesInstrumentationTest extends InstrumentationTestCase { +@RunWith(AndroidJUnit4.class) +public class HermesInstrumentationTest { @Test public void testEvaluatingJavaScript() { @@ -143,6 +146,6 @@ public void testGetHermesEpilogue() { byte[] epilogue = HermesEpilogue.getHermesBytecodeMetadata( HermesEpilogueTestData.getBytecodeWithEpilogue(EXPECTED_EPILOGUE)); - assertEquals(EXPECTED_EPILOGUE, new String(epilogue)); + assertThat(new String(epilogue)).isEqualTo(EXPECTED_EPILOGUE); } } diff --git a/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java b/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java index 0abd4470271..ff9cff1f3fc 100644 --- a/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java +++ b/android/intltest/java/com/facebook/hermes/test/HermesIntlAndroidTest.java @@ -10,18 +10,23 @@ import static org.assertj.core.api.Java6Assertions.assertThat; import android.content.res.AssetManager; -import android.test.InstrumentationTestCase; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.stream.Collectors; import org.junit.Test; +import org.junit.runner.RunWith; +import android.os.Build; + +@RunWith(AndroidJUnit4.class) +public class HermesIntlAndroidTest { -public class HermesIntlAndroidTest extends InstrumentationTestCase { @Test public void testIntlFromAsset() throws IOException { - AssetManager assets = getInstrumentation().getContext().getAssets(); + AssetManager assets = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets(); InputStream is = assets.open("intl.js"); String script = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n")); @@ -32,7 +37,7 @@ public void testIntlFromAsset() throws IOException { @Test public void testNumberFormatFractionDigitsFromAsset() throws IOException { - AssetManager assets = getInstrumentation().getContext().getAssets(); + AssetManager assets = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets(); InputStream is = assets.open("number-format-fraction-digits.js"); String script = new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n")); @@ -80,4 +85,46 @@ public void testDateTimeFormatCaseInsensitivity() { assertThat(result).isEqualTo("9/24, 6:00 PM"); } } + + @Test + public void testSignDisplayAlwaysForApiLevelAbove31() { + if (Build.VERSION.SDK_INT >= 31) { + try (JSRuntime rt = JSRuntime.makeHermesRuntime()) { + rt.evaluateJavaScript( + "var nf = new Intl.NumberFormat('de-DE', { signDisplay: 'always', currency: 'EUR', style: 'currency' });\n" + + "var result = nf.format(8537.71);"); + + String result = rt.getGlobalStringProperty("result"); + assertThat(result).isEqualTo("+8.537,71 €"); + } + } + } + + @Test + public void testSignDisplayNeverForApiLevelAbove31() { + if (Build.VERSION.SDK_INT >= 31) { + try (JSRuntime rt = JSRuntime.makeHermesRuntime()) { + rt.evaluateJavaScript( + "var nf = new Intl.NumberFormat('de-DE', { signDisplay: 'never', currency: 'EUR', style: 'currency' });\n" + + "var result = nf.format(8537.71);"); + + String result = rt.getGlobalStringProperty("result"); + assertThat(result).isEqualTo("8.537,71 €"); + } + } + } + + @Test + public void testSignDisplayExceptZeroForApiLevelAbove31() { + if (Build.VERSION.SDK_INT >= 31) { + try (JSRuntime rt = JSRuntime.makeHermesRuntime()) { + rt.evaluateJavaScript( + "var nf = new Intl.NumberFormat('de-DE', { signDisplay: 'exceptZero', currency: 'EUR', style: 'currency'});\n" + + "var result = nf.format(8537.71);"); + + String result = rt.getGlobalStringProperty("result"); + assertThat(result).isEqualTo("+8.537,71 €"); + } + } + } } diff --git a/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java b/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java index 2b491751f76..63a031cc951 100644 --- a/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java +++ b/android/intltest/java/com/facebook/hermes/test/HermesIntlTest262.java @@ -8,10 +8,13 @@ import static org.assertj.core.api.Java6Assertions.assertThat; import android.content.res.AssetManager; -import android.test.InstrumentationTestCase; import android.text.TextUtils; import android.util.Log; -import com.facebook.hermes.test.JSRuntime; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import org.junit.Test; +import org.junit.runner.RunWith; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -24,14 +27,16 @@ import java.util.Set; import java.util.Stack; +import com.facebook.hermes.test.JSRuntime; // Run "./gradlew :intltest:prepareTests" from the root to copy the test files to the // APK assets. -public class HermesIntlTest262 extends InstrumentationTestCase { +@RunWith(AndroidJUnit4.class) +public class HermesIntlTest262 { private static final String LOG_TAG = "HermesIntlTest"; protected void evalScriptFromAsset(JSRuntime rt, String filename) throws IOException { - AssetManager assets = getInstrumentation().getContext().getAssets(); + AssetManager assets = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets(); InputStream is = assets.open(filename); BufferedReader r = new BufferedReader(new InputStreamReader(is)); @@ -56,11 +61,12 @@ protected void evaluateCommonScriptsFromAsset(JSRuntime rt) throws IOException { evalScriptFromAsset(rt, "test262/harness/testTypedArray.js"); } + @Test public void test262Intl() throws IOException { Set skipList = getSkipList(); Stack testFiles = new Stack<>(); testFiles.push("test262/test"); - AssetManager assets = getInstrumentation().getContext().getAssets(); + AssetManager assets = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets(); ArrayList ranTests = new ArrayList<>(); HashMap failedTests = new HashMap<>(); diff --git a/lib/Platform/Intl/java/com/facebook/hermes/intl/PlatformNumberFormatterICU.java b/lib/Platform/Intl/java/com/facebook/hermes/intl/PlatformNumberFormatterICU.java index d4d3bcd668e..4ce7a0dc82d 100644 --- a/lib/Platform/Intl/java/com/facebook/hermes/intl/PlatformNumberFormatterICU.java +++ b/lib/Platform/Intl/java/com/facebook/hermes/intl/PlatformNumberFormatterICU.java @@ -155,24 +155,35 @@ public PlatformNumberFormatterICU setSignDisplay( android.icu.text.DecimalFormat decimalFormat = (android.icu.text.DecimalFormat) mNumberFormat; android.icu.text.DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols(); - switch (signDisplay) { - case NEVER: - decimalFormat.setPositivePrefix(""); - decimalFormat.setPositiveSuffix(""); - - decimalFormat.setNegativePrefix(""); - decimalFormat.setNegativeSuffix(""); - - break; - case ALWAYS: - case EXCEPTZERO: - if (!decimalFormat.getNegativePrefix().isEmpty()) - decimalFormat.setPositivePrefix(new String(new char[] {symbols.getPlusSign()})); - - if (!decimalFormat.getNegativeSuffix().isEmpty()) - decimalFormat.setPositiveSuffix(new String(new char[] {symbols.getPlusSign()})); - - break; + if (Build.VERSION.SDK_INT >= 31) { + switch (signDisplay) { + case NEVER: + decimalFormat.setSignAlwaysShown(false); + break; + case ALWAYS: + case EXCEPTZERO: + decimalFormat.setSignAlwaysShown(true); + break; + } + } else { + switch (signDisplay) { + case NEVER: + decimalFormat.setPositivePrefix(""); + decimalFormat.setPositiveSuffix(""); + + decimalFormat.setNegativePrefix(""); + decimalFormat.setNegativeSuffix(""); + + break; + case ALWAYS: + case EXCEPTZERO: + if (!decimalFormat.getNegativePrefix().isEmpty()) + decimalFormat.setPositivePrefix(new String(new char[] {symbols.getPlusSign()})); + + if (!decimalFormat.getNegativeSuffix().isEmpty()) + decimalFormat.setPositiveSuffix(new String(new char[] {symbols.getPlusSign()})); + break; + } } }