diff options
author | Victor Chang <vichang@google.com> | 2020-07-02 11:45:30 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-07-02 11:45:30 +0000 |
commit | 16e861acf2c34839a95958b1b216e0a2f7023a01 (patch) | |
tree | 0c6d15ed5759cab1aeacf7bbf05f9f2da31b3c6a /shadows | |
parent | cfb6930f31d12e8ee598d07dfd204d2b18b0ac28 (diff) | |
parent | 44be1fe7956ddeb4d5ec399ca31796985c74069d (diff) | |
download | robolectric-shadows-16e861acf2c34839a95958b1b216e0a2f7023a01.tar.gz |
Move libcore.timezone to the i18n module - Final part am: 3fdedcab75 am: 699fe3c85e am: 6e14f5baf2 am: c951935c05 am: 44be1fe795
Original change: https://android-review.googlesource.com/c/platform/external/robolectric-shadows/+/1348662
Change-Id: Icb7fd11ee3eb81008e8d2c4e316d78e8062c7415
Diffstat (limited to 'shadows')
-rw-r--r-- | shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderQ.java | 23 | ||||
-rw-r--r-- | shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderS.java | 73 |
2 files changed, 89 insertions, 7 deletions
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderQ.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderQ.java index 761a39b2b..324742adb 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderQ.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderQ.java @@ -8,24 +8,33 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import libcore.timezone.TimeZoneFinder; import org.robolectric.annotation.Implementation; import org.robolectric.annotation.Implements; +import org.robolectric.util.ReflectionHelpers; +import org.robolectric.util.ReflectionHelpers.ClassParameter; -/** Shadow for TimeZoneFinder on Q or above. */ +/** Shadow for TimeZoneFinder on Q,R. */ @Implements( - value = TimeZoneFinder.class, + className = "libcore.timezone.TimeZoneFinder", minSdk = Q, + maxSdk = Q, // maxSdk should be R, but it can't compile until AOSP switches current SDK to S. isInAndroidSdk = false, looseSignatures = true) public class ShadowTimeZoneFinderQ { private static final String TZLOOKUP_PATH = "/usr/share/zoneinfo/tzlookup.xml"; - @Implementation - protected static Object getInstance() { - return TimeZoneFinder.createInstanceForTests(readTzlookup()); - } + @Implementation + protected static Object getInstance() { + try { + return ReflectionHelpers.callStaticMethod( + Class.forName("libcore.timezone.TimeZoneFinder"), + "createInstanceForTests", + ClassParameter.from(String.class, readTzlookup())); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } /** * Reads tzlookup.xml from the files bundled inside android-all JARs. We need to read the file diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderS.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderS.java new file mode 100644 index 000000000..f7f8080b2 --- /dev/null +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowTimeZoneFinderS.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * 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. + */ + +// BEGIN-INTERNAL +package org.robolectric.shadows; + +import static android.os.Build.VERSION_CODES.R; +import static java.nio.charset.StandardCharsets.UTF_8; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import com.android.i18n.timezone.TimeZoneFinder; +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; + +/** Shadow for TimeZoneFinder on S or above. */ +@Implements( + value = TimeZoneFinder.class, + minSdk = R, // maxSdk should be R, but it can't compile until AOSP provides S version. + isInAndroidSdk = false, + looseSignatures = true) +public class ShadowTimeZoneFinderS { + + private static final String TZLOOKUP_PATH = "/usr/share/zoneinfo/tzlookup.xml"; + + @Implementation + protected static Object getInstance() { + return TimeZoneFinder.createInstanceForTests(readTzlookup()); + } + + /** + * Reads tzlookup.xml from the files bundled inside android-all JARs. We need to read the file + * instead of passing in the path because the real implementation uses {@link java.nio.file.Paths} + * which doesn't support reading from JARs. + */ + private static String readTzlookup() { + StringBuilder stringBuilder = new StringBuilder(); + InputStream is = null; + try { + try { + is = ShadowTimeZoneFinder.class.getResourceAsStream(TZLOOKUP_PATH); + BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF_8)); + for (String line; (line = reader.readLine()) != null; ) { + stringBuilder.append(line); + } + } finally { + if (is != null) { + is.close(); + } + } + } catch (IOException e) { + // ignore + } + + return stringBuilder.toString(); + } +} +// END-INTERNAL |