OSDN Git Service

Move text coretests to JUnit (2)
authorAndrei Stingaceanu <stg@google.com>
Fri, 28 Apr 2017 13:48:17 +0000 (14:48 +0100)
committerAndrei Stingaceanu <stg@google.com>
Tue, 2 May 2017 15:58:28 +0000 (16:58 +0100)
* moves android.text.util, android.text.format and
android.text.method packages core tests to AndroidJUnit
* replaced manual mocks with mockito usage
* cleaned up dead code

Bug: 31223263
Test: adb shell am instrument -w -e package android.text\
      com.android.frameworks.coretests/android.support.test.runner\
      .AndroidJUnitRunner

Change-Id: I7b13c10521381e4f26ca8e6189392914238a21dd

core/tests/coretests/src/android/text/format/DateFormatTest.java
core/tests/coretests/src/android/text/format/DateUtilsTest.java
core/tests/coretests/src/android/text/format/FormatterTest.java
core/tests/coretests/src/android/text/format/TimeTest.java
core/tests/coretests/src/android/text/method/BackspaceTest.java
core/tests/coretests/src/android/text/method/EditorState.java
core/tests/coretests/src/android/text/method/ForwardDeleteTest.java
core/tests/coretests/src/android/text/method/KeyListenerTestCase.java
core/tests/coretests/src/android/text/method/WordIteratorTest.java
core/tests/coretests/src/android/text/util/LinkifyTest.java

