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 18 package libcore.java.util; 19 20 import java.util.Locale; 21 import java.util.Objects; 22 23 import static java.util.Locale.Category.DISPLAY; 24 import static java.util.Locale.Category.FORMAT; 25 import static org.junit.Assert.assertEquals; 26 27 /** 28 * Helper class for tests that need to temporarily change the default Locales. 29 */ 30 class Locales { 31 private final Locale uncategorizedLocale; 32 private final Locale displayLocale; 33 private final Locale formatLocale; 34 35 private Locales(Locale uncategorizedLocale, Locale displayLocale, Locale formatLocale) { 36 this.uncategorizedLocale = uncategorizedLocale; 37 this.displayLocale = displayLocale; 38 this.formatLocale = formatLocale; 39 } 40 41 /** 42 * Sets the specified default Locale, default DISPLAY Locale and default FORMAT Locale. 43 * Every call to this method should be paired with exactly one corresponding call to 44 * reset the previous values: 45 * <pre> 46 * Locales locales = Locales.getAndSetDefaultForTest(Locale.US, Locale.CHINA, Locale.UK); 47 * try { 48 * ... 49 * } finally { 50 * locales.setAsDefault(); 51 * } 52 * </pre> 53 */ 54 public static Locales getAndSetDefaultForTest(Locale uncategorizedLocale, Locale displayLocale, 55 Locale formatLocale) { 56 Locales oldLocales = getDefault(); 57 Locales newLocales = new Locales(uncategorizedLocale, displayLocale, formatLocale); 58 newLocales.setAsDefault(); 59 assertEquals(newLocales, getDefault()); // sanity check 60 return oldLocales; 61 } 62 63 @Override 64 public boolean equals(Object obj) { 65 if (!(obj instanceof Locales)) { 66 return false; 67 } 68 Locales that = (Locales) obj; 69 return uncategorizedLocale.equals(that.uncategorizedLocale) 70 && displayLocale.equals(that.displayLocale) 71 && formatLocale.equals(that.formatLocale); 72 } 73 74 @Override 75 public int hashCode() { 76 return Objects.hash(uncategorizedLocale, displayLocale, formatLocale); 77 } 78 79 @Override 80 public String toString() { 81 return "Locales[displayLocale=" + displayLocale + ", locale=" + uncategorizedLocale + 82 ", formatLocale=" + formatLocale + ']'; 83 } 84 85 /** 86 * Reset the system's default Locale values to what they were when this 87 * Locales was obtained. 88 */ 89 public void setAsDefault() { 90 // The lines below must set the Locales in this order because setDefault(Locale) 91 // overwrites the other ones. 92 Locale.setDefault(uncategorizedLocale); 93 Locale.setDefault(DISPLAY, displayLocale); 94 Locale.setDefault(FORMAT, formatLocale); 95 } 96 97 public static Locales getDefault() { 98 return new Locales( 99 Locale.getDefault(), Locale.getDefault(DISPLAY), Locale.getDefault(FORMAT)); 100 } 101 102 } 103