TextSelectionTest.java revision f27b1ffc67228d73326ec3426fef4c9db75cd6fd
1/* 2 * Copyright (C) 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 17package androidx.textclassifier; 18 19import static org.junit.Assert.assertEquals; 20 21import android.os.Parcel; 22import android.support.test.filters.SmallTest; 23import android.support.test.runner.AndroidJUnit4; 24import android.support.v4.os.LocaleListCompat; 25 26import org.junit.Test; 27import org.junit.runner.RunWith; 28 29/** Instrumentation unit tests for {@link TextSelection}. */ 30@SmallTest 31@RunWith(AndroidJUnit4.class) 32public final class TextSelectionTest { 33 @Test 34 public void testParcel() { 35 final int startIndex = 13; 36 final int endIndex = 37; 37 final String signature = "signature"; 38 final TextSelection reference = new TextSelection.Builder(startIndex, endIndex) 39 .setEntityType(TextClassifier.TYPE_ADDRESS, 0.3f) 40 .setEntityType(TextClassifier.TYPE_PHONE, 0.7f) 41 .setEntityType(TextClassifier.TYPE_URL, 0.1f) 42 .setSignature(signature) 43 .build(); 44 45 final Parcel parcel = Parcel.obtain(); 46 reference.writeToParcel(parcel, reference.describeContents()); 47 parcel.setDataPosition(0); 48 final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel); 49 50 assertEquals(startIndex, result.getSelectionStartIndex()); 51 assertEquals(endIndex, result.getSelectionEndIndex()); 52 assertEquals(signature, result.getSignature()); 53 54 assertEquals(3, result.getEntityCount()); 55 assertEquals(TextClassifier.TYPE_PHONE, result.getEntity(0)); 56 assertEquals(TextClassifier.TYPE_ADDRESS, result.getEntity(1)); 57 assertEquals(TextClassifier.TYPE_URL, result.getEntity(2)); 58 assertEquals(0.7f, result.getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f); 59 assertEquals(0.3f, result.getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f); 60 assertEquals(0.1f, result.getConfidenceScore(TextClassifier.TYPE_URL), 1e-7f); 61 } 62 63 @Test 64 public void testParcelOptions() { 65 final String callingPackageName = "packageName"; 66 TextSelection.Options reference = new TextSelection.Options() 67 .setDefaultLocales(LocaleListCompat.forLanguageTags("en-US,de-DE")) 68 .setCallingPackageName(callingPackageName); 69 70 // Parcel and unparcel. 71 final Parcel parcel = Parcel.obtain(); 72 reference.writeToParcel(parcel, reference.describeContents()); 73 parcel.setDataPosition(0); 74 TextSelection.Options result = TextSelection.Options.CREATOR.createFromParcel( 75 parcel); 76 77 assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags()); 78 assertEquals(callingPackageName, result.getCallingPackageName()); 79 } 80} 81