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