Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: signDisplay plus sign behaviour fixed for NumberFormat #1483

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion android/intltest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand All @@ -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"));
Expand Down Expand Up @@ -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 €");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -56,11 +61,12 @@ protected void evaluateCommonScriptsFromAsset(JSRuntime rt) throws IOException {
evalScriptFromAsset(rt, "test262/harness/testTypedArray.js");
}

@Test
public void test262Intl() throws IOException {
Set<String> skipList = getSkipList();
Stack<String> testFiles = new Stack<>();
testFiles.push("test262/test");
AssetManager assets = getInstrumentation().getContext().getAssets();
AssetManager assets = InstrumentationRegistry.getInstrumentation().getTargetContext().getAssets();
ArrayList<String> ranTests = new ArrayList<>();
HashMap<String, String> failedTests = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down