Skip to content

Commit dce7f32

Browse files
ServiceTestRule.startService now verifies that the provided intent actually matched a service.
PiperOrigin-RevId: 752350818
1 parent 743fbcf commit dce7f32

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

runner/rules/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
**Breaking Changes**
1212

13+
* `ServiceTestRule.startService` will now throw if the provided intent does not
14+
launch a service.
15+
1316
**API Changes**
1417

1518
**Breaking API Changes**

runner/rules/java/androidx/test/rule/ServiceTestRule.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,20 @@ public static ServiceTestRule withTimeout(long timeout, TimeUnit timeUnit) {
120120
* {@link android.content.Context#startService(Intent) Context.startService (Intent)}.
121121
* @throws SecurityException if you do not have permission to bind to the given service.
122122
* @throws TimeoutException if timed out waiting for a successful connection with the service.
123+
* @throws IllegalArgumentException if the intent did not resolve to a service
123124
*/
124125
public void startService(@NonNull Intent intent) throws TimeoutException {
125126
serviceIntent = Checks.checkNotNull(intent, "intent can't be null");
126-
InstrumentationRegistry.getInstrumentation().getTargetContext().startService(serviceIntent);
127+
ComponentName componentName =
128+
InstrumentationRegistry.getInstrumentation().getTargetContext().startService(serviceIntent);
129+
if (componentName == null) {
130+
throw new IllegalArgumentException(
131+
"Intent "
132+
+ intent
133+
+ "did not start a service - this could be triggered by:\n"
134+
+ " 1. Your service is not declared in the manifest\n"
135+
+ " 2. The service is not visible to your app, because of package filtering\n");
136+
}
127137
serviceStarted = true;
128138

129139
// bind to the started service to guarantee its started and connected before test execution

runner/rules/javatests/androidx/test/rule/ServiceTestRuleTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
package androidx.test.rule;
1818

1919
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
20+
import static org.hamcrest.CoreMatchers.containsString;
2021
import static org.hamcrest.CoreMatchers.is;
22+
import static org.hamcrest.MatcherAssert.assertThat;
2123
import static org.junit.Assert.assertEquals;
2224
import static org.junit.Assert.assertFalse;
2325
import static org.junit.Assert.assertNotNull;
24-
import static org.junit.Assert.assertThat;
2526
import static org.junit.Assert.assertTrue;
2627
import static org.junit.Assert.fail;
2728
import static org.junit.runner.JUnitCore.runClasses;
@@ -250,6 +251,22 @@ public void checkServiceTimeoutLogic() {
250251

251252
@Rule public final ServiceTestRule serviceRule = new ServiceTestRule();
252253

254+
@Test
255+
public void verifyFailureIfServiceNotStarted() throws Exception {
256+
final class NotDeclaredService extends Service {
257+
@Override
258+
public IBinder onBind(Intent intent) {
259+
return null;
260+
}
261+
}
262+
try {
263+
serviceRule.startService(new Intent(getApplicationContext(), NotDeclaredService.class));
264+
fail("Should have thrown");
265+
} catch (IllegalArgumentException e) {
266+
assertThat(e.getMessage(), containsString("did not start a service"));
267+
}
268+
}
269+
253270
@Test
254271
public void verifySuccessfulServiceStart() throws TimeoutException {
255272
serviceRule.startService(new Intent(getApplicationContext(), TestService.class));

0 commit comments

Comments
 (0)