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 17 package androidx.emoji.widget; 18 19 import static androidx.emoji.util.Emoji.EMOJI_SINGLE_CODEPOINT; 20 import static androidx.emoji.util.EmojiMatcher.hasEmojiCount; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertNull; 24 import static org.junit.Assert.assertThat; 25 26 import android.app.Instrumentation; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.annotation.UiThreadTest; 29 import android.support.test.filters.MediumTest; 30 import android.support.test.filters.SdkSuppress; 31 import android.support.test.rule.ActivityTestRule; 32 import android.support.test.runner.AndroidJUnit4; 33 34 import androidx.emoji.test.R; 35 import androidx.emoji.text.EmojiCompat; 36 import androidx.emoji.text.TestActivity; 37 import androidx.emoji.text.TestConfigBuilder; 38 import androidx.emoji.util.TestString; 39 40 import org.junit.Before; 41 import org.junit.BeforeClass; 42 import org.junit.Rule; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 46 @MediumTest 47 @RunWith(AndroidJUnit4.class) 48 public class EmojiEditTextTest { 49 50 @Rule 51 public ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>( 52 TestActivity.class); 53 private Instrumentation mInstrumentation; 54 55 @BeforeClass 56 public static void setupEmojiCompat() { 57 EmojiCompat.reset(TestConfigBuilder.config()); 58 } 59 60 @Before 61 public void setup() { 62 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 63 } 64 65 @Test 66 public void testInflateWithMaxEmojiCount() { 67 final TestActivity activity = mActivityRule.getActivity(); 68 final EmojiEditText editText = activity.findViewById(R.id.editTextWithMaxCount); 69 70 // value set in XML 71 assertEquals(5, editText.getMaxEmojiCount()); 72 73 // set max emoji count 74 mInstrumentation.runOnMainSync(new Runnable() { 75 @Override 76 public void run() { 77 editText.setMaxEmojiCount(1); 78 } 79 }); 80 mInstrumentation.waitForIdleSync(); 81 82 assertEquals(1, editText.getMaxEmojiCount()); 83 } 84 85 @Test 86 @UiThreadTest 87 public void testSetKeyListener_withNull() { 88 final TestActivity activity = mActivityRule.getActivity(); 89 final EmojiEditText editText = activity.findViewById(R.id.editTextWithMaxCount); 90 editText.setKeyListener(null); 91 assertNull(editText.getKeyListener()); 92 } 93 94 @Test 95 @SdkSuppress(minSdkVersion = 19) 96 public void testSetMaxCount() { 97 final TestActivity activity = mActivityRule.getActivity(); 98 final EmojiEditText editText = activity.findViewById(R.id.editTextWithMaxCount); 99 100 // set max emoji count to 1 and set text with 2 emojis 101 mInstrumentation.runOnMainSync(new Runnable() { 102 @Override 103 public void run() { 104 editText.setMaxEmojiCount(1); 105 final String string = new TestString(EMOJI_SINGLE_CODEPOINT).append( 106 EMOJI_SINGLE_CODEPOINT).toString(); 107 editText.setText(string); 108 } 109 }); 110 mInstrumentation.waitForIdleSync(); 111 112 assertThat(editText.getText(), hasEmojiCount(1)); 113 } 114 } 115