Skip to content

Commit afb0034

Browse files
fix(ui): use reflection for currentTarget setter in tests
The currentTarget setter is internal in UIElements. Use reflection with a null check so tests gracefully skip if the API changes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: JasonXuDeveloper - 傑 <jason@xgamedev.net>
1 parent 2190b92 commit afb0034

File tree

1 file changed

+41
-6
lines changed
  • UnityProject/Packages/com.jasonxudeveloper.jengine.ui/Tests/Editor/Components/Navigation

1 file changed

+41
-6
lines changed

UnityProject/Packages/com.jasonxudeveloper.jengine.ui/Tests/Editor/Components/Navigation/JTabViewTests.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ public class JTabViewTests
1616
{
1717
private JTabView _tabView;
1818

19+
/// <summary>
20+
/// Sets <c>currentTarget</c> on a UIElements event via reflection,
21+
/// since the setter is internal. Returns false if the property or
22+
/// setter is not available (e.g. Unity API changes across versions).
23+
/// </summary>
24+
private static bool TrySetCurrentTarget(object evt, IEventHandler handler)
25+
{
26+
var prop = evt.GetType().GetProperty("currentTarget",
27+
BindingFlags.Public | BindingFlags.Instance);
28+
var setter = prop?.GetSetMethod(true);
29+
if (setter == null) return false;
30+
setter.Invoke(evt, new object[] { handler });
31+
return true;
32+
}
33+
1934
[SetUp]
2035
public void SetUp()
2136
{
@@ -570,7 +585,11 @@ public void OnTabClicked_SelectsCorrectTab()
570585
Assert.IsNotNull(method, "OnTabClicked method should exist");
571586

572587
var evt = (MouseDownEvent)Activator.CreateInstance(typeof(MouseDownEvent), true);
573-
evt.currentTarget = secondTab;
588+
if (!TrySetCurrentTarget(evt, secondTab))
589+
{
590+
Assert.Inconclusive("currentTarget setter not available in this Unity version");
591+
return;
592+
}
574593
method.Invoke(null, new object[] { evt });
575594

576595
Assert.AreEqual(1, _tabView.SelectedIndex);
@@ -585,7 +604,11 @@ public void OnTabClicked_WithNonLabelTarget_DoesNothing()
585604
BindingFlags.NonPublic | BindingFlags.Static);
586605

587606
var evt = (MouseDownEvent)Activator.CreateInstance(typeof(MouseDownEvent), true);
588-
evt.currentTarget = new VisualElement();
607+
if (!TrySetCurrentTarget(evt, new VisualElement()))
608+
{
609+
Assert.Inconclusive("currentTarget setter not available in this Unity version");
610+
return;
611+
}
589612
method.Invoke(null, new object[] { evt });
590613

591614
Assert.AreEqual(0, _tabView.SelectedIndex);
@@ -605,7 +628,11 @@ public void OnTabMouseEnter_InactiveTab_SetsHoverBackground()
605628
Assert.IsNotNull(method, "OnTabMouseEnter method should exist");
606629

607630
var evt = (MouseEnterEvent)Activator.CreateInstance(typeof(MouseEnterEvent), true);
608-
evt.currentTarget = inactiveTab;
631+
if (!TrySetCurrentTarget(evt, inactiveTab))
632+
{
633+
Assert.Inconclusive("currentTarget setter not available in this Unity version");
634+
return;
635+
}
609636
method.Invoke(_tabView, new object[] { evt });
610637

611638
Assert.AreEqual(Tokens.Colors.BgHover, inactiveTab.style.backgroundColor.value);
@@ -624,7 +651,11 @@ public void OnTabMouseEnter_ActiveTab_DoesNotChangeBackground()
624651
var method = typeof(JTabView).GetMethod("OnTabMouseEnter",
625652
BindingFlags.NonPublic | BindingFlags.Instance);
626653
var evt = (MouseEnterEvent)Activator.CreateInstance(typeof(MouseEnterEvent), true);
627-
evt.currentTarget = activeTab;
654+
if (!TrySetCurrentTarget(evt, activeTab))
655+
{
656+
Assert.Inconclusive("currentTarget setter not available in this Unity version");
657+
return;
658+
}
628659
method.Invoke(_tabView, new object[] { evt });
629660

630661
Assert.AreEqual(expectedBg, activeTab.style.backgroundColor.value);
@@ -643,14 +674,18 @@ public void OnTabMouseLeave_InactiveTab_ClearsBackground()
643674
var enterMethod = typeof(JTabView).GetMethod("OnTabMouseEnter",
644675
BindingFlags.NonPublic | BindingFlags.Instance);
645676
var enterEvt = (MouseEnterEvent)Activator.CreateInstance(typeof(MouseEnterEvent), true);
646-
enterEvt.target = inactiveTab;
677+
if (!TrySetCurrentTarget(enterEvt, inactiveTab))
678+
{
679+
Assert.Inconclusive("currentTarget setter not available in this Unity version");
680+
return;
681+
}
647682
enterMethod.Invoke(_tabView, new object[] { enterEvt });
648683

649684
// Then leave
650685
var leaveMethod = typeof(JTabView).GetMethod("OnTabMouseLeave",
651686
BindingFlags.NonPublic | BindingFlags.Instance);
652687
var leaveEvt = (MouseLeaveEvent)Activator.CreateInstance(typeof(MouseLeaveEvent), true);
653-
leaveEvt.target = inactiveTab;
688+
TrySetCurrentTarget(leaveEvt, inactiveTab);
654689
leaveMethod.Invoke(_tabView, new object[] { leaveEvt });
655690

656691
Assert.AreEqual(StyleKeyword.None, inactiveTab.style.backgroundColor.keyword);

0 commit comments

Comments
 (0)