diff options
author | adorokhine <adorokhine@google.com> | 2017-01-10 18:19:50 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-10 18:19:50 -0800 |
commit | 787e6a7f501a90a10c7ea043e7d93a84cdef1153 (patch) | |
tree | f2b8108afe2a6c95df5f2754fcd664328759f00b /examples/ex1_standalone_app | |
parent | 4440d4703224ccb815b4d8db647d58a244240571 (diff) | |
download | mobly-snippet-lib-787e6a7f501a90a10c7ea043e7d93a84cdef1153.tar.gz |
Rename example folders so they do not start with a number. (#15)
This causes problems for some systems.
Diffstat (limited to 'examples/ex1_standalone_app')
5 files changed, 195 insertions, 0 deletions
diff --git a/examples/ex1_standalone_app/README.md b/examples/ex1_standalone_app/README.md new file mode 100644 index 0000000..b5805ed --- /dev/null +++ b/examples/ex1_standalone_app/README.md @@ -0,0 +1,100 @@ +# Standalone App Snippet Example + +This tutorial shows you how to create a standalone Mobly snippet app, which runs +by itself and doesn't instrument a main app. + +## Tutorial + +1. Use Android Studio to create a new app project. + +1. Link against Mobly Snippet Lib in your `build.gradle` file + + ``` + dependencies { + compile 'com.google.android.mobly:snippetlib:0.0.1' + } + ``` + +1. Write a Java class implementing `Snippet` and add methods to trigger the + behaviour that you want. Annotate them with `@Rpc` + + ```java + package com.my.app; + ... + public class ExampleSnippet implements Snippet { + @Rpc(description='Returns a string containing the given number.') + public String getFoo(Integer input) { + return 'foo ' + input; + } + + @Override + public void shutdown() {} + } + ``` + +1. Add any classes that implement the `Snippet` interface in your + `AndroidManifest.xml` application section as `meta-data` + + ```xml + <manifest + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.my.app"> + <application> + <meta-data + android:name="mobly-snippets" + android:value="com.my.app.test.MySnippet1, + com.my.app.test.MySnippet2" /> + ... + ``` + + +1. Add an `instrumentation` tag to your `AndroidManifest.xml` so that the + framework can launch your server through an `instrument` command. + + ```xml + <manifest + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.my.app"> + <application>...</application> + <instrumentation + android:name="com.google.android.mobly.snippet.ServerRunner" + android:targetPackage="com.my.app" /> + </manifest> + ``` + +1. Build your apk and install it on your phone + +1. In your Mobly python test, connect to your snippet .apk in `setup_class` + + ```python + class HelloWorldTest(base_test.BaseTestClass): + def setup_class(self): + self.ads = self.register_controller(android_device) + self.dut1 = self.ads[0] + self.dut1.load_snippet(name='snippet', package='com.my.app.test') + ``` + +6. Invoke your needed functionality within your test + + ```python + def test_get_foo(self): + actual_foo = self.dut1.snippet.getFoo(5) + asserts.assert_equal("foo 5", actual_foo) + ``` + +## Running the example code + +This folder contains a fully working example of a standalone snippet apk. + +1. Compile the example + + ./gradlew examples:ex1_standalone_app:assembleDebug + +1. Install the apk on your phone + + adb install -r ./examples/ex1_standalone_app/build/outputs/apk/ex1_standalone_app-debug.apk + + <!-- TODO(adorokhine): create a snippet_client in mobly to allow you to + trigger snippets without having to create a test. Then update this + instruction. --> +1. Create a python test to trigger `getFoo` following the above instructions. diff --git a/examples/ex1_standalone_app/build.gradle b/examples/ex1_standalone_app/build.gradle new file mode 100644 index 0000000..28bd7ec --- /dev/null +++ b/examples/ex1_standalone_app/build.gradle @@ -0,0 +1,18 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 24 + buildToolsVersion "24.0.3" + + defaultConfig { + applicationId "com.google.android.mobly.snippet.example1" + minSdkVersion 11 + targetSdkVersion 24 + versionCode 1 + versionName "0.0.1" + } +} + +dependencies { + compile project(':third_party:sl4a') +} diff --git a/examples/ex1_standalone_app/src/main/AndroidManifest.xml b/examples/ex1_standalone_app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..dde911c --- /dev/null +++ b/examples/ex1_standalone_app/src/main/AndroidManifest.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="utf-8"?> +<manifest + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.google.android.mobly.snippet.example1"> + + <application> + <meta-data + android:name="mobly-snippets" + android:value="com.google.android.mobly.snippet.example1.ExampleSnippet, + com.google.android.mobly.snippet.example1.ExampleSnippet2" /> + </application> + + <instrumentation + android:name="com.google.android.mobly.snippet.SnippetRunner" + android:targetPackage="com.google.android.mobly.snippet.example1" /> + +</manifest> diff --git a/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet.java b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet.java new file mode 100644 index 0000000..46c55ba --- /dev/null +++ b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet.java @@ -0,0 +1,30 @@ +/* + * 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.example1; + +import com.google.android.mobly.snippet.Snippet; +import com.google.android.mobly.snippet.rpc.Rpc; + +public class ExampleSnippet implements Snippet { + @Rpc(description = "Returns the given integer with the prefix \"foo\"") + public String getFoo(Integer input) { + return "foo " + input; + } + + @Override + public void shutdown() {} +} diff --git a/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java new file mode 100644 index 0000000..43c3db8 --- /dev/null +++ b/examples/ex1_standalone_app/src/main/java/com/google/android/mobly/snippet/example1/ExampleSnippet2.java @@ -0,0 +1,30 @@ +/* + * 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.example1; + +import com.google.android.mobly.snippet.Snippet; +import com.google.android.mobly.snippet.rpc.Rpc; + +public class ExampleSnippet2 implements Snippet { + @Rpc(description = "Returns the given string with the prefix \"bar\"") + public String getBar(String input) { + return "bar " + input; + } + + @Override + public void shutdown() {} +} |