using System.Collections; using NUnit.Framework; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.TestTools; using UnityEngine.UI; using UnityEngine.XR.Interaction.Toolkit.UI; namespace jeanf.universalplayer.tests { /// /// Verifies the desktop world-canvas click/drag path without a gamepad: a /// DesktopWorldUiInteractor aiming the camera ray at a world-space canvas /// (TrackedDeviceGraphicRaycaster — the raycaster the module's screen pointer /// can never see) must produce real Button clicks, real Slider drags, honor /// the drag-vs-click threshold, yield to covering screen-space UI, and cancel /// cleanly mid-drag. Input and aim are driven through the component's test /// seams (PressProbe / ScreenPointProbe), the same pattern as /// BroadcastControlsStatus.HmdMountedProbe. /// public class DesktopWorldUiInteractorTests { private GameObject _eventSystemGo; private GameObject _cameraGo; private GameObject _canvasGo; private GameObject _interactorGo; private DesktopWorldUiInteractor _interactor; private Vector2 _aimPoint; private bool _pressed; private static Vector2 ScreenCenter => new Vector2(Screen.width * 0.5f, Screen.height * 0.5f); private class DragRecorder : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler { public int beginDragCount; public int dragCount; public int endDragCount; public int clickCount; public void OnBeginDrag(PointerEventData _) => beginDragCount++; public void OnDrag(PointerEventData _) => dragCount++; public void OnEndDrag(PointerEventData _) => endDragCount++; public void OnPointerClick(PointerEventData _) => clickCount++; } [UnitySetUp] public IEnumerator SetUp() { _aimPoint = ScreenCenter; _pressed = false; // fully qualified: inside the jeanf.* namespace, "EventSystem" resolves to // the jeanf.EventSystem namespace instead of the UGUI component _eventSystemGo = new GameObject("EventSystem", typeof(UnityEngine.EventSystems.EventSystem), typeof(XRUIInputModule)); // The interactor aims Camera.main — the tag is what resolves it. _cameraGo = new GameObject("TestCamera", typeof(Camera)) { tag = "MainCamera" }; // world-space canvas with the same raycaster the package's UI prefabs use _canvasGo = new GameObject("Canvas", typeof(Canvas), typeof(TrackedDeviceGraphicRaycaster)); var canvas = _canvasGo.GetComponent(); canvas.renderMode = RenderMode.WorldSpace; canvas.worldCamera = _cameraGo.GetComponent(); var canvasRect = _canvasGo.GetComponent(); canvasRect.position = new Vector3(0f, 0f, 2f); canvasRect.sizeDelta = new Vector2(400f, 400f); canvasRect.localScale = Vector3.one * 0.005f; // 2m x 2m — impossible to miss _interactorGo = new GameObject("DesktopWorldUiInteractor"); _interactorGo.SetActive(false); _interactor = _interactorGo.AddComponent(); _interactor.ForceActiveForTests = true; _interactor.FadedProbe = () => false; // no FadeMask in the test scene: the static defaults to faded _interactor.ScreenPointProbe = () => _aimPoint; _interactor.PressProbe = () => _pressed; _interactorGo.SetActive(true); // a couple frames so the canvas lays out and the raycaster registers yield return null; yield return null; } [UnityTearDown] public IEnumerator TearDown() { Object.Destroy(_interactorGo); Object.Destroy(_canvasGo); Object.Destroy(_eventSystemGo); Object.Destroy(_cameraGo); yield return null; } private GameObject AddFullCanvasChild(string name, params System.Type[] components) { var go = new GameObject(name, typeof(RectTransform), typeof(CanvasRenderer), typeof(Image)); foreach (var component in components) go.AddComponent(component); go.transform.SetParent(_canvasGo.transform, false); var rect = go.GetComponent(); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = Vector2.zero; rect.offsetMax = Vector2.zero; return go; } [UnityTest] public IEnumerator PressOverWorldButton_FiresOnClick() { var clickCount = 0; var button = AddFullCanvasChild("Button", typeof(Button)).GetComponent