index 93bc911..15c86f0 100644 (file)
 
 package android.text.format;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
 
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
-public class DateFormatTest extends TestCase {
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class DateFormatTest {
 
-    @SmallTest
-    public void testHasDesignator() throws Exception {
+    @Test
+    public void testHasDesignator() {
         assertTrue(DateFormat.hasDesignator("hh:mm:ss", DateFormat.MINUTE));
         assertTrue(DateFormat.hasDesignator("myyyy", DateFormat.MINUTE));
         assertTrue(DateFormat.hasDesignator("mmm", DateFormat.MINUTE));
@@ -31,8 +38,8 @@ public class DateFormatTest extends TestCase {
         assertFalse(DateFormat.hasDesignator("hh:MM:ss", DateFormat.MINUTE));
     }
 
-    @SmallTest
-    public void testHasDesignatorEscaped() throws Exception {
+    @Test
+    public void testHasDesignatorEscaped() {
         assertTrue(DateFormat.hasDesignator("hh:mm 'LOL'", DateFormat.MINUTE));
 
         assertFalse(DateFormat.hasDesignator("hh:mm 'yyyy'", DateFormat.YEAR));
index de43fc6..9271cb4 100644 (file)
 
 package android.text.format;
 
+import static org.junit.Assert.assertEquals;
+
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.os.LocaleList;
 import android.support.test.filters.SmallTest;
-
-import junit.framework.TestCase;
+import android.support.test.runner.AndroidJUnit4;
 
 import java.util.Locale;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
-public class DateUtilsTest extends TestCase {
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class DateUtilsTest {
 
     private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US);
     private LocaleList mOriginalLocales;
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
+    @Before
+    public void setup() {
         mOriginalLocales = Resources.getSystem().getConfiguration().getLocales();
         setLocales(LOCALE_LIST_US);
     }
 
-    @Override
-    protected void tearDown() throws Exception {
+    @After
+    public void teardown() {
         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 {
+    @Test
+    public void test_formatDuration_seconds() {
         assertEquals("0 seconds", DateUtils.formatDuration(0));
         assertEquals("0 seconds", DateUtils.formatDuration(1));
         assertEquals("0 seconds", DateUtils.formatDuration(499));
@@ -75,8 +70,8 @@ public class DateUtilsTest extends TestCase {
         assertEquals("2s", DateUtils.formatDuration(1500, DateUtils.LENGTH_SHORTEST));
     }
 
-    @SmallTest
-    public void test_formatDuration_Minutes() throws Exception {
+    @Test
+    public void test_formatDuration_Minutes() {
         assertEquals("59 seconds", DateUtils.formatDuration(59000));
         assertEquals("60 seconds", DateUtils.formatDuration(59500));
         assertEquals("1 minute", DateUtils.formatDuration(60000));
@@ -92,8 +87,8 @@ public class DateUtilsTest extends TestCase {
         assertEquals("2m", DateUtils.formatDuration(120000, DateUtils.LENGTH_SHORTEST));
     }
 
-    @SmallTest
-    public void test_formatDuration_Hours() throws Exception {
+    @Test
+    public void test_formatDuration_Hours() {
         assertEquals("59 minutes", DateUtils.formatDuration(3540000));
         assertEquals("1 hour", DateUtils.formatDuration(3600000));
         assertEquals("48 hours", DateUtils.formatDuration(172800000));
@@ -107,4 +102,15 @@ public class DateUtilsTest extends TestCase {
         assertEquals("1h", DateUtils.formatDuration(3600000, DateUtils.LENGTH_SHORTEST));
         assertEquals("48h", DateUtils.formatDuration(172800000, DateUtils.LENGTH_SHORTEST));
     }
+
+    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);
+    }
+
 }
index 2293094..a4ce911 100644 (file)
 
 package android.text.format;
 
+import static org.junit.Assert.assertEquals;
+
+import android.content.Context;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
-import android.test.AndroidTestCase;
+import android.support.test.runner.AndroidJUnit4;
 import android.text.format.Formatter.BytesResult;
 
 import java.util.Locale;
-
-public class FormatterTest extends AndroidTestCase {
-
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class FormatterTest {
     private Locale mOriginalLocale;
+    private Context mContext;
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        mOriginalLocale = mContext.getResources().getConfiguration().locale;
+    @Before
+    public void setup() {
+        mContext = InstrumentationRegistry.getContext();
+        mOriginalLocale = mContext.getResources()
+            .getConfiguration().locale;
     }
 
-    @Override
-    protected void tearDown() throws Exception {
+    @After
+    public void tearDown() {
         if (mOriginalLocale != null) {
             setLocale(mOriginalLocale);
         }
-        super.tearDown();
-    }
-
-    private void setLocale(Locale locale) {
-        Resources res = getContext().getResources();
-        Configuration config = res.getConfiguration();
-        config.locale = locale;
-        res.updateConfiguration(config, res.getDisplayMetrics());
-
-        Locale.setDefault(locale);
     }
 
-    @SmallTest
+    @Test
     public void testFormatBytes() {
         setLocale(Locale.ENGLISH);
 
@@ -90,7 +90,7 @@ public class FormatterTest extends AndroidTestCase {
         checkFormatBytes(-914, false, "-0.91", -910);
 
         // Missing FLAG_CALCULATE_ROUNDED case.
-        BytesResult r = Formatter.formatBytes(getContext().getResources(), 1, 0);
+        BytesResult r = Formatter.formatBytes(mContext.getResources(), 1, 0);
         assertEquals("1", r.value);
         assertEquals(0, r.roundedBytes); // Didn't pass FLAG_CALCULATE_ROUNDED
 
@@ -101,9 +101,18 @@ public class FormatterTest extends AndroidTestCase {
 
     private void checkFormatBytes(long bytes, boolean useShort,
             String expectedString, long expectedRounded) {
-        BytesResult r = Formatter.formatBytes(getContext().getResources(), bytes,
+        BytesResult r = Formatter.formatBytes(mContext.getResources(), bytes,
                 Formatter.FLAG_CALCULATE_ROUNDED | (useShort ? Formatter.FLAG_SHORTER : 0));
         assertEquals(expectedString, r.value);
         assertEquals(expectedRounded, r.roundedBytes);
     }
+
+    private void setLocale(Locale locale) {
+        Resources res = mContext.getResources();
+        Configuration config = res.getConfiguration();
+        config.locale = locale;
+        res.updateConfiguration(config, res.getDisplayMetrics());
+
+        Locale.setDefault(locale);
+    }
 }
index f6dd0d4..8983d15 100644 (file)
 
 package android.text.format;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import android.support.test.filters.SmallTest;
 import android.support.test.filters.Suppress;
+import android.support.test.runner.AndroidJUnit4;
 import android.util.Log;
 import android.util.TimeFormatException;
 
-import junit.framework.TestCase;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
-public class TimeTest extends TestCase {
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class TimeTest {
 
-    @SmallTest
-    public void testNormalize0() throws Exception {
+    @Test
+    public void testNormalize0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.parse("20060432T010203");
         t.normalize(false /* use isDst */);
@@ -174,8 +182,8 @@ public class TimeTest extends TestCase {
             new DateTest(2007, 10, 5, 2, 0, 60, 2007, 10, 5, 3, 0),
     };
 
-    @SmallTest
-    public void testNormalize1() throws Exception {
+    @Test
+    public void testNormalize1() {
         Time local = new Time("America/Los_Angeles");
 
         int len = dayTests.length;
@@ -265,70 +273,70 @@ public class TimeTest extends TestCase {
         }
     }
 
-    @SmallTest
-    public void testSwitchTimezone0() throws Exception {
+    @Test
+    public void testSwitchTimezone0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.parse("20061005T120000");
         t.switchTimezone("America/Los_Angeles");
         // System.out.println("got: " + t);
     }
 
-    @SmallTest
-    public void testCtor0() throws Exception {
+    @Test
+    public void testCtor0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         assertEquals(Time.TIMEZONE_UTC, t.timezone);
     }
 
-    @SmallTest
-    public void testGetActualMaximum0() throws Exception {
+    @Test
+    public void testGetActualMaximum0() {
         Time t = new Time(Time.TIMEZONE_UTC);
-        int r = t.getActualMaximum(Time.SECOND);
+        t.getActualMaximum(Time.SECOND);
         // System.out.println("r=" + r);
     }
 
-    @SmallTest
-    public void testClear0() throws Exception {
+    @Test
+    public void testClear0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.clear(Time.TIMEZONE_UTC);
     }
 
-    @SmallTest
-    public void testCompare0() throws Exception {
+    @Test
+    public void testCompare0() {
         Time a = new Time(Time.TIMEZONE_UTC);
         Time b = new Time("America/Los_Angeles");
         int r = Time.compare(a, b);
         // System.out.println("r=" + r);
     }
 
-    @SmallTest
-    public void testFormat0() throws Exception {
+    @Test
+    public void testFormat0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         String r = t.format("%Y%m%dT%H%M%S");
         // System.out.println("r='" + r + "'");
     }
 
-    @SmallTest
-    public void testToString0() throws Exception {
+    @Test
+    public void testToString0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         String r = t.toString();
         // System.out.println("r='" + r + "'");
     }
 
-    @SmallTest
-    public void testGetCurrentTimezone0() throws Exception {
+    @Test
+    public void testGetCurrentTimezone0() {
         String r = Time.getCurrentTimezone();
         // System.out.println("r='" + r + "'");
     }
 
-    @SmallTest
-    public void testSetToNow0() throws Exception {
+    @Test
+    public void testSetToNow0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.setToNow();
         // System.out.println("t=" + t);
     }
 
-    @SmallTest
-    public void testMillis0() throws Exception {
+    @Test
+    public void testMillis0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.set(0, 0, 0, 1, 1, 2006);
         long r = t.toMillis(true /* ignore isDst */);
@@ -338,23 +346,23 @@ public class TimeTest extends TestCase {
         // System.out.println("r=" + r);
     }
 
-    @SmallTest
-    public void testMillis1() throws Exception {
+    @Test
+    public void testMillis1() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.set(1, 0, 0, 1, 0, 1970);
         long r = t.toMillis(true /* ignore isDst */);
         // System.out.println("r=" + r);
     }
 
-    @SmallTest
-    public void testParse0() throws Exception {
+    @Test
+    public void testParse0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.parse("12345678T901234");
         // System.out.println("t=" + t);
     }
 
-    @SmallTest
-    public void testParse33390() throws Exception {
+    @Test
+    public void testParse33390() {
         Time t = new Time(Time.TIMEZONE_UTC);
 
         t.parse3339("1980-05-23");
@@ -435,8 +443,8 @@ public class TimeTest extends TestCase {
         }
     }
 
-    @SmallTest
-    public void testSet0() throws Exception {
+    @Test
+    public void testSet0() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.set(1000L);
         // System.out.println("t.year=" + t.year);
@@ -449,13 +457,13 @@ public class TimeTest extends TestCase {
         // System.out.println("t=" + t);
     }
 
-    @SmallTest
-    public void testSet1() throws Exception {
+    @Test
+    public void testSet1() {
         Time t = new Time(Time.TIMEZONE_UTC);
         t.set(1, 2, 3, 4, 5, 6);
         // System.out.println("t=" + t);
     }
-    
+
     // Timezones that cover the world.  Some GMT offsets occur more than
     // once in case some cities decide to change their GMT offset.
     private static final String[] mTimeZones = {
@@ -518,9 +526,9 @@ public class TimeTest extends TestCase {
         "Pacific/Honolulu",
         "Pacific/Midway",
     };
-    
+
     @Suppress
-    public void disableTestGetJulianDay() throws Exception {
+    public void disableTestGetJulianDay() {
         Time time = new Time();
 
         // For each day of the year, and for each timezone, get the Julian
@@ -560,11 +568,11 @@ public class TimeTest extends TestCase {
             }
         }
     }
-    
+
     @Suppress
-    public void disableTestSetJulianDay() throws Exception {
+    public void disableTestSetJulianDay() {
         Time time = new Time();
-        
+
         // For each day of the year in 2008, and for each timezone,
         // test that we can set the Julian day correctly.
         for (int monthDay = 1; monthDay <= 366; monthDay++) {
index 864b48a..6e41831 100644 (file)
 package android.text.method;
 
 import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
 import android.text.InputType;
 import android.view.KeyEvent;
 import android.widget.TextView.BufferType;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * Test backspace key handling of {@link android.text.method.BaseKeyListener}.
@@ -27,6 +31,8 @@ import android.widget.TextView.BufferType;
  * Only contains edge cases. For normal cases, see {@see android.text.method.cts.BackspaceTest}.
  * TODO: introduce test cases for surrogate pairs and replacement span.
  */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
 public class BackspaceTest extends KeyListenerTestCase {
     private static final BaseKeyListener mKeyListener = new BaseKeyListener() {
         public int getInputType() {
@@ -49,7 +55,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.mSelectionEnd = mTextView.getSelectionEnd();
     }
 
-    @SmallTest
+    @Test
     public void testCombiningEnclosingKeycaps() {
         EditorState state = new EditorState();
 
@@ -77,7 +83,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testVariationSelector() {
         EditorState state = new EditorState();
 
@@ -141,7 +147,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testEmojiZWJSequence() {
         EditorState state = new EditorState();
 
@@ -221,7 +227,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testFlags() {
         EditorState state = new EditorState();
 
@@ -283,7 +289,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.assertEquals("'a' |");
     }
 
-    @SmallTest
+    @Test
     public void testEmojiModifier() {
         EditorState state = new EditorState();
 
@@ -312,7 +318,7 @@ public class BackspaceTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testMixedEdgeCases() {
         EditorState state = new EditorState();
 
index bbbbd6d..12bb8c8 100644 (file)
 
 package android.text.method;
 
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.text.Editable;
@@ -59,19 +64,6 @@ public class EditorState {
     public EditorState() {
     }
 
-    /**
-     * A mocked {@link android.text.style.ReplacementSpan} for testing purpose.
-     */
-    private static class MockReplacementSpan extends ReplacementSpan {
-        public int getSize(Paint paint, CharSequence text, int start, int end,
-                Paint.FontMetricsInt fm) {
-            return 0;
-        }
-        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
-                int y, int bottom, Paint paint) {
-        }
-    }
-
     // Returns true if the code point is ASCII and graph.
     private boolean isGraphicAscii(int codePoint) {
         return 0x20 < codePoint && codePoint < 0x7F;
@@ -169,7 +161,14 @@ public class EditorState {
                 throw new IllegalArgumentException(
                         "ReplacementSpan start position appears after end position.");
             }
-            spannable.setSpan(new MockReplacementSpan(), replacementSpanStart, replacementSpanEnd,
+
+            ReplacementSpan mockReplacementSpan = mock(ReplacementSpan.class);
+            when(mockReplacementSpan.getSize(any(), any(), any(), any(), any()))
+                .thenReturn(0);
+            doNothing().when(mockReplacementSpan)
+                .draw(any(), any(), any(), any(), any(), any(), any(), any(), any());
+
+            spannable.setSpan(mockReplacementSpan, replacementSpanStart, replacementSpanEnd,
                     Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         }
         mText = Editable.Factory.getInstance().newEditable(spannable);
index 839d380..5a570a1 100644 (file)
 package android.text.method;
 
 import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
 import android.text.InputType;
 import android.view.KeyEvent;
 import android.widget.TextView.BufferType;
 
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
 /**
  * Test forward delete key handling of  {@link android.text.method.BaseKeyListener}.
  *
  * Only contains edge cases. For normal cases, see {@see android.text.method.cts.ForwardDeleteTest}.
  * TODO: introduce test cases for surrogate pairs and replacement span.
  */
+@SmallTest
+@RunWith(AndroidJUnit4.class)
 public class ForwardDeleteTest extends KeyListenerTestCase {
     private static final BaseKeyListener mKeyListener = new BaseKeyListener() {
         public int getInputType() {
@@ -49,7 +56,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.mSelectionEnd = mTextView.getSelectionEnd();
     }
 
-    @SmallTest
+    @Test
     public void testCombiningEnclosingKeycaps() {
         EditorState state = new EditorState();
 
@@ -69,7 +76,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testVariationSelector() {
         EditorState state = new EditorState();
 
@@ -117,7 +124,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testEmojiZeroWidthJoinerSequence() {
         EditorState state = new EditorState();
 
@@ -160,7 +167,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testFlags() {
         EditorState state = new EditorState();
 
@@ -217,7 +224,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.assertEquals("| 'b'");
     }
 
-    @SmallTest
+    @Test
     public void testEmojiModifier() {
         EditorState state = new EditorState();
 
@@ -246,7 +253,7 @@ public class ForwardDeleteTest extends KeyListenerTestCase {
         state.assertEquals("|");
     }
 
-    @SmallTest
+    @Test
     public void testMixedEdgeCases() {
         EditorState state = new EditorState();
 
index f005d7b..99a0091 100644 (file)
 package android.text.method;
 
 import android.app.Instrumentation;
-import android.test.InstrumentationTestCase;
+import android.support.test.InstrumentationRegistry;
 import android.view.KeyEvent;
 import android.widget.EditText;
 
-import com.android.frameworks.coretests.R;
-
-public abstract class KeyListenerTestCase extends InstrumentationTestCase {
-
+public abstract class KeyListenerTestCase {
     protected Instrumentation mInstrumentation;
     protected EditText mTextView;
 
     public KeyListenerTestCase() {
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-        mInstrumentation = getInstrumentation();
+    protected void setup() {
+        mInstrumentation = InstrumentationRegistry.getInstrumentation();
         mTextView = new EditText(mInstrumentation.getContext());
     }
 
index 66cf65f..3499a74 100644 (file)
 
 package android.text.method;
 
-import android.test.AndroidTestCase;
-import android.test.suitebuilder.annotation.SmallTest;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.filters.SmallTest;
 
 import java.text.BreakIterator;
 import java.util.Locale;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 // TODO(Bug: 24062099): Add more tests for non-ascii text.
-public class WordIteratorTest  extends AndroidTestCase {
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class WordIteratorTest {
 
-    @SmallTest
+    @Test
     public void testSetCharSequence() {
         final String text = "text";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -48,7 +57,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         wordIterator.setCharSequence(text, text.length(), text.length());
     }
 
-    @SmallTest
+    @Test
     public void testWindowWidth() {
         final String text = "aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -65,7 +74,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(BreakIterator.DONE, wordIterator.following(expectedWindowEnd));
     }
 
-    @SmallTest
+    @Test
     public void testPreceding() {
         final String text = "abc def-ghi. jkl";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -105,7 +114,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(text.indexOf('j'), wordIterator.preceding(text.indexOf('l')));
     }
 
-    @SmallTest
+    @Test
     public void testFollowing() {
         final String text = "abc def-ghi. jkl";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -145,7 +154,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(BreakIterator.DONE, wordIterator.following(text.length()));
     }
 
-    @SmallTest
+    @Test
     public void testIsBoundary() {
         final String text = "abc def-ghi. jkl";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -173,7 +182,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertTrue(wordIterator.isBoundary(text.length()));
     }
 
-    @SmallTest
+    @Test
     public void testNextBoundary() {
         final String text = "abc def-ghi. jkl";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -220,7 +229,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(BreakIterator.DONE, currentOffset);
     }
 
-    @SmallTest
+    @Test
     public void testPrevBoundary() {
         final String text = "abc def-ghi. jkl";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -266,7 +275,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(BreakIterator.DONE, currentOffset);
     }
 
-    @SmallTest
+    @Test
     public void testGetBeginning() {
         {
             final String text = "abc def-ghi. jkl";
@@ -340,7 +349,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         }
     }
 
-    @SmallTest
+    @Test
     public void testGetEnd() {
         {
             final String text = "abc def-ghi. jkl";
@@ -415,7 +424,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         }
     }
 
-    @SmallTest
+    @Test
     public void testGetPunctuationBeginning() {
         final String text = "abc!? (^^;) def";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -450,7 +459,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(text.indexOf(';'), wordIterator.getPunctuationBeginning(text.length()));
     }
 
-    @SmallTest
+    @Test
     public void testGetPunctuationEnd() {
         final String text = "abc!? (^^;) def";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -482,7 +491,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertEquals(BreakIterator.DONE, wordIterator.getPunctuationEnd(text.length()));
     }
 
-    @SmallTest
+    @Test
     public void testIsAfterPunctuation() {
         final String text = "abc!? (^^;) def";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -498,7 +507,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertFalse(wordIterator.isAfterPunctuation(text.length() + 1));
     }
 
-    @SmallTest
+    @Test
     public void testIsOnPunctuation() {
         final String text = "abc!? (^^;) def";
         WordIterator wordIterator = new WordIterator(Locale.ENGLISH);
@@ -517,7 +526,7 @@ public class WordIteratorTest  extends AndroidTestCase {
         assertFalse(wordIterator.isOnPunctuation(text.length() + 1));
     }
 
-    @SmallTest
+    @Test
     public void testApostropheMiddleOfWord() {
         // These tests confirm that the word "isn't" is treated like one word.
         final String text = "isn't he";
index 487f4e9..23c3408 100644 (file)
 
 package android.text.util;
 
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import android.content.Context;
 import android.content.res.Configuration;
 import android.os.LocaleList;
+import android.support.test.InstrumentationRegistry;
 import android.support.test.filters.SmallTest;
-import android.test.AndroidTestCase;
+import android.support.test.runner.AndroidJUnit4;
 import android.text.method.LinkMovementMethod;
 import android.widget.TextView;
 
 import java.util.Locale;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
 
 /**
  * LinkifyTest tests {@link Linkify}.
  */
-public class LinkifyTest extends AndroidTestCase {
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class LinkifyTest {
 
     private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US);
     private LocaleList mOriginalLocales;
+    private Context mContext;
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
+    @Before
+    public void setup() {
+        mContext = InstrumentationRegistry.getContext();
         mOriginalLocales = LocaleList.getDefault();
         LocaleList.setDefault(LOCALE_LIST_US);
     }
 
-    @Override
-    protected void tearDown() throws Exception {
+    @After
+    public void teardown() {
         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 {
+    @Test
+    public void testNothing() {
         TextView tv;
 
         tv = new TextView(createUsEnglishContext());
@@ -64,8 +68,8 @@ public class LinkifyTest extends AndroidTestCase {
         assertTrue(tv.getUrls().length == 0);
     }
 
-    @SmallTest
-    public void testNormal() throws Exception {
+    @Test
+    public void testNormal() {
         TextView tv;
 
         tv = new TextView(createUsEnglishContext());
@@ -76,8 +80,8 @@ public class LinkifyTest extends AndroidTestCase {
         assertTrue(tv.getUrls().length == 2);
     }
 
-    @SmallTest
-    public void testUnclickable() throws Exception {
+    @Test
+    public void testUnclickable() {
         TextView tv;
 
         tv = new TextView(createUsEnglishContext());
@@ -88,4 +92,10 @@ public class LinkifyTest extends AndroidTestCase {
         assertFalse(tv.getMovementMethod() instanceof LinkMovementMethod);
         assertTrue(tv.getUrls().length == 2);
     }
+
+    private Context createUsEnglishContext() {
+        final Configuration overrideConfig = new Configuration();
+        overrideConfig.setLocales(LOCALE_LIST_US);
+        return mContext.createConfigurationContext(overrideConfig);
+    }
 }