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;
25import android.util.ArrayMap;
26
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30import java.util.ArrayList;
31import java.util.Arrays;
32import java.util.Collections;
33import java.util.HashSet;
34import java.util.List;
35import java.util.Locale;
36import java.util.Map;
37
38@SmallTest
39@RunWith(AndroidJUnit4.class)
40public class TextLinksTest {
41
42    private Map<String, Float> getEntityScores(float address, float phone, float other) {
43        final Map<String, Float> result = new ArrayMap<>();
44        if (address > 0.f) {
45            result.put(TextClassifier.TYPE_ADDRESS, address);
46        }
47        if (phone > 0.f) {
48            result.put(TextClassifier.TYPE_PHONE, phone);
49        }
50        if (other > 0.f) {
51            result.put(TextClassifier.TYPE_OTHER, other);
52        }
53        return result;
54    }
55
56    @Test
57    public void testParcel() {
58        final String fullText = "this is just a test";
59        final TextLinks reference = new TextLinks.Builder(fullText)
60                .addLink(0, 4, getEntityScores(0.f, 0.f, 1.f))
61                .addLink(5, 12, getEntityScores(.8f, .1f, .5f))
62                .build();
63
64        // Parcel and unparcel.
65        final Parcel parcel = Parcel.obtain();
66        reference.writeToParcel(parcel, reference.describeContents());
67        parcel.setDataPosition(0);
68        final TextLinks result = TextLinks.CREATOR.createFromParcel(parcel);
69        final List<TextLinks.TextLink> resultList = new ArrayList<>(result.getLinks());
70
71        assertEquals(2, resultList.size());
72        assertEquals(0, resultList.get(0).getStart());
73        assertEquals(4, resultList.get(0).getEnd());
74        assertEquals(1, resultList.get(0).getEntityCount());
75        assertEquals(TextClassifier.TYPE_OTHER, resultList.get(0).getEntity(0));
76        assertEquals(1.f, resultList.get(0).getConfidenceScore(TextClassifier.TYPE_OTHER), 1e-7f);
77        assertEquals(5, resultList.get(1).getStart());
78        assertEquals(12, resultList.get(1).getEnd());
79        assertEquals(3, resultList.get(1).getEntityCount());
80        assertEquals(TextClassifier.TYPE_ADDRESS, resultList.get(1).getEntity(0));
81        assertEquals(TextClassifier.TYPE_OTHER, resultList.get(1).getEntity(1));
82        assertEquals(TextClassifier.TYPE_PHONE, resultList.get(1).getEntity(2));
83        assertEquals(.8f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f);
84        assertEquals(.5f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_OTHER), 1e-7f);
85        assertEquals(.1f, resultList.get(1).getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f);
86    }
87
88    @Test
89    public void testParcelOptions() {
90        final TextClassifier.EntityConfig entityConfig = TextClassifier.EntityConfig.create(
91                Arrays.asList(TextClassifier.HINT_TEXT_IS_EDITABLE),
92                Arrays.asList("a", "b", "c"),
93                Arrays.asList("b"));
94        final TextLinks.Request reference = new TextLinks.Request.Builder("text")
95                .setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY))
96                .setEntityConfig(entityConfig)
97                .build();
98
99        // Parcel and unparcel.
100        final Parcel parcel = Parcel.obtain();
101        reference.writeToParcel(parcel, reference.describeContents());
102        parcel.setDataPosition(0);
103        final TextLinks.Request result = TextLinks.Request.CREATOR.createFromParcel(parcel);
104
105        assertEquals("text", result.getText());
106        assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags());
107        assertEquals(new String[]{TextClassifier.HINT_TEXT_IS_EDITABLE},
108                result.getEntityConfig().getHints().toArray());
109        assertEquals(new HashSet<String>(Arrays.asList("a", "c")),
110                result.getEntityConfig().resolveEntityListModifications(Collections.emptyList()));
111    }
112}
113