Fix TimeZoneVersionTest failures on pre-V devices. am: b698513951

Original change: https://android-review.googlesource.com/c/platform/system/timezone/+/3505500

Change-Id: I8e07969b9bbffb55bcbc59477ab9f782b82de316
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/apex/tests/src/java/android/tzdata/mts/TimeZoneVersionTest.java b/apex/tests/src/java/android/tzdata/mts/TimeZoneVersionTest.java
index a4f432a..8185941 100644
--- a/apex/tests/src/java/android/tzdata/mts/TimeZoneVersionTest.java
+++ b/apex/tests/src/java/android/tzdata/mts/TimeZoneVersionTest.java
@@ -18,6 +18,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
+import static org.junit.Assume.assumeTrue;
 
 import static java.util.stream.Collectors.toMap;
 
@@ -33,6 +34,7 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.UncheckedIOException;
+import java.lang.reflect.Method;
 import java.nio.charset.StandardCharsets;
 import java.util.HashSet;
 import java.util.Map;
@@ -128,6 +130,10 @@
 
     @Test
     public void versionFiles_areConsistent() {
+        // Test validates data installed in /versioned/ directory. It was introduced in tzdata6,
+        // and it is targeted to Android V+ only.
+        assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM);
+
         // Version in tz_version under versioned/N should be N.
         for (int version = MINIMAL_SUPPORTED_MAJOR_VERSION;
              version <= THE_LATEST_MAJOR_VERSION;
@@ -156,7 +162,20 @@
     }
 
     private static int getCurrentFormatMajorVersion() {
-        return TzDataSetVersion.currentFormatMajorVersion();
+        // TzDataSetVersion was moved from /libcore to /external/icu in S.
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+            return TzDataSetVersion.currentFormatMajorVersion();
+        } else {
+            try {
+                Class<?> libcoreTzDataSetVersion =
+                        Class.forName("libcore.timezone.TzDataSetVersion");
+                Method m = libcoreTzDataSetVersion.getDeclaredMethod("currentFormatMajorVersion");
+                m.setAccessible(true);
+                return (int) m.invoke(null);
+            } catch (ReflectiveOperationException roe) {
+                throw new AssertionError(roe);
+            }
+        }
     }
 
     /**
@@ -204,7 +223,13 @@
     }
 
     private static String readMajorFormatVersionForVersion(int version) {
-        File tzVersion = new File("%s/%d/tz_version".formatted(VERSIONED_DATA_LOCATION, version));
+        File tzVersion;
+        if (version >= 8) {
+            tzVersion = new File(
+                    "%s/%d/tz_version".formatted(VERSIONED_DATA_LOCATION, version));
+        } else {
+            tzVersion = TIME_ZONE_MODULE_VERSION_FILE;
+        }
         return readMajorFormatVersionFrom(tzVersion);
     }
 }