Loading core/java/android/view/textclassifier/ExtrasUtils.java +12 −3 Original line number Original line Diff line number Diff line Loading @@ -85,15 +85,16 @@ public final class ExtrasUtils { } } /** /** * Returns the first "translate" action found in the {@code classification} object. * Returns the first action found in the {@code classification} object with an intent * action string, {@code intentAction}. */ */ @Nullable @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { public static RemoteAction findAction(TextClassification classification, String intentAction) { final ArrayList<Intent> actionIntents = getActionsIntents(classification); final ArrayList<Intent> actionIntents = getActionsIntents(classification); if (actionIntents != null) { if (actionIntents != null) { final int size = actionIntents.size(); final int size = actionIntents.size(); for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) { if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) { if (intentAction.equals(actionIntents.get(i).getAction())) { return classification.getActions().get(i); return classification.getActions().get(i); } } } } Loading @@ -101,6 +102,14 @@ public final class ExtrasUtils { return null; return null; } } /** * Returns the first "translate" action found in the {@code classification} object. */ @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { return findAction(classification, Intent.ACTION_TRANSLATE); } /** /** * Returns the entity type contained in the {@code extra}. * Returns the entity type contained in the {@code extra}. */ */ Loading core/java/android/view/textclassifier/LegacyIntentFactory.java +2 −1 Original line number Original line Diff line number Diff line Loading @@ -182,7 +182,8 @@ public final class LegacyIntentFactory implements IntentFactory { actions.add(new LabeledIntent( actions.add(new LabeledIntent( context.getString(com.android.internal.R.string.browse), context.getString(com.android.internal.R.string.browse), context.getString(com.android.internal.R.string.browse_desc), context.getString(com.android.internal.R.string.browse_desc), new Intent(Intent.ACTION_VIEW, Uri.parse(text)) new Intent(Intent.ACTION_VIEW) .setDataAndNormalize(Uri.parse(text)) .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()), .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()), LabeledIntent.DEFAULT_REQUEST_CODE)); LabeledIntent.DEFAULT_REQUEST_CODE)); return actions; return actions; Loading core/java/android/view/textclassifier/TemplateIntentFactory.java +43 −28 Original line number Original line Diff line number Diff line Loading @@ -49,20 +49,18 @@ public final class TemplateIntentFactory { } } final List<TextClassifierImpl.LabeledIntent> labeledIntents = new ArrayList<>(); final List<TextClassifierImpl.LabeledIntent> labeledIntents = new ArrayList<>(); for (RemoteActionTemplate remoteActionTemplate : remoteActionTemplates) { for (RemoteActionTemplate remoteActionTemplate : remoteActionTemplates) { Intent intent = createIntent(remoteActionTemplate); if (!isValidTemplate(remoteActionTemplate)) { if (intent == null) { Log.w(TAG, "Invalid RemoteActionTemplate skipped."); continue; continue; } } TextClassifierImpl.LabeledIntent labeledIntents.add( labeledIntent = new TextClassifierImpl.LabeledIntent( new TextClassifierImpl.LabeledIntent( remoteActionTemplate.title, remoteActionTemplate.title, remoteActionTemplate.description, remoteActionTemplate.description, intent, createIntent(remoteActionTemplate), remoteActionTemplate.requestCode == null remoteActionTemplate.requestCode == null ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE : remoteActionTemplate.requestCode : remoteActionTemplate.requestCode)); ); labeledIntents.add(labeledIntent); } } labeledIntents.forEach( labeledIntents.forEach( action -> action.getIntent() action -> action.getIntent() Loading @@ -70,31 +68,45 @@ public final class TemplateIntentFactory { return labeledIntents; return labeledIntents; } } @Nullable private static boolean isValidTemplate(@Nullable RemoteActionTemplate remoteActionTemplate) { private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) { if (remoteActionTemplate == null) { Intent intent = new Intent(); Log.w(TAG, "Invalid RemoteActionTemplate: is null"); if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) { return false; Log.w(TAG, "A RemoteActionTemplate is skipped as package name is set."); return null; } } if (!TextUtils.isEmpty(remoteActionTemplate.action)) { if (TextUtils.isEmpty(remoteActionTemplate.title)) { intent.setAction(remoteActionTemplate.action); Log.w(TAG, "Invalid RemoteActionTemplate: title is null"); return false; } } Uri data = null; if (TextUtils.isEmpty(remoteActionTemplate.description)) { if (!TextUtils.isEmpty(remoteActionTemplate.data)) { Log.w(TAG, "Invalid RemoteActionTemplate: description is null"); data = Uri.parse(remoteActionTemplate.data); return false; } } if (data != null || !TextUtils.isEmpty(remoteActionTemplate.type)) { if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) { intent.setDataAndType(data, remoteActionTemplate.type); Log.w(TAG, "Invalid RemoteActionTemplate: package name is set"); return false; } } if (remoteActionTemplate.flags != null) { if (TextUtils.isEmpty(remoteActionTemplate.action)) { intent.setFlags(remoteActionTemplate.flags); Log.w(TAG, "Invalid RemoteActionTemplate: intent action not set"); return false; } } return true; } private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) { final Intent intent = new Intent(remoteActionTemplate.action); final Uri uri = TextUtils.isEmpty(remoteActionTemplate.data) ? null : Uri.parse(remoteActionTemplate.data).normalizeScheme(); final String type = TextUtils.isEmpty(remoteActionTemplate.type) ? null : Intent.normalizeMimeType(remoteActionTemplate.type); intent.setDataAndType(uri, type); intent.setFlags(remoteActionTemplate.flags == null ? 0 : remoteActionTemplate.flags); if (remoteActionTemplate.category != null) { if (remoteActionTemplate.category != null) { for (String category : remoteActionTemplate.category) { for (String category : remoteActionTemplate.category) { if (category != null) { intent.addCategory(category); intent.addCategory(category); } } } } } intent.putExtras(createExtras(remoteActionTemplate.extras)); intent.putExtras(createExtras(remoteActionTemplate.extras)); return intent; return intent; } } Loading @@ -105,6 +117,9 @@ public final class TemplateIntentFactory { } } Bundle bundle = new Bundle(); Bundle bundle = new Bundle(); for (NamedVariant namedVariant : namedVariants) { for (NamedVariant namedVariant : namedVariants) { if (namedVariant == null) { continue; } switch (namedVariant.getType()) { switch (namedVariant.getType()) { case NamedVariant.TYPE_INT: case NamedVariant.TYPE_INT: bundle.putInt(namedVariant.getName(), namedVariant.getInt()); bundle.putInt(namedVariant.getName(), namedVariant.getInt()); Loading core/tests/coretests/src/android/view/textclassifier/TemplateClassificationIntentFactoryTest.java +20 −28 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ public class TemplateClassificationIntentFactoryTest { private static final String TEXT = "text"; private static final String TEXT = "text"; private static final String TITLE = "Map"; private static final String TITLE = "Map"; private static final String DESCRIPTION = "Opens in Maps"; private static final String ACTION = Intent.ACTION_VIEW; private static final String ACTION = Intent.ACTION_VIEW; @Mock @Mock Loading @@ -57,19 +58,6 @@ public class TemplateClassificationIntentFactoryTest { @Test @Test public void create_foreignText() { public void create_foreignText() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); AnnotatorModel.ClassificationResult classificationResult = AnnotatorModel.ClassificationResult classificationResult = new AnnotatorModel.ClassificationResult( new AnnotatorModel.ClassificationResult( TextClassifier.TYPE_ADDRESS, TextClassifier.TYPE_ADDRESS, Loading @@ -81,7 +69,7 @@ public class TemplateClassificationIntentFactoryTest { null, null, null, null, null, null, new RemoteActionTemplate[]{remoteActionTemplate}); createRemoteActionTemplates()); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateClassificationIntentFactory.create( mTemplateClassificationIntentFactory.create( Loading @@ -106,19 +94,6 @@ public class TemplateClassificationIntentFactoryTest { @Test @Test public void create_notForeignText() { public void create_notForeignText() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); AnnotatorModel.ClassificationResult classificationResult = AnnotatorModel.ClassificationResult classificationResult = new AnnotatorModel.ClassificationResult( new AnnotatorModel.ClassificationResult( TextClassifier.TYPE_ADDRESS, TextClassifier.TYPE_ADDRESS, Loading @@ -130,7 +105,7 @@ public class TemplateClassificationIntentFactoryTest { null, null, null, null, null, null, new RemoteActionTemplate[]{remoteActionTemplate}); createRemoteActionTemplates()); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateClassificationIntentFactory.create( mTemplateClassificationIntentFactory.create( Loading @@ -147,4 +122,21 @@ public class TemplateClassificationIntentFactoryTest { assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } private static RemoteActionTemplate[] createRemoteActionTemplates() { return new RemoteActionTemplate[]{ new RemoteActionTemplate( TITLE, DESCRIPTION, ACTION, null, null, null, null, null, null, null ) }; } } } core/tests/coretests/src/android/view/textclassifier/TemplateIntentFactoryTest.java +106 −14 Original line number Original line Diff line number Diff line Loading @@ -81,7 +81,6 @@ public class TemplateIntentFactoryTest { REQUEST_CODE REQUEST_CODE ); ); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); Loading @@ -97,23 +96,22 @@ public class TemplateIntentFactoryTest { assertThat(intent.getFlags()).isEqualTo(FLAG); assertThat(intent.getFlags()).isEqualTo(FLAG); assertThat(intent.getCategories()).containsExactly((Object[]) CATEGORY); assertThat(intent.getCategories()).containsExactly((Object[]) CATEGORY); assertThat(intent.getPackage()).isNull(); assertThat(intent.getPackage()).isNull(); assertThat( assertThat(intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE); intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE); assertThat(intent.getIntExtra(KEY_TWO, 0)).isEqualTo(VALUE_TWO); assertThat(intent.getIntExtra(KEY_TWO, 0)).isEqualTo(VALUE_TWO); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } @Test @Test public void create_packageIsNotNull() { public void normalizesScheme() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, TITLE, DESCRIPTION, DESCRIPTION, ACTION, ACTION, DATA, "HTTp://www.android.com", TYPE, TYPE, FLAG, FLAG, CATEGORY, CATEGORY, PACKAGE_NAME, /* packageName */ null, NAMED_VARIANTS, NAMED_VARIANTS, REQUEST_CODE REQUEST_CODE ); ); Loading @@ -121,15 +119,16 @@ public class TemplateIntentFactoryTest { List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).hasSize(0); String data = intents.get(0).getIntent().getData().toString(); assertThat(data).isEqualTo("http://www.android.com"); } } @Test @Test public void create_minimal() { public void create_minimal() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( null, TITLE, null, DESCRIPTION, null, ACTION, null, null, null, null, null, null, Loading @@ -142,15 +141,14 @@ public class TemplateIntentFactoryTest { List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); assertThat(intents).hasSize(1); assertThat(intents).hasSize(1); TextClassifierImpl.LabeledIntent labeledIntent = intents.get(0); TextClassifierImpl.LabeledIntent labeledIntent = intents.get(0); assertThat(labeledIntent.getTitle()).isNull(); assertThat(labeledIntent.getTitle()).isEqualTo(TITLE); assertThat(labeledIntent.getDescription()).isNull(); assertThat(labeledIntent.getDescription()).isEqualTo(DESCRIPTION); assertThat(labeledIntent.getRequestCode()).isEqualTo( assertThat(labeledIntent.getRequestCode()).isEqualTo( TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE); TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE); Intent intent = labeledIntent.getIntent(); Intent intent = labeledIntent.getIntent(); assertThat(intent.getAction()).isNull(); assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.getData()).isNull(); assertThat(intent.getData()).isNull(); assertThat(intent.getType()).isNull(); assertThat(intent.getType()).isNull(); assertThat(intent.getFlags()).isEqualTo(0); assertThat(intent.getFlags()).isEqualTo(0); Loading @@ -158,4 +156,98 @@ public class TemplateIntentFactoryTest { assertThat(intent.getPackage()).isNull(); assertThat(intent.getPackage()).isNull(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } @Test public void invalidTemplate_nullTemplate() { RemoteActionTemplate remoteActionTemplate = null; List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_nonEmptyPackageName() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, DESCRIPTION, ACTION, DATA, TYPE, FLAG, CATEGORY, PACKAGE_NAME, NAMED_VARIANTS, REQUEST_CODE ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyTitle() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( null, DESCRIPTION, ACTION, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyDescription() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyIntentAction() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, DESCRIPTION, null, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } } } Loading
core/java/android/view/textclassifier/ExtrasUtils.java +12 −3 Original line number Original line Diff line number Diff line Loading @@ -85,15 +85,16 @@ public final class ExtrasUtils { } } /** /** * Returns the first "translate" action found in the {@code classification} object. * Returns the first action found in the {@code classification} object with an intent * action string, {@code intentAction}. */ */ @Nullable @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { public static RemoteAction findAction(TextClassification classification, String intentAction) { final ArrayList<Intent> actionIntents = getActionsIntents(classification); final ArrayList<Intent> actionIntents = getActionsIntents(classification); if (actionIntents != null) { if (actionIntents != null) { final int size = actionIntents.size(); final int size = actionIntents.size(); for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) { if (Intent.ACTION_TRANSLATE.equals(actionIntents.get(i).getAction())) { if (intentAction.equals(actionIntents.get(i).getAction())) { return classification.getActions().get(i); return classification.getActions().get(i); } } } } Loading @@ -101,6 +102,14 @@ public final class ExtrasUtils { return null; return null; } } /** * Returns the first "translate" action found in the {@code classification} object. */ @Nullable public static RemoteAction findTranslateAction(TextClassification classification) { return findAction(classification, Intent.ACTION_TRANSLATE); } /** /** * Returns the entity type contained in the {@code extra}. * Returns the entity type contained in the {@code extra}. */ */ Loading
core/java/android/view/textclassifier/LegacyIntentFactory.java +2 −1 Original line number Original line Diff line number Diff line Loading @@ -182,7 +182,8 @@ public final class LegacyIntentFactory implements IntentFactory { actions.add(new LabeledIntent( actions.add(new LabeledIntent( context.getString(com.android.internal.R.string.browse), context.getString(com.android.internal.R.string.browse), context.getString(com.android.internal.R.string.browse_desc), context.getString(com.android.internal.R.string.browse_desc), new Intent(Intent.ACTION_VIEW, Uri.parse(text)) new Intent(Intent.ACTION_VIEW) .setDataAndNormalize(Uri.parse(text)) .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()), .putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()), LabeledIntent.DEFAULT_REQUEST_CODE)); LabeledIntent.DEFAULT_REQUEST_CODE)); return actions; return actions; Loading
core/java/android/view/textclassifier/TemplateIntentFactory.java +43 −28 Original line number Original line Diff line number Diff line Loading @@ -49,20 +49,18 @@ public final class TemplateIntentFactory { } } final List<TextClassifierImpl.LabeledIntent> labeledIntents = new ArrayList<>(); final List<TextClassifierImpl.LabeledIntent> labeledIntents = new ArrayList<>(); for (RemoteActionTemplate remoteActionTemplate : remoteActionTemplates) { for (RemoteActionTemplate remoteActionTemplate : remoteActionTemplates) { Intent intent = createIntent(remoteActionTemplate); if (!isValidTemplate(remoteActionTemplate)) { if (intent == null) { Log.w(TAG, "Invalid RemoteActionTemplate skipped."); continue; continue; } } TextClassifierImpl.LabeledIntent labeledIntents.add( labeledIntent = new TextClassifierImpl.LabeledIntent( new TextClassifierImpl.LabeledIntent( remoteActionTemplate.title, remoteActionTemplate.title, remoteActionTemplate.description, remoteActionTemplate.description, intent, createIntent(remoteActionTemplate), remoteActionTemplate.requestCode == null remoteActionTemplate.requestCode == null ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE ? TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE : remoteActionTemplate.requestCode : remoteActionTemplate.requestCode)); ); labeledIntents.add(labeledIntent); } } labeledIntents.forEach( labeledIntents.forEach( action -> action.getIntent() action -> action.getIntent() Loading @@ -70,31 +68,45 @@ public final class TemplateIntentFactory { return labeledIntents; return labeledIntents; } } @Nullable private static boolean isValidTemplate(@Nullable RemoteActionTemplate remoteActionTemplate) { private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) { if (remoteActionTemplate == null) { Intent intent = new Intent(); Log.w(TAG, "Invalid RemoteActionTemplate: is null"); if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) { return false; Log.w(TAG, "A RemoteActionTemplate is skipped as package name is set."); return null; } } if (!TextUtils.isEmpty(remoteActionTemplate.action)) { if (TextUtils.isEmpty(remoteActionTemplate.title)) { intent.setAction(remoteActionTemplate.action); Log.w(TAG, "Invalid RemoteActionTemplate: title is null"); return false; } } Uri data = null; if (TextUtils.isEmpty(remoteActionTemplate.description)) { if (!TextUtils.isEmpty(remoteActionTemplate.data)) { Log.w(TAG, "Invalid RemoteActionTemplate: description is null"); data = Uri.parse(remoteActionTemplate.data); return false; } } if (data != null || !TextUtils.isEmpty(remoteActionTemplate.type)) { if (!TextUtils.isEmpty(remoteActionTemplate.packageName)) { intent.setDataAndType(data, remoteActionTemplate.type); Log.w(TAG, "Invalid RemoteActionTemplate: package name is set"); return false; } } if (remoteActionTemplate.flags != null) { if (TextUtils.isEmpty(remoteActionTemplate.action)) { intent.setFlags(remoteActionTemplate.flags); Log.w(TAG, "Invalid RemoteActionTemplate: intent action not set"); return false; } } return true; } private static Intent createIntent(RemoteActionTemplate remoteActionTemplate) { final Intent intent = new Intent(remoteActionTemplate.action); final Uri uri = TextUtils.isEmpty(remoteActionTemplate.data) ? null : Uri.parse(remoteActionTemplate.data).normalizeScheme(); final String type = TextUtils.isEmpty(remoteActionTemplate.type) ? null : Intent.normalizeMimeType(remoteActionTemplate.type); intent.setDataAndType(uri, type); intent.setFlags(remoteActionTemplate.flags == null ? 0 : remoteActionTemplate.flags); if (remoteActionTemplate.category != null) { if (remoteActionTemplate.category != null) { for (String category : remoteActionTemplate.category) { for (String category : remoteActionTemplate.category) { if (category != null) { intent.addCategory(category); intent.addCategory(category); } } } } } intent.putExtras(createExtras(remoteActionTemplate.extras)); intent.putExtras(createExtras(remoteActionTemplate.extras)); return intent; return intent; } } Loading @@ -105,6 +117,9 @@ public final class TemplateIntentFactory { } } Bundle bundle = new Bundle(); Bundle bundle = new Bundle(); for (NamedVariant namedVariant : namedVariants) { for (NamedVariant namedVariant : namedVariants) { if (namedVariant == null) { continue; } switch (namedVariant.getType()) { switch (namedVariant.getType()) { case NamedVariant.TYPE_INT: case NamedVariant.TYPE_INT: bundle.putInt(namedVariant.getName(), namedVariant.getInt()); bundle.putInt(namedVariant.getName(), namedVariant.getInt()); Loading
core/tests/coretests/src/android/view/textclassifier/TemplateClassificationIntentFactoryTest.java +20 −28 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ public class TemplateClassificationIntentFactoryTest { private static final String TEXT = "text"; private static final String TEXT = "text"; private static final String TITLE = "Map"; private static final String TITLE = "Map"; private static final String DESCRIPTION = "Opens in Maps"; private static final String ACTION = Intent.ACTION_VIEW; private static final String ACTION = Intent.ACTION_VIEW; @Mock @Mock Loading @@ -57,19 +58,6 @@ public class TemplateClassificationIntentFactoryTest { @Test @Test public void create_foreignText() { public void create_foreignText() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); AnnotatorModel.ClassificationResult classificationResult = AnnotatorModel.ClassificationResult classificationResult = new AnnotatorModel.ClassificationResult( new AnnotatorModel.ClassificationResult( TextClassifier.TYPE_ADDRESS, TextClassifier.TYPE_ADDRESS, Loading @@ -81,7 +69,7 @@ public class TemplateClassificationIntentFactoryTest { null, null, null, null, null, null, new RemoteActionTemplate[]{remoteActionTemplate}); createRemoteActionTemplates()); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateClassificationIntentFactory.create( mTemplateClassificationIntentFactory.create( Loading @@ -106,19 +94,6 @@ public class TemplateClassificationIntentFactoryTest { @Test @Test public void create_notForeignText() { public void create_notForeignText() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); AnnotatorModel.ClassificationResult classificationResult = AnnotatorModel.ClassificationResult classificationResult = new AnnotatorModel.ClassificationResult( new AnnotatorModel.ClassificationResult( TextClassifier.TYPE_ADDRESS, TextClassifier.TYPE_ADDRESS, Loading @@ -130,7 +105,7 @@ public class TemplateClassificationIntentFactoryTest { null, null, null, null, null, null, new RemoteActionTemplate[]{remoteActionTemplate}); createRemoteActionTemplates()); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateClassificationIntentFactory.create( mTemplateClassificationIntentFactory.create( Loading @@ -147,4 +122,21 @@ public class TemplateClassificationIntentFactoryTest { assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } private static RemoteActionTemplate[] createRemoteActionTemplates() { return new RemoteActionTemplate[]{ new RemoteActionTemplate( TITLE, DESCRIPTION, ACTION, null, null, null, null, null, null, null ) }; } } }
core/tests/coretests/src/android/view/textclassifier/TemplateIntentFactoryTest.java +106 −14 Original line number Original line Diff line number Diff line Loading @@ -81,7 +81,6 @@ public class TemplateIntentFactoryTest { REQUEST_CODE REQUEST_CODE ); ); List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); Loading @@ -97,23 +96,22 @@ public class TemplateIntentFactoryTest { assertThat(intent.getFlags()).isEqualTo(FLAG); assertThat(intent.getFlags()).isEqualTo(FLAG); assertThat(intent.getCategories()).containsExactly((Object[]) CATEGORY); assertThat(intent.getCategories()).containsExactly((Object[]) CATEGORY); assertThat(intent.getPackage()).isNull(); assertThat(intent.getPackage()).isNull(); assertThat( assertThat(intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE); intent.getStringExtra(KEY_ONE)).isEqualTo(VALUE_ONE); assertThat(intent.getIntExtra(KEY_TWO, 0)).isEqualTo(VALUE_TWO); assertThat(intent.getIntExtra(KEY_TWO, 0)).isEqualTo(VALUE_TWO); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } @Test @Test public void create_packageIsNotNull() { public void normalizesScheme() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, TITLE, DESCRIPTION, DESCRIPTION, ACTION, ACTION, DATA, "HTTp://www.android.com", TYPE, TYPE, FLAG, FLAG, CATEGORY, CATEGORY, PACKAGE_NAME, /* packageName */ null, NAMED_VARIANTS, NAMED_VARIANTS, REQUEST_CODE REQUEST_CODE ); ); Loading @@ -121,15 +119,16 @@ public class TemplateIntentFactoryTest { List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).hasSize(0); String data = intents.get(0).getIntent().getData().toString(); assertThat(data).isEqualTo("http://www.android.com"); } } @Test @Test public void create_minimal() { public void create_minimal() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( null, TITLE, null, DESCRIPTION, null, ACTION, null, null, null, null, null, null, Loading @@ -142,15 +141,14 @@ public class TemplateIntentFactoryTest { List<TextClassifierImpl.LabeledIntent> intents = List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); mTemplateIntentFactory.create(new RemoteActionTemplate[]{remoteActionTemplate}); assertThat(intents).hasSize(1); assertThat(intents).hasSize(1); TextClassifierImpl.LabeledIntent labeledIntent = intents.get(0); TextClassifierImpl.LabeledIntent labeledIntent = intents.get(0); assertThat(labeledIntent.getTitle()).isNull(); assertThat(labeledIntent.getTitle()).isEqualTo(TITLE); assertThat(labeledIntent.getDescription()).isNull(); assertThat(labeledIntent.getDescription()).isEqualTo(DESCRIPTION); assertThat(labeledIntent.getRequestCode()).isEqualTo( assertThat(labeledIntent.getRequestCode()).isEqualTo( TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE); TextClassifierImpl.LabeledIntent.DEFAULT_REQUEST_CODE); Intent intent = labeledIntent.getIntent(); Intent intent = labeledIntent.getIntent(); assertThat(intent.getAction()).isNull(); assertThat(intent.getAction()).isEqualTo(ACTION); assertThat(intent.getData()).isNull(); assertThat(intent.getData()).isNull(); assertThat(intent.getType()).isNull(); assertThat(intent.getType()).isNull(); assertThat(intent.getFlags()).isEqualTo(0); assertThat(intent.getFlags()).isEqualTo(0); Loading @@ -158,4 +156,98 @@ public class TemplateIntentFactoryTest { assertThat(intent.getPackage()).isNull(); assertThat(intent.getPackage()).isNull(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); } } @Test public void invalidTemplate_nullTemplate() { RemoteActionTemplate remoteActionTemplate = null; List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_nonEmptyPackageName() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, DESCRIPTION, ACTION, DATA, TYPE, FLAG, CATEGORY, PACKAGE_NAME, NAMED_VARIANTS, REQUEST_CODE ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyTitle() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( null, DESCRIPTION, ACTION, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyDescription() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, null, ACTION, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } @Test public void invalidTemplate_emptyIntentAction() { RemoteActionTemplate remoteActionTemplate = new RemoteActionTemplate( TITLE, DESCRIPTION, null, null, null, null, null, null, null, null ); List<TextClassifierImpl.LabeledIntent> intents = mTemplateIntentFactory.create(new RemoteActionTemplate[] {remoteActionTemplate}); assertThat(intents).isEmpty(); } } }