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 | |
| 17 | package android.view.textclassifier; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.testng.Assert.assertThrows; |
| 22 | |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.net.Uri; |
| 26 | |
| 27 | import androidx.test.InstrumentationRegistry; |
| 28 | import androidx.test.filters.SmallTest; |
| 29 | import androidx.test.runner.AndroidJUnit4; |
| 30 | |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | import org.junit.runner.RunWith; |
| 34 | |
| 35 | @SmallTest |
| 36 | @RunWith(AndroidJUnit4.class) |
| 37 | public final class LabeledIntentTest { |
| 38 | private static final String TITLE_WITHOUT_ENTITY = "Map"; |
| 39 | private static final String TITLE_WITH_ENTITY = "Map NW14D1"; |
| 40 | private static final String DESCRIPTION = "Check the map"; |
| 41 | private static final Intent INTENT = |
| 42 | new Intent(Intent.ACTION_VIEW).setDataAndNormalize(Uri.parse("http://www.android.com")); |
| 43 | private static final int REQUEST_CODE = 42; |
| 44 | private Context mContext; |
| 45 | |
| 46 | @Before |
| 47 | public void setup() { |
| 48 | mContext = InstrumentationRegistry.getTargetContext(); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void resolve_preferTitleWithEntity() { |
| 53 | LabeledIntent labeledIntent = new LabeledIntent( |
| 54 | TITLE_WITHOUT_ENTITY, |
| 55 | TITLE_WITH_ENTITY, |
| 56 | DESCRIPTION, |
| 57 | INTENT, |
| 58 | REQUEST_CODE |
| 59 | ); |
| 60 | |
| 61 | LabeledIntent.Result result = |
| 62 | labeledIntent.resolve(mContext, /*titleChooser*/ null); |
| 63 | |
| 64 | assertThat(result).isNotNull(); |
| 65 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITH_ENTITY); |
| 66 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 67 | Intent intent = result.resolvedIntent; |
| 68 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 69 | assertThat(intent.getComponent()).isNotNull(); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void resolve_useAvailableTitle() { |
| 74 | LabeledIntent labeledIntent = new LabeledIntent( |
| 75 | TITLE_WITHOUT_ENTITY, |
| 76 | null, |
| 77 | DESCRIPTION, |
| 78 | INTENT, |
| 79 | REQUEST_CODE |
| 80 | ); |
| 81 | |
| 82 | LabeledIntent.Result result = |
| 83 | labeledIntent.resolve(mContext, /*titleChooser*/ null); |
| 84 | |
| 85 | assertThat(result).isNotNull(); |
| 86 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 87 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 88 | Intent intent = result.resolvedIntent; |
| 89 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 90 | assertThat(intent.getComponent()).isNotNull(); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void resolve_titleChooser() { |
| 95 | LabeledIntent labeledIntent = new LabeledIntent( |
| 96 | TITLE_WITHOUT_ENTITY, |
| 97 | null, |
| 98 | DESCRIPTION, |
| 99 | INTENT, |
| 100 | REQUEST_CODE |
| 101 | ); |
| 102 | |
| 103 | LabeledIntent.Result result = |
| 104 | labeledIntent.resolve(mContext, (labeledIntent1, resolveInfo) -> "chooser"); |
| 105 | |
| 106 | assertThat(result).isNotNull(); |
| 107 | assertThat(result.remoteAction.getTitle()).isEqualTo("chooser"); |
| 108 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 109 | Intent intent = result.resolvedIntent; |
| 110 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 111 | assertThat(intent.getComponent()).isNotNull(); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void resolve_titleChooserReturnsNull() { |
| 116 | LabeledIntent labeledIntent = new LabeledIntent( |
| 117 | TITLE_WITHOUT_ENTITY, |
| 118 | null, |
| 119 | DESCRIPTION, |
| 120 | INTENT, |
| 121 | REQUEST_CODE |
| 122 | ); |
| 123 | |
| 124 | LabeledIntent.Result result = |
| 125 | labeledIntent.resolve(mContext, (labeledIntent1, resolveInfo) -> null); |
| 126 | |
| 127 | assertThat(result).isNotNull(); |
| 128 | assertThat(result.remoteAction.getTitle()).isEqualTo(TITLE_WITHOUT_ENTITY); |
| 129 | assertThat(result.remoteAction.getContentDescription()).isEqualTo(DESCRIPTION); |
| 130 | Intent intent = result.resolvedIntent; |
| 131 | assertThat(intent.getAction()).isEqualTo(intent.getAction()); |
| 132 | assertThat(intent.getComponent()).isNotNull(); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | @Test |
| 137 | public void resolve_missingTitle() { |
| 138 | assertThrows( |
| 139 | IllegalArgumentException.class, |
| 140 | () -> |
| 141 | new LabeledIntent( |
| 142 | null, |
| 143 | null, |
| 144 | DESCRIPTION, |
| 145 | INTENT, |
| 146 | REQUEST_CODE |
| 147 | )); |
| 148 | } |
| 149 | } |