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