EmojiEditTextTest.java revision 38746a682208c764867ffe4415d0b62fb22b5b9a
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
17package androidx.emoji.widget;
18
19import static androidx.emoji.util.Emoji.EMOJI_SINGLE_CODEPOINT;
20import static androidx.emoji.util.EmojiMatcher.hasEmojiCount;
21
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertNull;
24import static org.junit.Assert.assertThat;
25
26import android.app.Instrumentation;
27import android.support.test.InstrumentationRegistry;
28import android.support.test.annotation.UiThreadTest;
29import android.support.test.filters.MediumTest;
30import android.support.test.filters.SdkSuppress;
31import android.support.test.rule.ActivityTestRule;
32import android.support.test.runner.AndroidJUnit4;
33
34import androidx.emoji.test.R;
35import androidx.emoji.text.EmojiCompat;
36import androidx.emoji.text.TestActivity;
37import androidx.emoji.text.TestConfigBuilder;
38import androidx.emoji.util.TestString;
39
40import org.junit.Before;
41import org.junit.BeforeClass;
42import org.junit.Rule;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45
46@MediumTest
47@RunWith(AndroidJUnit4.class)
48public 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