aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src
diff options
context:
space:
mode:
authoradorokhine <adorokhine@google.com>2017-01-27 15:24:49 -0800
committerGitHub <noreply@github.com>2017-01-27 15:24:49 -0800
commit03478b839c4dbef91869c617eba6410e3fba2ecc (patch)
treea953aab42cfed712283281b043e5612ca689c772 /third_party/sl4a/src
parent83f36f778244c54424f9a3df737582a999b249c3 (diff)
downloadmobly-snippet-lib-03478b839c4dbef91869c617eba6410e3fba2ecc.tar.gz
Don't explode if the binary into which snippet-lib is linked has junit tests. (#28)
Prior to this we would have tried to actually execute those tests as part of the 'stop' action. To avoid this, make an empty junit class and always run that as part of 'stop'. Fixes #27.
Diffstat (limited to 'third_party/sl4a/src')
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java11
-rw-r--r--third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/EmptyTestClass.java28
2 files changed, 37 insertions, 2 deletions
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
index 6bfa87c..aa51009 100644
--- 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
@@ -23,6 +23,7 @@ import android.os.Process;
import android.support.test.runner.AndroidJUnitRunner;
import com.google.android.mobly.snippet.rpc.AndroidProxy;
+import com.google.android.mobly.snippet.util.EmptyTestClass;
import com.google.android.mobly.snippet.util.Log;
import com.google.android.mobly.snippet.util.NotificationIdFactory;
import java.io.IOException;
@@ -32,7 +33,10 @@ import java.net.SocketException;
* 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'.
+ * <p>We have to extend some subclass of {@link android.test.InstrumentationTestRunner} because
+ * snippets are launched with 'am instrument'. We're specifically extending
+ * {@link AndroidJUnitRunner} because Espresso requires being called through it, since it sets up
+ * {@link android.support.test.InstrumentationRegistry} which Espresso requires.
*/
public class SnippetRunner extends AndroidJUnitRunner {
private static final String ARG_ACTION = "action";
@@ -49,9 +53,12 @@ public class SnippetRunner extends AndroidJUnitRunner {
@Override
public void onCreate(Bundle arguments) {
mArguments = arguments;
+ // Prevent this runner from triggering any real JUnit tests in the snippet by feeding it a
+ // hardcoded empty test class.
+ mArguments.putString("class", EmptyTestClass.class.getCanonicalName());
mNotificationManager = (NotificationManager)
getTargetContext().getSystemService(Context.NOTIFICATION_SERVICE);
- super.onCreate(arguments);
+ super.onCreate(mArguments);
}
@Override
diff --git a/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/EmptyTestClass.java b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/EmptyTestClass.java
new file mode 100644
index 0000000..ac1920f
--- /dev/null
+++ b/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/util/EmptyTestClass.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2016 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.google.android.mobly.snippet.util;
+
+import org.junit.Ignore;
+
+/**
+ * A stub JUnit class with no tests.
+ *
+ * <p>Used for 'safely' calling AndroidJUnitRunner methods on snippets that happen to have tests
+ * defined, to avoid actually calling those tests.
+ */
+@Ignore
+public class EmptyTestClass {}