diff options
Diffstat (limited to 'third_party/sl4a')
-rw-r--r-- | third_party/sl4a/build.gradle | 4 | ||||
-rw-r--r-- | third_party/sl4a/src/main/AndroidManifest.xml | 1 | ||||
-rw-r--r-- | third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java | 51 |
3 files changed, 56 insertions, 0 deletions
diff --git a/third_party/sl4a/build.gradle b/third_party/sl4a/build.gradle index 015499b..3f76d58 100644 --- a/third_party/sl4a/build.gradle +++ b/third_party/sl4a/build.gradle @@ -11,3 +11,7 @@ android { versionName "0.0.1" } } + +dependencies { + compile 'com.android.support.test:runner:0.5' +} diff --git a/third_party/sl4a/src/main/AndroidManifest.xml b/third_party/sl4a/src/main/AndroidManifest.xml index 6c42f77..bb254d4 100644 --- a/third_party/sl4a/src/main/AndroidManifest.xml +++ b/third_party/sl4a/src/main/AndroidManifest.xml @@ -11,5 +11,6 @@ <action android:name="com.google.android.mobly.snippet.action.LAUNCH_SERVER"/> </intent-filter> </service> + <uses-library android:name="android.test.runner"/> </application> </manifest> diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java new file mode 100644 index 0000000..56f2df5 --- /dev/null +++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java @@ -0,0 +1,51 @@ +package com.google.android.mobly.snippet; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.support.test.runner.AndroidJUnitRunner; + +import com.google.android.mobly.snippet.service.SnippetService; +import com.google.android.mobly.snippet.util.Log; + +/** + * A launcher that starts the snippet server as an instrumentation so that it has access to the + * target app's context. + * + * It is written this way to be compatible with 'am instrument'. + */ +public class SnippetRunner extends AndroidJUnitRunner { + private static final String ARG_PORT = "port"; + + private Context mContext; + private int mServicePort; + + @Override + public void onCreate(Bundle arguments) { + mContext = getContext(); + String servicePort = arguments.getString(ARG_PORT); + if (servicePort == null) { + throw new IllegalArgumentException("\"--e port <port>\" was not specified"); + } + mServicePort = Integer.parseInt(servicePort); + super.onCreate(arguments); + } + + @Override + public void onStart() { + Intent intent = new Intent(mContext, SnippetService.class); + intent.setAction(Constants.ACTION_LAUNCH_SERVER); + intent.putExtra(Constants.EXTRA_SERVICE_PORT, mServicePort); + mContext.startService(intent); + Log.e("Started up the snippet server on port " + mServicePort); + + // Wait forever. Once our instrumentation returns, everything related to the app is killed. + while (true) { + try { + Thread.sleep(24L * 60 * 60 * 1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } +} |