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 android.view.textclassifier;
18
19import static org.junit.Assert.assertEquals;
20
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;
39        final String id = "id";
40        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)
44                .setId(id)
45                .build();
46
47        // Parcel and unparcel
48        final Parcel parcel = Parcel.obtain();
49        reference.writeToParcel(parcel, reference.describeContents());
50        parcel.setDataPosition(0);
51        final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel);
52
53        assertEquals(startIndex, result.getSelectionStartIndex());
54        assertEquals(endIndex, result.getSelectionEndIndex());
55        assertEquals(id, result.getId());
56
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
67    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();
73
74        // Parcel and unparcel.
75        final Parcel parcel = Parcel.obtain();
76        reference.writeToParcel(parcel, reference.describeContents());
77        parcel.setDataPosition(0);
78        final TextSelection.Request result = TextSelection.Request.CREATOR.createFromParcel(parcel);
79
80        assertEquals(text, result.getText());
81        assertEquals(0, result.getStartIndex());
82        assertEquals(text.length(), result.getEndIndex());
83        assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags());
84    }
85}
86