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; |
| 20 | |
| 21 | import android.content.Intent; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Color; |
| 24 | import android.graphics.drawable.BitmapDrawable; |
| 25 | import android.graphics.drawable.ColorDrawable; |
| 26 | import android.os.LocaleList; |
| 27 | import android.os.Parcel; |
| 28 | import android.support.test.filters.SmallTest; |
| 29 | import android.support.test.runner.AndroidJUnit4; |
| 30 | import android.view.View; |
| 31 | |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
| 35 | import java.util.Locale; |
| 36 | |
| 37 | @SmallTest |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | public class TextClassificationTest { |
| 40 | |
| 41 | public BitmapDrawable generateTestDrawable(int width, int height, int colorValue) { |
| 42 | final int numPixels = width * height; |
| 43 | final int[] colors = new int[numPixels]; |
| 44 | for (int i = 0; i < numPixels; ++i) { |
| 45 | colors[i] = colorValue; |
| 46 | } |
| 47 | final Bitmap bitmap = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); |
| 48 | final BitmapDrawable drawable = new BitmapDrawable(null, bitmap); |
| 49 | drawable.setTargetDensity(bitmap.getDensity()); |
| 50 | return drawable; |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testParcel() { |
| 55 | final String text = "text"; |
| 56 | final BitmapDrawable primaryIcon = generateTestDrawable(16, 16, Color.RED); |
| 57 | final String primaryLabel = "primarylabel"; |
| 58 | final Intent primaryIntent = new Intent("primaryintentaction"); |
| 59 | final View.OnClickListener primaryOnClick = v -> { }; |
| 60 | final BitmapDrawable secondaryIcon0 = generateTestDrawable(32, 288, Color.GREEN); |
| 61 | final String secondaryLabel0 = "secondarylabel0"; |
| 62 | final Intent secondaryIntent0 = new Intent("secondaryintentaction0"); |
| 63 | final BitmapDrawable secondaryIcon1 = generateTestDrawable(576, 288, Color.BLUE); |
| 64 | final String secondaryLabel1 = "secondaryLabel1"; |
| 65 | final Intent secondaryIntent1 = null; |
| 66 | final BitmapDrawable secondaryIcon2 = null; |
| 67 | final String secondaryLabel2 = null; |
| 68 | final Intent secondaryIntent2 = new Intent("secondaryintentaction2"); |
| 69 | final ColorDrawable secondaryIcon3 = new ColorDrawable(Color.CYAN); |
| 70 | final String secondaryLabel3 = null; |
| 71 | final Intent secondaryIntent3 = null; |
| 72 | final String signature = "signature"; |
| 73 | final TextClassification reference = new TextClassification.Builder() |
| 74 | .setText(text) |
| 75 | .setPrimaryAction(primaryIntent, primaryLabel, primaryIcon) |
| 76 | .setOnClickListener(primaryOnClick) |
| 77 | .addSecondaryAction(null, null, null) // ignored |
| 78 | .addSecondaryAction(secondaryIntent0, secondaryLabel0, secondaryIcon0) |
| 79 | .addSecondaryAction(secondaryIntent1, secondaryLabel1, secondaryIcon1) |
| 80 | .addSecondaryAction(secondaryIntent2, secondaryLabel2, secondaryIcon2) |
| 81 | .addSecondaryAction(secondaryIntent3, secondaryLabel3, secondaryIcon3) |
| 82 | .setEntityType(TextClassifier.TYPE_ADDRESS, 0.3f) |
| 83 | .setEntityType(TextClassifier.TYPE_PHONE, 0.7f) |
| 84 | .setSignature(signature) |
| 85 | .build(); |
| 86 | |
| 87 | // Parcel and unparcel using ParcelableWrapper. |
| 88 | final TextClassification.ParcelableWrapper parcelableReference = new TextClassification |
| 89 | .ParcelableWrapper(reference); |
| 90 | final Parcel parcel = Parcel.obtain(); |
| 91 | parcelableReference.writeToParcel(parcel, parcelableReference.describeContents()); |
| 92 | parcel.setDataPosition(0); |
| 93 | final TextClassification result = |
| 94 | TextClassification.ParcelableWrapper.CREATOR.createFromParcel( |
| 95 | parcel).getTextClassification(); |
| 96 | |
| 97 | assertEquals(text, result.getText()); |
| 98 | assertEquals(signature, result.getSignature()); |
| 99 | assertEquals(4, result.getSecondaryActionsCount()); |
| 100 | |
| 101 | // Primary action (re-use existing icon). |
| 102 | final Bitmap resPrimaryIcon = ((BitmapDrawable) result.getIcon()).getBitmap(); |
| 103 | assertEquals(primaryIcon.getBitmap().getPixel(0, 0), resPrimaryIcon.getPixel(0, 0)); |
| 104 | assertEquals(16, resPrimaryIcon.getWidth()); |
| 105 | assertEquals(16, resPrimaryIcon.getHeight()); |
| 106 | assertEquals(primaryLabel, result.getLabel()); |
| 107 | assertEquals(primaryIntent.getAction(), result.getIntent().getAction()); |
| 108 | assertEquals(null, result.getOnClickListener()); // Non-parcelable. |
| 109 | |
| 110 | // Secondary action 0 (scale with height limit). |
| 111 | final Bitmap resSecondaryIcon0 = ((BitmapDrawable) result.getSecondaryIcon(0)).getBitmap(); |
| 112 | assertEquals(secondaryIcon0.getBitmap().getPixel(0, 0), resSecondaryIcon0.getPixel(0, 0)); |
| 113 | assertEquals(16, resSecondaryIcon0.getWidth()); |
| 114 | assertEquals(144, resSecondaryIcon0.getHeight()); |
| 115 | assertEquals(secondaryLabel0, result.getSecondaryLabel(0)); |
| 116 | assertEquals(secondaryIntent0.getAction(), result.getSecondaryIntent(0).getAction()); |
| 117 | |
| 118 | // Secondary action 1 (scale with width limit). |
| 119 | final Bitmap resSecondaryIcon1 = ((BitmapDrawable) result.getSecondaryIcon(1)).getBitmap(); |
| 120 | assertEquals(secondaryIcon1.getBitmap().getPixel(0, 0), resSecondaryIcon1.getPixel(0, 0)); |
| 121 | assertEquals(144, resSecondaryIcon1.getWidth()); |
| 122 | assertEquals(72, resSecondaryIcon1.getHeight()); |
| 123 | assertEquals(secondaryLabel1, result.getSecondaryLabel(1)); |
| 124 | assertEquals(null, result.getSecondaryIntent(1)); |
| 125 | |
| 126 | // Secondary action 2 (no icon). |
| 127 | assertEquals(null, result.getSecondaryIcon(2)); |
| 128 | assertEquals(null, result.getSecondaryLabel(2)); |
| 129 | assertEquals(secondaryIntent2.getAction(), result.getSecondaryIntent(2).getAction()); |
| 130 | |
| 131 | // Secondary action 3 (convert non-bitmap drawable with negative size). |
| 132 | final Bitmap resSecondaryIcon3 = ((BitmapDrawable) result.getSecondaryIcon(3)).getBitmap(); |
| 133 | assertEquals(secondaryIcon3.getColor(), resSecondaryIcon3.getPixel(0, 0)); |
| 134 | assertEquals(1, resSecondaryIcon3.getWidth()); |
| 135 | assertEquals(1, resSecondaryIcon3.getHeight()); |
| 136 | assertEquals(null, result.getSecondaryLabel(3)); |
| 137 | assertEquals(null, result.getSecondaryIntent(3)); |
| 138 | |
| 139 | // Entities. |
| 140 | assertEquals(2, result.getEntityCount()); |
| 141 | assertEquals(TextClassifier.TYPE_PHONE, result.getEntity(0)); |
| 142 | assertEquals(TextClassifier.TYPE_ADDRESS, result.getEntity(1)); |
| 143 | assertEquals(0.7f, result.getConfidenceScore(TextClassifier.TYPE_PHONE), 1e-7f); |
| 144 | assertEquals(0.3f, result.getConfidenceScore(TextClassifier.TYPE_ADDRESS), 1e-7f); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void testParcelOptions() { |
| 149 | TextClassification.Options reference = new TextClassification.Options(); |
| 150 | reference.setDefaultLocales(new LocaleList(Locale.US, Locale.GERMANY)); |
| 151 | |
| 152 | // Parcel and unparcel. |
| 153 | final Parcel parcel = Parcel.obtain(); |
| 154 | reference.writeToParcel(parcel, reference.describeContents()); |
| 155 | parcel.setDataPosition(0); |
| 156 | TextClassification.Options result = TextClassification.Options.CREATOR.createFromParcel( |
| 157 | parcel); |
| 158 | |
| 159 | assertEquals("en-US,de-DE", result.getDefaultLocales().toLanguageTags()); |
| 160 | } |
| 161 | } |