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.google.common.collect.Sets; 20 21 import org.eclipse.swt.graphics.Image; 22 23 import java.util.ArrayList; 24 import java.util.HashSet; 25 import java.util.List; 26 import java.util.Locale; 27 import java.util.Map; 28 import java.util.Set; 29 30 import junit.framework.TestCase; 31 32 @SuppressWarnings("javadoc") 33 public class LocaleManagerTest extends TestCase { 34 public void testIntegrity() { 35 LocaleManager localeManager = LocaleManager.get(); 36 assertSame(localeManager, LocaleManager.get()); 37 38 Map<String, String> languageToCountry = LocaleManager.getLanguageToCountryMap(); 39 Map<String, String> languageNames = LocaleManager.getLanguageNamesMap(); 40 Map<String, String> regionNames = LocaleManager.getRegionNamesMap(); 41 assertEquals("Make sure to update initial capacity in declaration after editing map", 42 177, languageToCountry.size()); 43 assertEquals("Make sure to update initial capacity in declaration after editing map", 44 187, languageNames.size()); 45 assertEquals("Make sure to update initial capacity in declaration after editing map", 46 249, regionNames.size()); 47 48 assertTrue(Sets.difference(languageToCountry.keySet(), 49 languageNames.keySet()).isEmpty()); 50 for (Map.Entry<String, String> entry : languageToCountry.entrySet()) { 51 assertTrue(entry.getValue(), entry.getKey().length() > 0); 52 assertTrue(entry.getKey(), entry.getValue().length() > 0); 53 } 54 for (Map.Entry<String, String> entry : regionNames.entrySet()) { 55 assertTrue(entry.getValue(), entry.getKey().length() > 0); 56 assertTrue(entry.getKey(), entry.getValue().length() > 0); 57 } 58 for (Map.Entry<String, String> entry : languageNames.entrySet()) { 59 assertTrue(entry.getValue(), entry.getKey().length() > 0); 60 assertTrue(entry.getKey(), entry.getValue().length() > 0); 61 } 62 for (String s : languageToCountry.keySet()) { 63 assertTrue(s, s.length() == 2 && s.equals(s.toLowerCase(Locale.US))); 64 } 65 for (String s : languageNames.keySet()) { 66 assertTrue(s, s.length() == 2 && s.equals(s.toLowerCase(Locale.US))); 67 } 68 for (String s : languageNames.values()) { 69 assertTrue(s, s.length() > 2 && Character.isUpperCase(s.charAt(0))); 70 } 71 for (String s : languageToCountry.values()) { 72 assertTrue(s, s.length() == 2 && s.equals(s.toUpperCase(Locale.US))); 73 } 74 for (String s : regionNames.keySet()) { 75 assertTrue(s, s.length() == 2 && s.equals(s.toUpperCase(Locale.US))); 76 } 77 for (String s : regionNames.values()) { 78 assertTrue(s, s.length() > 2 && Character.isUpperCase(s.charAt(0))); 79 } 80 assertNull(languageToCountry.get("")); 81 assertNull(languageNames.get("")); 82 assertTrue(Sets.difference(languageToCountry.keySet(), 83 languageNames.keySet()).isEmpty()); 84 } 85 86 public void testGetLanguageNames() throws Exception { 87 assertEquals("English", LocaleManager.getLanguageName("en")); 88 assertEquals("Norwegian Bokml", LocaleManager.getLanguageName("nb")); 89 assertEquals("Norwegian", LocaleManager.getLanguageName("no")); 90 assertEquals("French", LocaleManager.getLanguageName("fr")); 91 assertEquals("German", LocaleManager.getLanguageName("de")); 92 assertEquals("Hindi", LocaleManager.getLanguageName("hi")); 93 } 94 95 public void testGetFlagImage() { 96 LocaleManager manager = LocaleManager.get(); 97 Image us = manager.getFlag("US"); 98 Image gb = manager.getFlag("GB"); 99 Image ca = manager.getFlag("CA"); 100 Image es = manager.getFlag("ES"); 101 Image br = manager.getFlag("BR"); 102 Image pt = manager.getFlag("PT"); 103 assertSame(us, manager.getFlag("en", "US")); 104 assertSame(gb, manager.getFlag("en", "GB")); 105 assertSame(ca, manager.getFlag("en", "CA")); 106 Locale.setDefault(Locale.US); 107 assertSame(us, manager.getFlag("en", null)); 108 Locale.setDefault(Locale.UK); 109 assertSame(gb, manager.getFlag("en", null)); 110 Locale.setDefault(Locale.CANADA); 111 assertSame(ca, manager.getFlag("en", null)); 112 assertSame(manager.getFlag("NO"), manager.getFlag("nb", null)); 113 assertSame(manager.getFlag("FR"), manager.getFlag("fr", null)); 114 115 Locale.setDefault(new Locale("pt", "br")); 116 assertSame(br, manager.getFlag("pt", null)); 117 assertSame(pt, manager.getFlag("pt", "PT")); 118 Locale.setDefault(new Locale("pt", "pt")); 119 assertSame(pt, manager.getFlag("pt", null)); 120 assertSame(br, manager.getFlag("pt", "BR")); 121 122 // Special cases where we have custom flags 123 assertNotSame(gb, manager.getFlag("cy", null)); // Wales 124 assertNotSame(es, manager.getFlag("ca", null)); // Catalonia 125 126 // Aliases - http://developer.android.com/reference/java/util/Locale.html 127 assertSame(manager.getFlag("yi", null), manager.getFlag("ji", null)); 128 assertSame(manager.getFlag("in", null), manager.getFlag("id", null)); 129 assertSame(manager.getFlag("iw", null), manager.getFlag("he", null)); 130 assertSame(LocaleManager.getLanguageName("iw"), LocaleManager.getLanguageName("he")); 131 assertSame(LocaleManager.getLanguageName("in"), LocaleManager.getLanguageName("id")); 132 assertSame(LocaleManager.getLanguageName("yi"), LocaleManager.getLanguageName("ji")); 133 134 assertSame(us, manager.getFlagForFolderName("values-en-rUS")); 135 assertSame(gb, manager.getFlagForFolderName("values-en-rGB")); 136 Locale.setDefault(Locale.CANADA); 137 assertSame(ca, manager.getFlagForFolderName("values-en")); 138 } 139 140 public void testGetRegionNames() { 141 assertEquals("United States", LocaleManager.getRegionName("US")); 142 assertEquals("Norway", LocaleManager.getRegionName("NO")); 143 assertEquals("France", LocaleManager.getRegionName("FR")); 144 assertEquals("India", LocaleManager.getRegionName("IN")); 145 } 146 147 public void testAvailableIcons() { 148 // Icons we have in WindowBuilder 149 String[] icons = new String[] { 150 "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "ar", "as", "at", "au", "aw", "ax", 151 "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "br", 152 "bs", "bt", "bv", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", 153 "cl", "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de", "dj", "dk", "dm", 154 "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "fi", "fj", "fk", "fm", "fo", 155 "fr", "ga", "gb", "gd", "ge", "gf", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", 156 "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", 157 "in", "io", "iq", "ir", "is", "it", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", 158 "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", 159 "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm", "mn", "mo", 160 "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", 161 "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", 162 "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", 163 "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk", "sl", "sm", 164 "sn", "so", "sr", "st", "sv", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", 165 "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "um", "us", "uy", 166 "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", 167 "zw", 168 }; 169 Set<String> sIcons = new HashSet<String>(100); 170 Map<String, String> regionNames = LocaleManager.getRegionNamesMap(); 171 Map<String, String> languageToCountry = LocaleManager.getLanguageToCountryMap(); 172 Map<String, String> languageNames = LocaleManager.getLanguageNamesMap(); 173 List<String> unused = new ArrayList<String>(); 174 for (String code : icons) { 175 code = code.toUpperCase(Locale.US); 176 sIcons.add(code); 177 178 String country = regionNames.get(code); 179 if (country == null) { 180 System.out.println("No region name found for region code " + code); 181 } 182 183 if (!languageToCountry.values().contains(code)) { 184 unused.add(code.toLowerCase() + ".png"); 185 } 186 } 187 if (!unused.isEmpty()) { 188 System.out.println("The following icons are not referenced by any of the " + 189 "language to country bindings: " + unused); 190 } 191 192 // Make sure all our language bindings are languages we have maps for 193 for (Map.Entry<String, String> entry : languageToCountry.entrySet()) { 194 String language = entry.getKey(); 195 String region = entry.getValue(); 196 197 if (!sIcons.contains(region)) { 198 System.out.println("No icon found for region " + region + " " 199 + LocaleManager.getRegionName(region) + " (used for language " 200 + language + "(" + languageNames.get(language) + "))"); 201 } 202 } 203 } 204 205 /* Utility useful for identifying strings which must be using \\u in the string names 206 * to ensure that they are handled properly during the build (outside of Eclipse, 207 * where this source file is marked as using UTF-8. 208 public void testPrintable() { 209 Set<String> languageCodes = LocaleManager.getLanguageCodes(); 210 for (String code : languageCodes) { 211 String name = LocaleManager.getLanguageName(code); 212 assertNotNull(name); 213 checkEncoding(name); 214 } 215 216 Set<String> regionCodes = LocaleManager.getRegionCodes(); 217 for (String code : regionCodes) { 218 String name = LocaleManager.getRegionName(code); 219 assertNotNull(name); 220 checkEncoding(name); 221 } 222 } 223 224 private static void checkEncoding(String s) { 225 for (int i = 0, n = s.length(); i < n; i++) { 226 char c = s.charAt(i); 227 if (c >= 128) { 228 System.out.println("Need unicode encoding for '" + s + "'"); 229 StringBuilder sb = new StringBuilder(); 230 for (int j = 0, m = s.length(); j < m; j++) { 231 char d = s.charAt(j); 232 if (d < 128) { 233 sb.append(d); 234 } else { 235 sb.append('\\'); 236 sb.append('u'); 237 sb.append(String.format("%04x", (int)d)); 238 } 239 } 240 System.out.println(" Replacement=" + sb); 241 return; 242 } 243 } 244 } 245 */ 246 } 247