Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 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 | |
| 17 | package android.view.textclassifier; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 20 | |
| 21 | import android.os.LocaleList; |
| 22 | import android.os.Parcel; |
| 23 | import android.support.test.filters.SmallTest; |
| 24 | import android.support.test.runner.AndroidJUnit4; |
| 25 | |
| 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | |
| 29 | import java.util.Locale; |
| 30 | |
| 31 | @SmallTest |
| 32 | @RunWith(AndroidJUnit4.class) |
| 33 | public class TextSelectionTest { |
| 34 | |
| 35 | @Test |
| 36 | public void testParcel() { |
| 37 | final int startIndex = 13; |
| 38 | final int endIndex = 37; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 39 | final String id = "id"; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 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) |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 44 | .setId(id) |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 45 | .build(); |
| 46 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 47 | // Parcel and unparcel |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 48 | final Parcel parcel = Parcel.obtain(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 49 | reference.writeToParcel(parcel, reference.describeContents()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 50 | parcel.setDataPosition(0); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 51 | final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 52 | |
| 53 | assertEquals(startIndex, result.getSelectionStartIndex()); |
| 54 | assertEquals(endIndex, result.getSelectionEndIndex()); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 55 | assertEquals(id, result.getId()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 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 |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 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(); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 73 | |
| 74 | // Parcel and unparcel. |
| 75 | final Parcel parcel = Parcel.obtain(); |
| 76 | reference.writeToParcel(parcel, reference.describeContents()); |
| 77 | parcel.setDataPosition(0); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 78 | final TextSelection.Request result = TextSelection.Request.CREATOR.createFromParcel(parcel); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 79 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame^] | 80 | assertEquals(text, result.getText()); |
| 81 | assertEquals(0, result.getStartIndex()); |
| 82 | assertEquals(text.length(), result.getEndIndex()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 83 | assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 84 | } |
| 85 | } |