• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings.testutils.shadow;
18 
19 import libcore.icu.TimeZoneNames;
20 
21 import org.robolectric.annotation.Implementation;
22 import org.robolectric.annotation.Implements;
23 import org.robolectric.annotation.RealObject;
24 import org.robolectric.util.ReflectionHelpers;
25 
26 import java.util.Locale;
27 import java.util.TimeZone;
28 
29 /**
30  * System.logI used by ZoneStringsCache.create is a method new in API 24 and not available in
31  * Robolectric's 6.0 jar. Create a shadow which removes that log call.
32  */
33 @Implements(value = TimeZoneNames.class, isInAndroidSdk = false)
34 public class ShadowLibcoreTimeZoneNames {
35 
36     private static final String[] availableTimeZoneIds = TimeZone.getAvailableIDs();
37 
38     @Implements(value = TimeZoneNames.ZoneStringsCache.class, isInAndroidSdk = false)
39     public static class ShadowZoneStringsCache {
40 
41         @RealObject
42         private TimeZoneNames.ZoneStringsCache mRealObject;
43 
44         @Implementation
create(Locale locale)45         public String[][] create(Locale locale) {
46             // Set up the 2D array used to hold the names. The first column contains the Olson ids.
47             String[][] result = new String[availableTimeZoneIds.length][5];
48             for (int i = 0; i < availableTimeZoneIds.length; ++i) {
49                 result[i][0] = availableTimeZoneIds[i];
50             }
51 
52             ReflectionHelpers.callInstanceMethod(TimeZoneNames.class,
53                     mRealObject, "fillZoneStrings",
54                     ReflectionHelpers.ClassParameter.from(String.class, locale.toLanguageTag()),
55                     ReflectionHelpers.ClassParameter.from(String[][].class, result));
56 
57             return result;
58         }
59     }
60 }
61