1 /* 2 * Copyright 2018 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.text; 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.assertNotNull; 24 import static org.junit.Assert.assertThat; 25 26 import android.app.Instrumentation; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.filters.LargeTest; 29 import android.support.test.filters.SdkSuppress; 30 import android.support.test.rule.ActivityTestRule; 31 import android.support.test.runner.AndroidJUnit4; 32 import android.text.Spannable; 33 import android.text.Spanned; 34 import android.text.style.RelativeSizeSpan; 35 import android.util.TypedValue; 36 import android.widget.TextView; 37 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 @LargeTest 47 @RunWith(AndroidJUnit4.class) 48 @SdkSuppress(minSdkVersion = 19) 49 public class EmojiSpanInstrumentationTest { 50 51 @Rule 52 public ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>( 53 TestActivity.class); 54 private Instrumentation mInstrumentation; 55 56 @BeforeClass 57 public static void setupEmojiCompat() { 58 EmojiCompat.reset(TestConfigBuilder.config()); 59 } 60 61 @Before 62 public void setup() { 63 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 64 } 65 66 @Test 67 public void testGetSize_withRelativeSizeSpan() { 68 final TestActivity activity = mActivityRule.getActivity(); 69 final TextView textView = (TextView) activity.findViewById( 70 androidx.emoji.test.R.id.text); 71 72 // create a string with single codepoint emoji 73 final TestString string = new TestString(EMOJI_SINGLE_CODEPOINT).withPrefix().withSuffix(); 74 final CharSequence charSequence = EmojiCompat.get().process(string.toString()); 75 assertNotNull(charSequence); 76 assertThat(charSequence, hasEmojiCount(1)); 77 78 final Spannable spanned = (Spannable) charSequence; 79 final EmojiSpan[] spans = spanned.getSpans(0, charSequence.length(), EmojiSpan.class); 80 final EmojiSpan span = spans[0]; 81 82 // set text to the charSequence with the EmojiSpan 83 mInstrumentation.runOnMainSync(new Runnable() { 84 @Override 85 public void run() { 86 textView.setText(charSequence); 87 } 88 }); 89 mInstrumentation.waitForIdleSync(); 90 91 // record height of the default span 92 final int defaultHeight = span.getHeight(); 93 94 // cover the charsequence with RelativeSizeSpan which will triple the size of the 95 // characters. 96 final float multiplier = 3.0f; 97 final RelativeSizeSpan sizeSpan = new RelativeSizeSpan(multiplier); 98 spanned.setSpan(sizeSpan, 0, charSequence.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 99 // set the new text 100 mInstrumentation.runOnMainSync(new Runnable() { 101 @Override 102 public void run() { 103 textView.setText(charSequence); 104 } 105 }); 106 mInstrumentation.waitForIdleSync(); 107 108 // record the height measured after RelativeSizeSpan 109 final int heightWithRelativeSpan = span.getHeight(); 110 111 // accept 1sp error rate. 112 final float delta = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 1, 113 mInstrumentation.getTargetContext().getResources().getDisplayMetrics()); 114 assertEquals(defaultHeight * 3, heightWithRelativeSpan, delta); 115 } 116 } 117