Index: chrome/android/javatests/src/org/chromium/chrome/browser/payments/CurrencyStringFormatterTest.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/payments/CurrencyStringFormatterTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/payments/CurrencyStringFormatterTest.java |
index 810a0ae01d3af1831d5e6b854a8b7e8de82d8d64..cb80c7abe95f75b20260bd7a5ead252e39539b6c 100644 |
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/payments/CurrencyStringFormatterTest.java |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/payments/CurrencyStringFormatterTest.java |
@@ -4,106 +4,160 @@ |
package org.chromium.chrome.browser.payments; |
-import android.support.test.filters.SmallTest; |
-import android.test.InstrumentationTestCase; |
+import android.support.test.filters.MediumTest; |
-import java.util.Locale; |
+import junit.framework.Assert; |
+ |
+import org.chromium.base.LocaleUtils; |
+import org.chromium.content.browser.test.NativeLibraryTestBase; |
+ |
+import java.util.Arrays; |
+import java.util.List; |
/** |
* A lightweight integration test for CurrencyStringFormatter to run on an Android device. |
*/ |
-public class CurrencyStringFormatterTest extends InstrumentationTestCase { |
- @SmallTest |
- public void testCad() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("CAD", new Locale("en-US")); |
- assertEquals("$5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testAzn() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("AZN", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testBmd() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("BMD", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testAud() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("AUD", new Locale("en-US")); |
- assertEquals("$5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testBzd() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("BZD", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testClp() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("CLP", new Locale("en-US")); |
- assertEquals("5", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testLrd() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("LRD", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testNio() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("NIO", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testHrk() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("HRK", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testRub() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("RUB", new Locale("en-US")); |
- assertEquals("5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testEur() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("EUR", new Locale("en-US")); |
- assertEquals("€5.00", formatter.format("5")); |
- } |
- |
- @SmallTest |
- public void testPkr() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("PKR", new Locale("en-US")); |
- assertEquals("5", formatter.format("5")); |
+public class CurrencyStringFormatterTest extends NativeLibraryTestBase { |
+ /** |
+ * Unicode non-breaking space. |
+ */ |
+ private static final String SPACE = "\u00A0"; |
+ |
+ private enum ExpectedValidity { |
+ VALID_AMOUNT, |
+ INVALID_AMOUNT_CURRENCY_CODE, |
+ INVALID_AMOUNT_VALUE, |
} |
- @SmallTest |
- public void testCurrency6Chars() throws Exception { |
- CurrencyStringFormatter formatter = |
- new CurrencyStringFormatter("ABCDEF", new Locale("en-US")); |
- assertEquals("ABCDEF", formatter.getFormattedCurrencyCode()); |
+ @Override |
+ protected void setUp() throws Exception { |
+ super.setUp(); |
+ loadNativeLibraryAndInitBrowserProcess(); |
} |
- @SmallTest |
- public void testCurrencyEllipsized() throws Exception { |
- CurrencyStringFormatter formatter = |
- new CurrencyStringFormatter("ABCDEFG", new Locale("en-US")); |
- // Currency code more than 6 character is formatted to first 5 characters and ellipsis. |
- // "\u2026" is unicode for ellipsis. |
- assertEquals("ABCDE\u2026", formatter.getFormattedCurrencyCode()); |
+ private static String longStringOfLength(int len) { |
+ StringBuilder currency = new StringBuilder(); |
+ for (int i = 0; i < len; i++) { |
+ currency.append("A"); |
+ } |
+ return currency.toString(); |
} |
- @SmallTest |
- public void testCurrencyEmpty() throws Exception { |
- CurrencyStringFormatter formatter = new CurrencyStringFormatter("", new Locale("en-US")); |
- assertEquals("", formatter.getFormattedCurrencyCode()); |
+ @MediumTest |
+ public void testMultipleConversions() throws Exception { |
+ // Note, all spaces are expected to be unicode non-breaking spaces. Here they are shown as |
+ // normal spaces. |
+ List<Object[]> testCases = Arrays.asList(new Object[][] { |
+ {"55.00", "USD", "en-US", "USD", "$55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "USD", "en-CA", "USD", "$55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "USD", "fr-CA", "USD", "55,00 $", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "USD", "fr-FR", "USD", "55,00 $", ExpectedValidity.VALID_AMOUNT}, |
+ {"1234", "USD", "fr-FR", "USD", "1 234,00 $", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ {"55.5", "USD", "en-US", "USD", "$55.50", ExpectedValidity.VALID_AMOUNT}, |
+ {"55", "USD", "en-US", "USD", "$55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"-123", "USD", "en-US", "USD", "-$123.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"-1234", "USD", "en-US", "USD", "-$1,234.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"0.1234", "USD", "en-US", "USD", "$0.1234", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ {"55.00", "EUR", "en-US", "EUR", "€55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "EUR", "en-CA", "EUR", "€55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "EUR", "fr-CA", "EUR", "55,00 €", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "EUR", "fr-FR", "EUR", "55,00 €", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ {"55.00", "CAD", "en-US", "CAD", "$55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "CAD", "en-CA", "CAD", "$55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "CAD", "fr-CA", "CAD", "55,00 $", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "CAD", "fr-FR", "CAD", "55,00 $", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ {"55", "JPY", "ja-JP", "JPY", "¥55", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.0", "JPY", "ja-JP", "JPY", "¥55", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "JPY", "ja-JP", "JPY", "¥55", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.12", "JPY", "ja-JP", "JPY", "¥55.12", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.49", "JPY", "ja-JP", "JPY", "¥55.49", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.50", "JPY", "ja-JP", "JPY", "¥55.5", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.9999", "JPY", "ja-JP", "JPY", "¥55.9999", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ // Unofficial ISO 4217 currency code. |
+ {"55.00", "BTX", "en-US", "BTX", "55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"-0.00000001", "BTX", "en-US", "BTX", "-0.00000001", |
+ ExpectedValidity.VALID_AMOUNT}, |
+ {"-55.00", "BTX", "fr-FR", "BTX", "-55,00", ExpectedValidity.VALID_AMOUNT}, |
+ |
+ {"123456789012345678901234567890.123456789012345678901234567890", "USD", "fr-FR", |
+ "USD", "123 456 789 012 345 678 901 234 567 890,123456789 $", |
+ ExpectedValidity.VALID_AMOUNT}, |
+ {"123456789012345678901234567890.123456789012345678901234567890", "USD", "en-US", |
+ "USD", "$123,456,789,012,345,678,901,234,567,890.123456789", |
+ ExpectedValidity.VALID_AMOUNT}, |
+ |
+ // Any string of at most 2048 characters can be valid amount currency codes. |
+ {"55.00", "", "en-US", "", "55.00", ExpectedValidity.VALID_AMOUNT}, |
+ {"55.00", "ABCDEF", "en-US", "ABCDEF", "55.00", ExpectedValidity.VALID_AMOUNT}, |
+ // Currency code more than 6 character is formatted to first 5 characters and |
+ // ellipsis. |
+ // "\u2026" is unicode for ellipsis. |
+ {"55.00", longStringOfLength(2048), "en-US", "AAAAA\u2026", "55.00", |
+ ExpectedValidity.VALID_AMOUNT}, |
+ |
+ // Invalid amount currency codes. |
+ {"55.00", longStringOfLength(2049), "en-US", null, null, |
+ ExpectedValidity.INVALID_AMOUNT_CURRENCY_CODE}, |
+ |
+ // Invalid amount values. |
+ {"", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"-", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"notdigits", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"ALSONOTDIGITS", "USD", "en-US", "USD", null, |
+ ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"10.", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {".99", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"10-", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"1-0", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"1.0.0", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ {"1/3", "USD", "en-US", "USD", null, ExpectedValidity.INVALID_AMOUNT_VALUE}, |
+ }); |
+ |
+ for (int i = 0; i < testCases.size(); i++) { |
+ Object[] testCase = testCases.get(i); |
+ |
+ String amount = (String) testCase[0]; |
+ String currency = (String) testCase[1]; |
+ String locale = (String) testCase[2]; |
+ String expectedCurrencyFormatting = (String) testCase[3]; |
+ String expectedAmountFormatting = (String) testCase[4]; |
+ ExpectedValidity expectedValidity = (ExpectedValidity) testCase[5]; |
+ |
+ CurrencyStringFormatter formatter = |
+ new CurrencyStringFormatter(currency, null, LocaleUtils.forLanguageTag(locale)); |
+ |
+ if (expectedValidity == ExpectedValidity.INVALID_AMOUNT_CURRENCY_CODE) { |
+ Assert.assertFalse("\"" + currency + "\" should be invalid currency code", |
+ formatter.isValidAmountCurrencyCode(currency)); |
+ } else { |
+ Assert.assertTrue("\"" + currency + "\" should be valid currency code", |
+ formatter.isValidAmountCurrencyCode(currency)); |
+ } |
+ |
+ if (expectedValidity == ExpectedValidity.INVALID_AMOUNT_VALUE) { |
+ Assert.assertFalse("\"" + amount + "\" should be invalid amount value", |
+ formatter.isValidAmountValue(amount)); |
+ } else { |
+ Assert.assertTrue("\"" + amount + "\" should be valid amount value", |
+ formatter.isValidAmountValue(amount)); |
+ } |
+ |
+ if (expectedValidity == ExpectedValidity.VALID_AMOUNT) { |
+ String formattedAmount = formatter.format(amount).replace(SPACE, " "); |
+ Assert.assertEquals("\"" + currency + "\" \"" + amount + "\" (\"" + locale |
+ + "\" locale) should be formatted into \"" |
+ + expectedAmountFormatting + "\"", |
+ expectedAmountFormatting, formattedAmount); |
+ Assert.assertEquals("\"" + currency + "\"" |
+ + " should be formatted into \"" + expectedCurrencyFormatting |
+ + "\"", |
+ expectedCurrencyFormatting, formatter.getFormattedCurrencyCode()); |
+ } |
+ } |
} |
} |