Home | History | Annotate | Download | only in widget
      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  */
     16 package androidx.emoji.widget;
     17 
     18 import static org.hamcrest.Matchers.arrayWithSize;
     19 import static org.hamcrest.Matchers.instanceOf;
     20 import static org.junit.Assert.assertEquals;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertSame;
     23 import static org.junit.Assert.assertThat;
     24 import static org.mockito.Mockito.mock;
     25 
     26 import android.annotation.SuppressLint;
     27 import android.support.test.filters.SmallTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.text.Editable;
     30 import android.text.SpannableString;
     31 import android.text.Spanned;
     32 
     33 import androidx.emoji.text.EmojiMetadata;
     34 import androidx.emoji.text.EmojiSpan;
     35 import androidx.emoji.text.TypefaceEmojiSpan;
     36 
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 
     40 @SmallTest
     41 @RunWith(AndroidJUnit4.class)
     42 public class EmojiEditableFactoryTest {
     43 
     44     @Test
     45     public void testGetInstance() {
     46         final Editable.Factory instance = EmojiEditableFactory.getInstance();
     47         assertNotNull(instance);
     48 
     49         final Editable.Factory instance2 = EmojiEditableFactory.getInstance();
     50         assertSame(instance, instance2);
     51     }
     52 
     53     @Test
     54     public void testNewEditable_returnsEditable() {
     55         final Editable editable = EmojiEditableFactory.getInstance().newEditable("abc");
     56         assertNotNull(editable);
     57         assertThat(editable, instanceOf(Editable.class));
     58     }
     59 
     60     @Test
     61     public void testNewEditable_preservesCharSequenceData() {
     62         final String string = "abc";
     63         final SpannableString str = new SpannableString(string);
     64         final EmojiMetadata metadata = mock(EmojiMetadata.class);
     65         final EmojiSpan span = new TypefaceEmojiSpan(metadata);
     66         str.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
     67 
     68         final Editable editable = EmojiEditableFactory.getInstance().newEditable(str);
     69         assertNotNull(editable);
     70         assertEquals(string, editable.toString());
     71         final EmojiSpan[] spans = editable.getSpans(0, 1, EmojiSpan.class);
     72         assertThat(spans, arrayWithSize(1));
     73         assertSame(spans[0], span);
     74     }
     75 
     76     @SuppressLint("PrivateApi")
     77     @Test
     78     public void testNewEditable_returnsEmojiSpannableIfWatcherClassExists() {
     79         Class clazz = null;
     80         try {
     81             String className = "android.text.DynamicLayout$ChangeWatcher";
     82             clazz = getClass().getClassLoader().loadClass(className);
     83         } catch (Throwable t) {
     84             // ignore
     85         }
     86 
     87         if (clazz == null) {
     88             final Editable editable = EmojiEditableFactory.getInstance().newEditable("");
     89             assertThat(editable, instanceOf(Editable.class));
     90         } else {
     91             final Editable editable = EmojiEditableFactory.getInstance().newEditable("");
     92             assertThat(editable, instanceOf(SpannableBuilder.class));
     93         }
     94     }
     95 }
     96