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

Commit de0353db authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Replace mockito with a mock class in DynamicLayoutTest

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
parent a3112cee
Loading
Loading
Loading
Loading
+15 −12
Original line number Diff line number Diff line
@@ -17,15 +17,14 @@
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));