using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; using KernelPanic.Combat; using KernelPanic.Core; using KernelPanic.Data; using KernelPanic.Run; using KernelPanic.UI; using NUnit.Framework; using UnityEditor; using UnityEngine; using UnityEngine.TestTools; using UnityEngine.UIElements; public class CombatLayoutTests { private const BindingFlags Private = BindingFlags.Instance | BindingFlags.NonPublic; private static readonly Vector2[] Viewports = { new(1920, 1080), new(1280, 720), new(1080, 600), new(390, 844), new(960, 400) }; private GameObject _owner; private CombatManager _combat; private RunManager _run; private CombatSceneController _view; private TerminalFocusTestWindow _window; private VisualElement _root; private DistroDefinition _gentoo; private bool _reducedMotion; private UITextSize _textSize; private UILayoutDensity _density; private UnityEngine.Random.State _random; private static object Call(object owner, string method, params object[] args) => owner.GetType().GetMethod(method, Private).Invoke(owner, args); private static void Set(object owner, string field, object value) => owner.GetType().GetField(field, Private).SetValue(owner, value); [SetUp] public void SetUp() { if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null) Assert.Ignore("Layout regression tests require an editor panel; run Unity without -nographics."); _random = UnityEngine.Random.state; _reducedMotion = UIPreferences.ReducedMotion; _textSize = UIPreferences.TextSize; _density = UIPreferences.Density; UIPreferences.ReducedMotion = true; UIPreferences.TextSize = UITextSize.Standard; UIPreferences.Density = UILayoutDensity.Compact; _gentoo = AssetDatabase.LoadAssetAtPath("Assets/Data/Distros/Gentoo.asset"); _owner = new GameObject("Combat layout regression"); _run = _owner.AddComponent(); _combat = _owner.AddComponent(); Call(_combat, "OnDisable"); Call(_combat, "Awake"); _view = _owner.AddComponent(); _window = ScriptableObject.CreateInstance(); _window.titleContent = new GUIContent("Combat layout verification"); _window.minSize = new Vector2(100, 100); _window.maxSize = new Vector2(4000, 3000); _window.Show(); _root = _window.rootVisualElement; Set(_view, "_root", _root); Set(_view, "_runManager", _run); Set(_view, "_combatManager", _combat); Set(_view, "_distroAccent", _gentoo.AccentColor); Call(_view, "LoadStyles"); var theme = typeof(CombatSceneController).Assembly.GetType("KernelPanic.UI.TerminalTheme"); theme.GetMethod("Attach").Invoke(null, new object[] { _root }); theme.GetMethod("SetDistro").Invoke(null, new object[] { _root, _gentoo }); Call(_view, "BuildLayout"); Configure(1); } [TearDown] public void TearDown() { if (_view != null) Call(_view, "OnDisable"); if (_combat != null) Call(_combat, "OnDisable"); if (_owner != null) { UIPreferences.ReducedMotion = _reducedMotion; UIPreferences.TextSize = _textSize; UIPreferences.Density = _density; UnityEngine.Random.state = _random; UnityEngine.Object.DestroyImmediate(_owner); } if (_window != null) _window.Close(); } private void Configure(int version) { var config = new RunConfig(_gentoo, Language.Haskell, Language.C, _gentoo.ExclusiveCards.Take(4).ToArray(), 628207484, version); _run.StartRun(config); Set(_combat, "_runConfig", config); Set(_combat, "_runManager", _run); Set(_combat, "_playerState", new CombatantState(55, 6, 3)); Set(_combat, "_handController", new HandController(6)); Set(_combat, "currentPhase", TurnPhase.Execute); _combat.Compilation.BeginWave(version); var cards = AssetDatabase.LoadAssetAtPath("Assets/Data/Cards/CardDatabase.asset"); foreach (var id in new[] { "lang_hs_thunk", "gentoo_emerge_world", "lang_hs_map" }) _combat.HandController.Add(new CardInstance(cards.FindById(id))); var enemies = (List)_combat.Enemies; enemies.Clear(); enemies.Add(new EnemyInstance(EnemyArchetypeCatalog.Get("zombie_process"), 9)); enemies.Add(new EnemyInstance(EnemyArchetypeCatalog.Get("memory_leak"), 4)); } [UnityTest] public IEnumerator NixGovernorAndGoSchedulerStayReadableAndAcceptKeyboardActions() { var cards = AssetDatabase.LoadAssetAtPath("Assets/Data/Cards/CardDatabase.asset"); var nix = AssetDatabase.LoadAssetAtPath("Assets/Data/Distros/NixOS.asset"); var config = new RunConfig(nix, Language.Haskell, Language.Go, nix.ExclusiveCards.Take(4).ToArray(), 73, 3); _run.StartRun(config); Set(_combat, "_runConfig", config); Set(_combat, "_playerState", new CombatantState(60, 6, 3)); Set(_combat, "_handController", new HandController(6)); _combat.Compilation.BeginWave(0); for (int i = 0; i < 3; i++) _combat.Go.Deploy(new CardInstance(cards.FindById("lang_go_worker")), 3, 3, 1, _combat.Enemies[0].State); foreach (var size in Viewports) foreach (var textSize in new[] { UITextSize.Standard, UITextSize.Large }) { Resize(size); UIPreferences.TextSize = textSize; _combat.Nix.BeginWave(3); Call(_combat, "ApplyGovernorModifiers"); Call(_view, "RefreshPlayer"); Call(_view, "RefreshTracks"); for (int frame = 0; frame < 6; frame++) yield return null; string context = $"{size}/{textSize}"; var panel = _root.Q("NixGovernorPanel"); foreach (var button in panel.Query