diff options
author | Victor Chang <vichang@google.com> | 2020-06-24 12:56:48 +0100 |
---|---|---|
committer | Victor Chang <vichang@google.com> | 2020-07-01 23:33:46 +0100 |
commit | 3fdedcab7559373d037cb1fd6207b06633ea1253 (patch) | |
tree | 400dd2b94e0353c04285f2be4e3293666e85b0ad /shadows | |
parent | 71375a719f7f5d5c7997464976924f8f1cb69bf9 (diff) | |
download | robolectric-shadows-3fdedcab7559373d037cb1fd6207b06633ea1253.tar.gz |
Move libcore.timezone to the i18n module - Final partandroid-r-beta-3android-r-beta-2
Bug: 141747409
Test: m checkbuild
Change-Id: I19605f9a3041d0f51975c4b6c22d7a8b64006a34
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 |