blob: db0347c8608bd6e6e9f715b71e1dd888ca6359c1 [file] [log] [blame]
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -08001/*
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
17package com.android.internal.app;
18
19import com.android.internal.R;
20import com.android.internal.app.ResolverActivity.ResolvedComponentInfo;
21
22import org.junit.Before;
23import org.junit.Rule;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.mockito.Mockito;
27
28import android.content.Intent;
29import android.content.pm.ResolveInfo;
30import android.support.test.InstrumentationRegistry;
31import android.support.test.rule.ActivityTestRule;
32import android.support.test.runner.AndroidJUnit4;
33
34import java.util.ArrayList;
35import java.util.List;
36
37import static android.support.test.espresso.Espresso.onView;
38import static android.support.test.espresso.action.ViewActions.click;
39import static android.support.test.espresso.assertion.ViewAssertions.matches;
40import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
41import static android.support.test.espresso.matcher.ViewMatchers.withId;
42import static android.support.test.espresso.matcher.ViewMatchers.withText;
43import static com.android.internal.app.ChooserWrapperActivity.sOverrides;
44import static org.hamcrest.CoreMatchers.is;
45import static org.hamcrest.CoreMatchers.not;
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.mockito.Mockito.when;
48
49/**
50 * Chooser activity instrumentation tests
51 */
52@RunWith(AndroidJUnit4.class)
53public 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 Seyalioglu79b69f02017-01-12 17:08:02 -080067 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
68
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080069 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
70 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -080071 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080072 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 Seyalioglue1276bf2016-12-07 16:38:57 -080079 Intent sendIntent = createSendImageIntent();
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -080080 List<ResolvedComponentInfo> resolvedComponentInfos = createResolvedComponentsForTest(2);
81
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080082 when(sOverrides.resolverListController.getResolversForIntent(Mockito.anyBoolean(),
83 Mockito.anyBoolean(),
Hakan Seyalioglu79b69f02017-01-12 17:08:02 -080084 Mockito.isA(List.class))).thenReturn(resolvedComponentInfos);
Hakan Seyalioglue1276bf2016-12-07 16:38:57 -080085 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}