Skip to content

Commit 650cdea

Browse files
committed
feat: add automation testing helpers for getting button coordinates
1 parent 1124405 commit 650cdea

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

Assets/JSPlugin/JSBridge.jslib

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ mergeInto(LibraryManager.library, {
7878
'*'
7979
)
8080
},
81+
OnElementBounds: function (strPtr) {
82+
const json = UTF8ToString(strPtr)
83+
const targetWindow = (() => {
84+
try {
85+
return window.self !== window.top ? window : window.parent
86+
} catch (e) {
87+
return window.parent
88+
}
89+
})()
90+
targetWindow.postMessage(
91+
{
92+
type: 'unity-renderer',
93+
payload: {
94+
type: 'element-bounds',
95+
payload: json,
96+
},
97+
},
98+
'*'
99+
)
100+
},
81101
PreloadURLs: function(strPtr) {
82102
const csv = UTF8ToString(strPtr);
83103
const urls = csv.split(',');

Assets/Scripts/Configurator/ConfiguratorUIPresenter.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,5 +397,38 @@ public void ClearPresetSelection()
397397
{
398398
_presetsView.ClearSelection();
399399
}
400+
401+
public void GetElementBounds(string elementName)
402+
{
403+
var root = uiDocument.rootVisualElement;
404+
var element = root.Q(elementName);
405+
406+
if (element == null || element.resolvedStyle.display == DisplayStyle.None || element.worldBound.width <= 0)
407+
{
408+
JSBridge.NativeCalls.OnElementBounds(JsonUtility.ToJson(new ElementBoundsData { name = elementName }));
409+
return;
410+
}
411+
412+
var bounds = element.worldBound;
413+
var panelLayout = root.panel.visualTree.layout;
414+
415+
JSBridge.NativeCalls.OnElementBounds(JsonUtility.ToJson(new ElementBoundsData
416+
{
417+
name = elementName,
418+
found = true,
419+
x = bounds.x / panelLayout.width,
420+
y = bounds.y / panelLayout.height,
421+
width = bounds.width / panelLayout.width,
422+
height = bounds.height / panelLayout.height,
423+
}));
424+
}
425+
426+
[Serializable]
427+
private struct ElementBoundsData
428+
{
429+
public string name;
430+
public bool found;
431+
public float x, y, width, height;
432+
}
400433
}
401434
}

Assets/Scripts/JSBridge.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using Configurator;
34
using JetBrains.Annotations;
45
using Preview;
56
using UnityEngine;
@@ -17,6 +18,7 @@
1718
public class JSBridge : MonoBehaviour
1819
{
1920
[SerializeField] private PreviewController previewController;
21+
[SerializeField] private ConfiguratorUIPresenter configuratorUIPresenter;
2022

2123
[UsedImplicitly]
2224
public void ParseFromURL() => AangConfiguration.RecreateFrom(Application.absoluteURL);
@@ -79,6 +81,9 @@ public void SetUrns(string value) =>
7981
[UsedImplicitly]
8082
public void SetUsername(string value) => AangConfiguration.Instance.Username = value;
8183

84+
[UsedImplicitly]
85+
public void GetElementBounds(string elementName) => configuratorUIPresenter.GetElementBounds(elementName);
86+
8287
[UsedImplicitly]
8388
public void Reload() => previewController.InvokeReload();
8489

@@ -147,7 +152,9 @@ public static void OnScreenshotTaken(string base64Str) =>
147152
public static void OnError(string message) => Debug.LogError($"NativeCall OnError({message})");
148153

149154
public static void OnCustomizationDone(string message) => Debug.Log($"NativeCall OnCustomizationDone({message})");
150-
155+
156+
public static void OnElementBounds(string json) => Debug.Log($"NativeCall OnElementBounds({json})");
157+
151158
// ReSharper disable once InconsistentNaming
152159
public static void PreloadURLs(string urlsCSV) => Debug.Log($"NativeCall PreloadURLs({urlsCSV})");
153160
#else
@@ -162,7 +169,10 @@ public static void OnScreenshotTaken(string base64Str) =>
162169

163170
[System.Runtime.InteropServices.DllImport("__Internal")]
164171
public static extern void OnCustomizationDone(string message);
165-
172+
173+
[System.Runtime.InteropServices.DllImport("__Internal")]
174+
public static extern void OnElementBounds(string json);
175+
166176
[System.Runtime.InteropServices.DllImport("__Internal")]
167177
public static extern void PreloadURLs(string urlsCSV);
168178
#endif

0 commit comments

Comments
 (0)