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 | |
| 19 | import com.android.internal.R; |
| 20 | import com.android.internal.app.ResolverActivity.ResolvedComponentInfo; |
| 21 | |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Rule; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.mockito.Mockito; |
| 27 | |
| 28 | import android.content.Intent; |
| 29 | import android.content.pm.ResolveInfo; |
| 30 | import android.support.test.InstrumentationRegistry; |
| 31 | import android.support.test.rule.ActivityTestRule; |
| 32 | import android.support.test.runner.AndroidJUnit4; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
| 37 | import static android.support.test.espresso.Espresso.onView; |
| 38 | import static android.support.test.espresso.action.ViewActions.click; |
| 39 | import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 40 | import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 41 | import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 42 | import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 43 | import static com.android.internal.app.ChooserWrapperActivity.sOverrides; |
| 44 | import static org.hamcrest.CoreMatchers.is; |
| 45 | import static org.hamcrest.CoreMatchers.not; |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.mockito.Mockito.when; |
| 48 | |
| 49 | /** |
| 50 | * Chooser activity instrumentation tests |
| 51 | */ |
| 52 | @RunWith(AndroidJUnit4.class) |
| 53 | public class ChooserActivityTest { |
| 54 | @Rule |
| 55 | public ActivityTestRule<ChooserWrapperActivity> mActivityRule = |
| 56 | new ActivityTestRule<>(ChooserWrapperActivity.class, false, |
| 57 | false); |
| 58 | |
| 59 | @Before |
| 60 | public void cleanOverrideData() { |
| 61 | sOverrides.reset(); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void customTitle() throws InterruptedException { |
| 66 | Intent sendIntent = createSendImageIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame^] | 67 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 68 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 69 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 70 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame^] | 71 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 72 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, "chooser test")); |
| 73 | waitForIdle(); |
| 74 | onView(withId(R.id.title)).check(matches(withText("chooser test"))); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void emptyTitle() throws InterruptedException { |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 79 | Intent sendIntent = createSendImageIntent(); |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame^] | 80 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 81 | |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 82 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 83 | Mockito.anyBoolean(), |
Hakan Seyalioglu | 79b69f0 | 2017-01-12 17:08:02 -0800 | [diff] [blame^] | 84 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
Hakan Seyalioglu | e1276bf | 2016-12-07 16:38:57 -0800 | [diff] [blame] | 85 | mActivityRule.launchActivity(Intent.createChooser(sendIntent, null)); |
| 86 | waitForIdle(); |
| 87 | onView(withId(R.id.title)) |
| 88 | .check(matches(withText(R.string.whichSendApplication))); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void twoOptionsAndUserSelectsOne() throws InterruptedException { |
| 93 | Intent sendIntent = createSendImageIntent(); |
| 94 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2); |
| 95 | |
| 96 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 97 | Mockito.anyBoolean(), |
| 98 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 99 | |
| 100 | final ChooserWrapperActivity activity = mActivityRule |
| 101 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 102 | waitForIdle(); |
| 103 | |
| 104 | assertThat(activity.getAdapter().getCount(), is(2)); |
| 105 | onView(withId(R.id.profile_button)).check(matches(not(isDisplayed()))); |
| 106 | |
| 107 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 108 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 109 | chosen[0] = targetInfo.getResolveInfo(); |
| 110 | return true; |
| 111 | }; |
| 112 | |
| 113 | ResolveInfo toChoose = resolvedComponentInfos.get(0).getResolveInfoAt(0); |
| 114 | onView(withText(toChoose.activityInfo.name)) |
| 115 | .perform(click()); |
| 116 | waitForIdle(); |
| 117 | assertThat(chosen[0], is(toChoose)); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void noResultsFromPackageManager() { |
| 122 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 123 | Mockito.anyBoolean(), |
| 124 | Mockito.isA(List.class))).thenReturn(null); |
| 125 | Intent sendIntent = createSendImageIntent(); |
| 126 | final ChooserWrapperActivity activity = mActivityRule |
| 127 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 128 | waitForIdle(); |
| 129 | assertThat(activity.isFinishing(), is(false)); |
| 130 | |
| 131 | onView(withId(R.id.empty)).check(matches(isDisplayed())); |
| 132 | onView(withId(R.id.resolver_list)).check(matches(not(isDisplayed()))); |
| 133 | InstrumentationRegistry.getInstrumentation().runOnMainSync( |
| 134 | () -> activity.getAdapter().handlePackagesChanged() |
| 135 | ); |
| 136 | // backward compatibility. looks like we finish when data is empty after package change |
| 137 | assertThat(activity.isFinishing(), is(true)); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void autoLaunchSingleResult() throws InterruptedException { |
| 142 | ResolveInfo[] chosen = new ResolveInfo[1]; |
| 143 | sOverrides.onSafelyStartCallback = targetInfo -> { |
| 144 | chosen[0] = targetInfo.getResolveInfo(); |
| 145 | return true; |
| 146 | }; |
| 147 | |
| 148 | List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(1); |
| 149 | when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(), |
| 150 | Mockito.anyBoolean(), |
| 151 | Mockito.isA(List.class))).thenReturn(resolvedComponentInfos); |
| 152 | |
| 153 | Intent sendIntent = createSendImageIntent(); |
| 154 | final ChooserWrapperActivity activity = mActivityRule |
| 155 | .launchActivity(Intent.createChooser(sendIntent, null)); |
| 156 | waitForIdle(); |
| 157 | |
| 158 | assertThat(chosen[0], is(resolvedComponentInfos.get(0).getResolveInfoAt(0))); |
| 159 | assertThat(activity.isFinishing(), is(true)); |
| 160 | } |
| 161 | |
| 162 | private Intent createSendImageIntent() { |
| 163 | Intent sendIntent = new Intent(); |
| 164 | sendIntent.setAction(Intent.ACTION_SEND); |
| 165 | sendIntent.putExtra(Intent.EXTRA_TEXT, "testing intent sending"); |
| 166 | sendIntent.setType("image/jpeg"); |
| 167 | return sendIntent; |
| 168 | } |
| 169 | |
| 170 | private List<ResolvedComponentInfo> createResolvedComponentsForTest(int numberOfResults) { |
| 171 | List<ResolvedComponentInfo> infoList = new ArrayList<>(numberOfResults); |
| 172 | for (int i = 0; i < numberOfResults; i++) { |
| 173 | infoList.add(ChooserDataProvider.createResolvedComponentInfo(i)); |
| 174 | } |
| 175 | return infoList; |
| 176 | } |
| 177 | |
| 178 | private void waitForIdle() { |
| 179 | InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
| 180 | } |
| 181 | } |