1/*
2 * Copyright (C) 2017 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 android.support.text.emoji;
17
18import static junit.framework.Assert.assertEquals;
19
20import static org.mockito.Matchers.any;
21import static org.mockito.Matchers.eq;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.reset;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.annotation.TargetApi;
29import android.graphics.Canvas;
30import android.graphics.Paint;
31import android.graphics.Paint.FontMetricsInt;
32import android.support.test.filters.SdkSuppress;
33import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
35import android.text.TextPaint;
36
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
40import org.mockito.invocation.InvocationOnMock;
41import org.mockito.stubbing.Answer;
42
43@SmallTest
44@RunWith(AndroidJUnit4.class)
45@SdkSuppress(minSdkVersion = 19)
46@TargetApi(19)
47public class EmojiSpanTest {
48
49    @Before
50    public void setup() {
51        EmojiCompat.reset(TestConfigBuilder.config());
52    }
53
54    @Test
55    public void testGetSize() {
56        final short dimensionX = 18;
57        final short dimensionY = 20;
58        final int fontHeight = 10;
59        final float expectedRatio = fontHeight * 1.0f / dimensionY;
60        final TextPaint paint = mock(TextPaint.class);
61
62        // mock TextPaint to return test font metrics
63        when(paint.getFontMetricsInt(any(FontMetricsInt.class))).thenAnswer(new Answer<Object>() {
64            @Override
65            public Object answer(InvocationOnMock invocation) throws Throwable {
66                final FontMetricsInt fontMetrics = (FontMetricsInt) invocation.getArguments()[0];
67                fontMetrics.ascent = 0;
68                fontMetrics.descent = -fontHeight;
69                return null;
70            }
71        });
72
73        final EmojiMetadata metadata = mock(EmojiMetadata.class);
74        when(metadata.getWidth()).thenReturn(dimensionX);
75        when(metadata.getHeight()).thenReturn(dimensionY);
76        final EmojiSpan span = new TypefaceEmojiSpan(metadata);
77
78        final int resultSize = span.getSize(paint, "", 0, 0, null);
79        assertEquals((int) (dimensionX * expectedRatio), resultSize);
80        assertEquals(expectedRatio, span.getRatio());
81        assertEquals((int) (dimensionX * expectedRatio), span.getWidth());
82        assertEquals((int) (dimensionY * expectedRatio), span.getHeight());
83    }
84
85    @Test
86    public void testBackgroundIndicator() {
87        // control the size of the emoji span
88        final EmojiMetadata metadata = mock(EmojiMetadata.class);
89        when(metadata.getWidth()).thenReturn((short) 10);
90        when(metadata.getHeight()).thenReturn((short) 10);
91
92        final EmojiSpan span = new TypefaceEmojiSpan(metadata);
93        final int spanWidth = span.getSize(mock(Paint.class), "", 0, 0, null);
94        // prepare parameters for draw() call
95        final Canvas canvas = mock(Canvas.class);
96        final float x = 10;
97        final int top = 15;
98        final int y = 20;
99        final int bottom = 30;
100
101        // verify the case where indicators are disabled
102        EmojiCompat.reset(TestConfigBuilder.config().setEmojiSpanIndicatorEnabled(false));
103        span.draw(canvas, "a", 0 /*start*/, 1 /*end*/, x, top, y, bottom, mock(Paint.class));
104
105        verify(canvas, times(0)).drawRect(eq(x), eq((float) top), eq(x + spanWidth),
106                eq((float) bottom), any(Paint.class));
107
108        // verify the case where indicators are enabled
109        EmojiCompat.reset(TestConfigBuilder.config().setEmojiSpanIndicatorEnabled(true));
110        reset(canvas);
111        span.draw(canvas, "a", 0 /*start*/, 1 /*end*/, x, top, y, bottom, mock(Paint.class));
112
113        verify(canvas, times(1)).drawRect(eq(x), eq((float) top), eq(x + spanWidth),
114                eq((float) bottom), any(Paint.class));
115    }
116}
117