diff options
author | frankfeng <frankfeng@google.com> | 2022-01-05 23:53:39 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-01-05 23:53:39 +0000 |
commit | 7151acd88deae8709cb212b20494a4c38ecc8263 (patch) | |
tree | daa905c2cab4f307d9342908fc85ec54ab73bab3 /examples/ex6_complex_type_conversion/README.md | |
parent | a18b21585728971688495dbe384c61b0332c8ce1 (diff) | |
parent | a3c4e39314251b98b0ee70da2bb63dbc52137ac7 (diff) | |
download | mobly-snippet-lib-7151acd88deae8709cb212b20494a4c38ecc8263.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge am: 22b92531fc am: 7902e5747e am: a3c4e39314
Original change: https://android-review.googlesource.com/c/platform/external/mobly-snippet-lib/+/1932020
Change-Id: Id8702566f1ba34c44a867e3f61f4af94b7b4006d
Diffstat (limited to 'examples/ex6_complex_type_conversion/README.md')
-rw-r--r-- | examples/ex6_complex_type_conversion/README.md | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/examples/ex6_complex_type_conversion/README.md b/examples/ex6_complex_type_conversion/README.md new file mode 100644 index 0000000..104d91b --- /dev/null +++ b/examples/ex6_complex_type_conversion/README.md @@ -0,0 +1,108 @@ +# Complex Type Conversion in Snippet Example + +This tutorial shows you how to use a custom object in Snippet Lib. + +This example assumes basic familiarity with Snippet Lib as demonstrated in +[Example 1](../ex1_standalone_app/README.md). + +## Tutorial + +1. Use Android Studio to create a new app project, similar to + [Example 1](../ex1_standalone_app/README.md). + +1. Create a complex type in Java: + ```java + public class CustomType { + private String myValue; + CustomType(String value) { + myValue = value; + } + + String getMyValue() { + return myValue; + } + public void setMyValue(String newValue) { + myValue = newValue; + } + } + ``` +1. Create a Java class implementing `SnippetObjectConverter`, which defines how the complex type + should be converted against `JSONObject`: + ```java + public class ExampleObjectConverter implements SnippetObjectConverter { + @Override + public JSONObject serialize(Object object) throws JSONException { + JSONObject result = new JSONObject(); + if (object instanceof CustomType) { + CustomType input = (CustomType) object; + result.put("Value", input.getMyValue()); + return result; + } + return null; + } + + @Override + public Object deserialize(JSONObject jsonObject, Type type) throws JSONException { + if (type == CustomType.class) { + return new CustomType(jsonObject.getString("Value")); + } + return null; + } + } + ``` +1. Write a Java class implementing `Snippet` and add Rpc methods that takes your complex type as + a parameter and another Rpc method that returns the complext type directly. + + ```java + package com.my.app; + ... + public class ExampleSnippet implements Snippet { + @Rpc(description = "Pass a complex type as a snippet parameter.") + public String passComplexTypeToSnippet(CustomType input) { + Log.i("Old value is: " + input.getMyValue()); + return "The value in CustomType is: " + input.getMyValue(); + } + + @Rpc(description = "Returns a complex type from snippet.") + public CustomType returnComplexTypeFromSnippet(String value) { + return new CustomType(value); + } + @Override + public void shutdown() {} + } + ``` + +1. In `AndroidManifest.xml`, specify the converter class as a `meta-data` named + `mobly-object-converter` + + ```xml + <manifest + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.my.app"> + <application> + <meta-data + android:name="mobly-object-converter" + android:value="com.my.app.ExampleObjectConverter" /> + ... + ``` + +## Running the example code + +This folder contains a fully working example of a standalone snippet apk. + +1. Compile the example + + ./gradlew examples:ex6_complex_type_conversion:assembleDebug + +1. Install the apk on your phone + + adb install -r ./examples/ex6_complex_type_conversion/build/outputs/apk/debug/ex6_complex_type_conversion-debug.apk + +1. Use Mobly's `snippet_shell` from mobly to trigger the Rpc methods: + + snippet_shell.py com.google.android.mobly.snippet.example6 + + >>> s.passComplexTypeToSnippet({'Value': 'Hello'}) + 'The value in CustomType is: Hello' + >>> s.returnComplexTypeFromSnippet('Bye') + {'Value': 'Bye'} |