194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/*
294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Copyright (C) 2011 The Android Open Source Project
394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * you may not use this file except in compliance with the License.
694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * You may obtain a copy of the License at
794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
1094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Unless required by applicable law or agreed to in writing, software
1194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
1294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * See the License for the specific language governing permissions and
1494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * limitations under the License.
1594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
1694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpackage com.android.dialer.util;
1894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
1994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.Context;
2094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.res.Configuration;
2194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport android.content.res.Resources;
2294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengimport java.util.Locale;
2494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
2594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng/**
2694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Utility class to save and restore the locale of the system.
2794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * <p>
2894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * This can be used for tests that assume to be run in a certain locale, e.g., because they
2994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * check against strings in a particular language or require an assumption on how the system
3094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * will behave in a specific locale.
3194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * <p>
3294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * In your test, you can change the locale with the following code:
3394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * <pre>
3494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * public class CanadaFrenchTest extends AndroidTestCase {
3594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     private LocaleTestUtils mLocaleTestUtils;
3694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
3794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     &#64;Override
3894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     public void setUp() throws Exception {
3994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         super.setUp();
4094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         mLocaleTestUtils = new LocaleTestUtils(getContext());
4194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         mLocaleTestUtils.setLocale(Locale.CANADA_FRENCH);
4294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     }
4394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
4494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     &#64;Override
4594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     public void tearDown() throws Exception {
4694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         mLocaleTestUtils.restoreLocale();
4794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         mLocaleTestUtils = null;
4894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *         super.tearDown();
4994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     }
5094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *
5194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng *     ...
5294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * }
5394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * </pre>
5494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * Note that one should not call {@link #setLocale(Locale)} more than once without calling
5594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * {@link #restoreLocale()} first.
5694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * <p>
5794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng * This class is not thread-safe. Usually its methods should be invoked only from the test thread.
5894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng */
5994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Chengpublic class LocaleTestUtils {
6094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private final Context mContext;
6194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private boolean mSaved;
6294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Locale mSavedContextLocale;
6394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Locale mSavedSystemLocale;
6494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
6594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
6694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Create a new instance that can be used to set and reset the locale for the given context.
6794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
6894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param context the context on which to alter the locale
6994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
7094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public LocaleTestUtils(Context context) {
7194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mContext = context;
7294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mSaved = false;
7394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
7494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
7594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
7694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Set the locale to the given value and saves the previous value.
7794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
7894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param locale the value to which the locale should be set
7994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @throws IllegalStateException if the locale was already set
8094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
8194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void setLocale(Locale locale) {
8294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (mSaved) {
8394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            throw new IllegalStateException(
8494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng                    "call restoreLocale() before calling setLocale() again");
8594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
8694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mSavedContextLocale = setResourcesLocale(mContext.getResources(), locale);
8794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mSavedSystemLocale = setResourcesLocale(Resources.getSystem(), locale);
8894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mSaved = true;
8994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
9094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
9194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
9294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Restores the previously set locale.
9394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
9494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @throws IllegalStateException if the locale was not set using {@link #setLocale(Locale)}
9594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
9694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    public void restoreLocale() {
9794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        if (!mSaved) {
9894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng            throw new IllegalStateException("call setLocale() before calling restoreLocale()");
9994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        }
10094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setResourcesLocale(mContext.getResources(), mSavedContextLocale);
10194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        setResourcesLocale(Resources.getSystem(), mSavedSystemLocale);
10294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        mSaved = false;
10394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
10494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng
10594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    /**
10694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * Sets the locale for the given resources and returns the previous locale.
10794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     *
10894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param resources the resources on which to set the locale
10994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @param locale the value to which to set the locale
11094b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     * @return the previous value of the locale for the resources
11194b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng     */
11294b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    private Locale setResourcesLocale(Resources resources, Locale locale) {
11394b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Configuration contextConfiguration = new Configuration(resources.getConfiguration());
11494b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        Locale savedLocale = contextConfiguration.locale;
11594b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        contextConfiguration.locale = locale;
11694b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        resources.updateConfiguration(contextConfiguration, null);
11794b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng        return savedLocale;
11894b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng    }
11994b10b530c0fc297e2974e57e094c500d3ee6003Chiao Cheng}
120