Skip to content

Commit

Permalink
Add test support for checking OS version, used it to comment out loca…
Browse files Browse the repository at this point in the history
…le tests that don’t pass on macOS 10.11.

	Change on 2017/06/09 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158538445
  • Loading branch information
tomball authored and kstanger committed Jun 15, 2017
1 parent 671b2ea commit 7c7e442
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 16 deletions.
99 changes: 99 additions & 0 deletions jre_emul/Tests/com/google/j2objc/EnvironmentUtil.java
@@ -0,0 +1,99 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.j2objc;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import junit.framework.TestCase;

/**
* Utility test support methods to check for minimum OS and SDK versions.
*/
public class EnvironmentUtil extends TestCase {

private static final Pattern VERSION_REGEX = Pattern.compile("(\\d+)\\.(\\d+)(\\.(\\d+))?");

/**
* Returns true if test is executing on macOS.
*/
public static boolean onMacOSX() {
return System.getProperty("os.name").equals("Mac OS X");
}

/**
* Returns true if test is executing on iPhone device.
*/
public static boolean onIPhone() {
return System.getProperty("os.name").equals("iPhone");
}

/**
* Returns true if test is executing on macOS.
*/
public static boolean onIPhoneSimulator() {
return System.getProperty("os.name").equals("iPhone Simulator");
}

/**
* Returns the version of the operating system being run.
*/
public static String osVersion() {
return System.getProperty("os.version");
}

/**
* Returns true if test is running on a minimum OS version.
* Specified version strings must be of the form "n.n[.n]", such as
* "10.12" or "9.3.1".
*/
public static boolean onMinimumOSVersion(String minimum) {
return compareVersions(minimum, osVersion()) >= 0;
}

private static int compareVersions(String v1, String v2) {
int[] version1 = parseVersion(v1, 0);
int[] version2 = parseVersion(v2, Integer.MAX_VALUE);
for (int i = 0; i < 3; i++) {
if (version1[i] != version2[i]) {
return version1[i] < version2[i] ? 1 : -1;
}
}
return 0;
}

private static int[] parseVersion(String version, int defaultRevision) {
Matcher m = VERSION_REGEX.matcher(version);
if (!m.matches()) {
throw new IllegalArgumentException("invalid version string: \"" + version + "\"");
}
String revision = m.group(4);
return new int[] {
Integer.parseInt(m.group(1)), // major version number
Integer.parseInt(m.group(2)), // minor version number
revision == null ? defaultRevision : Integer.parseInt(revision),
};
}

// Unit test for above.
public void testCompareVersions() {
assertEquals(0, compareVersions("1.2.3", "1.2.3"));
assertEquals(1, compareVersions("4.5", "4.5.6"));
assertEquals(1, compareVersions("7.8.9", "7.8.10"));
assertEquals(1, compareVersions("7.8.9", "7.9.3"));
assertEquals(-1, compareVersions("6.5.4", "6.5.3"));
assertEquals(-1, compareVersions("6.5.4", "6.4.2"));
}

}
4 changes: 0 additions & 4 deletions jre_emul/Tests/java/lang/SystemTest.java
Expand Up @@ -127,8 +127,4 @@ public void testSystemProperties() {
assertTrue("empty key returned: " + key, System.getProperty(key).length() > 0);
}
}

private static boolean onMac() {
return System.getProperty("os.name").equals("Mac OS X");
}
}
Expand Up @@ -16,6 +16,7 @@

package libcore.icu;

import com.google.j2objc.EnvironmentUtil;
import java.util.Locale;

