1 /*
2  * Copyright (C) 2018 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.car.settings.testutils;
18 
19 import android.content.Context;
20 
21 import com.android.internal.app.LocaleStore;
22 
23 import org.robolectric.annotation.Implementation;
24 import org.robolectric.annotation.Implements;
25 import org.robolectric.annotation.Resetter;
26 
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Set;
32 
33 @Implements(LocaleStore.class)
34 public class ShadowLocaleStore {
35 
36     private static Map<LocaleStore.LocaleInfo, Set<LocaleStore.LocaleInfo>>
37             sLocaleInfoRelationships = new HashMap<>();
38 
addLocaleRelationship(Locale parent, Locale child)39     public static void addLocaleRelationship(Locale parent, Locale child) {
40         LocaleStore.LocaleInfo parentInfo = LocaleStore.getLocaleInfo(parent);
41         LocaleStore.LocaleInfo childInfo = LocaleStore.getLocaleInfo(child);
42         Set<LocaleStore.LocaleInfo> set = sLocaleInfoRelationships.getOrDefault(parentInfo,
43                 new HashSet<>());
44         set.add(childInfo);
45         sLocaleInfoRelationships.put(parentInfo, set);
46     }
47 
48     @Resetter
reset()49     public static void reset() {
50         sLocaleInfoRelationships.clear();
51     }
52 
53     @Implementation
getLevelLocales(Context context, Set<String> ignorables, LocaleStore.LocaleInfo parent, boolean translatedOnly)54     protected static Set<LocaleStore.LocaleInfo> getLevelLocales(Context context,
55             Set<String> ignorables, LocaleStore.LocaleInfo parent, boolean translatedOnly) {
56         if (parent != null) {
57             return sLocaleInfoRelationships.getOrDefault(parent, new HashSet<>());
58         } else {
59             return sLocaleInfoRelationships.keySet();
60         }
61     }
62 }
63