Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4262e94a authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Set the locale explicitly for tests that depend on it

Previously, the tests in DateUtilsTest and LinkifyTest assumed the US
English locale, which would not necessarily be the case when the
tests were run. Now we save the original locale, set the locale to
US English, and finally restore the original when the tests are done.

Bug: 29880704
Change-Id: Ieef59e5fc128b484b0db13f35bfa4372fed650d0
parent b8e12866
Loading
Loading
Loading
Loading
+32 −8
Original line number Diff line number Diff line
@@ -16,17 +16,45 @@

package android.text.format;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.LocaleList;
import android.support.test.filters.SmallTest;

import java.util.Locale;

import junit.framework.TestCase;

import java.util.Locale;

public class DateUtilsTest extends TestCase {
    // This test is not in CTS because formatDuration is @hidden.

    private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US);
    private LocaleList mOriginalLocales;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mOriginalLocales = Resources.getSystem().getConfiguration().getLocales();
        setLocales(LOCALE_LIST_US);
    }

    @Override
    protected void tearDown() throws Exception {
        setLocales(mOriginalLocales);
        super.tearDown();
    }

    private void setLocales(LocaleList locales) {
        final Resources systemResources = Resources.getSystem();
        final Configuration config = new Configuration(systemResources.getConfiguration());
        config.setLocales(locales);
        // This is not very safe to call, but since DateUtils.formatDuration() is a static method
        // (it gets its format strings from the system resources), we can't pass a modified Context
        // to it.
        systemResources.updateConfiguration(config, null);
    }

    @SmallTest
    public void test_formatDuration_seconds() throws Exception {
        assertEquals("en-US", Locale.getDefault().toLanguageTag());
        assertEquals("0 seconds", DateUtils.formatDuration(0));
        assertEquals("0 seconds", DateUtils.formatDuration(1));
        assertEquals("0 seconds", DateUtils.formatDuration(499));
@@ -35,20 +63,16 @@ public class DateUtilsTest extends TestCase {
        assertEquals("2 seconds", DateUtils.formatDuration(1500));
    }

    // This test is not in CTS because formatDuration is @hidden.
    @SmallTest
    public void test_formatDuration_Minutes() throws Exception {
        assertEquals("en-US", Locale.getDefault().toLanguageTag());
        assertEquals("59 seconds", DateUtils.formatDuration(59000));
        assertEquals("60 seconds", DateUtils.formatDuration(59500));
        assertEquals("1 minute", DateUtils.formatDuration(60000));
        assertEquals("2 minutes", DateUtils.formatDuration(120000));
    }

    // This test is not in CTS because formatDuration is @hidden.
    @SmallTest
    public void test_formatDuration_Hours() throws Exception {
        assertEquals("en-US", Locale.getDefault().toLanguageTag());
        assertEquals("59 minutes", DateUtils.formatDuration(3540000));
        assertEquals("1 hour", DateUtils.formatDuration(3600000));
        assertEquals("48 hours", DateUtils.formatDuration(172800000));
+30 −3
Original line number Diff line number Diff line
@@ -16,21 +16,48 @@

package android.text.util;

import android.content.Context;
import android.content.res.Configuration;
import android.os.LocaleList;
import android.support.test.filters.SmallTest;
import android.test.AndroidTestCase;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

import java.util.Locale;

/**
 * LinkifyTest tests {@link Linkify}.
 */
public class LinkifyTest extends AndroidTestCase {

    private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US);
    private LocaleList mOriginalLocales;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mOriginalLocales = LocaleList.getDefault();
        LocaleList.setDefault(LOCALE_LIST_US);
    }

    @Override
    protected void tearDown() throws Exception {
        LocaleList.setDefault(mOriginalLocales);
        super.tearDown();
    }

    private Context createUsEnglishContext() {
        final Configuration overrideConfig = new Configuration();
        overrideConfig.setLocales(LOCALE_LIST_US);
        return getContext().createConfigurationContext(overrideConfig);
    }

    @SmallTest
    public void testNothing() throws Exception {
        TextView tv;

        tv = new TextView(getContext());
        tv = new TextView(createUsEnglishContext());
        tv.setText("Hey, foo@google.com, call 415-555-1212.");

        assertFalse(tv.getMovementMethod() instanceof LinkMovementMethod);
@@ -41,7 +68,7 @@ public class LinkifyTest extends AndroidTestCase {
    public void testNormal() throws Exception {
        TextView tv;

        tv = new TextView(getContext());
        tv = new TextView(createUsEnglishContext());
        tv.setAutoLinkMask(Linkify.ALL);
        tv.setText("Hey, foo@google.com, call 415-555-1212.");

@@ -53,7 +80,7 @@ public class LinkifyTest extends AndroidTestCase {
    public void testUnclickable() throws Exception {
        TextView tv;

        tv = new TextView(getContext());
        tv = new TextView(createUsEnglishContext());
        tv.setAutoLinkMask(Linkify.ALL);
        tv.setLinksClickable(false);
        tv.setText("Hey, foo@google.com, call 415-555-1212.");