GenericRemoteMessage

public final class GenericRemoteMessage
extends Object implements To<MessageLite>

java.lang.Object
   ↳ android.support.test.espresso.remote.GenericRemoteMessage


Generic implementation of the EspressoRemoteMessage interface, which uses reflection for proto message serialization and deserialization.

Every Espresso matcher, view action or view assertion needs to support proto serialization to participate in any kind of cross process UI interaction using Espresso Remote.

Each serializable type T needs to provide two things. First a proto message declaration of type T. Second an EspressoRemoteMessage class which implements the EspressoRemoteMessage.To and EspressoRemoteMessage.From interfaces to serialize/deserialization any state into their proto message equivalent.

This GenericRemoteMessage class is special type of EspressoRemoteMessage, which implements the the EspressoRemoteMessage.To and EspressoRemoteMessage.From interfaces and uses reflection to perform serialisation of an object of type T into its proto message representation.

Espresso Remote uses a global RemoteDescriptorRegistry for RemoteDescriptor lookups. Where a RemoteDescriptor is a descriptor object which contains all the necessary meta information for proto serialization.

Usage:

For a type Foo with fields bar and baz:

public class Foo {
   private final String bar;
   private final int baz;

   protected Foo(String bar, int baz) { // set fields }
 }
 

Note: A protected constructor with serializable fields in declared order is required for proto deserialization.

Create a corresponding proto definition, FooProto.proto for Foo:

message FooProto {
   bytes bar = 1; // Name needs to match Foo#bar
   bytes baz = 2; // Name needs to match Foo#baz
 }
 

The proto type definitions used with GenericRemoteMessage need to be of type byte and the type names must match the variable name of the serializable fields in Foo.java.

The last step is to create a RemoteDescriptor using a RemoteDescriptor.Builder, to configure all the meta data required for generic serialization. Finally register the descriptor with the RemoteDescriptorRegistry. A typical descriptor builder looks like this:

new RemoteDescriptor.Builder()
   .setInstanceType(Foo.class)
   .setInstanceFieldDescriptors(
     FieldDescriptor.of(String.class, "bar")),
     FieldDescriptor.of(int.class, 32))
   .setRemoteType(GenericRemoteMessage.class)
   .setProtoType(FooProto.class)
   .build();
 

First set the instance type, Foo.class. Then specify the serializable fields using a FieldDescriptor. Where a FieldDescriptor represents the name and the type of a reflective field of Foo. Any fields described by the field properties and passed to setInstanceFieldDescriptors(FieldDescriptor) will be serialised into FooProto.proto. Next specify that type Foo will use for it's serialization entry point, in this case GenericRemoteMessage and lastly set the proto message class.

Note: The declared field properties order, must match the protected constructor of Foo, which takes the serializable fields in declared order!

Summary

Fields

public static final From<Object, MessageLite> FROM

This is used to create and deserialize a proto message into an instance type

Public constructors

GenericRemoteMessage(Object instance)

Creates a new GenericRemoteMessage.

Public methods

MessageLite toProto()

Inherited methods

From class java.lang.Object
From interface android.support.test.espresso.remote.EspressoRemoteMessage.To

Fields

FROM

From<Object, MessageLite> FROM

This is used to create and deserialize a proto message into an instance type

Public constructors

GenericRemoteMessage

GenericRemoteMessage (Object instance)

Creates a new GenericRemoteMessage.

This constructor is called reflectively and should not be used.

Parameters
instance Object: the object that needs to be serialized into a proto

Public methods

toProto

MessageLite toProto ()

Returns
MessageLite