Home | History | Annotate | Download | only in text
      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  */
     16 package androidx.emoji.text;
     17 
     18 import static org.junit.Assert.assertEquals;
     19 import static org.mockito.Matchers.any;
     20 import static org.mockito.Matchers.eq;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.reset;
     23 import static org.mockito.Mockito.times;
     24 import static org.mockito.Mockito.verify;
     25 import static org.mockito.Mockito.when;
     26 
     27 import android.graphics.Canvas;
     28 import android.graphics.Paint;
     29 import android.graphics.Paint.FontMetricsInt;
     30 import android.support.test.filters.SdkSuppress;
     31 import android.support.test.filters.SmallTest;
     32 import android.support.test.runner.AndroidJUnit4;
     33 import android.text.TextPaint;
     34 
     35 import org.junit.Before;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 import org.mockito.invocation.InvocationOnMock;
     39 import org.mockito.stubbing.Answer;
     40 
     41 @SmallTest
     42 @RunWith(AndroidJUnit4.class)
     43 @SdkSuppress(minSdkVersion = 19)
     44 public 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