Hi,
I’m adding UnitTest to my project and now I’m trying to simulate interaction with the UI, but I can’t figure out how I can simulate pressing a key from a keyboard.
I’ve tried several ways but I can’t get it to work.
[UnityTest]
public IEnumerator Write_Text_Into_Selected_Input_Field()
{
//ARRANGE
yield return Click_InputField_With_Touch();
TMP_InputField inputField = Object.FindObjectOfType<TMP_InputField>();
var keyboard = InputSystem.AddDevice<Keyboard>();
SetKeyboardLayout("QWERTY", keyboard);
string text = "qwerty";
//ACT
//First try
Click(keyboard.qKey);
yield return null;
Click(keyboard.wKey);
yield return null;
Click(keyboard.eKey);
yield return null;
Click(keyboard.rKey);
yield return null;
Click(keyboard.tKey);
yield return null;
Click(keyboard.yKey);
yield return null;
//Second try
Set(keyboard.qKey, 1f);
yield return null;
Set(keyboard.wKey, 1f);
yield return null;
Set(keyboard.eKey, 1f);
yield return null;
Set(keyboard.rKey, 1f);
yield return null;
Set(keyboard.tKey, 1f);
yield return null;
Set(keyboard.yKey, 1f);
yield return null;
//Third try
foreach (char item in text)
{
KeyUtility.charToKey.TryGetValue(char.ToLower(keyChar), out Key key);
InputSystem.QueueStateEvent(keyboard, new KeyboardState(key));
InputSystem.Update();
yield return null;
}
//ASSERT
Assert.AreEqual(text, inputField.text);
}