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 | |
Tony Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 21 | import android.os.Bundle; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 22 | import android.os.LocaleList; |
| 23 | import android.os.Parcel; |
| 24 | import android.support.test.filters.SmallTest; |
| 25 | import android.support.test.runner.AndroidJUnit4; |
| 26 | |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | |
| 30 | import java.util.Locale; |
| 31 | |
| 32 | @SmallTest |
| 33 | @RunWith(AndroidJUnit4.class) |
| 34 | public class TextSelectionTest { |
Tony Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 35 | 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 Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 41 | |
| 42 | @Test |
| 43 | public void testParcel() { |
| 44 | final int startIndex = 13; |
| 45 | final int endIndex = 37; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 46 | final String id = "id"; |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 47 | 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 Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 51 | .setId(id) |
Tony Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 52 | .setExtras(BUNDLE) |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 53 | .build(); |
| 54 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 55 | // Parcel and unparcel |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 56 | final Parcel parcel = Parcel.obtain(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 57 | reference.writeToParcel(parcel, reference.describeContents()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 58 | parcel.setDataPosition(0); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 59 | final TextSelection result = TextSelection.CREATOR.createFromParcel(parcel); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 60 | |
| 61 | assertEquals(startIndex, result.getSelectionStartIndex()); |
| 62 | assertEquals(endIndex, result.getSelectionEndIndex()); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 63 | assertEquals(id, result.getId()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 64 | |
| 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 Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 72 | assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 76 | 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 Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 81 | .setExtras(BUNDLE) |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 82 | .build(); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 83 | |
| 84 | // Parcel and unparcel. |
| 85 | final Parcel parcel = Parcel.obtain(); |
| 86 | reference.writeToParcel(parcel, reference.describeContents()); |
| 87 | parcel.setDataPosition(0); |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 88 | final TextSelection.Request result = TextSelection.Request.CREATOR.createFromParcel(parcel); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 89 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 90 | assertEquals(text, result.getText()); |
| 91 | assertEquals(0, result.getStartIndex()); |
| 92 | assertEquals(text.length(), result.getEndIndex()); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 93 | assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags()); |
Tony Mak | 916bb9e | 2018-11-08 20:45:20 +0000 | [diff] [blame] | 94 | assertEquals(BUNDLE_VALUE, result.getExtras().getString(BUNDLE_KEY)); |
Jan Althaus | 0d9fbb9 | 2017-11-28 12:19:33 +0100 | [diff] [blame] | 95 | } |
| 96 | } |