blob: 7ea5108bc3bbe83d25cd3bdecf26f4b2f87e34e8 [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
Tony Mak916bb9e2018-11-08 20:45:20 +000021import android.os.Bundle;
Jan Althaus0d9fbb92017-11-28 12:19:33 +010022import android.os.LocaleList;
23import android.os.Parcel;
24import android.support.test.filters.SmallTest;
25import android.support.test.runner.AndroidJUnit4;
26
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30import java.util.Locale;
31
32@SmallTest
33@RunWith(AndroidJUnit4.class)
34public class TextSelectionTest {
Tony Mak916bb9e2018-11-08 20:45:20 +000035 private static final String BUNDLE_KEY = "key";
36 private static final String BUNDLE_VALUE = "value";
37 private static final Bundle BUNDLE = new Bundle();
38 static {
39 BUNDLE.putString(BUNDLE_KEY, BUNDLE_VALUE);
40 }
Jan Althaus0d9fbb92017-11-28 12:19:33 +010041
42 @Test
43 public void testParcel() {
44 final int startIndex = 13;
45 final int endIndex = 37;
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010046 final String id = "id";
Jan Althaus0d9fbb92017-11-28 12:19:33 +010047 final TextSelection reference = new TextSelection.Builder(startIndex, endIndex)
48 .setEntityType(TextClassifier.TYPE_ADDRESS, 0.3f)
49 .setEntityType(TextClassifier.TYPE_PHONE, 0.7f)
50 .setEntityType(TextClassifier.TYPE_URL, 0.1f)
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010051 .setId(id)
Tony Mak916bb9e2018-11-08 20:45:20 +000052 .setExtras(BUNDLE)
Jan Althaus0d9fbb92017-11-28 12:19:33 +010053 .build();
54
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080055 // Parcel and unparcel
Jan Althaus0d9fbb92017-11-28 12:19:33 +010056 final Parcel parcel = Parcel.obtain();
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080057 reference.writeToParcel(parcel, reference.describeContents());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010058 parcel.setDataPosition(0);
Abodunrinwa Tokid32906c2018-01-18 04:34:44 -080059 final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel);
Jan Althaus0d9fbb92017-11-28 12:19:33 +010060
61 assertEquals(startIndex, result.getSelectionStartIndex());
62 assertEquals(endIndex, result.getSelectionEndIndex());
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010063 assertEquals(id, result.getId());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010064
65 assertEquals(3, result.getEntityCount());
66 assertEquals(TextClassifier.TYPE_PHONE, result.getEntity(0));
67 assertEquals(TextClassifier.TYPE_ADDRESS, result.getEntity(1));
68 assertEquals(TextClassifier.TYPE_URL, result.getEntity(2));
69 assertEquals(0.7f, result.getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f);
70 assertEquals(0.3f, result.getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f);
71 assertEquals(0.1f, result.getConfidenceScore(TextClassifier.TYPE_URL), 1e-7f);
Tony Mak916bb9e2018-11-08 20:45:20 +000072 assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY));
Jan Althaus0d9fbb92017-11-28 12:19:33 +010073 }
74
75 @Test
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010076 public void testParcelRequest() {
77 final String text = "text";
78 final TextSelection.Request reference =
79 new TextSelection.Request.Builder(text, 0, text.length())
80 .setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY))
Tony Mak916bb9e2018-11-08 20:45:20 +000081 .setExtras(BUNDLE)
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010082 .build();
Jan Althaus0d9fbb92017-11-28 12:19:33 +010083
84 // Parcel and unparcel.
85 final Parcel parcel = Parcel.obtain();
86 reference.writeToParcel(parcel, reference.describeContents());
87 parcel.setDataPosition(0);
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010088 final TextSelection.Request result = TextSelection.Request.CREATOR.createFromParcel(parcel);
Jan Althaus0d9fbb92017-11-28 12:19:33 +010089
Abodunrinwa Toki080c8542018-03-27 00:04:06 +010090 assertEquals(text, result.getText());
91 assertEquals(0, result.getStartIndex());
92 assertEquals(text.length(), result.getEndIndex());
Jan Althaus0d9fbb92017-11-28 12:19:33 +010093 assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags());
Tony Mak916bb9e2018-11-08 20:45:20 +000094 assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY));
Jan Althaus0d9fbb92017-11-28 12:19:33 +010095 }
96}