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.widget; 17 18import static android.support.text.emoji.util.EmojiMatcher.sameCharSequence; 19 20import static junit.framework.TestCase.assertNotNull; 21import static junit.framework.TestCase.assertSame; 22 23import static org.junit.Assert.assertNull; 24import static org.junit.Assert.assertTrue; 25import static org.mockito.Matchers.any; 26import static org.mockito.Matchers.anyInt; 27import static org.mockito.Matchers.eq; 28import static org.mockito.Mockito.mock; 29import static org.mockito.Mockito.never; 30import static org.mockito.Mockito.times; 31import static org.mockito.Mockito.verify; 32import static org.mockito.Mockito.when; 33 34import android.support.test.filters.SmallTest; 35import android.support.test.runner.AndroidJUnit4; 36import android.support.text.emoji.EmojiCompat; 37import android.text.Spannable; 38import android.text.SpannableString; 39import android.widget.TextView; 40 41import org.junit.Before; 42import org.junit.Test; 43import org.junit.runner.RunWith; 44 45@SmallTest 46@RunWith(AndroidJUnit4.class) 47public class EmojiInputFilterTest { 48 49 private EmojiInputFilter mInputFilter; 50 private EmojiCompat mEmojiCompat; 51 52 @Before 53 public void setup() { 54 final TextView textView = mock(TextView.class); 55 mEmojiCompat = mock(EmojiCompat.class); 56 EmojiCompat.reset(mEmojiCompat); 57 when(mEmojiCompat.getLoadState()).thenReturn(EmojiCompat.LOAD_STATE_SUCCEEDED); 58 mInputFilter = new EmojiInputFilter(textView); 59 } 60 61 @Test 62 public void testFilter_withNullSource() { 63 assertNull(mInputFilter.filter(null, 0, 1, null, 0, 1)); 64 verify(mEmojiCompat, never()).process(any(CharSequence.class)); 65 verify(mEmojiCompat, never()).process(any(CharSequence.class), anyInt(), anyInt()); 66 } 67 68 @Test 69 public void testFilter_withString() { 70 final String testString = "abc"; 71 when(mEmojiCompat.process(any(CharSequence.class), anyInt(), anyInt())) 72 .thenReturn(new SpannableString(testString)); 73 final CharSequence result = mInputFilter.filter(testString, 0, 1, null, 0, 1); 74 75 assertNotNull(result); 76 assertTrue(result instanceof Spannable); 77 verify(mEmojiCompat, times(1)).process(sameCharSequence("a"), eq(0), eq(1)); 78 } 79 80 @Test 81 public void testFilter_withSpannable() { 82 final Spannable testString = new SpannableString("abc"); 83 when(mEmojiCompat.process(any(Spannable.class), anyInt(), anyInt())).thenReturn(testString); 84 85 final CharSequence result = mInputFilter.filter(testString, 0, 1, null, 0, 1); 86 87 assertNotNull(result); 88 assertSame(result, testString); 89 verify(mEmojiCompat, times(1)).process(sameCharSequence(testString.subSequence(0, 1)), 90 eq(0), eq(1)); 91 } 92 93 @Test 94 public void testFilter_whenEmojiCompatLoading() { 95 final Spannable testString = new SpannableString("abc"); 96 when(mEmojiCompat.getLoadState()).thenReturn(EmojiCompat.LOAD_STATE_LOADING); 97 98 final CharSequence result = mInputFilter.filter(testString, 0, 1, null, 0, 1); 99 100 assertNotNull(result); 101 assertSame(result, testString); 102 verify(mEmojiCompat, times(0)).process(any(Spannable.class), anyInt(), anyInt()); 103 verify(mEmojiCompat, times(1)).registerInitCallback(any(EmojiCompat.InitCallback.class)); 104 } 105 106 @Test 107 public void testFilter_whenEmojiCompatLoadFailed() { 108 final Spannable testString = new SpannableString("abc"); 109 when(mEmojiCompat.getLoadState()).thenReturn(EmojiCompat.LOAD_STATE_FAILED); 110 111 final CharSequence result = mInputFilter.filter(testString, 0, 1, null, 0, 1); 112 113 assertNotNull(result); 114 verify(mEmojiCompat, times(0)).process(any(Spannable.class), anyInt(), anyInt()); 115 verify(mEmojiCompat, times(0)).registerInitCallback(any(EmojiCompat.InitCallback.class)); 116 } 117} 118