blob: 4855dada361aa20ae6f92af87bc2d1e4cfd6df6f [file] [log] [blame]
Jan Althaus0d9fbb92017-11-28 12:19:33 +01001/*
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 android.view.textclassifier;
18
19import static org.junit.Assert.assertEquals;
Jan Althaus0d9fbb92017-11-28 12:19:33 +010020
21import android.os.LocaleList;
22import android.os.Parcel;
23import android.support.test.filters.SmallTest;
24import android.support.test.runner.AndroidJUnit4;
25
26import org.junit.Test;
27import org.junit.runner.RunWith;
28
29import java.util.Locale;
30
31@SmallTest
32@RunWith(AndroidJUnit4.class)
33public class TextSelectionTest {
34
35 @Test
36 public void testParcel() {
37 final int startIndex = 13;
38 final int endIndex = 37;
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010039 final String id = "id";
Jan Althaus0d9fbb92017-11-28 12:19:33 +010040 final TextSelection reference = new TextSelection.Builder(startIndex, endIndex)
41 .setEntityType(TextClassifier.TYPE_ADDRESS, 0.3f)
42 .setEntityType(TextClassifier.TYPE_PHONE, 0.7f)
43 .setEntityType(TextClassifier.TYPE_URL, 0.1f)
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010044 .setId(id)
Jan Althaus0d9fbb92017-11-28 12:19:33 +010045 .build();
46
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080047 // Parcel and unparcel
Jan Althaus0d9fbb92017-11-28 12:19:33 +010048 final Parcel parcel = Parcel.obtain();
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080049 reference.writeToParcel(parcel, reference.describeContents());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010050 parcel.setDataPosition(0);
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080051 final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel);
Jan Althaus0d9fbb92017-11-28 12:19:33 +010052
53 assertEquals(startIndex, result.getSelectionStartIndex());
54 assertEquals(endIndex, result.getSelectionEndIndex());
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010055 assertEquals(id, result.getId());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010056
57 assertEquals(3, result.getEntityCount());
58 assertEquals(TextClassifier.TYPE_PHONE, result.getEntity(0));
59 assertEquals(TextClassifier.TYPE_ADDRESS, result.getEntity(1));
60 assertEquals(TextClassifier.TYPE_URL, result.getEntity(2));
61 assertEquals(0.7f, result.getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f);
62 assertEquals(0.3f, result.getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f);
63 assertEquals(0.1f, result.getConfidenceScore(TextClassifier.TYPE_URL), 1e-7f);
64 }
65
66 @Test
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010067 public void testParcelRequest() {
68 final String text = "text";
69 final TextSelection.Request reference =
70 new TextSelection.Request.Builder(text, 0, text.length())
71 .setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY))
72 .build();
Jan Althaus0d9fbb92017-11-28 12:19:33 +010073
74 // Parcel and unparcel.
75 final Parcel parcel = Parcel.obtain();
76 reference.writeToParcel(parcel, reference.describeContents());
77 parcel.setDataPosition(0);
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010078 final TextSelection.Request result = TextSelection.Request.CREATOR.createFromParcel(parcel);
Jan Althaus0d9fbb92017-11-28 12:19:33 +010079
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010080 assertEquals(text, result.getText());
81 assertEquals(0, result.getStartIndex());
82 assertEquals(text.length(), result.getEndIndex());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010083 assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010084 }
85}