DO NOT MERGE [CtsLibcoreOjTestCases] Old Android releases can optionally support the new Japanese era

Add a extra test to ensure java.time and ICU are consistent in the
latest Japanese era, even though the new era is optionally supported.

Bug: 138560701
Test: CtsLibcoreOjTestCases on old and new system image
Test: CtsLibcoreTestCases:libcore.java.time.chrono.JapaneseChronologyTest
Change-Id: I4a33b3b07269ca1274093cdd963c42036613100e
diff --git a/luni/src/test/java/libcore/java/time/chrono/JapaneseChronologyTest.java b/luni/src/test/java/libcore/java/time/chrono/JapaneseChronologyTest.java
index 3c1f0cf..1f36b2c 100644
--- a/luni/src/test/java/libcore/java/time/chrono/JapaneseChronologyTest.java
+++ b/luni/src/test/java/libcore/java/time/chrono/JapaneseChronologyTest.java
@@ -16,6 +16,8 @@
 package libcore.java.time.chrono;
 
 import org.junit.Test;
+import android.icu.util.JapaneseCalendar;
+import java.util.List;
 import java.time.Instant;
 import java.time.LocalDate;
 import java.time.LocalTime;
@@ -23,6 +25,7 @@
 import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.time.chrono.ChronoZonedDateTime;
+import java.time.chrono.Era;
 import java.time.chrono.JapaneseChronology;
 import java.time.chrono.JapaneseDate;
 import java.time.chrono.JapaneseEra;
@@ -110,4 +113,14 @@
         assertEquals(true, date.isSupported(ChronoField.YEAR));
         assertEquals(true, date.isSupported(ChronoField.YEAR_OF_ERA));
     }
+
+    @Test
+    public void test_eras_isLatestEraConsistency() {
+        List<Era> japaneseEras = JapaneseChronology.INSTANCE.eras();
+        boolean isHeiseiLatestInJavaTime =
+            japaneseEras.get(japaneseEras.size()-1).getValue() <= JapaneseEra.HEISEI.getValue();
+        boolean isHeiseiLatestInIcu = JapaneseCalendar.CURRENT_ERA == JapaneseCalendar.HEISEI;
+        assertEquals("java.time and ICU4J are not consistent in the latest japanese era",
+            isHeiseiLatestInJavaTime, isHeiseiLatestInIcu);
+    }
 }
