tree: c8460ed972f7c11d0c18805e5cff1b0bcbbf81c8 [path history] [tgz]
  1. src/
  2. build.gradle
  3. README.md
examples/ex1_standalone_app/README.md

Standalone Snippet App Example

This tutorial shows you how to create a standalone Mobly snippet app. To create a snippet app that controls (instruments) another app under test, please see Example 2.

Tutorial

  1. Use Android Studio to create a new app project.

  2. Link against Mobly Snippet Lib in your build.gradle file

    dependencies {
      implementation 'com.google.android.mobly:mobly-snippet-lib:1.3.1'
    }
    
  3. Write a Java class implementing Snippet and add methods to trigger the behaviour that you want. Annotate them with @Rpc

    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() {}
    }
    
  4. Add any classes that implement the Snippet interface in your AndroidManifest.xml application section as meta-data

    <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" />
        ...
    
  5. Add an instrumentation tag to your AndroidManifest.xml so that the framework can launch your server through an instrument command.

    <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>
    
  6. Build your apk and install it on your phone

  7. In your Mobly python test, connect to your snippet .apk in setup_class

    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')
    
  8. Invoke your needed functionality within your test

    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
    
  2. Install the apk on your phone

    adb install -r ./examples/ex1_standalone_app/build/outputs/apk/debug/ex1_standalone_app-debug.apk
    
  3. Use snippet_shell from mobly to trigger getFoo():

    snippet_shell.py com.google.android.mobly.snippet.example1
    
    >>> print(s.help())
    Known methods:
      getBar(String) returns String  // Returns the given string with the prefix "bar"
      getFoo(Integer) returns String  // Returns the given integer with the prefix "foo"
    
    >>> s.getFoo(5)
    u'foo 5'