Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.internal.app; |
| 18 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 19 | import static androidx.test.espresso.Espresso.onView; |
| 20 | import static androidx.test.espresso.action.ViewActions.click; |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 21 | import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 22 | import static androidx.test.espresso.assertion.ViewAssertions.matches; |
| 23 | import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 24 | import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 25 | import static androidx.test.espresso.matcher.ViewMatchers.withText; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 26 | |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 27 | import static com.android.internal.app.ChooserActivity.TARGET_TYPE_CHOOSER_TARGET; |
| 28 | import static com.android.internal.app.ChooserActivity.TARGET_TYPE_DEFAULT; |
| 29 | import static com.android.internal.app.ChooserActivity.TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE; |
| 30 | import static com.android.internal.app.ChooserActivity.TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 31 | import static com.android.internal.app.ChooserListAdapter.CALLER_TARGET_SCORE_BOOST; |
| 32 | import static com.android.internal.app.ChooserListAdapter.SHORTCUT_TARGET_SCORE_BOOST; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 33 | import static com.android.internal.app.ChooserWrapperActivity.sOverrides; |
| 34 | |
| 35 | import static org.hamcrest.CoreMatchers.is; |
| 36 | import static org.hamcrest.CoreMatchers.not; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 37 | import static org.hamcrest.CoreMatchers.notNullValue; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 38 | import static org.hamcrest.MatcherAssert.assertThat; |
Mehdi Alizadeh | 707c0cf | 2019-09-03 18:11:48 -0700 | [diff] [blame] | 39 | import static org.junit.Assert.assertEquals; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 40 | import static org.mockito.Mockito.atLeastOnce; |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 41 | import static org.mockito.Mockito.mock; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 42 | import static org.mockito.Mockito.times; |
| 43 | import static org.mockito.Mockito.verify; |
| 44 | import static org.mockito.Mockito.when; |
| 45 | |
| 46 | import android.app.usage.UsageStatsManager; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 47 | import android.content.ClipData; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 48 | import android.content.ClipDescription; |
| 49 | import android.content.ClipboardManager; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 50 | import android.content.ComponentName; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 51 | import android.content.Context; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 52 | import android.content.Intent; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 53 | import android.content.pm.PackageManager; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 54 | import android.content.pm.ResolveInfo; |
Mehdi Alizadeh | 707c0cf | 2019-09-03 18:11:48 -0700 | [diff] [blame] | 55 | import android.content.pm.ShortcutInfo; |
| 56 | import android.content.pm.ShortcutManager.ShareShortcutInfo; |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 57 | import android.database.Cursor; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 58 | import android.graphics.Bitmap; |
| 59 | import android.graphics.Canvas; |
| 60 | import android.graphics.Color; |
| 61 | import android.graphics.Paint; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 62 | import android.graphics.drawable.Icon; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 63 | import android.metrics.LogMaker; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 64 | import android.net.Uri; |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 65 | import android.service.chooser.ChooserTarget; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 66 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 67 | import androidx.test.platform.app.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 68 | import androidx.test.rule.ActivityTestRule; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 69 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 70 | import com.android.internal.R; |
| 71 | import com.android.internal.app.ResolverActivity.ResolvedComponentInfo; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 72 | import com.android.internal.app.chooser.DisplayResolveInfo; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 73 | import com.android.internal.logging.MetricsLogger; |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 74 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 75 | |
| 76 | import org.junit.Before; |
| 77 | import org.junit.Rule; |
| 78 | import org.junit.Test; |
| 79 | import org.junit.runner.RunWith; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 80 | import org.junit.runners.Parameterized; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 81 | import org.mockito.ArgumentCaptor; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 82 | import org.mockito.Mockito; |
| 83 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 84 | import java.util.ArrayList; |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 85 | import java.util.Arrays; |
| 86 | import java.util.Collection; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 87 | import java.util.List; |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 88 | import java.util.function.Function; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Chooser activity instrumentation tests |
| 92 | */ |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 93 | @RunWith(Parameterized.class) |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 94 | public class ChooserActivityTest { |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 95 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 96 | private static final Function<PackageManager, PackageManager> DEFAULT_PM = pm -> pm; |
| 97 | private static final Function<PackageManager, PackageManager> NO_APP_PREDICTION_SERVICE_PM = |
| 98 | pm -> { |
| 99 | PackageManager mock = Mockito.spy(pm); |
| 100 | when(mock.getAppPredictionServicePackageName()).thenReturn(null); |
| 101 | return mock; |
| 102 | }; |
| 103 | |
| 104 | @Parameterized.Parameters |
| 105 | public static Collection packageManagers() { |
| 106 | return Arrays.asList(new Object[][] { |
| 107 | {0, "Default PackageManager", DEFAULT_PM}, |
| 108 | {1, "No App Prediction Service", NO_APP_PREDICTION_SERVICE_PM} |
| 109 | }); |
| 110 | } |
| 111 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 112 | private static final int CONTENT_PREVIEW_IMAGE = 1; |
| 113 | private static final int CONTENT_PREVIEW_FILE = 2; |
| 114 | private static final int CONTENT_PREVIEW_TEXT = 3; |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 115 | private Function<PackageManager, PackageManager> mPackageManagerOverride; |
| 116 | private int mTestNum; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 117 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 118 | @Rule |
| 119 | public ActivityTestRule<ChooserWrapperActivity> mActivityRule = |
| 120 | new ActivityTestRule<>(ChooserWrapperActivity.class, false, |
| 121 | false); |
| 122 | |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 123 | public ChooserActivityTest( |
| 124 | int testNum, |
| 125 | String testName, |
| 126 | Function<PackageManager, PackageManager> packageManagerOverride) { |
| 127 | mPackageManagerOverride = packageManagerOverride; |
| 128 | mTestNum = testNum; |
| 129 | } |
| 130 | |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 131 | @Before |
| 132 | public void cleanOverrideData() { |
| 133 | sOverrides.reset(); |
George Hodulik | 145b3a5 | 2019-03-27 11:18:43 -0700 | [diff] [blame] | 134 | sOverrides.createPackageManager = mPackageManagerOverride; |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 137 | @Test |
| 138 | public void customTitle() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 139 | Intent viewIntent = createViewTextIntent(); |
| 140 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 141 | |
| 142 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 143 | Mockito.anyBoolean(), |
| 144 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 145 | final ChooserWrapperActivity activity = mActivityRule.launchActivity( |
| 146 | Intent.createChooser(viewIntent, "chooser test")); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 147 | |
| 148 | waitForIdle(); |
Matt Pietal | 95574b0 | 2019-03-13 08:12:25 -0400 | [diff] [blame] | 149 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 150 | assertThat(activity.getAdapter().getServiceTargetCount(), is(0)); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 151 | onView(withId(R.id.title)).check(matches(withText("chooser test"))); |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void customTitleIgnoredForSendIntents() throws InterruptedException { |
| 156 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 157 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 158 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 159 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 160 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 161 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 162 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "chooser test")); |
| 163 | waitForIdle(); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 164 | onView(withId(R.id.title)).check(matches(withText(R.string.whichSendApplication))); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void emptyTitle() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 169 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 170 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 171 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 172 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 173 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 174 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 175 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 176 | waitForIdle(); |
| 177 | onView(withId(R.id.title)) |
| 178 | .check(matches(withText(R.string.whichSendApplication))); |
| 179 | } |
| 180 | |
| 181 | @Test |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 182 | public void emptyPreviewTitleAndThumbnail() throws InterruptedException { |
| 183 | Intent sendIntent = createSendTextIntentWithPreview(null, null); |
| 184 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 185 | |
| 186 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 187 | Mockito.anyBoolean(), |
| 188 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 189 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 190 | waitForIdle(); |
| 191 | onView(withId(R.id.content_preview_title)).check(matches(not(isDisplayed()))); |
| 192 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void visiblePreviewTitleWithoutThumbnail() throws InterruptedException { |
| 197 | String previewTitle = "My Content Preview Title"; |
| 198 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, null); |
| 199 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 200 | |
| 201 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 202 | Mockito.anyBoolean(), |
| 203 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 204 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 205 | waitForIdle(); |
| 206 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 207 | onView(withId(R.id.content_preview_title)).check(matches(withText(previewTitle))); |
| 208 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | public void visiblePreviewTitleWithInvalidThumbnail() throws InterruptedException { |
| 213 | String previewTitle = "My Content Preview Title"; |
| 214 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, |
| 215 | Uri.parse("tel:(+49)12345789")); |
| 216 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 217 | |
| 218 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 219 | Mockito.anyBoolean(), |
| 220 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 221 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 222 | waitForIdle(); |
| 223 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 224 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void visiblePreviewTitleAndThumbnail() throws InterruptedException { |
| 229 | String previewTitle = "My Content Preview Title"; |
| 230 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, |
| 231 | Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 232 | + com.android.frameworks.coretests.R.drawable.test320x240)); |
| 233 | sOverrides.previewThumbnail = createBitmap(); |
| 234 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 235 | |
| 236 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 237 | Mockito.anyBoolean(), |
| 238 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 239 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 240 | waitForIdle(); |
| 241 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 242 | onView(withId(R.id.content_preview_thumbnail)).check(matches(isDisplayed())); |
| 243 | } |
| 244 | |
| 245 | @Test |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 246 | public void twoOptionsAndUserSelectsOne() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 247 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 248 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 249 | |
| 250 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 251 | Mockito.anyBoolean(), |
| 252 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 253 | |
| 254 | final ChooserWrapperActivity activity = mActivityRule |
| 255 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 256 | waitForIdle(); |
| 257 | |
| 258 | assertThat(activity.getAdapter().getCount(), is(2)); |
Matt Pietal | 74c6ed0 | 2019-04-18 13:38:46 -0400 | [diff] [blame] | 259 | onView(withId(R.id.profile_button)).check(doesNotExist()); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 260 | |
| 261 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 262 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 263 | chosen[0] = targetInfo.getResolveInfo(); |
| 264 | return true; |
| 265 | }; |
| 266 | |
| 267 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
| 268 | onView(withText(toChoose.activityInfo.name)) |
| 269 | .perform(click()); |
| 270 | waitForIdle(); |
| 271 | assertThat(chosen[0], is(toChoose)); |
| 272 | } |
| 273 | |
| 274 | @Test |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 275 | public void updateChooserCountsAndModelAfterUserSelection() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 276 | Intent sendIntent = createSendTextIntent(); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 277 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 278 | |
| 279 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 280 | Mockito.anyBoolean(), |
| 281 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 282 | |
| 283 | final ChooserWrapperActivity activity = mActivityRule |
| 284 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 285 | waitForIdle(); |
| 286 | UsageStatsManager usm = activity.getUsageStatsManager(); |
| 287 | verify(sOverrides.resolverListController, times(1)) |
Zhen Zhang | aa458b1 | 2019-10-16 12:46:44 -0700 | [diff] [blame] | 288 | .topK(Mockito.any(List.class), Mockito.anyInt()); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 289 | assertThat(activity.getIsSelected(), is(false)); |
| 290 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 291 | return true; |
| 292 | }; |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 293 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 294 | onView(withText(toChoose.activityInfo.name)) |
| 295 | .perform(click()); |
| 296 | waitForIdle(); |
| 297 | verify(sOverrides.resolverListController, times(1)) |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 298 | .updateChooserCounts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()); |
| 299 | verify(sOverrides.resolverListController, times(1)) |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 300 | .updateModel(toChoose.activityInfo.getComponentName()); |
| 301 | assertThat(activity.getIsSelected(), is(true)); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 305 | public void noResultsFromPackageManager() { |
| 306 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 307 | Mockito.anyBoolean(), |
| 308 | Mockito.isA(List.class))).thenReturn(null); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 309 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 310 | final ChooserWrapperActivity activity = mActivityRule |
| 311 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 312 | waitForIdle(); |
| 313 | assertThat(activity.isFinishing(), is(false)); |
| 314 | |
| 315 | onView(withId(R.id.empty)).check(matches(isDisplayed())); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 316 | onView(withId(R.id.profile_pager)).check(matches(not(isDisplayed()))); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 317 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
| 318 | () -> activity.getAdapter().handlePackagesChanged() |
| 319 | ); |
| 320 | // backward compatibility. looks like we finish when data is empty after package change |
| 321 | assertThat(activity.isFinishing(), is(true)); |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | public void autoLaunchSingleResult() throws InterruptedException { |
| 326 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 327 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 328 | chosen[0] = targetInfo.getResolveInfo(); |
| 329 | return true; |
| 330 | }; |
| 331 | |
| 332 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(1); |
| 333 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 334 | Mockito.anyBoolean(), |
| 335 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 336 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 337 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 338 | final ChooserWrapperActivity activity = mActivityRule |
| 339 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 340 | waitForIdle(); |
| 341 | |
| 342 | assertThat(chosen[0], is(resolvedComponentInfos.get(0).getResolveInfoAt(0))); |
| 343 | assertThat(activity.isFinishing(), is(true)); |
| 344 | } |
| 345 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 346 | @Test |
| 347 | public void hasOtherProfileOneOption() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 348 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 349 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 350 | createResolvedComponentsForTestWithOtherProfile(2); |
| 351 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 352 | |
| 353 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 354 | Mockito.anyBoolean(), |
| 355 | Mockito.anyBoolean(), |
| 356 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 357 | |
| 358 | final ChooserWrapperActivity activity = mActivityRule |
| 359 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 360 | waitForIdle(); |
| 361 | |
| 362 | // The other entry is filtered to the other profile slot |
| 363 | assertThat(activity.getAdapter().getCount(), is(1)); |
| 364 | |
| 365 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 366 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 367 | chosen[0] = targetInfo.getResolveInfo(); |
| 368 | return true; |
| 369 | }; |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 370 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 371 | // Make a stable copy of the components as the original list may be modified |
| 372 | List<ResolvedComponentInfo> stableCopy = |
| 373 | createResolvedComponentsForTestWithOtherProfile(2); |
| 374 | // Check that the "Other Profile" activity is put in the right spot |
| 375 | onView(withId(R.id.profile_button)).check(matches( |
| 376 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 377 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 378 | .perform(click()); |
| 379 | waitForIdle(); |
| 380 | assertThat(chosen[0], is(toChoose)); |
| 381 | } |
| 382 | |
| 383 | @Test |
| 384 | public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 385 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 386 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 387 | createResolvedComponentsForTestWithOtherProfile(3); |
| 388 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 389 | |
| 390 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 391 | Mockito.anyBoolean(), |
| 392 | Mockito.anyBoolean(), |
| 393 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 394 | when(ChooserWrapperActivity.sOverrides.resolverListController.getLastChosen()) |
| 395 | .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0)); |
| 396 | |
| 397 | final ChooserWrapperActivity activity = mActivityRule |
| 398 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 399 | waitForIdle(); |
| 400 | |
| 401 | // The other entry is filtered to the other profile slot |
| 402 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 403 | |
| 404 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 405 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 406 | chosen[0] = targetInfo.getResolveInfo(); |
| 407 | return true; |
| 408 | }; |
| 409 | |
| 410 | // Make a stable copy of the components as the original list may be modified |
| 411 | List<ResolvedComponentInfo> stableCopy = |
| 412 | createResolvedComponentsForTestWithOtherProfile(3); |
| 413 | // Check that the "Other Profile" activity is put in the right spot |
| 414 | onView(withId(R.id.profile_button)).check(matches( |
| 415 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 416 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 417 | .perform(click()); |
| 418 | waitForIdle(); |
| 419 | assertThat(chosen[0], is(toChoose)); |
| 420 | } |
| 421 | |
| 422 | @Test |
| 423 | public void hasLastChosenActivityAndOtherProfile() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 424 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 425 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 426 | createResolvedComponentsForTestWithOtherProfile(3); |
| 427 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 428 | |
| 429 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 430 | Mockito.anyBoolean(), |
| 431 | Mockito.anyBoolean(), |
| 432 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 433 | |
| 434 | final ChooserWrapperActivity activity = mActivityRule |
| 435 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 436 | waitForIdle(); |
| 437 | |
| 438 | // The other entry is filtered to the last used slot |
| 439 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 440 | |
| 441 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 442 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 443 | chosen[0] = targetInfo.getResolveInfo(); |
| 444 | return true; |
| 445 | }; |
| 446 | |
| 447 | // Make a stable copy of the components as the original list may be modified |
| 448 | List<ResolvedComponentInfo> stableCopy = |
| 449 | createResolvedComponentsForTestWithOtherProfile(3); |
| 450 | // Check that the "Other Profile" activity is put in the right spot |
| 451 | onView(withId(R.id.profile_button)).check(matches( |
| 452 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 453 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 454 | .perform(click()); |
| 455 | waitForIdle(); |
| 456 | assertThat(chosen[0], is(toChoose)); |
| 457 | } |
| 458 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 459 | @Test |
| 460 | public void copyTextToClipboard() throws Exception { |
| 461 | Intent sendIntent = createSendTextIntent(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 462 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 463 | |
| 464 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 465 | Mockito.anyBoolean(), |
| 466 | Mockito.anyBoolean(), |
| 467 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 468 | |
| 469 | final ChooserWrapperActivity activity = mActivityRule |
| 470 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 471 | waitForIdle(); |
| 472 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 473 | onView(withId(R.id.copy_button)).check(matches(isDisplayed())); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 474 | onView(withId(R.id.copy_button)).perform(click()); |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 475 | ClipboardManager clipboard = (ClipboardManager) activity.getSystemService( |
| 476 | Context.CLIPBOARD_SERVICE); |
| 477 | ClipData clipData = clipboard.getPrimaryClip(); |
| 478 | assertThat("testing intent sending", is(clipData.getItemAt(0).getText())); |
| 479 | |
| 480 | ClipDescription clipDescription = clipData.getDescription(); |
| 481 | assertThat("text/plain", is(clipDescription.getMimeType(0))); |
| 482 | } |
| 483 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 484 | @Test |
Susi Kharraz-Post | 9e91318 | 2019-09-19 11:20:01 -0400 | [diff] [blame] | 485 | public void copyTextToClipboardLogging() throws Exception { |
| 486 | Intent sendIntent = createSendTextIntent(); |
| 487 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 488 | |
| 489 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 490 | Mockito.anyBoolean(), |
| 491 | Mockito.anyBoolean(), |
| 492 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 493 | |
| 494 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 495 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 496 | |
| 497 | final ChooserWrapperActivity activity = mActivityRule |
| 498 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 499 | waitForIdle(); |
| 500 | |
| 501 | onView(withId(R.id.copy_button)).check(matches(isDisplayed())); |
| 502 | onView(withId(R.id.copy_button)).perform(click()); |
| 503 | |
| 504 | verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture()); |
| 505 | // First is Activity shown, Second is "with preview" |
| 506 | assertThat(logMakerCaptor.getAllValues().get(2).getCategory(), |
| 507 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SYSTEM_TARGET)); |
| 508 | assertThat(logMakerCaptor |
| 509 | .getAllValues().get(2) |
| 510 | .getSubtype(), |
| 511 | is(1)); |
| 512 | } |
| 513 | |
| 514 | @Test |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 515 | public void oneVisibleImagePreview() throws InterruptedException { |
| 516 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 517 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 518 | |
| 519 | ArrayList<Uri> uris = new ArrayList<>(); |
| 520 | uris.add(uri); |
| 521 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 522 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 523 | sOverrides.previewThumbnail = createBitmap(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 524 | sOverrides.isImageType = true; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 525 | |
| 526 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 527 | |
| 528 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 529 | Mockito.anyBoolean(), |
| 530 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 531 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 532 | waitForIdle(); |
| 533 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 534 | onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed()))); |
| 535 | onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed()))); |
| 536 | onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed()))); |
| 537 | } |
| 538 | |
| 539 | @Test |
| 540 | public void twoVisibleImagePreview() throws InterruptedException { |
| 541 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 542 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 543 | |
| 544 | ArrayList<Uri> uris = new ArrayList<>(); |
| 545 | uris.add(uri); |
| 546 | uris.add(uri); |
| 547 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 548 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 549 | sOverrides.previewThumbnail = createBitmap(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 550 | sOverrides.isImageType = true; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 551 | |
| 552 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 553 | |
| 554 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 555 | Mockito.anyBoolean(), |
| 556 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 557 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 558 | waitForIdle(); |
| 559 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 560 | onView(withId(R.id.content_preview_image_2_large)).check(matches(isDisplayed())); |
| 561 | onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed()))); |
| 562 | onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed()))); |
| 563 | } |
| 564 | |
| 565 | @Test |
| 566 | public void threeOrMoreVisibleImagePreview() throws InterruptedException { |
| 567 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 568 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 569 | |
| 570 | ArrayList<Uri> uris = new ArrayList<>(); |
| 571 | uris.add(uri); |
| 572 | uris.add(uri); |
| 573 | uris.add(uri); |
| 574 | uris.add(uri); |
| 575 | uris.add(uri); |
| 576 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 577 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 578 | sOverrides.previewThumbnail = createBitmap(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 579 | sOverrides.isImageType = true; |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 580 | |
| 581 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 582 | |
| 583 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 584 | Mockito.anyBoolean(), |
| 585 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 586 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 587 | waitForIdle(); |
| 588 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 589 | onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed()))); |
| 590 | onView(withId(R.id.content_preview_image_2_small)).check(matches(isDisplayed())); |
| 591 | onView(withId(R.id.content_preview_image_3_small)).check(matches(isDisplayed())); |
| 592 | } |
| 593 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 594 | @Test |
| 595 | public void testOnCreateLogging() { |
| 596 | Intent sendIntent = createSendTextIntent(); |
| 597 | sendIntent.setType("TestType"); |
| 598 | |
| 599 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 600 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 601 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "logger test")); |
| 602 | waitForIdle(); |
| 603 | verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture()); |
| 604 | assertThat(logMakerCaptor.getAllValues().get(0).getCategory(), |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 605 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN)); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 606 | assertThat(logMakerCaptor |
| 607 | .getAllValues().get(0) |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 608 | .getTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS), |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 609 | is(notNullValue())); |
| 610 | assertThat(logMakerCaptor |
| 611 | .getAllValues().get(0) |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 612 | .getTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE), |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 613 | is("TestType")); |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 614 | assertThat(logMakerCaptor |
| 615 | .getAllValues().get(0) |
| 616 | .getSubtype(), |
| 617 | is(MetricsEvent.PARENT_PROFILE)); |
| 618 | } |
| 619 | |
| 620 | @Test |
| 621 | public void testOnCreateLoggingFromWorkProfile() { |
| 622 | Intent sendIntent = createSendTextIntent(); |
| 623 | sendIntent.setType("TestType"); |
| 624 | sOverrides.alternateProfileSetting = MetricsEvent.MANAGED_PROFILE; |
| 625 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 626 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 627 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "logger test")); |
| 628 | waitForIdle(); |
| 629 | verify(mockLogger, atLeastOnce()).write(logMakerCaptor.capture()); |
| 630 | assertThat(logMakerCaptor.getAllValues().get(0).getCategory(), |
| 631 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN)); |
| 632 | assertThat(logMakerCaptor |
| 633 | .getAllValues().get(0) |
| 634 | .getTaggedData(MetricsEvent.FIELD_TIME_TO_APP_TARGETS), |
| 635 | is(notNullValue())); |
| 636 | assertThat(logMakerCaptor |
| 637 | .getAllValues().get(0) |
| 638 | .getTaggedData(MetricsEvent.FIELD_SHARESHEET_MIMETYPE), |
| 639 | is("TestType")); |
| 640 | assertThat(logMakerCaptor |
| 641 | .getAllValues().get(0) |
| 642 | .getSubtype(), |
| 643 | is(MetricsEvent.MANAGED_PROFILE)); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | @Test |
| 647 | public void testEmptyPreviewLogging() { |
| 648 | Intent sendIntent = createSendTextIntentWithPreview(null, null); |
| 649 | |
| 650 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 651 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 652 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "empty preview logger test")); |
| 653 | waitForIdle(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 654 | |
| 655 | verify(mockLogger, Mockito.times(1)).write(logMakerCaptor.capture()); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 656 | // First invocation is from onCreate |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 657 | assertThat(logMakerCaptor.getAllValues().get(0).getCategory(), |
Susi Kharraz-Post | 0446fab | 2019-02-21 09:42:31 -0500 | [diff] [blame] | 658 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN)); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @Test |
| 662 | public void testTitlePreviewLogging() { |
| 663 | Intent sendIntent = createSendTextIntentWithPreview("TestTitle", null); |
| 664 | |
| 665 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 666 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 667 | |
| 668 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 669 | |
| 670 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 671 | Mockito.anyBoolean(), |
| 672 | Mockito.anyBoolean(), |
| 673 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 674 | |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 675 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 676 | waitForIdle(); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 677 | // Second invocation is from onCreate |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 678 | verify(mockLogger, Mockito.times(2)).write(logMakerCaptor.capture()); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 679 | assertThat(logMakerCaptor.getAllValues().get(0).getSubtype(), |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 680 | is(CONTENT_PREVIEW_TEXT)); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 681 | assertThat(logMakerCaptor.getAllValues().get(0).getCategory(), |
| 682 | is(MetricsEvent.ACTION_SHARE_WITH_PREVIEW)); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | @Test |
| 686 | public void testImagePreviewLogging() { |
| 687 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 688 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 689 | |
| 690 | ArrayList<Uri> uris = new ArrayList<>(); |
| 691 | uris.add(uri); |
| 692 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 693 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 694 | sOverrides.previewThumbnail = createBitmap(); |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 695 | sOverrides.isImageType = true; |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 696 | |
| 697 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 698 | |
| 699 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 700 | Mockito.anyBoolean(), |
| 701 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 702 | |
| 703 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 704 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 705 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 706 | waitForIdle(); |
Matt Pietal | fe28f9a | 2019-03-22 07:59:58 -0400 | [diff] [blame] | 707 | verify(mockLogger, Mockito.times(2)).write(logMakerCaptor.capture()); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 708 | // First invocation is from onCreate |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 709 | assertThat(logMakerCaptor.getAllValues().get(0).getSubtype(), |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 710 | is(CONTENT_PREVIEW_IMAGE)); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 711 | assertThat(logMakerCaptor.getAllValues().get(0).getCategory(), |
| 712 | is(MetricsEvent.ACTION_SHARE_WITH_PREVIEW)); |
Susi Kharraz-Post | 7e2115d | 2019-02-01 16:51:22 -0500 | [diff] [blame] | 713 | } |
| 714 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 715 | @Test |
| 716 | public void oneVisibleFilePreview() throws InterruptedException { |
| 717 | Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf"); |
| 718 | |
| 719 | ArrayList<Uri> uris = new ArrayList<>(); |
| 720 | uris.add(uri); |
| 721 | |
| 722 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
| 723 | |
| 724 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 725 | |
| 726 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 727 | Mockito.anyBoolean(), |
| 728 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 729 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 730 | waitForIdle(); |
| 731 | onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed())); |
| 732 | onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf"))); |
| 733 | onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed())); |
| 734 | } |
| 735 | |
| 736 | |
| 737 | @Test |
| 738 | public void moreThanOneVisibleFilePreview() throws InterruptedException { |
| 739 | Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf"); |
| 740 | |
| 741 | ArrayList<Uri> uris = new ArrayList<>(); |
| 742 | uris.add(uri); |
| 743 | uris.add(uri); |
| 744 | uris.add(uri); |
| 745 | |
| 746 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
| 747 | |
| 748 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 749 | |
| 750 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 751 | Mockito.anyBoolean(), |
| 752 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 753 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 754 | waitForIdle(); |
| 755 | onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed())); |
| 756 | onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf + 2 files"))); |
| 757 | onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed())); |
| 758 | } |
| 759 | |
Matt Pietal | f38e9d2 | 2019-02-15 10:01:03 -0500 | [diff] [blame] | 760 | @Test |
| 761 | public void contentProviderThrowSecurityException() throws InterruptedException { |
| 762 | Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf"); |
| 763 | |
| 764 | ArrayList<Uri> uris = new ArrayList<>(); |
| 765 | uris.add(uri); |
| 766 | |
| 767 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
| 768 | |
| 769 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 770 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 771 | Mockito.anyBoolean(), |
| 772 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 773 | |
| 774 | sOverrides.resolverForceException = true; |
| 775 | |
| 776 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 777 | waitForIdle(); |
| 778 | onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed())); |
| 779 | onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf"))); |
| 780 | onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed())); |
| 781 | } |
| 782 | |
| 783 | @Test |
| 784 | public void contentProviderReturnsNoColumns() throws InterruptedException { |
| 785 | Uri uri = Uri.parse("content://com.android.frameworks.coretests/app.pdf"); |
| 786 | |
| 787 | ArrayList<Uri> uris = new ArrayList<>(); |
| 788 | uris.add(uri); |
| 789 | uris.add(uri); |
| 790 | |
| 791 | Intent sendIntent = createSendUriIntentWithPreview(uris); |
| 792 | |
| 793 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 794 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 795 | Mockito.anyBoolean(), |
| 796 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 797 | |
| 798 | Cursor cursor = mock(Cursor.class); |
| 799 | when(cursor.getCount()).thenReturn(1); |
| 800 | Mockito.doNothing().when(cursor).close(); |
| 801 | when(cursor.moveToFirst()).thenReturn(true); |
| 802 | when(cursor.getColumnIndex(Mockito.anyString())).thenReturn(-1); |
| 803 | |
| 804 | sOverrides.resolverCursor = cursor; |
| 805 | |
| 806 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 807 | waitForIdle(); |
| 808 | onView(withId(R.id.content_preview_filename)).check(matches(isDisplayed())); |
| 809 | onView(withId(R.id.content_preview_filename)).check(matches(withText("app.pdf + 1 file"))); |
| 810 | onView(withId(R.id.content_preview_file_icon)).check(matches(isDisplayed())); |
| 811 | } |
| 812 | |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 813 | @Test |
| 814 | public void testGetBaseScore() { |
| 815 | final float testBaseScore = 0.89f; |
| 816 | |
| 817 | Intent sendIntent = createSendTextIntent(); |
| 818 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 819 | |
| 820 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 821 | Mockito.anyBoolean(), |
| 822 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 823 | when(sOverrides.resolverListController.getScore(Mockito.isA(DisplayResolveInfo.class))) |
| 824 | .thenReturn(testBaseScore); |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 825 | |
| 826 | final ChooserWrapperActivity activity = mActivityRule |
| 827 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 828 | waitForIdle(); |
| 829 | |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 830 | final DisplayResolveInfo testDri = |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 831 | activity.createTestDisplayResolveInfo(sendIntent, |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 832 | ResolverDataProvider.createResolveInfo(3, 0), "testLabel", "testInfo", sendIntent, |
| 833 | /* resolveInfoPresentationGetter */ null); |
| 834 | final ChooserListAdapter adapter = activity.getAdapter(); |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 835 | |
| 836 | assertThat(adapter.getBaseScore(null, 0), is(CALLER_TARGET_SCORE_BOOST)); |
| 837 | assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_DEFAULT), is(testBaseScore)); |
| 838 | assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_CHOOSER_TARGET), is(testBaseScore)); |
| 839 | assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE), |
| 840 | is(SHORTCUT_TARGET_SCORE_BOOST)); |
| 841 | assertThat(adapter.getBaseScore(testDri, TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER), |
| 842 | is(testBaseScore * SHORTCUT_TARGET_SCORE_BOOST)); |
| 843 | } |
| 844 | |
Mehdi Alizadeh | e870e97 | 2019-09-11 17:54:15 -0700 | [diff] [blame] | 845 | /** |
| 846 | * The case when AppPrediction service is not defined in PackageManager is already covered |
| 847 | * as a test parameter {@link ChooserActivityTest#packageManagers}. This test is checking the |
| 848 | * case when the prediction service is defined but the component is not available on the device. |
| 849 | */ |
| 850 | @Test |
| 851 | public void testIsAppPredictionServiceAvailable() { |
| 852 | Intent sendIntent = createSendTextIntent(); |
| 853 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 854 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 855 | Mockito.anyBoolean(), |
| 856 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 857 | |
| 858 | final ChooserWrapperActivity activity = mActivityRule |
| 859 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 860 | waitForIdle(); |
| 861 | |
| 862 | if (activity.getPackageManager().getAppPredictionServicePackageName() == null) { |
| 863 | assertThat(activity.isAppPredictionServiceAvailable(), is(false)); |
| 864 | } else { |
| 865 | assertThat(activity.isAppPredictionServiceAvailable(), is(true)); |
| 866 | |
| 867 | sOverrides.resources = Mockito.spy(activity.getResources()); |
| 868 | when(sOverrides.resources.getString(R.string.config_defaultAppPredictionService)) |
| 869 | .thenReturn("ComponentNameThatDoesNotExist"); |
| 870 | |
| 871 | assertThat(activity.isAppPredictionServiceAvailable(), is(false)); |
| 872 | } |
| 873 | } |
| 874 | |
Mehdi Alizadeh | 707c0cf | 2019-09-03 18:11:48 -0700 | [diff] [blame] | 875 | @Test |
| 876 | public void testConvertToChooserTarget_predictionService() { |
| 877 | Intent sendIntent = createSendTextIntent(); |
| 878 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 879 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 880 | Mockito.anyBoolean(), |
| 881 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 882 | |
| 883 | final ChooserWrapperActivity activity = mActivityRule |
| 884 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 885 | waitForIdle(); |
| 886 | |
| 887 | List<ShareShortcutInfo> shortcuts = createShortcuts(activity); |
| 888 | |
| 889 | int[] expectedOrderAllShortcuts = {0, 1, 2, 3}; |
| 890 | float[] expectedScoreAllShortcuts = {1.0f, 0.99f, 0.98f, 0.97f}; |
| 891 | |
| 892 | List<ChooserTarget> chooserTargets = activity.convertToChooserTarget(shortcuts, shortcuts, |
| 893 | null, TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE); |
| 894 | assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets, |
| 895 | expectedOrderAllShortcuts, expectedScoreAllShortcuts); |
| 896 | |
| 897 | List<ShareShortcutInfo> subset = new ArrayList<>(); |
| 898 | subset.add(shortcuts.get(1)); |
| 899 | subset.add(shortcuts.get(2)); |
| 900 | subset.add(shortcuts.get(3)); |
| 901 | |
| 902 | int[] expectedOrderSubset = {1, 2, 3}; |
| 903 | float[] expectedScoreSubset = {0.99f, 0.98f, 0.97f}; |
| 904 | |
| 905 | chooserTargets = activity.convertToChooserTarget(subset, shortcuts, null, |
| 906 | TARGET_TYPE_SHORTCUTS_FROM_PREDICTION_SERVICE); |
| 907 | assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets, |
| 908 | expectedOrderSubset, expectedScoreSubset); |
| 909 | } |
| 910 | |
| 911 | @Test |
| 912 | public void testConvertToChooserTarget_shortcutManager() { |
| 913 | Intent sendIntent = createSendTextIntent(); |
| 914 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 915 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 916 | Mockito.anyBoolean(), |
| 917 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 918 | |
| 919 | final ChooserWrapperActivity activity = mActivityRule |
| 920 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 921 | waitForIdle(); |
| 922 | |
| 923 | List<ShareShortcutInfo> shortcuts = createShortcuts(activity); |
| 924 | |
| 925 | int[] expectedOrderAllShortcuts = {2, 0, 3, 1}; |
| 926 | float[] expectedScoreAllShortcuts = {1.0f, 0.99f, 0.99f, 0.98f}; |
| 927 | |
| 928 | List<ChooserTarget> chooserTargets = activity.convertToChooserTarget(shortcuts, shortcuts, |
| 929 | null, TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER); |
| 930 | assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets, |
| 931 | expectedOrderAllShortcuts, expectedScoreAllShortcuts); |
| 932 | |
| 933 | List<ShareShortcutInfo> subset = new ArrayList<>(); |
| 934 | subset.add(shortcuts.get(1)); |
| 935 | subset.add(shortcuts.get(2)); |
| 936 | subset.add(shortcuts.get(3)); |
| 937 | |
| 938 | int[] expectedOrderSubset = {2, 3, 1}; |
| 939 | float[] expectedScoreSubset = {1.0f, 0.99f, 0.98f}; |
| 940 | |
| 941 | chooserTargets = activity.convertToChooserTarget(subset, shortcuts, null, |
| 942 | TARGET_TYPE_SHORTCUTS_FROM_SHORTCUT_MANAGER); |
| 943 | assertCorrectShortcutToChooserTargetConversion(shortcuts, chooserTargets, |
| 944 | expectedOrderSubset, expectedScoreSubset); |
| 945 | } |
| 946 | |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 947 | // This test is too long and too slow and should not be taken as an example for future tests. |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 948 | @Test |
| 949 | public void testDirectTargetSelectionLogging() throws InterruptedException { |
| 950 | Intent sendIntent = createSendTextIntent(); |
| 951 | // We need app targets for direct targets to get displayed |
| 952 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 953 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 954 | Mockito.anyBoolean(), |
| 955 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 956 | |
| 957 | // Set up resources |
| 958 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 959 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 960 | // Create direct share target |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 961 | List<ChooserTarget> serviceTargets = createDirectShareTargets(1, ""); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 962 | ResolveInfo ri = ResolverDataProvider.createResolveInfo(3, 0); |
| 963 | |
| 964 | // Start activity |
| 965 | final ChooserWrapperActivity activity = mActivityRule |
| 966 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 967 | |
| 968 | // Insert the direct share target |
| 969 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
| 970 | () -> activity.getAdapter().addServiceResults( |
| 971 | activity.createTestDisplayResolveInfo(sendIntent, |
| 972 | ri, |
| 973 | "testLabel", |
| 974 | "testInfo", |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 975 | sendIntent, |
| 976 | /* resolveInfoPresentationGetter */ null), |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 977 | serviceTargets, |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 978 | TARGET_TYPE_CHOOSER_TARGET) |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 979 | ); |
| 980 | // Thread.sleep shouldn't be a thing in an integration test but it's |
| 981 | // necessary here because of the way the code is structured |
| 982 | // TODO: restructure the tests b/129870719 |
| 983 | Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 984 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 985 | assertThat("Chooser should have 3 targets (2 apps, 1 direct)", |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 986 | activity.getAdapter().getCount(), is(3)); |
| 987 | assertThat("Chooser should have exactly one selectable direct target", |
| 988 | activity.getAdapter().getSelectableServiceTargetCount(), is(1)); |
| 989 | assertThat("The resolver info must match the resolver info used to create the target", |
| 990 | activity.getAdapter().getItem(0).getResolveInfo(), is(ri)); |
| 991 | |
| 992 | // Click on the direct target |
| 993 | String name = serviceTargets.get(0).getTitle().toString(); |
| 994 | onView(withText(name)) |
| 995 | .perform(click()); |
| 996 | waitForIdle(); |
| 997 | |
| 998 | // Currently we're seeing 3 invocations |
| 999 | // 1. ChooserActivity.onCreate() |
| 1000 | // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView() |
| 1001 | // 3. ChooserActivity.startSelected -- which is the one we're after |
| 1002 | verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture()); |
| 1003 | assertThat(logMakerCaptor.getAllValues().get(2).getCategory(), |
| 1004 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET)); |
| 1005 | String hashedName = (String) logMakerCaptor |
| 1006 | .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_HASHED_TARGET_NAME); |
| 1007 | assertThat("Hash is not predictable but must be obfuscated", |
| 1008 | hashedName, is(not(name))); |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1009 | assertThat("The packages shouldn't match for app target and direct target", logMakerCaptor |
| 1010 | .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(-1)); |
| 1011 | } |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1012 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1013 | // This test is too long and too slow and should not be taken as an example for future tests. |
| 1014 | @Test |
| 1015 | public void testDirectTargetLoggingWithRankedAppTarget() throws InterruptedException { |
| 1016 | Intent sendIntent = createSendTextIntent(); |
| 1017 | // We need app targets for direct targets to get displayed |
| 1018 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 1019 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 1020 | Mockito.anyBoolean(), |
| 1021 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1022 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1023 | // Set up resources |
| 1024 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 1025 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 1026 | // Create direct share target |
| 1027 | List<ChooserTarget> serviceTargets = createDirectShareTargets(1, |
| 1028 | resolvedComponentInfos.get(0).getResolveInfoAt(0).activityInfo.packageName); |
| 1029 | ResolveInfo ri = ResolverDataProvider.createResolveInfo(3, 0); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1030 | |
| 1031 | // Start activity |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1032 | final ChooserWrapperActivity activity = mActivityRule |
| 1033 | .launchActivity(Intent.createChooser(sendIntent, null)); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1034 | |
| 1035 | // Insert the direct share target |
| 1036 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1037 | () -> activity.getAdapter().addServiceResults( |
| 1038 | activity.createTestDisplayResolveInfo(sendIntent, |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1039 | ri, |
| 1040 | "testLabel", |
| 1041 | "testInfo", |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1042 | sendIntent, |
| 1043 | /* resolveInfoPresentationGetter */ null), |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1044 | serviceTargets, |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 1045 | TARGET_TYPE_CHOOSER_TARGET) |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1046 | ); |
| 1047 | // Thread.sleep shouldn't be a thing in an integration test but it's |
| 1048 | // necessary here because of the way the code is structured |
| 1049 | // TODO: restructure the tests b/129870719 |
| 1050 | Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 1051 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1052 | assertThat("Chooser should have 3 targets (2 apps, 1 direct)", |
| 1053 | activity.getAdapter().getCount(), is(3)); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1054 | assertThat("Chooser should have exactly one selectable direct target", |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1055 | activity.getAdapter().getSelectableServiceTargetCount(), is(1)); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1056 | assertThat("The resolver info must match the resolver info used to create the target", |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1057 | activity.getAdapter().getItem(0).getResolveInfo(), is(ri)); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1058 | |
| 1059 | // Click on the direct target |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1060 | String name = serviceTargets.get(0).getTitle().toString(); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1061 | onView(withText(name)) |
| 1062 | .perform(click()); |
| 1063 | waitForIdle(); |
| 1064 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1065 | // Currently we're seeing 3 invocations |
| 1066 | // 1. ChooserActivity.onCreate() |
| 1067 | // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView() |
| 1068 | // 3. ChooserActivity.startSelected -- which is the one we're after |
| 1069 | verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture()); |
| 1070 | assertThat(logMakerCaptor.getAllValues().get(2).getCategory(), |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1071 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET)); |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1072 | assertThat("The packages should match for app target and direct target", logMakerCaptor |
| 1073 | .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(0)); |
| 1074 | } |
| 1075 | |
| 1076 | // This test is too long and too slow and should not be taken as an example for future tests. |
| 1077 | @Test |
| 1078 | public void testDirectTargetLoggingWithAppTargetNotRanked() throws InterruptedException { |
| 1079 | Intent sendIntent = createSendTextIntent(); |
| 1080 | // We need app targets for direct targets to get displayed |
| 1081 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(15); |
| 1082 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 1083 | Mockito.anyBoolean(), |
| 1084 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 1085 | |
| 1086 | // Set up resources |
| 1087 | MetricsLogger mockLogger = sOverrides.metricsLogger; |
| 1088 | ArgumentCaptor<LogMaker> logMakerCaptor = ArgumentCaptor.forClass(LogMaker.class); |
| 1089 | // Create direct share target |
| 1090 | List<ChooserTarget> serviceTargets = createDirectShareTargets(1, |
| 1091 | resolvedComponentInfos.get(14).getResolveInfoAt(0).activityInfo.packageName); |
| 1092 | ResolveInfo ri = ResolverDataProvider.createResolveInfo(16, 0); |
| 1093 | |
| 1094 | // Start activity |
| 1095 | final ChooserWrapperActivity activity = mActivityRule |
| 1096 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 1097 | // Insert the direct share target |
| 1098 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
| 1099 | () -> activity.getAdapter().addServiceResults( |
| 1100 | activity.createTestDisplayResolveInfo(sendIntent, |
| 1101 | ri, |
| 1102 | "testLabel", |
| 1103 | "testInfo", |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 1104 | sendIntent, |
| 1105 | /* resolveInfoPresentationGetter */ null), |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1106 | serviceTargets, |
Mehdi Alizadeh | 06955f6 | 2019-09-11 17:23:10 -0700 | [diff] [blame] | 1107 | TARGET_TYPE_CHOOSER_TARGET) |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1108 | ); |
| 1109 | // Thread.sleep shouldn't be a thing in an integration test but it's |
| 1110 | // necessary here because of the way the code is structured |
| 1111 | // TODO: restructure the tests b/129870719 |
| 1112 | Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 1113 | |
| 1114 | assertThat("Chooser should have 20 targets (4 apps, 1 direct, 15 A-Z)", |
| 1115 | activity.getAdapter().getCount(), is(20)); |
| 1116 | assertThat("Chooser should have exactly one selectable direct target", |
| 1117 | activity.getAdapter().getSelectableServiceTargetCount(), is(1)); |
| 1118 | assertThat("The resolver info must match the resolver info used to create the target", |
| 1119 | activity.getAdapter().getItem(0).getResolveInfo(), is(ri)); |
| 1120 | |
| 1121 | // Click on the direct target |
| 1122 | String name = serviceTargets.get(0).getTitle().toString(); |
| 1123 | onView(withText(name)) |
| 1124 | .perform(click()); |
| 1125 | waitForIdle(); |
| 1126 | |
| 1127 | // Currently we're seeing 3 invocations |
| 1128 | // 1. ChooserActivity.onCreate() |
| 1129 | // 2. ChooserActivity$ChooserRowAdapter.createContentPreviewView() |
| 1130 | // 3. ChooserActivity.startSelected -- which is the one we're after |
| 1131 | verify(mockLogger, Mockito.times(3)).write(logMakerCaptor.capture()); |
| 1132 | assertThat(logMakerCaptor.getAllValues().get(2).getCategory(), |
| 1133 | is(MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET)); |
| 1134 | assertThat("The packages shouldn't match for app target and direct target", logMakerCaptor |
| 1135 | .getAllValues().get(2).getTaggedData(MetricsEvent.FIELD_RANKED_POSITION), is(-1)); |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1138 | private Intent createSendTextIntent() { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1139 | Intent sendIntent = new Intent(); |
| 1140 | sendIntent.setAction(Intent.ACTION_SEND); |
| 1141 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
Matt Pietal | cdfcd9a | 2019-03-05 08:31:47 -0500 | [diff] [blame] | 1142 | sendIntent.setType("text/plain"); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1143 | return sendIntent; |
| 1144 | } |
| 1145 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1146 | private Intent createSendTextIntentWithPreview(String title, Uri imageThumbnail) { |
| 1147 | Intent sendIntent = new Intent(); |
| 1148 | sendIntent.setAction(Intent.ACTION_SEND); |
| 1149 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
| 1150 | sendIntent.putExtra(Intent.EXTRA_TITLE, title); |
| 1151 | if (imageThumbnail != null) { |
| 1152 | ClipData.Item clipItem = new ClipData.Item(imageThumbnail); |
| 1153 | sendIntent.setClipData(new ClipData("Clip Label", new String[]{"image/png"}, clipItem)); |
| 1154 | } |
| 1155 | |
| 1156 | return sendIntent; |
| 1157 | } |
| 1158 | |
Matt Pietal | 46d828c | 2019-02-05 08:07:07 -0500 | [diff] [blame] | 1159 | private Intent createSendUriIntentWithPreview(ArrayList<Uri> uris) { |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 1160 | Intent sendIntent = new Intent(); |
| 1161 | |
| 1162 | if (uris.size() > 1) { |
| 1163 | sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE); |
| 1164 | sendIntent.putExtra(Intent.EXTRA_STREAM, uris); |
| 1165 | } else { |
| 1166 | sendIntent.setAction(Intent.ACTION_SEND); |
| 1167 | sendIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0)); |
| 1168 | } |
| 1169 | |
| 1170 | return sendIntent; |
| 1171 | } |
| 1172 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1173 | private Intent createViewTextIntent() { |
| 1174 | Intent viewIntent = new Intent(); |
| 1175 | viewIntent.setAction(Intent.ACTION_VIEW); |
| 1176 | viewIntent.putExtra(Intent.EXTRA_TEXT, "testing intent viewing"); |
| 1177 | return viewIntent; |
| 1178 | } |
| 1179 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1180 | private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) { |
| 1181 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 1182 | for (int i = 0; i < numberOfResults; i++) { |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 1183 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1184 | } |
| 1185 | return infoList; |
| 1186 | } |
| 1187 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 1188 | private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile( |
| 1189 | int numberOfResults) { |
| 1190 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 1191 | for (int i = 0; i < numberOfResults; i++) { |
| 1192 | if (i == 0) { |
| 1193 | infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i)); |
| 1194 | } else { |
| 1195 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
| 1196 | } |
| 1197 | } |
| 1198 | return infoList; |
| 1199 | } |
| 1200 | |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1201 | private List<ChooserTarget> createDirectShareTargets(int numberOfResults, String packageName) { |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1202 | Icon icon = Icon.createWithBitmap(createBitmap()); |
| 1203 | String testTitle = "testTitle"; |
| 1204 | List<ChooserTarget> targets = new ArrayList<>(); |
| 1205 | for (int i = 0; i < numberOfResults; i++) { |
Susi Kharraz-Post | 8c14f77 | 2019-04-17 16:33:41 -0400 | [diff] [blame] | 1206 | ComponentName componentName; |
| 1207 | if (packageName.isEmpty()) { |
| 1208 | componentName = ResolverDataProvider.createComponentName(i); |
| 1209 | } else { |
| 1210 | componentName = new ComponentName(packageName, packageName + ".class"); |
| 1211 | } |
Susi Kharraz-Post | 14cbfcd | 2019-04-01 11:07:59 -0400 | [diff] [blame] | 1212 | ChooserTarget tempTarget = new ChooserTarget( |
| 1213 | testTitle + i, |
| 1214 | icon, |
| 1215 | (float) (1 - ((i + 1) / 10.0)), |
| 1216 | componentName, |
| 1217 | null); |
| 1218 | targets.add(tempTarget); |
| 1219 | } |
| 1220 | return targets; |
| 1221 | } |
| 1222 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 1223 | private void waitForIdle() { |
| 1224 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 1225 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1226 | |
| 1227 | private Bitmap createBitmap() { |
| 1228 | int width = 200; |
| 1229 | int height = 200; |
| 1230 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 1231 | Canvas canvas = new Canvas(bitmap); |
| 1232 | |
| 1233 | Paint paint = new Paint(); |
| 1234 | paint.setColor(Color.RED); |
| 1235 | paint.setStyle(Paint.Style.FILL); |
| 1236 | canvas.drawPaint(paint); |
| 1237 | |
| 1238 | paint.setColor(Color.WHITE); |
| 1239 | paint.setAntiAlias(true); |
| 1240 | paint.setTextSize(14.f); |
| 1241 | paint.setTextAlign(Paint.Align.CENTER); |
| 1242 | canvas.drawText("Hi!", (width / 2.f), (height / 2.f), paint); |
| 1243 | |
| 1244 | return bitmap; |
| 1245 | } |
Mehdi Alizadeh | 707c0cf | 2019-09-03 18:11:48 -0700 | [diff] [blame] | 1246 | |
| 1247 | private List<ShareShortcutInfo> createShortcuts(Context context) { |
| 1248 | Intent testIntent = new Intent("TestIntent"); |
| 1249 | |
| 1250 | List<ShareShortcutInfo> shortcuts = new ArrayList<>(); |
| 1251 | shortcuts.add(new ShareShortcutInfo( |
| 1252 | new ShortcutInfo.Builder(context, "shortcut1") |
| 1253 | .setIntent(testIntent).setShortLabel("label1").setRank(3).build(), // 0 2 |
| 1254 | new ComponentName("package1", "class1"))); |
| 1255 | shortcuts.add(new ShareShortcutInfo( |
| 1256 | new ShortcutInfo.Builder(context, "shortcut2") |
| 1257 | .setIntent(testIntent).setShortLabel("label2").setRank(7).build(), // 1 3 |
| 1258 | new ComponentName("package2", "class2"))); |
| 1259 | shortcuts.add(new ShareShortcutInfo( |
| 1260 | new ShortcutInfo.Builder(context, "shortcut3") |
| 1261 | .setIntent(testIntent).setShortLabel("label3").setRank(1).build(), // 2 0 |
| 1262 | new ComponentName("package3", "class3"))); |
| 1263 | shortcuts.add(new ShareShortcutInfo( |
| 1264 | new ShortcutInfo.Builder(context, "shortcut4") |
| 1265 | .setIntent(testIntent).setShortLabel("label4").setRank(3).build(), // 3 2 |
| 1266 | new ComponentName("package4", "class4"))); |
| 1267 | |
| 1268 | return shortcuts; |
| 1269 | } |
| 1270 | |
| 1271 | private void assertCorrectShortcutToChooserTargetConversion(List<ShareShortcutInfo> shortcuts, |
| 1272 | List<ChooserTarget> chooserTargets, int[] expectedOrder, float[] expectedScores) { |
| 1273 | assertEquals(expectedOrder.length, chooserTargets.size()); |
| 1274 | for (int i = 0; i < chooserTargets.size(); i++) { |
| 1275 | ChooserTarget ct = chooserTargets.get(i); |
| 1276 | ShortcutInfo si = shortcuts.get(expectedOrder[i]).getShortcutInfo(); |
| 1277 | ComponentName cn = shortcuts.get(expectedOrder[i]).getTargetComponent(); |
| 1278 | |
| 1279 | assertEquals(si.getId(), ct.getIntentExtras().getString(Intent.EXTRA_SHORTCUT_ID)); |
| 1280 | assertEquals(si.getShortLabel(), ct.getTitle()); |
| 1281 | assertThat(Math.abs(expectedScores[i] - ct.getScore()) < 0.000001, is(true)); |
| 1282 | assertEquals(cn.flattenToString(), ct.getComponentName().flattenToString()); |
| 1283 | } |
| 1284 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 1285 | } |