diff --git a/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseChronology.java b/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseChronology.java
index 091b385..394d6d5 100644
--- a/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseChronology.java
+++ b/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseChronology.java
@@ -112,6 +112,21 @@
 @Test
 public class TCKJapaneseChronology {
 
+    // Android-added: Add a static field to indicate if the device supports the new Japanese era.
+    /**
+     * Indicates if the device support newer Japenese Era than Heisei. Old Android releases can
+     * optionally support new Japanese Era, e.g. Reiwa, and Android test suites, e.g. CTS, can use
+     * this flag to alter the expected result. This flag can be placed in other classes, but
+     * TCKJapaneseChronology is picked arbitrarily.
+     */
+    public static final boolean IS_HEISEI_LATEST;
+    static {
+        List<Era> japaneseEras = JapaneseChronology.INSTANCE.eras();
+        IS_HEISEI_LATEST =
+            japaneseEras.get(japaneseEras.size()-1).getValue() <= JapaneseEra.HEISEI.getValue();
+    }
+
+
     // Year differences from Gregorian years.
     private static final int YDIFF_REIWA = 2018;
     private static final int YDIFF_HEISEI = 1988;
@@ -176,7 +191,10 @@
     @DataProvider(name="createByEra")
     Object[][] data_createByEra() {
         return new Object[][] {
-                {JapaneseEra.of(3), 2020 - YDIFF_REIWA, 2, 29, 60, LocalDate.of(2020, 2, 29)}, // NEWERA
+                // Android-changed: Old Android releases can optionally support the new Japanese era.
+                IS_HEISEI_LATEST
+                    ? new Object[] {JapaneseEra.HEISEI, 2020 - YDIFF_HEISEI, 2, 29, 60, LocalDate.of(2020, 2, 29)}
+                    : new Object[] {JapaneseEra.of(3), 2020 - YDIFF_REIWA, 2, 29, 60, LocalDate.of(2020, 2, 29)}, // NEWERA
                 {JapaneseEra.HEISEI, 1996 - YDIFF_HEISEI, 2, 29, 60, LocalDate.of(1996, 2, 29)},
                 {JapaneseEra.HEISEI, 2000 - YDIFF_HEISEI, 2, 29, 60, LocalDate.of(2000, 2, 29)},
                 {JapaneseEra.MEIJI, 1874 - YDIFF_MEIJI, 2, 28, 59, LocalDate.of(1874, 2, 28)},
@@ -369,8 +387,13 @@
     @DataProvider(name="prolepticYear")
     Object[][] data_prolepticYear() {
         return new Object[][] {
-                {3, JapaneseEra.of(3), 1, 1 + YDIFF_REIWA, false},
-                {3, JapaneseEra.of(3), 102, 102 + YDIFF_REIWA, true},
+                // Android-changed: Old Android releases can optionally support the new Japanese era.
+                IS_HEISEI_LATEST
+                    ? new Object[] {2, JapaneseEra.HEISEI, 1, 1 + YDIFF_HEISEI, false}
+                    : new Object[] {3, JapaneseEra.of(3), 1, 1 + YDIFF_REIWA, false},
+                IS_HEISEI_LATEST
+                    ? new Object[] {2, JapaneseEra.HEISEI, 102, 102 + YDIFF_HEISEI, false}
+                    : new Object[] {3, JapaneseEra.of(3), 102, 102 + YDIFF_REIWA, true},
 
                 {2, JapaneseEra.HEISEI, 1, 1 + YDIFF_HEISEI, false},
                 {2, JapaneseEra.HEISEI, 4, 4 + YDIFF_HEISEI, true},
@@ -550,12 +573,21 @@
     //-----------------------------------------------------------------------
     @DataProvider(name="japaneseEras")
     Object[][] data_japanseseEras() {
+        // Android-changed: Old Android releases can optionally support the new Japanese era.
+        if (!IS_HEISEI_LATEST) {
+            return new Object[][] {
+                { JapaneseEra.MEIJI, -1, "Meiji"},
+                { JapaneseEra.TAISHO, 0, "Taisho"},
+                { JapaneseEra.SHOWA, 1, "Showa"},
+                { JapaneseEra.HEISEI, 2, "Heisei"},
+                { JapaneseEra.of(3), 3, "Reiwa"},
+            };
+        }
         return new Object[][] {
             { JapaneseEra.MEIJI, -1, "Meiji"},
             { JapaneseEra.TAISHO, 0, "Taisho"},
             { JapaneseEra.SHOWA, 1, "Showa"},
             { JapaneseEra.HEISEI, 2, "Heisei"},
-            { JapaneseEra.of(3), 3, "Reiwa"},
         };
     }
 
@@ -691,7 +723,10 @@
             {JapaneseChronology.INSTANCE.date(1989,  1,  7), "Japanese Showa 64-01-07"},
             {JapaneseChronology.INSTANCE.date(1989,  1,  8), "Japanese Heisei 1-01-08"},
             {JapaneseChronology.INSTANCE.date(2012, 12,  6), "Japanese Heisei 24-12-06"},
-            {JapaneseChronology.INSTANCE.date(2020,  1,  6), "Japanese Reiwa 2-01-06"},
+            // Android-changed: Old Android releases can optionally support the new Japanese era.
+            IS_HEISEI_LATEST
+                ? new Object[] {JapaneseChronology.INSTANCE.date(2020,  1,  6), "Japanese Heisei 32-01-06"}
+                : new Object[] {JapaneseChronology.INSTANCE.date(2020,  1,  6), "Japanese Reiwa 2-01-06"},
         };
     }
 
diff --git a/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java b/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java
index e37a6a5..815517c 100644
--- a/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java
+++ b/ojluni/src/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java
@@ -59,6 +59,7 @@
 import static java.time.temporal.ChronoField.ERA;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
+import static tck.java.time.chrono.TCKJapaneseChronology.IS_HEISEI_LATEST;
 
 import java.time.chrono.Era;
 import java.time.chrono.JapaneseChronology;
@@ -76,8 +77,17 @@
 
     @DataProvider(name = "JapaneseEras")
     Object[][] data_of_eras() {
+        // Android-changed: Old Android releases can optionally support the new Japanese era.
+        if (!IS_HEISEI_LATEST) {
+            return new Object[][] {
+                        {JapaneseEra.of(3), "Reiwa", 3},
+                        {JapaneseEra.HEISEI, "Heisei", 2},
+                        {JapaneseEra.SHOWA, "Showa", 1},
+                        {JapaneseEra.TAISHO, "Taisho", 0},
+                        {JapaneseEra.MEIJI, "Meiji", -1},
+            };
+        }
         return new Object[][] {
-                    {JapaneseEra.of(3), "Reiwa", 3},
                     {JapaneseEra.HEISEI, "Heisei", 2},
                     {JapaneseEra.SHOWA, "Showa", 1},
                     {JapaneseEra.TAISHO, "Taisho", 0},
diff --git a/ojluni/src/test/java/time/test/java/time/chrono/TestJapaneseChronology.java b/ojluni/src/test/java/time/test/java/time/chrono/TestJapaneseChronology.java
index 03443d9..337a8db 100644
--- a/ojluni/src/test/java/time/test/java/time/chrono/TestJapaneseChronology.java
+++ b/ojluni/src/test/java/time/test/java/time/chrono/TestJapaneseChronology.java
@@ -28,6 +28,7 @@
 import java.time.*;
 import java.time.chrono.*;
 import java.time.temporal.*;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Locale;
 
@@ -35,6 +36,7 @@
 import org.testng.annotations.Test;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
+import static tck.java.time.chrono.TCKJapaneseChronology.IS_HEISEI_LATEST;
 
 /**
  * Tests for the Japanese chronology
@@ -46,7 +48,8 @@
 
     @DataProvider(name="transitions")
     Object[][] transitionData() {
-        return new Object[][] {
+        // Android-changed: Old Android releases can optionally support the new Japanese era.
+        List<Object[]> data = Arrays.asList(new Object[][] {
             // Japanese era, yearOfEra, month, dayOfMonth, gregorianYear
             { JapaneseEra.MEIJI,      6,  1,  1, 1873 },
             // Meiji-Taisho transition isn't accurate. 1912-07-30 is the last day of Meiji
@@ -59,14 +62,20 @@
             { JapaneseEra.SHOWA,      1, 12, 25, 1926 },
             { JapaneseEra.SHOWA,     64,  1,  7, 1989 },
             { JapaneseEra.HEISEI,     1,  1,  8, 1989 },
-            { JapaneseEra.HEISEI,    31,  4, 30, 2019 },
-            { JapaneseEra.of(3),      1,  5,  1, 2019 },
-        };
+        });
+        if (IS_HEISEI_LATEST) {
+            data.addAll(Arrays.asList(new Object[][] {
+                { JapaneseEra.HEISEI,    31,  4, 30, 2019 },
+                { JapaneseEra.of(3),      1,  5,  1, 2019 },
+            }));
+        }
+        return data.toArray(new Object[data.size()][]);
     }
 
     @DataProvider(name="day_year_data")
     Object[][] dayYearData() {
-        return new Object[][] {
+        // Android-changed: Old Android releases can optionally support the new Japanese era.
+        List<Object[]> data = Arrays.asList(new Object[][] {
             // Japanese era, yearOfEra, dayOfYear, month, dayOfMonth
             { JapaneseEra.MEIJI,  45,  211,  7, 29 },
             { JapaneseEra.TAISHO,  1,    1,  7, 30 },
@@ -77,17 +86,25 @@
             { JapaneseEra.SHOWA,  64,    7,  1,  7 },
             { JapaneseEra.HEISEI,  1,    1,  1,  8 },
             { JapaneseEra.HEISEI,  2,    8,  1,  8 },
-            { JapaneseEra.HEISEI, 31,  120,  4, 30 },
-            { JapaneseEra.of(3),   1,    1,  5,  1 },
-        };
+        });
+        if (IS_HEISEI_LATEST) {
+            data.addAll(Arrays.asList(new Object[][] {
+                { JapaneseEra.HEISEI, 31,  120,  4, 30 },
+                { JapaneseEra.of(3),   1,    1,  5,  1 },
+            }));
+        }
+        return data.toArray(new Object[data.size()][]);
     }
 
     @DataProvider(name="range_data")
     Object[][] rangeData() {
+        // Android-changed: Old Android releases can optionally support the new Japanese era.
+        int maxEra = IS_HEISEI_LATEST ? 2 : 3;
+        int yearOfLatestEra = IS_HEISEI_LATEST ? 1989 : 2019;
         return new Object[][] {
             // field, minSmallest, minLargest, maxSmallest, maxLargest
-            { ChronoField.ERA,         -1, -1, 3, 3},
-            { ChronoField.YEAR_OF_ERA, 1, 1, 15, 999999999-2019}, // depends on the current era
+            { ChronoField.ERA,         -1, -1, maxEra, maxEra},
+            { ChronoField.YEAR_OF_ERA, 1, 1, 15, 999999999-yearOfLatestEra}, // depends on the current era
             { ChronoField.DAY_OF_YEAR, 1, 1, 7, 366},
             { ChronoField.YEAR, 1873, 1873, 999999999, 999999999},
         };
diff --git a/ojluni/src/test/java/time/test/java/time/chrono/TestUmmAlQuraChronology.java b/ojluni/src/test/java/time/test/java/time/chrono/TestUmmAlQuraChronology.java
index f86f144..e2f2b77 100644
--- a/ojluni/src/test/java/time/test/java/time/chrono/TestUmmAlQuraChronology.java
+++ b/ojluni/src/test/java/time/test/java/time/chrono/TestUmmAlQuraChronology.java
@@ -33,6 +33,7 @@
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
+import static tck.java.time.chrono.TCKJapaneseChronology.IS_HEISEI_LATEST;
 
 import java.time.DateTimeException;
 import java.time.DayOfWeek;
@@ -776,9 +777,10 @@
             {HijrahDate.of(1434,5,1), "Japanese Heisei 25-03-13"},
             {HijrahDate.of(1436,1,1), "Japanese Heisei 26-10-25"},
             {HijrahDate.of(1440,8,25), "Japanese Heisei 31-04-30"},
-            {HijrahDate.of(1440,8,26), "Japanese Reiwa 1-05-01"},
-            {HijrahDate.of(1500,6,12), "Japanese Reiwa 59-05-05"},
-            {HijrahDate.of(1550,3,11), "Japanese Reiwa 107-08-11"},
+            // Android-changed: Old Android releases can optionally support the new Japanese era.
+            {HijrahDate.of(1440,8,26), IS_HEISEI_LATEST ? "Japanese Heisei 31-05-01" : "Japanese Reiwa 1-05-01"},
+            {HijrahDate.of(1500,6,12), IS_HEISEI_LATEST ? "Japanese Heisei 89-05-05" : "Japanese Reiwa 59-05-05"},
+            {HijrahDate.of(1550,3,11), IS_HEISEI_LATEST ? "Japanese Heisei 137-08-11" : "Japanese Reiwa 107-08-11"},
         };
     }
 
diff --git a/ojluni/src/test/java/time/test/java/time/format/TestNonIsoFormatter.java b/ojluni/src/test/java/time/test/java/time/format/TestNonIsoFormatter.java
index 3fbab00..df74377 100644
--- a/ojluni/src/test/java/time/test/java/time/format/TestNonIsoFormatter.java
+++ b/ojluni/src/test/java/time/test/java/time/format/TestNonIsoFormatter.java
@@ -30,6 +30,7 @@
 package test.java.time.format;
 
 import static org.testng.Assert.assertEquals;
+import static tck.java.time.chrono.TCKJapaneseChronology.IS_HEISEI_LATEST;
 
 import java.time.LocalDate;
 import java.time.chrono.ChronoLocalDate;
@@ -150,7 +151,8 @@
             // Android-changed: Eras names have been changed in CLDR data.
             // { JAPANESE, "Showa 65", "Heisei 2" }
             { JAPANESE, "Shōwa 65", "Heisei 2" },
-            { JAPANESE, "Heisei 32", "Reiwa 2" },
+            // Android-changed: Old Android releases can optionally support the new Japanese era.
+            { JAPANESE, "Heisei 32", IS_HEISEI_LATEST ? "Heisei 32" : "Reiwa 2" },
         };
     }