From a31d2dcc13f3010cad42bdda9280ad28e8cbed99 Mon Sep 17 00:00:00 2001 From: Kriszta Gombos Date: Tue, 14 Feb 2023 18:22:42 +0200 Subject: [PATCH] PoC Click --- Assets/AltTester/AltServer/Input.cs | 187 ++++++++++-------- Assets/AltTester/AltServer/InputController.cs | 16 +- .../Editor/Driver/Testforscene1TestSample.cs | 2 +- 3 files changed, 115 insertions(+), 90 deletions(-) diff --git a/Assets/AltTester/AltServer/Input.cs b/Assets/AltTester/AltServer/Input.cs index 91577e675..6e1ff6343 100644 --- a/Assets/AltTester/AltServer/Input.cs +++ b/Assets/AltTester/AltServer/Input.cs @@ -11,6 +11,7 @@ using UnityEngine.EventSystems; using UnityEngine.InputSystem.UI; using UnityEngine.Scripting; +using System.Runtime.InteropServices; [Preserve] public class Input : MonoBehaviour @@ -1105,90 +1106,114 @@ internal static IEnumerator tapClickCoordinatesLifeCycle(UnityEngine.Vector2 scr if (monoBehaviourTarget != null) monoBehaviourTarget.SendMessage("OnMouseExit", UnityEngine.SendMessageOptions.DontRequireReceiver); } - internal static IEnumerator tapClickElementLifeCycle(UnityEngine.GameObject target, int count, float interval, bool tap) - { - UnityEngine.Vector3 screenPosition; - AltRunner._altRunner.FindCameraThatSeesObject(target, out screenPosition); - yield return new WaitForEndOfFrame();//run after Update + [System.Runtime.InteropServices.DllImport("user32.dll")] +static extern bool SetCursorPos(int x, int y); - var pointerEventData = new UnityEngine.EventSystems.PointerEventData(UnityEngine.EventSystems.EventSystem.current) - { - position = screenPosition, - button = UnityEngine.EventSystems.PointerEventData.InputButton.Left, - eligibleForClick = true, - pressPosition = screenPosition - }; - mousePosition = screenPosition; - pointerEventData.pointerEnter = target; - //repeat - for (int i = 0; i < count; i++) - { - float time = 0; - AltRunner._altRunner.ShowClick(screenPosition); +[System.Runtime.InteropServices.DllImport("user32.dll")] +public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); - /* pointer/touch down */ - UnityEngine.Touch touch = new UnityEngine.Touch(); - int pointerId = 0; - if (tap) - { - touch = createTouch(screenPosition); - pointerId = touch.fingerId; - } - pointerEventData.pointerId = pointerId; - - var keyStructure = new KeyStructure(UnityEngine.KeyCode.Mouse0, 1.0f);//power 1 - _keyCodesPressedDown.Add(keyStructure); - _keyCodesPressed.Add(keyStructure); - if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) - { - UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.initializePotentialDrag); - - UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerDownHandler); - } - if (target != null) target.SendMessage("OnMouseDown", UnityEngine.SendMessageOptions.DontRequireReceiver); - pointerEventData.pointerPress = target; - - time += UnityEngine.Time.unscaledDeltaTime; - yield return null; +public const int MOUSEEVENTF_LEFTDOWN = 0x02; +public const int MOUSEEVENTF_LEFTUP = 0x04; - _keyCodesPressedDown.Remove(keyStructure); - beginKeyUpTouchEndedLifecycle(keyStructure, tap, ref touch); - - if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) - { - UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerUpHandler); - UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerClickHandler); - } - - if (target != null) - { - target.SendMessage("OnMouseUp", UnityEngine.SendMessageOptions.DontRequireReceiver); - target.SendMessage("OnMouseUpAsButton", UnityEngine.SendMessageOptions.DontRequireReceiver); - } - - time += UnityEngine.Time.unscaledDeltaTime; - yield return null; - - endKeyUpTouchEndedLifecycle(keyStructure, tap, touch); - - if (i != count - 1 && time < interval)//do not wait at last click/tap - { - float elapsedTime = 0; - while (elapsedTime < interval - time) - { - elapsedTime += UnityEngine.Time.unscaledDeltaTime; - yield return null; - } - } - } +//This simulates a left mouse click +public static void LeftMouseClick(int xpos, int ypos) +{ + SetCursorPos(xpos, ypos); + mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); + mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); +} - // mouse position doesn't change but we fire on mouse exit - if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) - { - UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerExitHandler); - } - if (target != null) - target.SendMessage("OnMouseExit", UnityEngine.SendMessageOptions.DontRequireReceiver); + internal static IEnumerator tapClickElementLifeCycle(UnityEngine.GameObject target, int count, float interval, bool tap) + { + UnityEngine.Debug.Log("1"); + UnityEngine.Debug.Log("posx: " + (int)target.transform.position.x); + UnityEngine.Debug.Log("posy: " + (int)target.transform.position.y); + LeftMouseClick(810, 390); + UnityEngine.Debug.Log("2"); + return null; + // yield return new WaitForEndOfFrame(); + // UnityEngine.Vector3 screenPosition; + // AltRunner._altRunner.FindCameraThatSeesObject(target, out screenPosition); + // yield return new WaitForEndOfFrame();//run after Update + + // var pointerEventData = new UnityEngine.EventSystems.PointerEventData(UnityEngine.EventSystems.EventSystem.current) + // { + // position = screenPosition, + // button = UnityEngine.EventSystems.PointerEventData.InputButton.Left, + // eligibleForClick = true, + // pressPosition = screenPosition + // }; + // mousePosition = screenPosition; + // pointerEventData.pointerEnter = target; + // //repeat + // for (int i = 0; i < count; i++) + // { + // float time = 0; + // AltRunner._altRunner.ShowClick(screenPosition); + + // /* pointer/touch down */ + // UnityEngine.Touch touch = new UnityEngine.Touch(); + // int pointerId = 0; + // if (tap) + // { + // touch = createTouch(screenPosition); + // pointerId = touch.fingerId; + // } + // pointerEventData.pointerId = pointerId; + + // var keyStructure = new KeyStructure(UnityEngine.KeyCode.Mouse0, 1.0f);//power 1 + // _keyCodesPressedDown.Add(keyStructure); + // _keyCodesPressed.Add(keyStructure); + // if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) + // { + // UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.initializePotentialDrag); + + // UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerDownHandler); + // } + // if (target != null) target.SendMessage("OnMouseDown", UnityEngine.SendMessageOptions.DontRequireReceiver); + // pointerEventData.pointerPress = target; + + // time += UnityEngine.Time.unscaledDeltaTime; + // yield return null; + + // _keyCodesPressedDown.Remove(keyStructure); + // beginKeyUpTouchEndedLifecycle(keyStructure, tap, ref touch); + + // if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) + // { + // UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerUpHandler); + // UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerClickHandler); + // } + + // if (target != null) + // { + // target.SendMessage("OnMouseUp", UnityEngine.SendMessageOptions.DontRequireReceiver); + // target.SendMessage("OnMouseUpAsButton", UnityEngine.SendMessageOptions.DontRequireReceiver); + // } + + // time += UnityEngine.Time.unscaledDeltaTime; + // yield return null; + + // endKeyUpTouchEndedLifecycle(keyStructure, tap, touch); + + // if (i != count - 1 && time < interval)//do not wait at last click/tap + // { + // float elapsedTime = 0; + // while (elapsedTime < interval - time) + // { + // elapsedTime += UnityEngine.Time.unscaledDeltaTime; + // yield return null; + // } + // } + // } + + // // mouse position doesn't change but we fire on mouse exit + // if (EventSystem.current.currentInputModule.GetType().Name != typeof(InputSystemUIInputModule).Name) + // { + // UnityEngine.EventSystems.ExecuteEvents.Execute(target, pointerEventData, UnityEngine.EventSystems.ExecuteEvents.pointerExitHandler); + // } + // if (target != null) + // target.SendMessage("OnMouseExit", UnityEngine.SendMessageOptions.DontRequireReceiver); } private static void updateTouchInTouchList(Touch touch) @@ -1553,7 +1578,7 @@ public override string ToString() } } #else - using UnityEngine; +using UnityEngine; namespace Altom.AltTester.InputModule { diff --git a/Assets/AltTester/AltServer/InputController.cs b/Assets/AltTester/AltServer/InputController.cs index 18e55cbc9..5296ead3e 100644 --- a/Assets/AltTester/AltServer/InputController.cs +++ b/Assets/AltTester/AltServer/InputController.cs @@ -104,15 +104,15 @@ public static void ClickElement(UnityEngine.GameObject target, int count, float { #if ALTTESTER List coroutines = new List(); -#if ENABLE_INPUT_SYSTEM - coroutines.Add(NewInputSystem.ClickElementLifeCycle(target, count, interval)); -#endif -#if ENABLE_LEGACY_INPUT_MANAGER +// #if ENABLE_INPUT_SYSTEM +// coroutines.Add(NewInputSystem.ClickElementLifeCycle(target, count, interval)); +// #endif +// #if ENABLE_LEGACY_INPUT_MANAGER coroutines.Add(Input.tapClickElementLifeCycle(target, count, interval, false)); -#endif - AltRunner._altRunner.StartCoroutine(runThrowingIterator(coroutines, onFinish)); -#else - throw new AltInputModuleException(AltErrors.errorInputModule); +// #endif +// AltRunner._altRunner.StartCoroutine(runThrowingIterator(coroutines, onFinish)); +// #else +// throw new AltInputModuleException(AltErrors.errorInputModule); #endif } diff --git a/Assets/AltTester/Examples/Test/Editor/Driver/Testforscene1TestSample.cs b/Assets/AltTester/Examples/Test/Editor/Driver/Testforscene1TestSample.cs index 451cbb09c..2ab78a94c 100644 --- a/Assets/AltTester/Examples/Test/Editor/Driver/Testforscene1TestSample.cs +++ b/Assets/AltTester/Examples/Test/Editor/Driver/Testforscene1TestSample.cs @@ -718,7 +718,7 @@ public void TestPressKeyWaitTheDuration() public void TestClickElement() { const string name = "Capsule"; - var altElement = altDriver.FindObject(By.NAME, name).Tap(); + var altElement = altDriver.FindObject(By.NAME, name).Click(); Assert.AreEqual(name, altElement.name); // altDriver.WaitForObjectWithText(By.NAME, "CapsuleInfo", "Capsule was clicked to jump!"); altDriver.WaitForObject(By.PATH, "//CapsuleInfo[@text=Capsule was clicked to jump!]");