Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -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 | |
Tadashi G. Takaoka | 7a4047f | 2019-02-02 09:55:59 +0900 | [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; |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 22 | import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 23 | import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; |
Tadashi G. Takaoka | 7a4047f | 2019-02-02 09:55:59 +0900 | [diff] [blame] | 24 | import static androidx.test.espresso.matcher.ViewMatchers.isEnabled; |
| 25 | import static androidx.test.espresso.matcher.ViewMatchers.withId; |
| 26 | import static androidx.test.espresso.matcher.ViewMatchers.withText; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 27 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 28 | import static com.android.internal.app.MatcherUtils.first; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 29 | import static com.android.internal.app.ResolverDataProvider.createPackageManagerMockedInfo; |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 30 | import static com.android.internal.app.ResolverDataProvider.createResolvedComponentInfoWithOtherId; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 31 | import static com.android.internal.app.ResolverWrapperActivity.sOverrides; |
| 32 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 33 | import static org.hamcrest.CoreMatchers.allOf; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 34 | import static org.hamcrest.CoreMatchers.is; |
| 35 | import static org.hamcrest.CoreMatchers.not; |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 37 | import static org.mockito.ArgumentMatchers.eq; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 38 | import static org.mockito.Mockito.when; |
| 39 | |
| 40 | import android.content.Intent; |
| 41 | import android.content.pm.ResolveInfo; |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 42 | import android.os.UserHandle; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 43 | import android.text.TextUtils; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 44 | import android.view.View; |
| 45 | import android.widget.RelativeLayout; |
| 46 | |
| 47 | import androidx.test.InstrumentationRegistry; |
Zhen Zhang | cb55c8a | 2019-10-22 14:49:09 -0700 | [diff] [blame] | 48 | import androidx.test.espresso.Espresso; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 49 | import androidx.test.rule.ActivityTestRule; |
| 50 | import androidx.test.runner.AndroidJUnit4; |
| 51 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 52 | import com.android.internal.R; |
| 53 | import com.android.internal.app.ResolverActivity.ResolvedComponentInfo; |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 54 | import com.android.internal.app.ResolverDataProvider.PackageManagerMockedInfo; |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 55 | import com.android.internal.app.ResolverListAdapter.ActivityInfoPresentationGetter; |
| 56 | import com.android.internal.app.ResolverListAdapter.ResolveInfoPresentationGetter; |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 57 | import com.android.internal.widget.ResolverDrawerLayout; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 58 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 59 | import org.hamcrest.BaseMatcher; |
| 60 | import org.hamcrest.Description; |
| 61 | import org.hamcrest.Matcher; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 62 | import org.junit.Before; |
Alison Cichowlas | c1a1bfe | 2019-11-21 15:52:27 -0500 | [diff] [blame] | 63 | import org.junit.Ignore; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 64 | import org.junit.Rule; |
| 65 | import org.junit.Test; |
| 66 | import org.junit.runner.RunWith; |
| 67 | import org.mockito.Mockito; |
| 68 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.List; |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Resolver activity instrumentation tests |
| 74 | */ |
| 75 | @RunWith(AndroidJUnit4.class) |
| 76 | public class ResolverActivityTest { |
| 77 | @Rule |
| 78 | public ActivityTestRule<ResolverWrapperActivity> mActivityRule = |
| 79 | new ActivityTestRule<>(ResolverWrapperActivity.class, false, |
| 80 | false); |
| 81 | |
| 82 | @Before |
| 83 | public void cleanOverrideData() { |
| 84 | sOverrides.reset(); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void twoOptionsAndUserSelectsOne() throws InterruptedException { |
| 89 | Intent sendIntent = createSendImageIntent(); |
| 90 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 91 | |
| 92 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 93 | Mockito.anyBoolean(), |
| 94 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 95 | |
| 96 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 97 | Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource()); |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 98 | waitForIdle(); |
| 99 | |
| 100 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 101 | |
| 102 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 103 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 104 | chosen[0] = targetInfo.getResolveInfo(); |
| 105 | return true; |
| 106 | }; |
| 107 | |
| 108 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
| 109 | onView(withText(toChoose.activityInfo.name)) |
| 110 | .perform(click()); |
| 111 | onView(withId(R.id.button_once)) |
| 112 | .perform(click()); |
| 113 | waitForIdle(); |
| 114 | assertThat(chosen[0], is(toChoose)); |
| 115 | } |
| 116 | |
Alison Cichowlas | c1a1bfe | 2019-11-21 15:52:27 -0500 | [diff] [blame] | 117 | @Ignore // Failing - b/144929805 |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 118 | @Test |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 119 | public void setMaxHeight() throws Exception { |
| 120 | Intent sendIntent = createSendImageIntent(); |
| 121 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 122 | |
| 123 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 124 | Mockito.anyBoolean(), |
| 125 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 126 | waitForIdle(); |
| 127 | |
| 128 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 129 | final View viewPager = activity.findViewById(R.id.profile_pager); |
| 130 | final int initialResolverHeight = viewPager.getHeight(); |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 131 | |
| 132 | activity.runOnUiThread(() -> { |
| 133 | ResolverDrawerLayout layout = (ResolverDrawerLayout) |
| 134 | activity.findViewById( |
| 135 | R.id.contentPanel); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 136 | ((ResolverDrawerLayout.LayoutParams) viewPager.getLayoutParams()).maxHeight |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 137 | = initialResolverHeight - 1; |
| 138 | // Force a relayout |
| 139 | layout.invalidate(); |
| 140 | layout.requestLayout(); |
| 141 | }); |
| 142 | waitForIdle(); |
| 143 | assertThat("Drawer should be capped at maxHeight", |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 144 | viewPager.getHeight() == (initialResolverHeight - 1)); |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 145 | |
| 146 | activity.runOnUiThread(() -> { |
| 147 | ResolverDrawerLayout layout = (ResolverDrawerLayout) |
| 148 | activity.findViewById( |
| 149 | R.id.contentPanel); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 150 | ((ResolverDrawerLayout.LayoutParams) viewPager.getLayoutParams()).maxHeight |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 151 | = initialResolverHeight + 1; |
| 152 | // Force a relayout |
| 153 | layout.invalidate(); |
| 154 | layout.requestLayout(); |
| 155 | }); |
| 156 | waitForIdle(); |
| 157 | assertThat("Drawer should not change height if its height is less than maxHeight", |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 158 | viewPager.getHeight() == initialResolverHeight); |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Alison Cichowlas | c1a1bfe | 2019-11-21 15:52:27 -0500 | [diff] [blame] | 161 | @Ignore // Failing - b/144929805 |
Ben Lin | 058ab3e | 2018-02-23 11:28:09 -0800 | [diff] [blame] | 162 | @Test |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 163 | public void setShowAtTopToTrue() throws Exception { |
| 164 | Intent sendIntent = createSendImageIntent(); |
| 165 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 166 | |
| 167 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 168 | Mockito.anyBoolean(), |
| 169 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 170 | waitForIdle(); |
| 171 | |
| 172 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 173 | final View viewPager = activity.findViewById(R.id.profile_pager); |
| 174 | final View divider = activity.findViewById(R.id.divider); |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 175 | final RelativeLayout profileView = |
| 176 | (RelativeLayout) activity.findViewById(R.id.profile_button).getParent(); |
| 177 | assertThat("Drawer should show at bottom by default", |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 178 | profileView.getBottom() + divider.getHeight() == viewPager.getTop() |
| 179 | && profileView.getTop() > 0); |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 180 | |
| 181 | activity.runOnUiThread(() -> { |
| 182 | ResolverDrawerLayout layout = (ResolverDrawerLayout) |
| 183 | activity.findViewById( |
| 184 | R.id.contentPanel); |
| 185 | layout.setShowAtTop(true); |
| 186 | }); |
| 187 | waitForIdle(); |
| 188 | assertThat("Drawer should show at top with new attribute", |
arangelov | 2c1c37a | 2019-12-06 14:43:34 +0000 | [diff] [blame] | 189 | profileView.getBottom() + divider.getHeight() == viewPager.getTop() |
| 190 | && profileView.getTop() == 0); |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Test |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 194 | public void hasLastChosenActivity() throws Exception { |
| 195 | Intent sendIntent = createSendImageIntent(); |
| 196 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 197 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 198 | |
| 199 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 200 | Mockito.anyBoolean(), |
| 201 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 202 | when(sOverrides.resolverListController.getLastChosen()) |
| 203 | .thenReturn(resolvedComponentInfos.get(0).getResolveInfoAt(0)); |
| 204 | |
| 205 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 206 | waitForIdle(); |
| 207 | |
| 208 | // The other entry is filtered to the last used slot |
| 209 | assertThat(activity.getAdapter().getCount(), is(1)); |
Sumir Kataria | db688af | 2017-05-10 17:33:47 -0700 | [diff] [blame] | 210 | assertThat(activity.getAdapter().getPlaceholderCount(), is(1)); |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 211 | |
| 212 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 213 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 214 | chosen[0] = targetInfo.getResolveInfo(); |
| 215 | return true; |
| 216 | }; |
| 217 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 218 | onView(withId(R.id.button_once)).perform(click()); |
| 219 | waitForIdle(); |
| 220 | assertThat(chosen[0], is(toChoose)); |
| 221 | } |
| 222 | |
| 223 | @Test |
| 224 | public void hasOtherProfileOneOption() throws Exception { |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 225 | // enable the work tab feature flag |
| 226 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 227 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 228 | Intent sendIntent = createSendImageIntent(); |
| 229 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 230 | createResolvedComponentsForTestWithOtherProfile(2); |
| 231 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 232 | |
| 233 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 234 | Mockito.anyBoolean(), |
| 235 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 236 | |
| 237 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 238 | Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource()); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 239 | waitForIdle(); |
| 240 | |
| 241 | // The other entry is filtered to the last used slot |
| 242 | assertThat(activity.getAdapter().getCount(), is(1)); |
| 243 | |
| 244 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 245 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 246 | chosen[0] = targetInfo.getResolveInfo(); |
| 247 | return true; |
| 248 | }; |
| 249 | |
| 250 | // Make a stable copy of the components as the original list may be modified |
| 251 | List<ResolvedComponentInfo> stableCopy = |
| 252 | createResolvedComponentsForTestWithOtherProfile(2); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 253 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 254 | .perform(click()); |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 255 | onView(withId(R.id.button_once)) |
| 256 | .perform(click()); |
| 257 | waitForIdle(); |
| 258 | assertThat(chosen[0], is(toChoose)); |
| 259 | } |
| 260 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 261 | @Test |
| 262 | public void hasOtherProfileTwoOptionsAndUserSelectsOne() throws Exception { |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 263 | // enable the work tab feature flag |
| 264 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 265 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 266 | Intent sendIntent = createSendImageIntent(); |
| 267 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 268 | createResolvedComponentsForTestWithOtherProfile(3); |
| 269 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 270 | |
| 271 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 272 | Mockito.anyBoolean(), |
| 273 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 274 | |
| 275 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 276 | Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource()); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 277 | waitForIdle(); |
| 278 | |
| 279 | // The other entry is filtered to the other profile slot |
| 280 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 281 | |
| 282 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 283 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 284 | chosen[0] = targetInfo.getResolveInfo(); |
| 285 | return true; |
| 286 | }; |
| 287 | |
| 288 | // Confirm that the button bar is disabled by default |
| 289 | onView(withId(R.id.button_once)).check(matches(not(isEnabled()))); |
| 290 | |
| 291 | // Make a stable copy of the components as the original list may be modified |
| 292 | List<ResolvedComponentInfo> stableCopy = |
| 293 | createResolvedComponentsForTestWithOtherProfile(2); |
| 294 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 295 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 296 | .perform(click()); |
| 297 | onView(withId(R.id.button_once)).perform(click()); |
| 298 | waitForIdle(); |
| 299 | assertThat(chosen[0], is(toChoose)); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | @Test |
| 304 | public void hasLastChosenActivityAndOtherProfile() throws Exception { |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 305 | // enable the work tab feature flag |
| 306 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 307 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 308 | // In this case we prefer the other profile and don't display anything about the last |
| 309 | // chosen activity. |
| 310 | Intent sendIntent = createSendImageIntent(); |
| 311 | List<ResolvedComponentInfo> resolvedComponentInfos = |
| 312 | createResolvedComponentsForTestWithOtherProfile(3); |
| 313 | ResolveInfo toChoose = resolvedComponentInfos.get(1).getResolveInfoAt(0); |
| 314 | |
| 315 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 316 | Mockito.anyBoolean(), |
| 317 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 318 | when(sOverrides.resolverListController.getLastChosen()) |
| 319 | .thenReturn(resolvedComponentInfos.get(1).getResolveInfoAt(0)); |
| 320 | |
| 321 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
arangelov | b0802dc | 2019-10-18 18:03:44 +0100 | [diff] [blame] | 322 | Espresso.registerIdlingResources(activity.getAdapter().getLabelIdlingResource()); |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 323 | waitForIdle(); |
| 324 | |
| 325 | // The other entry is filtered to the other profile slot |
| 326 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 327 | |
| 328 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 329 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 330 | chosen[0] = targetInfo.getResolveInfo(); |
| 331 | return true; |
| 332 | }; |
| 333 | |
| 334 | // Confirm that the button bar is disabled by default |
| 335 | onView(withId(R.id.button_once)).check(matches(not(isEnabled()))); |
| 336 | |
| 337 | // Make a stable copy of the components as the original list may be modified |
| 338 | List<ResolvedComponentInfo> stableCopy = |
| 339 | createResolvedComponentsForTestWithOtherProfile(2); |
| 340 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 341 | onView(withText(stableCopy.get(1).getResolveInfoAt(0).activityInfo.name)) |
| 342 | .perform(click()); |
| 343 | onView(withId(R.id.button_once)).perform(click()); |
| 344 | waitForIdle(); |
| 345 | assertThat(chosen[0], is(toChoose)); |
| 346 | } |
| 347 | |
Mike Digman | ba23268 | 2019-03-27 14:55:26 -0700 | [diff] [blame] | 348 | @Test |
| 349 | public void getActivityLabelAndSubLabel() throws Exception { |
| 350 | ActivityInfoPresentationGetter pg; |
| 351 | PackageManagerMockedInfo info; |
| 352 | |
| 353 | info = createPackageManagerMockedInfo(false); |
| 354 | pg = new ActivityInfoPresentationGetter( |
| 355 | info.ctx, 0, info.activityInfo); |
| 356 | assertThat("Label should match app label", pg.getLabel().equals( |
| 357 | info.setAppLabel)); |
| 358 | assertThat("Sublabel should match activity label if set", |
| 359 | pg.getSubLabel().equals(info.setActivityLabel)); |
| 360 | |
| 361 | info = createPackageManagerMockedInfo(true); |
| 362 | pg = new ActivityInfoPresentationGetter( |
| 363 | info.ctx, 0, info.activityInfo); |
| 364 | assertThat("With override permission label should match activity label if set", |
| 365 | pg.getLabel().equals(info.setActivityLabel)); |
| 366 | assertThat("With override permission sublabel should be empty", |
| 367 | TextUtils.isEmpty(pg.getSubLabel())); |
| 368 | } |
| 369 | |
| 370 | @Test |
| 371 | public void getResolveInfoLabelAndSubLabel() throws Exception { |
| 372 | ResolveInfoPresentationGetter pg; |
| 373 | PackageManagerMockedInfo info; |
| 374 | |
| 375 | info = createPackageManagerMockedInfo(false); |
| 376 | pg = new ResolveInfoPresentationGetter( |
| 377 | info.ctx, 0, info.resolveInfo); |
| 378 | assertThat("Label should match app label", pg.getLabel().equals( |
| 379 | info.setAppLabel)); |
| 380 | assertThat("Sublabel should match resolve info label if set", |
| 381 | pg.getSubLabel().equals(info.setResolveInfoLabel)); |
| 382 | |
| 383 | info = createPackageManagerMockedInfo(true); |
| 384 | pg = new ResolveInfoPresentationGetter( |
| 385 | info.ctx, 0, info.resolveInfo); |
| 386 | assertThat("With override permission label should match resolve info label if set", |
| 387 | pg.getLabel().equals(info.setResolveInfoLabel)); |
| 388 | assertThat("With override permission sublabel should be empty", |
| 389 | TextUtils.isEmpty(pg.getSubLabel())); |
| 390 | } |
| 391 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 392 | @Test |
| 393 | public void testWorkTab_displayedWhenWorkProfileUserAvailable() { |
| 394 | // enable the work tab feature flag |
| 395 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 396 | Intent sendIntent = createSendImageIntent(); |
| 397 | markWorkProfileUserAvailable(); |
| 398 | |
| 399 | mActivityRule.launchActivity(sendIntent); |
| 400 | waitForIdle(); |
| 401 | |
| 402 | onView(withId(R.id.tabs)).check(matches(isDisplayed())); |
| 403 | } |
| 404 | |
| 405 | @Test |
| 406 | public void testWorkTab_hiddenWhenWorkProfileUserNotAvailable() { |
| 407 | // enable the work tab feature flag |
| 408 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 409 | Intent sendIntent = createSendImageIntent(); |
| 410 | |
| 411 | mActivityRule.launchActivity(sendIntent); |
| 412 | waitForIdle(); |
| 413 | |
| 414 | onView(withId(R.id.tabs)).check(matches(not(isDisplayed()))); |
| 415 | } |
| 416 | |
| 417 | @Test |
| 418 | public void testWorkTab_workTabListEmptyBeforeGoingToTab() { |
| 419 | // enable the work tab feature flag |
| 420 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 421 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
| 422 | createResolvedComponentsForTest(3); |
| 423 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
| 424 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 425 | Mockito.anyBoolean(), |
| 426 | Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos); |
| 427 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 428 | Mockito.anyBoolean(), |
| 429 | Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos); |
| 430 | Intent sendIntent = createSendImageIntent(); |
| 431 | markWorkProfileUserAvailable(); |
| 432 | |
| 433 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 434 | waitForIdle(); |
| 435 | |
| 436 | assertThat(activity.getCurrentUserHandle().getIdentifier(), is(0)); |
| 437 | // The work list adapter must only be filled when we open the work tab |
| 438 | assertThat(activity.getWorkListAdapter().getCount(), is(0)); |
| 439 | } |
| 440 | |
| 441 | @Test |
| 442 | public void testWorkTab_workTabUsesExpectedAdapter() { |
| 443 | // enable the work tab feature flag |
| 444 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 445 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
| 446 | createResolvedComponentsForTest(3); |
| 447 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
| 448 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 449 | Mockito.anyBoolean(), |
| 450 | Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos); |
| 451 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 452 | Mockito.anyBoolean(), |
| 453 | Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos); |
| 454 | Intent sendIntent = createSendImageIntent(); |
| 455 | markWorkProfileUserAvailable(); |
| 456 | |
| 457 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 458 | waitForIdle(); |
| 459 | onView(withText(R.string.resolver_work_tab)).perform(click()); |
| 460 | |
| 461 | assertThat(activity.getCurrentUserHandle().getIdentifier(), is(10)); |
| 462 | assertThat(activity.getWorkListAdapter().getCount(), is(4)); |
| 463 | } |
| 464 | |
| 465 | @Test |
| 466 | public void testWorkTab_personalTabUsesExpectedAdapter() { |
| 467 | // enable the work tab feature flag |
| 468 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 469 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 470 | createResolvedComponentsForTestWithOtherProfile(3); |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 471 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 472 | when(sOverrides.resolverListController.getResolversForIntentAsUser( |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 473 | Mockito.anyBoolean(), |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 474 | Mockito.anyBoolean(), |
| 475 | Mockito.isA(List.class), |
| 476 | eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 477 | when(sOverrides.resolverListController.getResolversForIntentAsUser( |
| 478 | Mockito.anyBoolean(), |
| 479 | Mockito.anyBoolean(), |
| 480 | Mockito.isA(List.class), |
| 481 | eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
| 482 | when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(), |
| 483 | Mockito.anyBoolean(), |
| 484 | Mockito.isA(List.class), |
| 485 | eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 486 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 487 | Mockito.anyBoolean(), |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 488 | Mockito.isA(List.class))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
| 489 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 490 | Mockito.anyBoolean(), |
| 491 | Mockito.isA(List.class))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 492 | when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(), |
| 493 | Mockito.anyBoolean(), |
| 494 | Mockito.isA(List.class), |
| 495 | eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 496 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 497 | Intent sendIntent = createSendImageIntent(); |
| 498 | markWorkProfileUserAvailable(); |
| 499 | |
| 500 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 501 | waitForIdle(); |
| 502 | onView(withText(R.string.resolver_work_tab)).perform(click()); |
| 503 | |
| 504 | assertThat(activity.getCurrentUserHandle().getIdentifier(), is(10)); |
| 505 | assertThat(activity.getPersonalListAdapter().getCount(), is(3)); |
| 506 | } |
| 507 | |
| 508 | @Test |
| 509 | public void testWorkTab_workProfileHasExpectedNumberOfTargets() throws InterruptedException { |
| 510 | // enable the work tab feature flag |
| 511 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 512 | markWorkProfileUserAvailable(); |
| 513 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
| 514 | createResolvedComponentsForTest(3); |
| 515 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
| 516 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 517 | Mockito.anyBoolean(), |
| 518 | Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos); |
| 519 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 520 | Mockito.anyBoolean(), |
| 521 | Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos); |
| 522 | Intent sendIntent = createSendImageIntent(); |
| 523 | |
| 524 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 525 | waitForIdle(); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 526 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 527 | onView(withText(R.string.resolver_work_tab)) |
| 528 | .perform(click()); |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 529 | waitForIdle(); |
| 530 | assertThat(activity.getWorkListAdapter().getCount(), is(4)); |
| 531 | } |
| 532 | |
| 533 | @Test |
| 534 | public void testWorkTab_selectingWorkTabAppOpensAppInWorkProfile() throws InterruptedException { |
| 535 | // enable the work tab feature flag |
| 536 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 537 | markWorkProfileUserAvailable(); |
| 538 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
| 539 | createResolvedComponentsForTest(3); |
| 540 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
| 541 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 542 | Mockito.anyBoolean(), |
| 543 | Mockito.isA(List.class))).thenReturn(personalResolvedComponentInfos); |
| 544 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 545 | Mockito.anyBoolean(), |
| 546 | Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos); |
| 547 | Intent sendIntent = createSendImageIntent(); |
| 548 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 549 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 550 | chosen[0] = targetInfo.getResolveInfo(); |
| 551 | return true; |
| 552 | }; |
| 553 | |
| 554 | mActivityRule.launchActivity(sendIntent); |
| 555 | waitForIdle(); |
| 556 | onView(withText(R.string.resolver_work_tab)) |
| 557 | .perform(click()); |
| 558 | waitForIdle(); |
| 559 | // wait for the share sheet to expand |
| 560 | Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 561 | onView(first(allOf(withText(workResolvedComponentInfos.get(0) |
| 562 | .getResolveInfoAt(0).activityInfo.applicationInfo.name), isCompletelyDisplayed()))) |
| 563 | .perform(click()); |
| 564 | onView(withId(R.id.button_once)) |
| 565 | .perform(click()); |
| 566 | |
| 567 | waitForIdle(); |
| 568 | assertThat(chosen[0], is(workResolvedComponentInfos.get(0).getResolveInfoAt(0))); |
| 569 | } |
| 570 | |
| 571 | @Test |
| 572 | public void testWorkTab_noPersonalApps_workTabHasExpectedNumberOfTargets() |
| 573 | throws InterruptedException { |
| 574 | // enable the work tab feature flag |
| 575 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 576 | markWorkProfileUserAvailable(); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 577 | List<ResolvedComponentInfo> personalResolvedComponentInfos = |
| 578 | createResolvedComponentsForTestWithOtherProfile(1); |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 579 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 580 | |
| 581 | when(sOverrides.resolverListController.getResolversForIntentAsUser( |
| 582 | Mockito.anyBoolean(), |
| 583 | Mockito.anyBoolean(), |
| 584 | Mockito.isA(List.class), |
| 585 | eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 586 | when(sOverrides.resolverListController.getResolversForIntentAsUser( |
| 587 | Mockito.anyBoolean(), |
| 588 | Mockito.anyBoolean(), |
| 589 | Mockito.isA(List.class), |
| 590 | eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
| 591 | when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(), |
| 592 | Mockito.anyBoolean(), |
| 593 | Mockito.isA(List.class), |
| 594 | eq(sOverrides.workProfileUserHandle))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 595 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 596 | Mockito.anyBoolean(), |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 597 | Mockito.isA(List.class))).thenReturn(new ArrayList<>(workResolvedComponentInfos)); |
| 598 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 599 | Mockito.anyBoolean(), |
| 600 | Mockito.isA(List.class))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 601 | when(sOverrides.workResolverListController.getResolversForIntentAsUser(Mockito.anyBoolean(), |
| 602 | Mockito.anyBoolean(), |
| 603 | Mockito.isA(List.class), |
| 604 | eq(UserHandle.SYSTEM))).thenReturn(new ArrayList<>(personalResolvedComponentInfos)); |
| 605 | |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 606 | Intent sendIntent = createSendImageIntent(); |
| 607 | |
| 608 | final ResolverWrapperActivity activity = mActivityRule.launchActivity(sendIntent); |
| 609 | waitForIdle(); |
| 610 | onView(withText(R.string.resolver_work_tab)) |
| 611 | .perform(click()); |
| 612 | |
| 613 | waitForIdle(); |
| 614 | assertThat(activity.getWorkListAdapter().getCount(), is(4)); |
| 615 | } |
| 616 | |
| 617 | @Ignore // b/148156663 |
| 618 | @Test |
| 619 | public void testWorkTab_noPersonalApps_canStartWorkApps() |
| 620 | throws InterruptedException { |
| 621 | // enable the work tab feature flag |
| 622 | ResolverActivity.ENABLE_TABBED_VIEW = true; |
| 623 | markWorkProfileUserAvailable(); |
| 624 | List<ResolvedComponentInfo> workResolvedComponentInfos = createResolvedComponentsForTest(4); |
| 625 | when(sOverrides.workResolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 626 | Mockito.anyBoolean(), |
| 627 | Mockito.isA(List.class))).thenReturn(workResolvedComponentInfos); |
| 628 | Intent sendIntent = createSendImageIntent(); |
| 629 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 630 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 631 | chosen[0] = targetInfo.getResolveInfo(); |
| 632 | return true; |
| 633 | }; |
| 634 | |
| 635 | mActivityRule.launchActivity(sendIntent); |
| 636 | waitForIdle(); |
| 637 | onView(withText(R.string.resolver_work_tab)) |
| 638 | .perform(click()); |
| 639 | waitForIdle(); |
| 640 | // wait for the share sheet to expand |
| 641 | Thread.sleep(ChooserActivity.LIST_VIEW_UPDATE_INTERVAL_IN_MILLIS); |
| 642 | onView(first(allOf(withText(workResolvedComponentInfos.get(0) |
| 643 | .getResolveInfoAt(0).activityInfo.applicationInfo.name), isCompletelyDisplayed()))) |
| 644 | .perform(click()); |
| 645 | onView(withId(R.id.button_once)) |
| 646 | .perform(click()); |
| 647 | waitForIdle(); |
| 648 | |
| 649 | assertThat(chosen[0], is(workResolvedComponentInfos.get(0).getResolveInfoAt(0))); |
| 650 | } |
| 651 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 652 | private Intent createSendImageIntent() { |
| 653 | Intent sendIntent = new Intent(); |
| 654 | sendIntent.setAction(Intent.ACTION_SEND); |
| 655 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
| 656 | sendIntent.setType("image/jpeg"); |
| 657 | return sendIntent; |
| 658 | } |
| 659 | |
| 660 | private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) { |
| 661 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 662 | for (int i = 0; i < numberOfResults; i++) { |
| 663 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
| 664 | } |
| 665 | return infoList; |
| 666 | } |
| 667 | |
Hakan Seyalioglu | 13405c5 | 2017-01-31 19:01:31 -0800 | [diff] [blame] | 668 | private List<ResolvedComponentInfo> createResolvedComponentsForTestWithOtherProfile( |
| 669 | int numberOfResults) { |
| 670 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 671 | for (int i = 0; i < numberOfResults; i++) { |
| 672 | if (i == 0) { |
| 673 | infoList.add(ResolverDataProvider.createResolvedComponentInfoWithOtherId(i)); |
| 674 | } else { |
| 675 | infoList.add(ResolverDataProvider.createResolvedComponentInfo(i)); |
| 676 | } |
| 677 | } |
| 678 | return infoList; |
| 679 | } |
| 680 | |
Hakan Seyalioglu | 9149dca | 2017-01-17 12:20:01 -0800 | [diff] [blame] | 681 | private void waitForIdle() { |
| 682 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 683 | } |
arangelov | b4fc397 | 2020-01-15 15:10:12 +0000 | [diff] [blame] | 684 | |
| 685 | private void markWorkProfileUserAvailable() { |
| 686 | ResolverWrapperActivity.sOverrides.workProfileUserHandle = UserHandle.of(10); |
| 687 | } |
Ben Lin | 93b8697 | 2017-10-25 11:38:40 -0700 | [diff] [blame] | 688 | } |