OSDN Git Service

Replace mockito with a mock class in DynamicLayoutTest
authorRoozbeh Pournader <roozbeh@google.com>
Tue, 6 Jun 2017 22:44:03 +0000 (15:44 -0700)
committerRoozbeh Pournader <roozbeh@google.com>
Tue, 6 Jun 2017 22:49:42 +0000 (15:49 -0700)
Previously, a mocked object was used incorrectly in the test. It was
attached twice as a span, which would result in the second identical
span causing the first span to get removed.

Now we use two different instances of a mock class, to make sure we
are attaching different spans.

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

Change-Id: Ib9f7a467410d28280d77a1023e0fc328c652e2af

core/tests/coretests/src/android/text/DynamicLayoutTest.java

index da6dc7e..811bf2c 100644 (file)
 package android.text;
 
 import static android.text.Layout.Alignment.ALIGN_NORMAL;
+
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-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.support.test.filters.SmallTest;
 import android.support.test.runner.AndroidJUnit4;
 import android.text.style.ReplacementSpan;
@@ -54,6 +53,16 @@ public class DynamicLayoutTest {
         assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
     }
 
+    private class MockReplacementSpan extends ReplacementSpan {
+        public int getSize(Paint paint, CharSequence text, int start, int end,
+                Paint.FontMetricsInt fm) {
+            return 10;
+        }
+
+        public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
+                int y, int bottom, Paint paint) { }
+    }
+
     @Test
     public void testGetBlocksAlwaysNeedToBeRedrawn_replacementSpan() {
         final SpannableStringBuilder builder = new SpannableStringBuilder();
@@ -66,17 +75,11 @@ public class DynamicLayoutTest {
         builder.append("hijk lmn\n");
         assertNull(layout.getBlocksAlwaysNeedToBeRedrawn());
 
-        ReplacementSpan mockReplacementSpan = mock(ReplacementSpan.class);
-        when(mockReplacementSpan.getSize(any(), any(), any(), any(), any()))
-            .thenReturn(10);
-        doNothing().when(mockReplacementSpan)
-            .draw(any(), any(), any(), any(), any(), any(), any(), any(), any());
-
-        builder.setSpan(mockReplacementSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        builder.setSpan(new MockReplacementSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         assertNotNull(layout.getBlocksAlwaysNeedToBeRedrawn());
         assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
 
-        builder.setSpan(mockReplacementSpan, 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        builder.setSpan(new MockReplacementSpan(), 9, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
         assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(0));
         assertTrue(layout.getBlocksAlwaysNeedToBeRedrawn().contains(1));