Make test_clear_45877 more reliable and test more behavior

The test depends on the System.currentMillis() method and so its
behavior varies based on the time. It was failing consistently
but only when the current time was in the afternoon in Los
Angeles. This is a change in behavior from M but it looks as
though the old behavior was wrong, the new behavior is
consistent with the RI. This behavior is described in the class
documentation in the #time_resolution section, although it's not
very clear.

The code works as follows. The test clears HOUR and HOUR_OF_DAY
fields whose stamp are marked as UNSET(0), AM_PM was previously
computed so is marked as COMPUTED(1). The selectFields() will
then select fields HOUR and AM_PM as being the best source of
the hours value. HOUR was cleared and set to 0, AM_PM was what
was previously computed (which in the morning was 0 and in the
afternoon was 1). HOUR_OF_DAY is calculated by multiplying AM_PM
by 12 so, HOUR_OF_DAY is set to 0 in the morning and 12 in the
afternoon which is exactly what is seen.

The test was only added as part of a documentation update so it
doesn't look as though it was trying to enforce the M behavior.

It looks as though the difference in behavior is related to
limitations in how M tracked which fields had been set. M only
remembered the last field set not a relative ordering over all
the set fields as the new OpenJDK based code does. So in some
cases, such as this one, M did not have enough information to
make the correct choice.

The test has been made more robust (uses a fixed time) and been
split into two tests, one of which tests the morning and one the
afternoon behavior.

Bug: 26022884
Change-Id: I15c8a9173a0819798bec3f11146fa7d5ea85600d
diff --git a/luni/src/test/java/libcore/java/util/CalendarTest.java b/luni/src/test/java/libcore/java/util/CalendarTest.java
index f7113d0..7f30b3d 100644
--- a/luni/src/test/java/libcore/java/util/CalendarTest.java
+++ b/luni/src/test/java/libcore/java/util/CalendarTest.java
@@ -210,8 +210,22 @@
     }
 
     // https://code.google.com/p/android/issues/detail?id=45877
-    public void test_clear_45877() {
+    public void test_clear_45877_morning() {
       GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
+      // 3rd February 2016 05:32:40.000 America/Los_Angeles time.
+      cal.setTimeInMillis(1454506360000L);
+      checkClear(cal, 0, 28800000);
+    }
+
+    // https://code.google.com/p/android/issues/detail?id=45877
+    public void test_clear_45877_afternoon() {
+      GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Los_Angeles"));
+      // 3rd February 2016 12:32:40.000 America/Los_Angeles time.
+      cal.setTimeInMillis(1454531560000L);
+      checkClear(cal, 12, 72000000);
+    }
+
+    private void checkClear(GregorianCalendar cal, int expectedHourOfDay, long expectedMillis) {
       cal.set(Calendar.YEAR, 1970);
       cal.set(Calendar.MONTH, Calendar.JANUARY);
       cal.set(Calendar.DAY_OF_MONTH, 1);
@@ -226,7 +240,7 @@
       assertFalse(cal.isSet(Calendar.HOUR_OF_DAY));
 
       // When we call get, unset fields are computed.
-      assertEquals(0, cal.get(Calendar.HOUR_OF_DAY));
+      assertEquals(expectedHourOfDay, cal.get(Calendar.HOUR_OF_DAY));
       // And set fields stay the same.
       assertEquals(1, cal.get(Calendar.DAY_OF_MONTH));
 
@@ -234,7 +248,7 @@
       assertTrue(cal.isSet(Calendar.DAY_OF_MONTH));
       assertTrue(cal.isSet(Calendar.HOUR_OF_DAY));
 
-      assertEquals(28800000, cal.getTimeInMillis());
+      assertEquals(expectedMillis, cal.getTimeInMillis());
 
       cal.set(Calendar.HOUR_OF_DAY, 1);
       assertEquals(32400000, cal.getTimeInMillis());