|
21 | 21 | import static org.mockito.ArgumentMatchers.any;
|
22 | 22 | import static org.mockito.ArgumentMatchers.anyInt;
|
23 | 23 | import static org.mockito.ArgumentMatchers.anyList;
|
| 24 | +import static org.mockito.ArgumentMatchers.argThat; |
24 | 25 | import static org.mockito.Mockito.mock;
|
25 | 26 | import static org.mockito.Mockito.spy;
|
26 | 27 | import static org.mockito.Mockito.times;
|
|
29 | 30 |
|
30 | 31 | import android.content.ComponentName;
|
31 | 32 | import android.content.Context;
|
| 33 | +import android.content.Intent; |
32 | 34 | import android.content.pm.PackageManager;
|
33 | 35 | import android.content.pm.ResolveInfo;
|
34 | 36 | import android.content.pm.ServiceInfo;
|
|
42 | 44 | import org.junit.Test;
|
43 | 45 | import org.junit.runner.RunWith;
|
44 | 46 | import org.mockito.ArgumentCaptor;
|
| 47 | +import org.mockito.ArgumentMatcher; |
45 | 48 | import org.robolectric.RobolectricTestRunner;
|
46 | 49 | import org.robolectric.RuntimeEnvironment;
|
47 | 50 |
|
@@ -72,19 +75,26 @@ public void setUp() {
|
72 | 75 | .build();
|
73 | 76 | }
|
74 | 77 |
|
| 78 | + private ArgumentMatcher<Intent> filterEquals(Intent intent) { |
| 79 | + return (test) -> { |
| 80 | + return intent.filterEquals(test); |
| 81 | + }; |
| 82 | + } |
| 83 | + |
75 | 84 | @Test
|
76 | 85 | public void testValidator() {
|
77 | 86 | ServiceInfo s1 = new ServiceInfo();
|
78 | 87 | s1.permission = "testPermission";
|
79 | 88 | s1.packageName = "pkg";
|
| 89 | + s1.name = "Service1"; |
80 | 90 | ServiceInfo s2 = new ServiceInfo();
|
81 | 91 | s2.permission = "testPermission";
|
82 | 92 | s2.packageName = "pkg2";
|
| 93 | + s2.name = "service2"; |
83 | 94 | ResolveInfo r1 = new ResolveInfo();
|
84 | 95 | r1.serviceInfo = s1;
|
85 | 96 | ResolveInfo r2 = new ResolveInfo();
|
86 | 97 | r2.serviceInfo = s2;
|
87 |
| - |
88 | 98 | when(mPm.queryIntentServicesAsUser(any(), anyInt(), anyInt())).thenReturn(
|
89 | 99 | ImmutableList.of(r1, r2));
|
90 | 100 |
|
@@ -118,9 +128,11 @@ public void testNoValidator() {
|
118 | 128 | ServiceInfo s1 = new ServiceInfo();
|
119 | 129 | s1.permission = "testPermission";
|
120 | 130 | s1.packageName = "pkg";
|
| 131 | + s1.name = "Service1"; |
121 | 132 | ServiceInfo s2 = new ServiceInfo();
|
122 | 133 | s2.permission = "testPermission";
|
123 | 134 | s2.packageName = "pkg2";
|
| 135 | + s2.name = "service2"; |
124 | 136 | ResolveInfo r1 = new ResolveInfo();
|
125 | 137 | r1.serviceInfo = s1;
|
126 | 138 | ResolveInfo r2 = new ResolveInfo();
|
@@ -193,4 +205,56 @@ public void testSaveLoad() {
|
193 | 205 | assertThat(Settings.Secure.getString(RuntimeEnvironment.application.getContentResolver(),
|
194 | 206 | TEST_SETTING)).contains(testComponent2.flattenToString());
|
195 | 207 | }
|
| 208 | + |
| 209 | + @Test |
| 210 | + public void testHasPermissionWithoutMeetingCurrentRegs() { |
| 211 | + ServiceInfo s1 = new ServiceInfo(); |
| 212 | + s1.permission = "testPermission"; |
| 213 | + s1.packageName = "pkg"; |
| 214 | + s1.name = "Service1"; |
| 215 | + ServiceInfo s2 = new ServiceInfo(); |
| 216 | + s2.permission = "testPermission"; |
| 217 | + s2.packageName = "pkg2"; |
| 218 | + s2.name = "service2"; |
| 219 | + ResolveInfo r1 = new ResolveInfo(); |
| 220 | + r1.serviceInfo = s1; |
| 221 | + ResolveInfo r2 = new ResolveInfo(); |
| 222 | + r2.serviceInfo = s2; |
| 223 | + |
| 224 | + ComponentName approvedComponent = new ComponentName(s2.packageName, s2.name); |
| 225 | + |
| 226 | + Settings.Secure.putString( |
| 227 | + mContext.getContentResolver(), TEST_SETTING, approvedComponent.flattenToString()); |
| 228 | + |
| 229 | + when(mPm.queryIntentServicesAsUser(argThat( |
| 230 | + filterEquals(new Intent(TEST_INTENT))), anyInt(), anyInt())) |
| 231 | + .thenReturn(ImmutableList.of(r1)); |
| 232 | + when(mPm.queryIntentServicesAsUser(argThat( |
| 233 | + filterEquals(new Intent().setComponent(approvedComponent))), |
| 234 | + anyInt(), anyInt())) |
| 235 | + .thenReturn(ImmutableList.of(r2)); |
| 236 | + |
| 237 | + mServiceListing = new ServiceListing.Builder(mContext) |
| 238 | + .setTag("testTag") |
| 239 | + .setSetting(TEST_SETTING) |
| 240 | + .setNoun("testNoun") |
| 241 | + .setIntentAction(TEST_INTENT) |
| 242 | + .setValidator(info -> { |
| 243 | + if (info.packageName.equals("pkg")) { |
| 244 | + return true; |
| 245 | + } |
| 246 | + return false; |
| 247 | + }) |
| 248 | + .setPermission("testPermission") |
| 249 | + .build(); |
| 250 | + ServiceListing.Callback callback = mock(ServiceListing.Callback.class); |
| 251 | + mServiceListing.addCallback(callback); |
| 252 | + mServiceListing.reload(); |
| 253 | + |
| 254 | + verify(mPm, times(2)).queryIntentServicesAsUser(any(), anyInt(), anyInt()); |
| 255 | + ArgumentCaptor<List<ServiceInfo>> captor = ArgumentCaptor.forClass(List.class); |
| 256 | + verify(callback, times(1)).onServicesReloaded(captor.capture()); |
| 257 | + |
| 258 | + assertThat(captor.getValue()).containsExactlyElementsIn(ImmutableList.of(s2, s1)); |
| 259 | + } |
196 | 260 | }
|
0 commit comments