public class LocaleDataTest extends junit.framework.TestCase {
Expand Down Expand Up @@ -87,16 +88,19 @@ public void test_ko_KR() throws Exception {
}

public void test_ru_RU() throws Exception {
LocaleData l = LocaleData.get(new Locale("ru", "RU"));

assertEquals("воскресенье", l.longWeekdayNames[1]);
assertEquals("вс", l.shortWeekdayNames[1]);
assertEquals("вс", l.tinyWeekdayNames[1]);

// Russian stand-alone weekday names have no initial capital since CLDR 28/ICU 56.
assertEquals("воскресенье", l.longStandAloneWeekdayNames[1]);
assertEquals("вс", l.shortStandAloneWeekdayNames[1]);
assertEquals("В", l.tinyStandAloneWeekdayNames[1]);
// Russian locale strings updated in macOS 10.12 to match iOS.
if (!EnvironmentUtil.onMacOSX() || EnvironmentUtil.onMinimumOSVersion("10.12")) {
LocaleData l = LocaleData.get(new Locale("ru", "RU"));

assertEquals("воскресенье", l.longWeekdayNames[1]);
assertEquals("вс", l.shortWeekdayNames[1]);
assertEquals("вс", l.tinyWeekdayNames[1]);

// Russian stand-alone weekday names have no initial capital since CLDR 28/ICU 56.
assertEquals("воскресенье", l.longStandAloneWeekdayNames[1]);
assertEquals("вс", l.shortStandAloneWeekdayNames[1]);
assertEquals("В", l.tinyStandAloneWeekdayNames[1]);
}
}

// http://code.google.com/p/android/issues/detail?id=38844
Expand Down
Expand Up @@ -16,6 +16,7 @@

package libcore.java.text;

import com.google.j2objc.EnvironmentUtil;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
Expand Down Expand Up @@ -244,6 +245,8 @@ public void test_setCurrency() throws Exception {

// Test the setting of locale specific patterns which have different fractional digits.
public void test_currencyWithPatternDigits() throws Exception {
// Locale strings updated in macOS 10.12 to match iOS.
if (!EnvironmentUtil.onMacOSX() || EnvironmentUtil.onMinimumOSVersion("10.12")) {
// Japanese Yen 0 fractional digits.
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.JAPAN);
String result = nf.format(50.00);
Expand All @@ -257,6 +260,7 @@ public void test_currencyWithPatternDigits() throws Exception {
// Swiss Francs 2 fractional digits.
nf = NumberFormat.getCurrencyInstance(Locale.forLanguageTag("de-CH"));
assertEquals("CHF\u00a050.00", nf.format(50.00));
}
}

// http://b/28893763
Expand Down
Expand Up @@ -16,6 +16,7 @@

package libcore.java.text;

import com.google.j2objc.EnvironmentUtil;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
Expand Down Expand Up @@ -68,7 +69,9 @@ public void test2DigitYearStartIsCloned() throws Exception {
// The RI fails this test because this is an ICU-compatible Android extension.
// Necessary for correct localization in various languages (http://b/2633414).
public void testStandAloneNames() throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
// Locale strings updated in macOS 10.12 to match iOS.
if (!EnvironmentUtil.onMacOSX() || EnvironmentUtil.onMinimumOSVersion("10.12")) {
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
Locale en = Locale.ENGLISH;
Locale pl = new Locale("pl");
Locale ru = new Locale("ru");
Expand All @@ -92,6 +95,7 @@ public void testStandAloneNames() throws Exception {
assertEquals(Calendar.TUESDAY, parseDate(en, "cccc", "Tuesday").get(Calendar.DAY_OF_WEEK));
assertEquals(Calendar.TUESDAY, parseDate(ru, "EEEE", "\u0432\u0442\u043e\u0440\u043d\u0438\u043a").get(Calendar.DAY_OF_WEEK));
assertEquals(Calendar.TUESDAY, parseDate(ru, "cccc", "\u0412\u0442\u043e\u0440\u043d\u0438\u043a").get(Calendar.DAY_OF_WEEK));
}
}

// The RI fails this test because it doesn't fully support UTS #35.
Expand Down
Expand Up @@ -16,6 +16,7 @@

package libcore.java.util;

import com.google.j2objc.EnvironmentUtil;
import java.io.ObjectInputStream;
//import java.text.BreakIterator;
import java.text.Collator;
Expand Down Expand Up @@ -149,10 +150,13 @@ public void test_tl_and_fil() throws Exception {

// http://b/3452611; Locale.getDisplayLanguage fails for the obsolete language codes.
public void test_getDisplayName_obsolete() throws Exception {
// he (new) -> iw (obsolete)
// Locale strings updated in macOS 10.12 to match iOS.
if (!EnvironmentUtil.onMacOSX() || EnvironmentUtil.onMinimumOSVersion("10.12")) {
// he (new) -> iw (obsolete)
assertObsolete("he", "iw", "עברית");
// id (new) -> in (obsolete)
assertObsolete("id", "in", "Indonesia");
}
}

private static void assertObsolete(String newCode, String oldCode, String displayName) {
Expand Down
1 change: 1 addition & 0 deletions jre_emul/test_sources.mk
Expand Up @@ -193,6 +193,7 @@ TEST_SOURCES := \
com/google/j2objc/ArrayTest.java \
com/google/j2objc/AssertTest.java \
com/google/j2objc/ClassTest.java \
com/google/j2objc/EnvironmentUtil.java \
com/google/j2objc/FieldTest.java \
com/google/j2objc/LinkedBlockingQueueTest.java \
com/google/j2objc/LinkedListTest.java \
Expand Down

0 comments on commit 7c7e442

Please sign in to comment.