blob: 2d5379a3665f927ad189cd80ee7b1641621a525c [file] [log] [blame] [view]
Alexander Dorokhineb1b8b502016-11-10 13:52:59 -08001# Getting Started with Snippets for Mobly
2
3Mobly Snippet Lib is a library for triggering device-side code from host-side
4[Mobly](http://github.com/google/mobly) tests. This tutorial teaches you how to
5use the snippet lib to trigger custom device-side actions.
6
7This is not an official Google product.
8
9## Prerequisites
10- This tutorial assumes basic familiarity with the Mobly framework, so please
11 follow the [Mobly tutorial](http://github.com/google/mobly) before doing
12 this one.
13- You should know how to create an Android app and build it with gradle. If
14 not, follow the
15 [Android app tutorial](https://developer.android.com/training/basics/firstapp/index.html).
16
17## Using Mobly Snippet Lib
18
191. Link against Mobly Snippet Lib in your `build.gradle` file
20
21 ```
22 apply plugin: 'com.android.application'
23 dependencies {
24 androidTestCompile 'com.google.android.mobly:snippetlib:0.0.1'
25 }
26 ```
27
282. In your `androidTest` source tree, write a Java class implementing `Snippet`
29 and add methods to trigger the behaviour that you want. Annotate them with
30 `@Rpc`
31
32 ```java
33 package com.my.app.test;
34
35 ...
36
37 public class ExampleSnippet implements Snippet {
38 public ExampleSnippet(Context context) {}
39
40 @Rpc(description='Returns a string containing the given number.')
41 public String getFoo(Integer input) {
42 return 'foo ' + input;
43 }
44
45 @Override
46 public void shutdown() {}
47 }
48 ```
49
503. Add any classes that implement the `Snippet` interface in your
51 `AndroidManifest.xml` application section as `meta-data`
52
53 ```xml
54 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
55 package="com.my.app.test">
56 <application>
57 <meta-data
58 android:name="mobly-snippets"
59 android:value="com.my.app.test.MySnippet1,
60 com.my.app.test.MySnippet2" />
61 ...
62 ```
63
644. Build your apk and install it on your phone
65
665. In your Mobly python test, connect to your snippet .apk in `setup_class`
67
68 ```python
69 class HelloWorldTest(base_test.BaseTestClass):
70 def setup_class(self):
71 self.ads = self.register_controller(android_device)
72 self.dut1 = self.ads[0]
73 self.dut1.load_snippets(name='snippet', package='com.my.app.test')
74
75 if __name__ == '__main__':
76 test_runner.main()
77 ```
78
796. Invoke your needed functionality within your test
80
81 ```python
82 def test_get_foo(self):
83 actual_foo = self.dut1.snippet.getFoo(5)
84 asserts.assert_equal("foo 5", actual_foo)
85 ```