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; |
| 21 | import static androidx.test.espresso.assertion.ViewAssertions.matches; |
| 22 | import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 23 | import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 24 | import static androidx.test.espresso.matcher.ViewMatchers.withText; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 25 | |
| 26 | import static com.android.internal.app.ChooserWrapperActivity.sOverrides; |
| 27 | |
| 28 | import static org.hamcrest.CoreMatchers.is; |
| 29 | import static org.hamcrest.CoreMatchers.not; |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.mockito.Mockito.times; |
| 32 | import static org.mockito.Mockito.verify; |
| 33 | import static org.mockito.Mockito.when; |
| 34 | |
| 35 | import android.app.usage.UsageStatsManager; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 36 | import android.content.ClipData; |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 37 | import android.content.ClipDescription; |
| 38 | import android.content.ClipboardManager; |
| 39 | import android.content.Context; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 40 | import android.content.Intent; |
| 41 | import android.content.pm.ResolveInfo; |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.Canvas; |
| 44 | import android.graphics.Color; |
| 45 | import android.graphics.Paint; |
| 46 | import android.net.Uri; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 47 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 48 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 49 | import androidx.test.platform.app.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 50 | import androidx.test.rule.ActivityTestRule; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 51 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 52 | import com.android.internal.R; |
| 53 | import com.android.internal.app.ResolverActivity.ResolvedComponentInfo; |
| 54 | |
| 55 | import org.junit.Before; |
| 56 | import org.junit.Rule; |
| 57 | import org.junit.Test; |
| 58 | import org.junit.runner.RunWith; |
| 59 | import org.mockito.Mockito; |
| 60 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 61 | import java.util.ArrayList; |
| 62 | import java.util.List; |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Chooser activity instrumentation tests |
| 66 | */ |
| 67 | @RunWith(AndroidJUnit4.class) |
| 68 | public class ChooserActivityTest { |
| 69 | @Rule |
| 70 | public ActivityTestRule<ChooserWrapperActivity> mActivityRule = |
| 71 | new ActivityTestRule<>(ChooserWrapperActivity.class, false, |
| 72 | false); |
| 73 | |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 74 | @Before |
| 75 | public void cleanOverrideData() { |
| 76 | sOverrides.reset(); |
| 77 | } |
| 78 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 79 | @Test |
| 80 | public void customTitle() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 81 | Intent viewIntent = createViewTextIntent(); |
| 82 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 83 | |
| 84 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 85 | Mockito.anyBoolean(), |
| 86 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 87 | mActivityRule.launchActivity(Intent.createChooser(viewIntent, "chooser test")); |
| 88 | |
| 89 | waitForIdle(); |
| 90 | onView(withId(R.id.title)).check(matches(withText("chooser test"))); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void customTitleIgnoredForSendIntents() throws InterruptedException { |
| 95 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 96 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 97 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 98 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 99 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 100 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 101 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "chooser test")); |
| 102 | waitForIdle(); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 103 | onView(withId(R.id.title)).check(matches(withText(R.string.whichSendApplication))); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void emptyTitle() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 108 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 109 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 110 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 111 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 112 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame] | 113 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 114 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 115 | waitForIdle(); |
| 116 | onView(withId(R.id.title)) |
| 117 | .check(matches(withText(R.string.whichSendApplication))); |
| 118 | } |
| 119 | |
| 120 | @Test |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 121 | public void emptyPreviewTitleAndThumbnail() throws InterruptedException { |
| 122 | Intent sendIntent = createSendTextIntentWithPreview(null, null); |
| 123 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 124 | |
| 125 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 126 | Mockito.anyBoolean(), |
| 127 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 128 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 129 | waitForIdle(); |
| 130 | onView(withId(R.id.content_preview_title)).check(matches(not(isDisplayed()))); |
| 131 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void visiblePreviewTitleWithoutThumbnail() throws InterruptedException { |
| 136 | String previewTitle = "My Content Preview Title"; |
| 137 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, null); |
| 138 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 139 | |
| 140 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 141 | Mockito.anyBoolean(), |
| 142 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 143 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 144 | waitForIdle(); |
| 145 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 146 | onView(withId(R.id.content_preview_title)).check(matches(withText(previewTitle))); |
| 147 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void visiblePreviewTitleWithInvalidThumbnail() throws InterruptedException { |
| 152 | String previewTitle = "My Content Preview Title"; |
| 153 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, |
| 154 | Uri.parse("tel:(+49)12345789")); |
| 155 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 156 | |
| 157 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 158 | Mockito.anyBoolean(), |
| 159 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 160 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 161 | waitForIdle(); |
| 162 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 163 | onView(withId(R.id.content_preview_thumbnail)).check(matches(not(isDisplayed()))); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void visiblePreviewTitleAndThumbnail() throws InterruptedException { |
| 168 | String previewTitle = "My Content Preview Title"; |
| 169 | Intent sendIntent = createSendTextIntentWithPreview(previewTitle, |
| 170 | Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 171 | + com.android.frameworks.coretests.R.drawable.test320x240)); |
| 172 | sOverrides.previewThumbnail = createBitmap(); |
| 173 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 174 | |
| 175 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 176 | Mockito.anyBoolean(), |
| 177 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 178 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 179 | waitForIdle(); |
| 180 | onView(withId(R.id.content_preview_title)).check(matches(isDisplayed())); |
| 181 | onView(withId(R.id.content_preview_thumbnail)).check(matches(isDisplayed())); |
| 182 | } |
| 183 | |
| 184 | @Test |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 185 | public void twoOptionsAndUserSelectsOne() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 186 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 187 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 188 | |
| 189 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 190 | Mockito.anyBoolean(), |
| 191 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 192 | |
| 193 | final ChooserWrapperActivity activity = mActivityRule |
| 194 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 195 | waitForIdle(); |
| 196 | |
| 197 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 198 | onView(withId(R.id.profile_button)).check(matches(not(isDisplayed()))); |
| 199 | |
| 200 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 201 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 202 | chosen[0] = targetInfo.getResolveInfo(); |
| 203 | return true; |
| 204 | }; |
| 205 | |
| 206 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
| 207 | onView(withText(toChoose.activityInfo.name)) |
| 208 | .perform(click()); |
| 209 | waitForIdle(); |
| 210 | assertThat(chosen[0], is(toChoose)); |
| 211 | } |
| 212 | |
| 213 | @Test |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 214 | public void updateChooserCountsAndModelAfterUserSelection() throws InterruptedException { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 215 | Intent sendIntent = createSendTextIntent(); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 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 | |
| 222 | final ChooserWrapperActivity activity = mActivityRule |
| 223 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 224 | waitForIdle(); |
| 225 | UsageStatsManager usm = activity.getUsageStatsManager(); |
| 226 | verify(sOverrides.resolverListController, times(1)) |
| 227 | .sort(Mockito.any(List.class)); |
| 228 | assertThat(activity.getIsSelected(), is(false)); |
| 229 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 230 | return true; |
| 231 | }; |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 232 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 233 | onView(withText(toChoose.activityInfo.name)) |
| 234 | .perform(click()); |
| 235 | waitForIdle(); |
| 236 | verify(sOverrides.resolverListController, times(1)) |
Kang Li | 9fa2a2c | 2017-01-06 13:33:24 -0800 | [diff] [blame] | 237 | .updateChooserCounts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString()); |
| 238 | verify(sOverrides.resolverListController, times(1)) |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 239 | .updateModel(toChoose.activityInfo.getComponentName()); |
| 240 | assertThat(activity.getIsSelected(), is(true)); |
Kang Li | 64b018e | 2017-01-05 17:30:06 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Test |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 244 | public void noResultsFromPackageManager() { |
| 245 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 246 | Mockito.anyBoolean(), |
| 247 | Mockito.isA(List.class))).thenReturn(null); |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 248 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 249 | final ChooserWrapperActivity activity = mActivityRule |
| 250 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 251 | waitForIdle(); |
| 252 | assertThat(activity.isFinishing(), is(false)); |
| 253 | |
| 254 | onView(withId(R.id.empty)).check(matches(isDisplayed())); |
| 255 | onView(withId(R.id.resolver_list)).check(matches(not(isDisplayed()))); |
| 256 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
| 257 | () -> activity.getAdapter().handlePackagesChanged() |
| 258 | ); |
| 259 | // backward compatibility. looks like we finish when data is empty after package change |
| 260 | assertThat(activity.isFinishing(), is(true)); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void autoLaunchSingleResult() throws InterruptedException { |
| 265 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 266 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 267 | chosen[0] = targetInfo.getResolveInfo(); |
| 268 | return true; |
| 269 | }; |
| 270 | |
| 271 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(1); |
| 272 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 273 | Mockito.anyBoolean(), |
| 274 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 275 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 276 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 277 | final ChooserWrapperActivity activity = mActivityRule |
| 278 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 279 | waitForIdle(); |
| 280 | |
| 281 | assertThat(chosen[0], is(resolvedComponentInfos.get(0).getResolveInfoAt(0))); |
| 282 | assertThat(activity.isFinishing(), is(true)); |
| 283 | } |
| 284 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 285 | @Test |
| 286 | public void hasOtherProfileOneOption() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 287 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 288 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 289 | createResolvedComponentsForTestWithOtherProfile(2); |
| 290 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 291 | |
| 292 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 293 | Mockito.anyBoolean(), |
| 294 | Mockito.anyBoolean(), |
| 295 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 296 | |
| 297 | final ChooserWrapperActivity activity = mActivityRule |
| 298 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 299 | waitForIdle(); |
| 300 | |
| 301 | // The other entry is filtered to the other profile slot |
| 302 | assertThat(activity.getAdapter().getCount(), is(1)); |
| 303 | |
| 304 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 305 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 306 | chosen[0] = targetInfo.getResolveInfo(); |
| 307 | return true; |
| 308 | }; |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 309 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 310 | // Make a stable copy of the components as the original list may be modified |
| 311 | List<ResolvedComponentInfo> stableCopy = |
| 312 | createResolvedComponentsForTestWithOtherProfile(2); |
| 313 | // Check that the "Other Profile" activity is put in the right spot |
| 314 | onView(withId(R.id.profile_button)).check(matches( |
| 315 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 316 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 317 | .perform(click()); |
| 318 | waitForIdle(); |
| 319 | assertThat(chosen[0], is(toChoose)); |
| 320 | } |
| 321 | |
| 322 | @Test |
| 323 | public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 324 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 325 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 326 | createResolvedComponentsForTestWithOtherProfile(3); |
| 327 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 328 | |
| 329 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 330 | Mockito.anyBoolean(), |
| 331 | Mockito.anyBoolean(), |
| 332 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 333 | when(ChooserWrapperActivity.sOverrides.resolverListController.getLastChosen()) |
| 334 | .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0)); |
| 335 | |
| 336 | final ChooserWrapperActivity activity = mActivityRule |
| 337 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 338 | waitForIdle(); |
| 339 | |
| 340 | // The other entry is filtered to the other profile slot |
| 341 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 342 | |
| 343 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 344 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 345 | chosen[0] = targetInfo.getResolveInfo(); |
| 346 | return true; |
| 347 | }; |
| 348 | |
| 349 | // Make a stable copy of the components as the original list may be modified |
| 350 | List<ResolvedComponentInfo> stableCopy = |
| 351 | createResolvedComponentsForTestWithOtherProfile(3); |
| 352 | // Check that the "Other Profile" activity is put in the right spot |
| 353 | onView(withId(R.id.profile_button)).check(matches( |
| 354 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 355 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 356 | .perform(click()); |
| 357 | waitForIdle(); |
| 358 | assertThat(chosen[0], is(toChoose)); |
| 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void hasLastChosenActivityAndOtherProfile() throws Exception { |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 363 | Intent sendIntent = createSendTextIntent(); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 364 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 365 | createResolvedComponentsForTestWithOtherProfile(3); |
| 366 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 367 | |
| 368 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 369 | Mockito.anyBoolean(), |
| 370 | Mockito.anyBoolean(), |
| 371 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 372 | |
| 373 | final ChooserWrapperActivity activity = mActivityRule |
| 374 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 375 | waitForIdle(); |
| 376 | |
| 377 | // The other entry is filtered to the last used slot |
| 378 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 379 | |
| 380 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 381 | ChooserWrapperActivity.sOverrides.onSafelyStartCallback = targetInfo -> { |
| 382 | chosen[0] = targetInfo.getResolveInfo(); |
| 383 | return true; |
| 384 | }; |
| 385 | |
| 386 | // Make a stable copy of the components as the original list may be modified |
| 387 | List<ResolvedComponentInfo> stableCopy = |
| 388 | createResolvedComponentsForTestWithOtherProfile(3); |
| 389 | // Check that the "Other Profile" activity is put in the right spot |
| 390 | onView(withId(R.id.profile_button)).check(matches( |
| 391 | withText(stableCopy.get(0).getResolveInfoAt(0).activityInfo.name))); |
| 392 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 393 | .perform(click()); |
| 394 | waitForIdle(); |
| 395 | assertThat(chosen[0], is(toChoose)); |
| 396 | } |
| 397 | |
Matt Pietal | 1fa7d80 | 2019-01-30 10:44:15 -0500 | [diff] [blame] | 398 | @Test |
| 399 | public void copyTextToClipboard() throws Exception { |
| 400 | Intent sendIntent = createSendTextIntent(); |
| 401 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 402 | createResolvedComponentsForTestWithOtherProfile(1); |
| 403 | |
| 404 | when(ChooserWrapperActivity.sOverrides.resolverListController.getResolversForIntent( |
| 405 | Mockito.anyBoolean(), |
| 406 | Mockito.anyBoolean(), |
| 407 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 408 | |
| 409 | final ChooserWrapperActivity activity = mActivityRule |
| 410 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 411 | waitForIdle(); |
| 412 | |
| 413 | onView(withId(R.id.copy_button)).perform(click()); |
| 414 | |
| 415 | ClipboardManager clipboard = (ClipboardManager) activity.getSystemService( |
| 416 | Context.CLIPBOARD_SERVICE); |
| 417 | ClipData clipData = clipboard.getPrimaryClip(); |
| 418 | assertThat("testing intent sending", is(clipData.getItemAt(0).getText())); |
| 419 | |
| 420 | ClipDescription clipDescription = clipData.getDescription(); |
| 421 | assertThat("text/plain", is(clipDescription.getMimeType(0))); |
| 422 | } |
| 423 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame^] | 424 | @Test |
| 425 | public void oneVisibleImagePreview() throws InterruptedException { |
| 426 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 427 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 428 | |
| 429 | ArrayList<Uri> uris = new ArrayList<>(); |
| 430 | uris.add(uri); |
| 431 | |
| 432 | Intent sendIntent = createSendImageIntentWithPreview(uris); |
| 433 | sOverrides.previewThumbnail = createBitmap(); |
| 434 | |
| 435 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 436 | |
| 437 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 438 | Mockito.anyBoolean(), |
| 439 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 440 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 441 | waitForIdle(); |
| 442 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 443 | onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed()))); |
| 444 | onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed()))); |
| 445 | onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed()))); |
| 446 | } |
| 447 | |
| 448 | @Test |
| 449 | public void twoVisibleImagePreview() throws InterruptedException { |
| 450 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 451 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 452 | |
| 453 | ArrayList<Uri> uris = new ArrayList<>(); |
| 454 | uris.add(uri); |
| 455 | uris.add(uri); |
| 456 | |
| 457 | Intent sendIntent = createSendImageIntentWithPreview(uris); |
| 458 | sOverrides.previewThumbnail = createBitmap(); |
| 459 | |
| 460 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 461 | |
| 462 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 463 | Mockito.anyBoolean(), |
| 464 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 465 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 466 | waitForIdle(); |
| 467 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 468 | onView(withId(R.id.content_preview_image_2_large)).check(matches(isDisplayed())); |
| 469 | onView(withId(R.id.content_preview_image_2_small)).check(matches(not(isDisplayed()))); |
| 470 | onView(withId(R.id.content_preview_image_3_small)).check(matches(not(isDisplayed()))); |
| 471 | } |
| 472 | |
| 473 | @Test |
| 474 | public void threeOrMoreVisibleImagePreview() throws InterruptedException { |
| 475 | Uri uri = Uri.parse("android.resource://com.android.frameworks.coretests/" |
| 476 | + com.android.frameworks.coretests.R.drawable.test320x240); |
| 477 | |
| 478 | ArrayList<Uri> uris = new ArrayList<>(); |
| 479 | uris.add(uri); |
| 480 | uris.add(uri); |
| 481 | uris.add(uri); |
| 482 | uris.add(uri); |
| 483 | uris.add(uri); |
| 484 | |
| 485 | Intent sendIntent = createSendImageIntentWithPreview(uris); |
| 486 | sOverrides.previewThumbnail = createBitmap(); |
| 487 | |
| 488 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 489 | |
| 490 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 491 | Mockito.anyBoolean(), |
| 492 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 493 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 494 | waitForIdle(); |
| 495 | onView(withId(R.id.content_preview_image_1_large)).check(matches(isDisplayed())); |
| 496 | onView(withId(R.id.content_preview_image_2_large)).check(matches(not(isDisplayed()))); |
| 497 | onView(withId(R.id.content_preview_image_2_small)).check(matches(isDisplayed())); |
| 498 | onView(withId(R.id.content_preview_image_3_small)).check(matches(isDisplayed())); |
| 499 | } |
| 500 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 501 | private Intent createSendTextIntent() { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 502 | Intent sendIntent = new Intent(); |
| 503 | sendIntent.setAction(Intent.ACTION_SEND); |
| 504 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 505 | return sendIntent; |
| 506 | } |
| 507 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 508 | private Intent createSendTextIntentWithPreview(String title, Uri imageThumbnail) { |
| 509 | Intent sendIntent = new Intent(); |
| 510 | sendIntent.setAction(Intent.ACTION_SEND); |
| 511 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
| 512 | sendIntent.putExtra(Intent.EXTRA_TITLE, title); |
| 513 | if (imageThumbnail != null) { |
| 514 | ClipData.Item clipItem = new ClipData.Item(imageThumbnail); |
| 515 | sendIntent.setClipData(new ClipData("Clip Label", new String[]{"image/png"}, clipItem)); |
| 516 | } |
| 517 | |
| 518 | return sendIntent; |
| 519 | } |
| 520 | |
Matt Pietal | 0ea391b | 2019-01-30 10:44:15 -0500 | [diff] [blame^] | 521 | private Intent createSendImageIntentWithPreview(ArrayList<Uri> uris) { |
| 522 | Intent sendIntent = new Intent(); |
| 523 | |
| 524 | if (uris.size() > 1) { |
| 525 | sendIntent.setAction(Intent.ACTION_SEND_MULTIPLE); |
| 526 | sendIntent.putExtra(Intent.EXTRA_STREAM, uris); |
| 527 | } else { |
| 528 | sendIntent.setAction(Intent.ACTION_SEND); |
| 529 | sendIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0)); |
| 530 | } |
| 531 | |
| 532 | return sendIntent; |
| 533 | } |
| 534 | |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 535 | private Intent createViewTextIntent() { |
| 536 | Intent viewIntent = new Intent(); |
| 537 | viewIntent.setAction(Intent.ACTION_VIEW); |
| 538 | viewIntent.putExtra(Intent.EXTRA_TEXT, "testing intent viewing"); |
| 539 | return viewIntent; |
| 540 | } |
| 541 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 542 | private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) { |
| 543 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 544 | for (int i = 0; i < numberOfResults; i++) { |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 545 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 546 | } |
| 547 | return infoList; |
| 548 | } |
| 549 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 550 | private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile( |
| 551 | int numberOfResults) { |
| 552 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 553 | for (int i = 0; i < numberOfResults; i++) { |
| 554 | if (i == 0) { |
| 555 | infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i)); |
| 556 | } else { |
| 557 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
| 558 | } |
| 559 | } |
| 560 | return infoList; |
| 561 | } |
| 562 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 563 | private void waitForIdle() { |
| 564 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 565 | } |
Matt Pietal | 2603840 | 2019-01-08 07:29:34 -0500 | [diff] [blame] | 566 | |
| 567 | private Bitmap createBitmap() { |
| 568 | int width = 200; |
| 569 | int height = 200; |
| 570 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 571 | Canvas canvas = new Canvas(bitmap); |
| 572 | |
| 573 | Paint paint = new Paint(); |
| 574 | paint.setColor(Color.RED); |
| 575 | paint.setStyle(Paint.Style.FILL); |
| 576 | canvas.drawPaint(paint); |
| 577 | |
| 578 | paint.setColor(Color.WHITE); |
| 579 | paint.setAntiAlias(true); |
| 580 | paint.setTextSize(14.f); |
| 581 | paint.setTextAlign(Paint.Align.CENTER); |
| 582 | canvas.drawText("Hi!", (width / 2.f), (height / 2.f), paint); |
| 583 | |
| 584 | return bitmap; |
| 585 | } |
| 586 | } |