xref: /aosp_15_r20/external/mobly-snippet-lib/README.md (revision ae5b1ec8a57d9cd6259556f14d3f2bb4f9fb3a85)
1*ae5b1ec8SZiwei Zhang# Getting Started with Snippets for Mobly
2*ae5b1ec8SZiwei Zhang
3*ae5b1ec8SZiwei ZhangMobly Snippet Lib is a library for triggering device-side code from host-side
4*ae5b1ec8SZiwei Zhang[Mobly](http://github.com/google/mobly) tests. This tutorial teaches you how to
5*ae5b1ec8SZiwei Zhanguse the snippet lib to trigger custom device-side actions.
6*ae5b1ec8SZiwei Zhang
7*ae5b1ec8SZiwei ZhangNote: Mobly and the snippet lib are not official Google products.
8*ae5b1ec8SZiwei Zhang
9*ae5b1ec8SZiwei Zhang
10*ae5b1ec8SZiwei Zhang## Prerequisites
11*ae5b1ec8SZiwei Zhang
12*ae5b1ec8SZiwei Zhang-   These examples and tutorials assume basic familiarity with the Mobly
13*ae5b1ec8SZiwei Zhang    framework, so please follow the
14*ae5b1ec8SZiwei Zhang    [Mobly tutorial](http://github.com/google/mobly) before doing this one.
15*ae5b1ec8SZiwei Zhang-   You should know how to create an Android app and build it with gradle. If
16*ae5b1ec8SZiwei Zhang    not, follow the
17*ae5b1ec8SZiwei Zhang    [Android app tutorial](https://developer.android.com/training/basics/firstapp/index.html).
18*ae5b1ec8SZiwei Zhang
19*ae5b1ec8SZiwei Zhang
20*ae5b1ec8SZiwei Zhang## Overview
21*ae5b1ec8SZiwei Zhang
22*ae5b1ec8SZiwei ZhangThe Mobly Snippet Lib allows you to write Java methods that run on Android
23*ae5b1ec8SZiwei Zhangdevices, and trigger the methods from inside a Mobly test case. The Java methods
24*ae5b1ec8SZiwei Zhanginvoked this way are called `snippets`.
25*ae5b1ec8SZiwei Zhang
26*ae5b1ec8SZiwei ZhangThe `snippet` code can either be written in its own standalone apk, or as a
27*ae5b1ec8SZiwei Zhang[product flavor](https://developer.android.com/studio/build/build-variants.html#product-flavors)
28*ae5b1ec8SZiwei Zhangof an existing apk. This allows you to write snippets that instrument or
29*ae5b1ec8SZiwei Zhangautomate another app.
30*ae5b1ec8SZiwei Zhang
31*ae5b1ec8SZiwei Zhang
32*ae5b1ec8SZiwei Zhang## Under The Hood
33*ae5b1ec8SZiwei Zhang
34*ae5b1ec8SZiwei ZhangA snippet is launched by an `am instrument` call. Snippets use a custom
35*ae5b1ec8SZiwei Zhang`InstrumentationTestRunner` derived from `AndroidJUnitRunner`. This allows
36*ae5b1ec8SZiwei Zhangfor snippets that interact with a main app's classes, such as Espresso snippets,
37*ae5b1ec8SZiwei Zhangand allows you to get either the test app's or the main app's context from
38*ae5b1ec8SZiwei Zhang`InstrumentationRegistry`.
39*ae5b1ec8SZiwei Zhang
40*ae5b1ec8SZiwei ZhangOnce started, the special runner starts a web server which listens for requests
41*ae5b1ec8SZiwei Zhangto trigger snippets. The server's handler locates the corresponding methods by
42*ae5b1ec8SZiwei Zhangreflection, runs them, and returns results over the TCP socket. All common
43*ae5b1ec8SZiwei Zhangbuilt-in variable types are supported as arguments.
44*ae5b1ec8SZiwei Zhang
45*ae5b1ec8SZiwei Zhang
46*ae5b1ec8SZiwei Zhang## Usage
47*ae5b1ec8SZiwei Zhang
48*ae5b1ec8SZiwei ZhangThe [examples/](examples/) folder contains examples of how to use the
49*ae5b1ec8SZiwei Zhangmobly snippet lib along with detailed tutorials.
50*ae5b1ec8SZiwei Zhang
51*ae5b1ec8SZiwei Zhang*   [ex1_standalone_app](examples/ex1_standalone_app): Basic example of a
52*ae5b1ec8SZiwei Zhang    snippet which is compiled into its own standalone apk.
53*ae5b1ec8SZiwei Zhang*   [ex2_espresso](examples/ex2_espresso): Example of a snippet which
54*ae5b1ec8SZiwei Zhang    instruments a primary app to drive its UI using
55*ae5b1ec8SZiwei Zhang    [Espresso](https://google.github.io/android-testing-support-library/docs/espresso/).
56*ae5b1ec8SZiwei Zhang*   [ex3_async_event](examples/ex3_async_event): Example of how to use the
57*ae5b1ec8SZiwei Zhang    @AsyncRpc annotation to handle asynchronous callbacks.
58*ae5b1ec8SZiwei Zhang*   [ex4_uiautomator](examples/ex4_uiautomator): Example of how to create
59*ae5b1ec8SZiwei Zhang    snippets that automate the UI actions using UIAutomator. Unlike Espresso
60*ae5b1ec8SZiwei Zhang    UIAutomator works even without access to app source code.
61*ae5b1ec8SZiwei Zhang*   [ex5_schedule_rpc](examples/ex5_schedule_rpc): Example of how to use the
62*ae5b1ec8SZiwei Zhang    'scheduleRpc' RPC to execute another RPC at a later time, potentially after
63*ae5b1ec8SZiwei Zhang    device disconnection.
64*ae5b1ec8SZiwei Zhang*   [ex6_complex_type_conversion](examples/ex6_complex_type_conversion): Example of how to pass a
65*ae5b1ec8SZiwei Zhang    non-primitive type to the Rpc methods and return non-primitive type from Rpc methods by
66*ae5b1ec8SZiwei Zhang    supplying a type converter.
67