1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt.internal.editors.layout.configuration;
18 
19 import com.android.SdkConstants;
20 import com.android.ide.common.resources.LocaleManager;
21 import com.google.common.base.Function;
22 import com.google.common.collect.ArrayListMultimap;
23 import com.google.common.collect.Multimap;
24 import com.google.common.collect.Sets;
25 
26 import org.eclipse.swt.graphics.Image;
27 
28 import java.util.ArrayList;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.Set;
34 
35 import junit.framework.TestCase;
36 
37 @SuppressWarnings("javadoc")
38 public class FlagManagerTest extends TestCase {
testGetFlagImage()39     public void testGetFlagImage() {
40         FlagManager manager = FlagManager.get();
41         Image us = manager.getFlag("US");
42         Image gb = manager.getFlag("GB");
43         Image ca = manager.getFlag("CA");
44         Image es = manager.getFlag("ES");
45         Image br = manager.getFlag("BR");
46         Image pt = manager.getFlag("PT");
47         assertSame(us, manager.getFlag("en", "US"));
48         assertSame(gb, manager.getFlag("en", "GB"));
49         assertSame(ca, manager.getFlag("en", "CA"));
50         Locale.setDefault(Locale.US);
51         assertSame(us, manager.getFlag("en", null));
52         Locale.setDefault(Locale.UK);
53         assertSame(gb, manager.getFlag("en", null));
54         Locale.setDefault(Locale.CANADA);
55         assertSame(ca, manager.getFlag("en", null));
56         assertSame(manager.getFlag("NO"), manager.getFlag("nb", null));
57         assertSame(manager.getFlag("FR"), manager.getFlag("fr", null));
58 
59         Locale.setDefault(new Locale("pt", "br"));
60         assertSame(br, manager.getFlag("pt", null));
61         assertSame(pt, manager.getFlag("pt", "PT"));
62         Locale.setDefault(new Locale("pt", "pt"));
63         assertSame(pt, manager.getFlag("pt", null));
64         assertSame(br, manager.getFlag("pt", "BR"));
65 
66         // Special cases where we have custom flags
67         assertNotSame(gb, manager.getFlag("cy", null)); // Wales
68         assertNotSame(es, manager.getFlag("ca", null)); // Catalonia
69 
70         // Aliases - http://developer.android.com/reference/java/util/Locale.html
71         assertSame(manager.getFlag("yi", null), manager.getFlag("ji", null));
72         assertSame(manager.getFlag("in", null), manager.getFlag("id", null));
73         assertSame(manager.getFlag("iw", null), manager.getFlag("he", null));
74         assertSame(us, manager.getFlagForFolderName("values-en-rUS"));
75         assertSame(gb, manager.getFlagForFolderName("values-en-rGB"));
76         Locale.setDefault(Locale.CANADA);
77         assertSame(ca, manager.getFlagForFolderName("values-en"));
78     }
79 
testAvailableImages()80     public void testAvailableImages() {
81         // Images we have from WindowBuilder (which are really the famfamfam
82         // icons;
83         // see http://www.famfamfam.com/lab/icons/flags)
84         String[] icons = new String[] {
85                 "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "ar", "as", "at", "au", "aw", "ax",
86                 "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br",
87                 "bs", "bt", "bv", "bw", "by", "bz", "ca", "catalonia", "cc", "cd", "cf", "cg",
88                 "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de",
89                 "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "england", "er", "es", "et",
90                 "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi",
91                 "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn",
92                 "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "jm",
93                 "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la",
94                 "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me",
95                 "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv",
96                 "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr",
97                 "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps",
98                 "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "scotland",
99                 "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st",
100                 "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to",
101                 "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy", "uz", "va", "vc", "ve",
102                 "vg", "vi", "vn", "vu", "wales", "wf", "ws", "ye", "yt", "za", "zm", "zw"
103         };
104 
105         Set<String> sImages = new HashSet<String>(100);
106         for (String code : icons) {
107             if (code.length() > 2) {
108                 continue;
109             }
110             code = code.toUpperCase(Locale.US);
111             sImages.add(code);
112 
113             if (!LocaleManager.isValidRegionCode(code)) {
114                 System.out.println("No region name found for region code " + code);
115             }
116         }
117 
118         Set<String> unused = Sets.newHashSet(LocaleManager.getRegionCodes(false));
119         Set<String> reachable = Sets.newHashSet();
120         Multimap<String, String> regionToLanguages = ArrayListMultimap.create();
121         for (String language : LocaleManager.getLanguageCodes(false)) {
122             for (String region : LocaleManager.getRelevantRegions(language)) {
123                 reachable.add(region);
124                 regionToLanguages.put(region, language);
125             }
126         }
127         unused.removeAll(reachable);
128 
129         for (String region : reachable) {
130             if (!sImages.contains(region)) {
131                 StringBuilder sb = new StringBuilder();
132 
133                 sb.append("No icon found for region ").append(region).append("  ")
134                         .append(LocaleManager.getRegionName(region));
135                 sb.append(", used for languages ");
136 
137                 for (String language : regionToLanguages.get(region)) {
138                     sb.append(language).append("(").append(LocaleManager.getLanguageName(language))
139                             .append(") ");
140                 }
141                 System.out.println(sb.toString());
142             }
143         }
144 
145         // Known regions that we don't have language to region mappings for
146         unused.remove("AQ");
147         unused.remove("VA");
148         unused.remove("GS");
149         unused.remove("TF");
150         unused.remove("BV");
151         unused.remove("HM");
152 
153         if (!unused.isEmpty()) {
154             StringBuilder sb = new StringBuilder();
155             sb.append("The following icons are not referenced by any of the "
156                     + "language to country bindings:");
157             for (String code : unused) {
158                 sb.append(code.toLowerCase(Locale.US)).append(SdkConstants.DOT_PNG).append(" (");
159                 sb.append(LocaleManager.getRegionName(code)).append(") ");
160             }
161             System.out.println(sb.toString());
162         }
163     }
164 
testMissingFlag()165     public void testMissingFlag() {
166         Image icon = FlagManager.get().getFlag("AQ");
167         assertNotNull(icon);
168         assertSame(FlagManager.get().getEmptyIcon(), icon);
169 
170         icon = FlagManager.get().getFlag("AQ");
171         assertNotNull(icon);
172         assertSame(FlagManager.get().getEmptyIcon(), icon);
173 
174         icon = FlagManager.get().getFlag("WO"); // Not used in ISO 3166-1
175         assertNotNull(icon);
176         assertSame(FlagManager.get().getEmptyIcon(), icon);
177     }
178 
testKnownFlag()179     public void testKnownFlag() {
180         Image icon = FlagManager.get().getFlag("US");
181         assertNotNull(icon);
182         assertNotSame(FlagManager.get().getEmptyIcon(), icon);
183     }
184 }
185 
186