Learn how to extend your GameDriver tests to include custom object types
GameDriver object serialization uses the gdio.common.objects library, which contains all of the object types used by GameDriver to facilitate testing. These are representations of the native objects that can be found in UnityEngine, some of which are lite versions of those objects in order to keep object size to a minimum. The full library definition can be found on our GitHub here.
In case the common objects library does not include the definition of an object type required to test your project, we have provided the ability to extend GameDriver's object serialization, and easily leverage it within your test.
Attached below is a sample custom serialization project that you can follow to build your own serialization class. Simply define the custom object class as needed to fit your project needs, compile it, and add the resulting library into the GDIO folder in your Unity project. The GameDriver agent will pick it up automatically.
Enum serialization example
Let's say you have an enum defined in the game namespace.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace com.samplegame.stats
{
public enum GameEnum
{
Value1, Value2, Value3
}
}
And a method that takes the enum as a parameter.
void TestMethodGameEnum(com.samplegame.stats.GameEnum gameEnum)
{
Debug.Log($"<color=orange>Game enum val: {gameEnum}</color>");
}
You can call this method from a test script using a CallMethod API call by creating a custom serializer for GameEnum.
Step 1 - Make a custom type
Add a Custom Enum to the sample serialization project. Note that the enum values should be the same as the in-game enum.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SampleSerialization
{
public enum CustomEnum
{
Value1, Value2, Value3
}
}
Step 2 - Make a serializer class
Here the serializer will serialize the CustomEnum to a byte array and then deserialize the enum back into the in-game GameEnum type.
using System;
using System.Reflection;
using System.Text;
namespace SampleSerialization
{
public class GameEnumSerializer : gdio.plugin.serializer.ICustomSerializer
{
public GameEnumSerializer() { }
public object Deserialize(byte[] rawObject)
{
string data = Encoding.ASCII.GetString(rawObject);
if (!data.StartsWith("CustomEnum:"))
throw new NotImplementedException();
data = data.Substring("CustomEnum:".Length);
// Using reflection to cast the Enum to the one in Game.
string enumTypeName = "com.samplegame.stats.GameEnum";
Type t = Type.GetType(enumTypeName);
if (t == null)
{
foreach (Assembly anAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
string n = enumTypeName + ", " + anAssembly.FullName;
t = Type.GetType(n);
if (t != null)
{
break;
}
}
if (t == null)
throw new Exception($"Type - {gameEnumTypeName} not found!");
}
return Enum.ToObject(t, int.Parse(data));
}
public byte[] Serialize(object theObject)
{
if (!theObject.GetType().Equals(typeof(CustomEnum)))
throw new NotImplementedException();
return Encoding.ASCII.GetBytes($"CustomEnum:{(CustomEnum)Enum.ToObject(typeof(CustomEnum), theObject)}");
}
public Type[] SupportedTypes()
{
return new Type[] { typeof(CustomEnum) };
}
}
}
Step 3 - Build and reference the DLL
Build the serialization project, copy the DLL to the GDIO folder, and reference the DLL in the test script.
Step 4 - Call the method
Use CallMethod to call TestMethodGameEnum method from the test script.
api.CallMethod("/*[@name='Canvas']/fn:component('TestScript')", "TestMethodGameEnum", new object[] { SampleSerialization.CustomEnum.Value3 });
Note: This is a one-way serialization and can only be used to pass parameters into a method and not as a return type since we would only have access to the deserialized type in the game.