Skip to content

Commit e756dfc

Browse files
committed
Added ServiceTestRule sample
Change-Id: I93462ba469747c847cb0bd1c4f33313c6f5e0b3a
1 parent d816aac commit e756dfc

File tree

24 files changed

+652
-0
lines changed

24 files changed

+652
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
local.properties
3+
.idea
4+
.DS_Store
5+
build
6+
*.iml
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Basic sample for ServiceTestRule
2+
3+
This rule provides a simplified mechanism to start and shutdown your service before and after
4+
the duration of your test. It also guarantees that the service is successfully connected when starting
5+
(or binding to) a service. The service can be started (or bound) using one of the helper methods.
6+
It will automatically be stopped (or unbound) after the test completes and any methods annotated with @After are finished.
7+
8+
Note: This rule doesn't support `IntentService` because it's automatically destroyed when
9+
`IntentService#onHandleIntent(android.content.Intent)`
10+
finishes all outstanding commands. So there is no guarantee to establish a successful connection in a timely manner.
11+
12+
This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended.
13+
14+
1. Download the project code, preferably using `git clone`.
15+
1. Open the Android SDK Manager (*Tools* Menu | *Android*) and make sure you have installed the *Support Repository* under *Extras*.
16+
1. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file.
17+
1. Check out the relevant code:
18+
* The application under test is located in `src/main/java`
19+
* Tests are in `src/androidTest/java`
20+
1. Connect a device or start an emulator
21+
1. Run the newly created configuration
22+
23+
The application will be started on the device/emulator and a series of actions will be performed automatically.
24+
25+
If you are using Android Studio, the *Run* window will show the test results.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 22
5+
buildToolsVersion '22.0.1'
6+
defaultConfig {
7+
applicationId "com.example.android.testing.integrationtesting.ServiceTestRuleSample"
8+
minSdkVersion 10
9+
targetSdkVersion 22
10+
versionCode 1
11+
versionName "1.0"
12+
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
packagingOptions {
16+
exclude 'LICENSE.txt'
17+
}
18+
lintOptions {
19+
abortOnError false
20+
}
21+
productFlavors {
22+
}
23+
}
24+
25+
dependencies {
26+
// Testing-only dependencies
27+
androidTestCompile 'com.android.support.test:runner:0.3'
28+
androidTestCompile 'com.android.support.test:rules:0.3'
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2015, 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.example.android.testing.ServiceTestRuleSample;
18+
19+
import android.content.Intent;
20+
import android.os.IBinder;
21+
import android.support.test.InstrumentationRegistry;
22+
import android.support.test.rule.ServiceTestRule;
23+
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
27+
import java.util.concurrent.TimeoutException;
28+
29+
import static org.hamcrest.CoreMatchers.any;
30+
import static org.hamcrest.CoreMatchers.is;
31+
import static org.junit.Assert.assertThat;
32+
33+
/**
34+
* JUnit4 test that uses a {@link ServiceTestRule} to interact with a bound service.
35+
* <p>
36+
* {@link ServiceTestRule} is a JUnit rule that provides a
37+
* simplified mechanism to start and shutdown your service before
38+
* and after the duration of your test. It also guarantees that the service is successfully
39+
* connected when starting (or binding to) a service. The service can be started
40+
* (or bound) using one of the helper methods. It will automatically be stopped (or unbound) after
41+
* the test completes and any methods annotated with
42+
* <a href="http://junit.sourceforge.net/javadoc/org/junit/After.html"><code>After</code></a> are
43+
* finished.
44+
* <p>
45+
* Note: This rule doesn't support {@link android.app.IntentService} because it's automatically
46+
* destroyed when {@link android.app.IntentService#onHandleIntent(android.content.Intent)} finishes
47+
* all outstanding commands. So there is no guarantee to establish a successful connection
48+
* in a timely manner.
49+
*/
50+
public class LocalServiceTest {
51+
@Rule
52+
public final ServiceTestRule mServiceRule = new ServiceTestRule();
53+
54+
@Test
55+
public void testWithBoundService() throws TimeoutException {
56+
// Create the service Intent.
57+
Intent serviceIntent =
58+
new Intent(InstrumentationRegistry.getTargetContext(), LocalService.class);
59+
60+
// Data can be passed to the service via the Intent.
61+
serviceIntent.putExtra(LocalService.SEED_KEY, 42L);
62+
63+
// Bind the service and grab a reference to the binder.
64+
IBinder binder = mServiceRule.bindService(serviceIntent);
65+
66+
// Get the reference to the service, or you can call public methods on the binder directly.
67+
LocalService service = ((LocalService.LocalBinder) binder).getService();
68+
69+
// Verify that the service is working correctly.
70+
assertThat(service.getRandomInt(), is(any(Integer.class)));
71+
}
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2015 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
19+
<manifest package="com.example.android.testing.ServiceTestRuleSample"
20+
xmlns:android="http://schemas.android.com/apk/res/android">
21+
22+
<application
23+
android:icon="@mipmap/ic_launcher"
24+
android:label="@string/app_name"
25+
android:theme="@style/AppTheme">
26+
<service android:name=".LocalService"/>
27+
</application>
28+
29+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2015, 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.example.android.testing.ServiceTestRuleSample;
18+
19+
import android.app.Service;
20+
import android.content.Intent;
21+
import android.os.Binder;
22+
import android.os.IBinder;
23+
24+
import java.util.Random;
25+
26+
/**
27+
* {@link Service} that generates random numbers.
28+
* <p>
29+
* A seed for the random number generator can be set via the {@link Intent} passed to
30+
* {@link #onBind(Intent)}.
31+
*/
32+
public class LocalService extends Service {
33+
// Used as a key for the Intent.
34+
public static final String SEED_KEY = "SEED_KEY";
35+
36+
// Binder given to clients
37+
private final IBinder mBinder = new LocalBinder();
38+
39+
// Random number generator
40+
private Random mGenerator = new Random();
41+
42+
private long mSeed;
43+
44+
@Override
45+
public IBinder onBind(Intent intent) {
46+
// If the Intent comes with a seed for the number generator, apply it.
47+
if (intent.hasExtra(SEED_KEY)) {
48+
mSeed = intent.getLongExtra(SEED_KEY, 0);
49+
mGenerator.setSeed(mSeed);
50+
}
51+
return mBinder;
52+
}
53+
54+
public class LocalBinder extends Binder {
55+
56+
public LocalService getService() {
57+
// Return this instance of LocalService so clients can call public methods.
58+
return LocalService.this;
59+
}
60+
}
61+
62+
/**
63+
* Returns a random integer in [0, 100).
64+
*/
65+
public int getRandomInt() {
66+
return mGenerator.nextInt(100);
67+
}
68+
}
3.15 KB
Loading
1.98 KB
Loading
3.92 KB
Loading
6.05 KB
Loading

0 commit comments

Comments
 (0)