Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Tony Mak | 8ab9b18 | 2019-03-01 16:44:17 +0000 | [diff] [blame] | 17 | package android.view.textclassifier.intent; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.testng.Assert.assertThrows; |
| 22 | |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 23 | import android.content.ComponentName; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.net.Uri; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 27 | import android.os.Bundle; |
| 28 | import android.view.textclassifier.FakeContextBuilder; |
| 29 | import android.view.textclassifier.TextClassifier; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 30 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 31 | import androidx.test.filters.SmallTest; |
| 32 | import androidx.test.runner.AndroidJUnit4; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
| 38 | @SmallTest |
| 39 | @RunWith(AndroidJUnit4.class) |
| 40 | public final class LabeledIntentTest { |
| 41 | private static final String TITLE_WITHOUT_ENTITY = "Map"; |
| 42 | private static final String TITLE_WITH_ENTITY = "Map NW14D1"; |
| 43 | private static final String DESCRIPTION = "Check the map"; |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 44 | private static final String DESCRIPTION_WITH_APP_NAME = "Use %1$s to open map"; |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 45 | private static final Intent INTENT = |
| 46 | new Intent(Intent.ACTION_VIEW).setDataAndNormalize(Uri.parse("http://www.android.com")); |
| 47 | private static final int REQUEST_CODE = 42; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 48 | private static final Bundle TEXT_LANGUAGES_BUNDLE = Bundle.EMPTY; |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 49 | private static final String APP_LABEL = "fake"; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 50 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 51 | private Context mContext; |
| 52 | |
| 53 | @Before |
| 54 | public void setup() { |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 55 | final ComponentName component = FakeContextBuilder.DEFAULT_COMPONENT; |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 56 | mContext = new FakeContextBuilder() |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 57 | .setIntentComponent(Intent.ACTION_VIEW, component) |
| 58 | .setAppLabel(component.getPackageName(), APP_LABEL) |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 59 | .build(); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void resolve_preferTitleWithEntity() { |
| 64 | LabeledIntent labeledIntent = new LabeledIntent( |
| 65 | TITLE_WITHOUT_ENTITY, |
| 66 | TITLE_WITH_ENTITY, |
| 67 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 68 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 69 | INTENT, |
| 70 | REQUEST_CODE |
| 71 | ); |
| 72 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 73 | LabeledIntent.Result result = labeledIntent.resolve( |
| 74 | mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 75 | |
| 76 | assertThat(result).isNotNull(); |
| 77 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITH_ENTITY); |
| 78 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 79 | Intent intent = result.resolvedIntent; |
| 80 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 81 | assertThat(intent.getComponent()).isNotNull(); |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 82 | assertThat(intent.hasExtra(TextClassifier.EXTRA_FROM_TEXT_CLASSIFIER)).isTrue(); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void resolve_useAvailableTitle() { |
| 87 | LabeledIntent labeledIntent = new LabeledIntent( |
| 88 | TITLE_WITHOUT_ENTITY, |
| 89 | null, |
| 90 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 91 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 92 | INTENT, |
| 93 | REQUEST_CODE |
| 94 | ); |
| 95 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 96 | LabeledIntent.Result result = labeledIntent.resolve( |
| 97 | mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 98 | |
| 99 | assertThat(result).isNotNull(); |
| 100 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 101 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 102 | Intent intent = result.resolvedIntent; |
| 103 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 104 | assertThat(intent.getComponent()).isNotNull(); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void resolve_titleChooser() { |
| 109 | LabeledIntent labeledIntent = new LabeledIntent( |
| 110 | TITLE_WITHOUT_ENTITY, |
| 111 | null, |
| 112 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 113 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 114 | INTENT, |
| 115 | REQUEST_CODE |
| 116 | ); |
| 117 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 118 | LabeledIntent.Result result = labeledIntent.resolve( |
| 119 | mContext, (labeledIntent1, resolveInfo) -> "chooser", TEXT_LANGUAGES_BUNDLE); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 120 | |
| 121 | assertThat(result).isNotNull(); |
| 122 | assertThat(result.remoteAction.getTitle()).isEqualTo("chooser"); |
| 123 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 124 | Intent intent = result.resolvedIntent; |
| 125 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 126 | assertThat(intent.getComponent()).isNotNull(); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void resolve_titleChooserReturnsNull() { |
| 131 | LabeledIntent labeledIntent = new LabeledIntent( |
| 132 | TITLE_WITHOUT_ENTITY, |
| 133 | null, |
| 134 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 135 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 136 | INTENT, |
| 137 | REQUEST_CODE |
| 138 | ); |
| 139 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 140 | LabeledIntent.Result result = labeledIntent.resolve( |
| 141 | mContext, (labeledIntent1, resolveInfo) -> null, TEXT_LANGUAGES_BUNDLE); |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 142 | |
| 143 | assertThat(result).isNotNull(); |
| 144 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 145 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 146 | Intent intent = result.resolvedIntent; |
| 147 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 148 | assertThat(intent.getComponent()).isNotNull(); |
| 149 | } |
| 150 | |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 151 | @Test |
| 152 | public void resolve_missingTitle() { |
| 153 | assertThrows( |
| 154 | IllegalArgumentException.class, |
| 155 | () -> |
| 156 | new LabeledIntent( |
| 157 | null, |
| 158 | null, |
| 159 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 160 | null, |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 161 | INTENT, |
| 162 | REQUEST_CODE |
| 163 | )); |
| 164 | } |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 165 | |
| 166 | @Test |
| 167 | public void resolve_noIntentHandler() { |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 168 | // See setup(). mContext can only resolve Intent.ACTION_VIEW. |
| 169 | Intent unresolvableIntent = new Intent(Intent.ACTION_TRANSLATE); |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 170 | LabeledIntent labeledIntent = new LabeledIntent( |
| 171 | TITLE_WITHOUT_ENTITY, |
| 172 | null, |
| 173 | DESCRIPTION, |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 174 | null, |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 175 | unresolvableIntent, |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 176 | REQUEST_CODE); |
| 177 | |
Tony Mak | 72e1797 | 2019-03-16 10:28:42 +0000 | [diff] [blame] | 178 | LabeledIntent.Result result = labeledIntent.resolve(mContext, null, null); |
Tony Mak | c12035e | 2019-02-26 17:45:34 +0000 | [diff] [blame] | 179 | |
| 180 | assertThat(result).isNull(); |
| 181 | } |
Tony Mak | 15b64be | 2019-04-01 20:02:29 +0100 | [diff] [blame] | 182 | |
| 183 | @Test |
| 184 | public void resolve_descriptionWithAppName() { |
| 185 | LabeledIntent labeledIntent = new LabeledIntent( |
| 186 | TITLE_WITHOUT_ENTITY, |
| 187 | TITLE_WITH_ENTITY, |
| 188 | DESCRIPTION, |
| 189 | DESCRIPTION_WITH_APP_NAME, |
| 190 | INTENT, |
| 191 | REQUEST_CODE |
| 192 | ); |
| 193 | |
| 194 | LabeledIntent.Result result = labeledIntent.resolve( |
| 195 | mContext, /*titleChooser*/ null, TEXT_LANGUAGES_BUNDLE); |
| 196 | |
| 197 | assertThat(result).isNotNull(); |
| 198 | assertThat(result.remoteAction.getContentDescription()).isEqualTo("Use fake to open map"); |
| 199 | } |
Tony Mak | ac9b4d8 | 2019-02-15 13:57:38 +0000 | [diff] [blame] | 200 | } |