using System; using System.Collections; using System.Reflection; using KernelPanic.UI; using NUnit.Framework; using UnityEditor; using UnityEngine; using UnityEngine.TestTools; using UnityEngine.UIElements; public class ReadabilityTests { private string _testKey; private bool _motion; private UITextSize _size; private UILayoutDensity _density; private int _soundVolume; private const BindingFlags InternalStatic = BindingFlags.NonPublic | BindingFlags.Static; [SetUp] public void SetUp() { _testKey = "KernelPanic.UI.Tests." + Guid.NewGuid().ToString("N"); _motion = UIPreferences.ReducedMotion; _size = UIPreferences.TextSize; _density = UIPreferences.Density; _soundVolume = UIPreferences.SoundVolume; } [TearDown] public void TearDown() { UIPreferences.ReducedMotion = _motion; UIPreferences.TextSize = _size; UIPreferences.Density = _density; UIPreferences.SoundVolume = _soundVolume; PlayerPrefs.DeleteKey(_testKey); } [TestCase(false, UITextSize.Standard, UILayoutDensity.Compact)] [TestCase(true, UITextSize.Large, UILayoutDensity.Compact)] [TestCase(false, UITextSize.Standard, UILayoutDensity.Comfortable)] [TestCase(true, UITextSize.Large, UILayoutDensity.Comfortable)] public void PreferencesRoundTripWithoutChangingTheProgressionSaveOrPersistingTemporaryOverrides( bool motion, UITextSize size, UILayoutDensity density) { string productionKey = (string)typeof(UIPreferences).GetField("StorageKey", InternalStatic).GetRawConstantValue(); string productionBefore = PlayerPrefs.GetString(productionKey, ""); UIPreferences.ReducedMotion = motion; UIPreferences.TextSize = size; UIPreferences.Density = density; InvokeStore("SaveToKey"); string stored = PlayerPrefs.GetString(_testKey); UIPreferences.ReducedMotion = !motion; UIPreferences.TextSize = size == UITextSize.Standard ? UITextSize.Large : UITextSize.Standard; UIPreferences.Density = density == UILayoutDensity.Compact ? UILayoutDensity.Comfortable : UILayoutDensity.Compact; Assert.That(PlayerPrefs.GetString(_testKey), Is.EqualTo(stored), "Transient overrides must not write preferences."); InvokeStore("LoadFromKey"); Assert.That(UIPreferences.ReducedMotion, Is.EqualTo(motion)); Assert.That(UIPreferences.TextSize, Is.EqualTo(size)); Assert.That(UIPreferences.Density, Is.EqualTo(density)); Assert.That(PlayerPrefs.GetString(productionKey, ""), Is.EqualTo(productionBefore), "Fixtures never save to the player's preference key."); } [TestCase(null)] [TestCase("not-json")] [TestCase("{\"version\":99,\"reducedMotion\":true,\"textSize\":1,\"density\":1}")] [TestCase("{\"version\":1,\"textSize\":38,\"density\":-7}")] public void ExistingPlayersAndUnknownPreferenceValuesUseReadableDefaults(string json) { if (json != null) PlayerPrefs.SetString(_testKey, json); UIPreferences.ReducedMotion = true; UIPreferences.TextSize = UITextSize.Large; UIPreferences.Density = UILayoutDensity.Comfortable; InvokeStore("LoadFromKey"); Assert.That(UIPreferences.ReducedMotion, Is.False); Assert.That(UIPreferences.TextSize, Is.EqualTo(UITextSize.Standard)); Assert.That(UIPreferences.Density, Is.EqualTo(UILayoutDensity.Compact)); Assert.That(PlayerPrefs.GetString(_testKey, ""), Is.EqualTo(json ?? ""), "Loading must not rewrite old or unknown preferences."); } [TestCase(0)] [TestCase(25)] [TestCase(100)] public void SoundVolumeRoundTripsAndMuteIsPreserved(int volume) { UIPreferences.SoundVolume = volume; InvokeStore("SaveToKey"); UIPreferences.SoundVolume = 75; InvokeStore("LoadFromKey"); Assert.That(UIPreferences.SoundVolume, Is.EqualTo(volume)); UIPreferences.SoundVolume = -1; Assert.That(UIPreferences.SoundVolume, Is.Zero); UIPreferences.SoundVolume = 999; Assert.That(UIPreferences.SoundVolume, Is.EqualTo(100)); } [UnityTest] public IEnumerator PreferencesUpdateEveryAttachedRootAndDetachSubscriptionsAreReleased() { RequireGraphics(); var window = ScriptableObject.CreateInstance(); try { window.Show(); var first = new VisualElement(); var second = new VisualElement(); window.rootVisualElement.Add(first); window.rootVisualElement.Add(second); UIPreferences.ReducedMotion = false; UIPreferences.TextSize = UITextSize.Standard; UIPreferences.Density = UILayoutDensity.Compact; Attach(first); Attach(second); Attach(first); // Repeated screen binding must not add duplicate subscriptions or sheets. UIPreferences.ReducedMotion = true; UIPreferences.TextSize = UITextSize.Large; UIPreferences.Density = UILayoutDensity.Comfortable; yield return null; foreach (var root in new[] { first, second }) { Assert.That(root.ClassListContains("reduced-motion"), Is.True); Assert.That(root.ClassListContains("text-large"), Is.True); Assert.That(root.ClassListContains("density-comfortable"), Is.True); Assert.That(root.styleSheets.count, Is.EqualTo(2)); } second.RemoveFromHierarchy(); UIPreferences.ReducedMotion = false; UIPreferences.TextSize = UITextSize.Standard; UIPreferences.Density = UILayoutDensity.Compact; Assert.That(first.ClassListContains("text-large"), Is.False); Assert.That(second.ClassListContains("text-large"), Is.True, "A detached screen must not remain subscribed."); window.rootVisualElement.Add(second); Assert.That(second.ClassListContains("text-large"), Is.False, "Reattached screens immediately receive current preferences."); Assert.That(second.ClassListContains("density-comfortable"), Is.False); Assert.That(second.ClassListContains("reduced-motion"), Is.False); } finally { window.Close(); } } [UnityTest] public IEnumerator SettingsRemainReadableAndKeyboardReachableAtEveryTextAndSpacingPreset() { RequireGraphics(); var window = ScriptableObject.CreateInstance(); try { window.minSize = new Vector2(100, 100); window.Show(); var root = window.rootVisualElement; root.styleSheets.Add(AssetDatabase.LoadAssetAtPath("Assets/UI/MainMenu.uss")); Attach(root); var template = AssetDatabase.LoadAssetAtPath("Assets/UI/SettingsScreen.uxml").CloneTree(); var settings = template.Q("SettingsPanel"); settings.RemoveFromClassList("hidden"); root.Add(settings); var scroll = settings.Q(); foreach (var viewport in new[] { new Vector2(960, 600), new Vector2(390, 600) }) foreach (UITextSize size in Enum.GetValues(typeof(UITextSize))) foreach (UILayoutDensity density in Enum.GetValues(typeof(UILayoutDensity))) { window.position = new Rect(24, 24, viewport.x, viewport.y); root.style.width = viewport.x; root.style.height = viewport.y; UIPreferences.TextSize = size; UIPreferences.Density = density; for (int frame = 0; frame < 5; frame++) yield return null; string context = $"{viewport}, {size}, {density}"; foreach (var label in settings.Query