1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package androidx.emoji.text;
17
18import static org.junit.Assert.assertEquals;
19import static org.mockito.Matchers.any;
20import static org.mockito.Matchers.eq;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.reset;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
27import android.graphics.Canvas;
28import android.graphics.Paint;
29import android.graphics.Paint.FontMetricsInt;
30import android.support.test.filters.SdkSuppress;
31import android.support.test.filters.SmallTest;
32import android.support.test.runner.AndroidJUnit4;
33import android.text.TextPaint;
34
35import org.junit.Before;
36import org.junit.Test;
37import org.junit.runner.RunWith;
38import org.mockito.invocation.InvocationOnMock;
39import org.mockito.stubbing.Answer;
40
41@SmallTest
42@RunWith(AndroidJUnit4.class)
43@SdkSuppress(minSdkVersion = 19)
44public class EmojiSpanTest {
45
46    @Before
47    public void setup() {
48        EmojiCompat.reset(TestConfigBuilder.config());
49    }
50
51    @Test
52    public void testGetSize() {
53        final short dimensionX = 18;
54        final short dimensionY = 20;
55        final int fontHeight = 10;
56        final float expectedRatio = fontHeight * 1.0f / dimensionY;
57        final TextPaint paint = mock(TextPaint.class);
58
59        // mock TextPaint to return test font metrics
60        when(paint.getFontMetricsInt(any(FontMetricsInt.class))).thenAnswer(new Answer<Object>() {
61            @Override
62            public Object answer(InvocationOnMock invocation) throws Throwable {
63                final FontMetricsInt fontMetrics = (FontMetricsInt) invocation.getArguments()[0];
64                fontMetrics.ascent = 0;
65                fontMetrics.descent = -fontHeight;
66                return null;
67            }
68        });
69
70        final EmojiMetadata metadata = mock(EmojiMetadata.class);
71        when(metadata.getWidth()).thenReturn(dimensionX);
72        when(metadata.getHeight()).thenReturn(dimensionY);
73        final EmojiSpan span = new TypefaceEmojiSpan(metadata);
74
75        final int resultSize = span.getSize(paint, "", 0, 0, null);
76        assertEquals((int) (dimensionX * expectedRatio), resultSize);
77        assertEquals(expectedRatio, span.getRatio(), 0.01f);
78        assertEquals((int) (dimensionX * expectedRatio), span.getWidth());
79        assertEquals((int) (dimensionY * expectedRatio), span.getHeight());
80    }
81
82    @Test
83    public void testBackgroundIndicator() {
84        // control the size of the emoji span
85        final EmojiMetadata metadata = mock(EmojiMetadata.class);
86        when(metadata.getWidth()).thenReturn((short) 10);
87        when(metadata.getHeight()).thenReturn((short) 10);
88
89        final EmojiSpan span = new TypefaceEmojiSpan(metadata);
90        final int spanWidth = span.getSize(mock(Paint.class), "", 0, 0, null);
91        // prepare parameters for draw() call
92        final Canvas canvas = mock(Canvas.class);
93        final float x = 10;
94        final int top = 15;
95        final int y = 20;
96        final int bottom = 30;
97
98        // verify the case where indicators are disabled
99        EmojiCompat.reset(TestConfigBuilder.config().setEmojiSpanIndicatorEnabled(false));
100        span.draw(canvas, "a", 0 /*start*/, 1 /*end*/, x, top, y, bottom, mock(Paint.class));
101
102        verify(canvas, times(0)).drawRect(eq(x), eq((float) top), eq(x + spanWidth),
103                eq((float) bottom), any(Paint.class));
104
105        // verify the case where indicators are enabled
106        EmojiCompat.reset(TestConfigBuilder.config().setEmojiSpanIndicatorEnabled(true));
107        reset(canvas);
108        span.draw(canvas, "a", 0 /*start*/, 1 /*end*/, x, top, y, bottom, mock(Paint.class));
109
110        verify(canvas, times(1)).drawRect(eq(x), eq((float) top), eq(x + spanWidth),
111                eq((float) bottom), any(Paint.class));
112    }
113}
114