147 lines
7.8 KiB
C#
147 lines
7.8 KiB
C#
using System.Collections;
|
|
using System.Reflection;
|
|
using KernelPanic.Data;
|
|
using KernelPanic.Meta;
|
|
using KernelPanic.UI;
|
|
using NUnit.Framework;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class CollectionLayoutTests
|
|
{
|
|
private static readonly Vector2[] Viewports =
|
|
{
|
|
new(1280, 720), new(1080, 600), new(390, 844), new(960, 400)
|
|
};
|
|
|
|
[UnityTest]
|
|
public IEnumerator EveryDistroOverviewKeepsReadableSpacingAndWrappedTextAcrossViewports()
|
|
{
|
|
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null)
|
|
Assert.Ignore("Collection layout tests require an editor panel; run Unity without -nographics.");
|
|
|
|
var window = ScriptableObject.CreateInstance<TerminalFocusTestWindow>();
|
|
var originalTextSize = UIPreferences.TextSize;
|
|
var originalDensity = UIPreferences.Density;
|
|
try
|
|
{
|
|
window.titleContent = new GUIContent("Collection layout verification");
|
|
window.minSize = new Vector2(100, 100);
|
|
window.maxSize = new Vector2(4000, 3000);
|
|
window.Show();
|
|
var root = window.rootVisualElement;
|
|
root.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/UI/MainMenu.uss"));
|
|
typeof(MainMenuController).Assembly.GetType("KernelPanic.UI.TerminalTheme")
|
|
.GetMethod("Attach", BindingFlags.Public | BindingFlags.Static)
|
|
.Invoke(null, new object[] { root });
|
|
|
|
var template = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/CollectionScreen.uxml").CloneTree();
|
|
var panel = template.Q("CollectionPanel");
|
|
panel.RemoveFromClassList("hidden");
|
|
root.Add(panel);
|
|
var database = AssetDatabase.LoadAssetAtPath<DistroDatabase>("Assets/Data/Distros/DistroDatabase.asset");
|
|
Assert.That(database.AllDistros, Is.Not.Empty);
|
|
var owned = new PlayerCollection();
|
|
foreach (var unit in database.AllDistros) owned.Add(unit);
|
|
var screen = new CollectionScreenController();
|
|
screen.Bind(root, null, null, null, owned);
|
|
var scroll = root.Q<ScrollView>("CollectionDetail");
|
|
|
|
foreach (var size in Viewports)
|
|
foreach (var textSize in new[] { UITextSize.Standard, UITextSize.Large })
|
|
foreach (var density in new[] { UILayoutDensity.Compact, UILayoutDensity.Comfortable })
|
|
foreach (var unit in database.AllDistros)
|
|
{
|
|
UIPreferences.TextSize = textSize;
|
|
UIPreferences.Density = density;
|
|
window.position = new Rect(32, 32, size.x, size.y);
|
|
root.style.width = size.x;
|
|
root.style.height = size.y;
|
|
screen.RefreshUnits(new[] { unit });
|
|
screen.ActivateSelected();
|
|
for (int frame = 0; frame < 6; frame++) yield return null;
|
|
|
|
string context = $"{unit.Id}, {size}, {textSize}, {density}";
|
|
var identity = scroll.Q(className: "unit-identity-section");
|
|
var passive = scroll.Q(className: "unit-passive-section");
|
|
Assert.That(identity, Is.Not.Null, context);
|
|
Assert.That(passive, Is.Not.Null, context);
|
|
var identityTitle = identity.Q<Label>(className: "detail-section-title");
|
|
var languages = identity.Q(className: "kv-row");
|
|
var description = identity.Q<Label>(className: "collection-detail-description");
|
|
var passiveTitle = passive.Q<Label>(className: "detail-section-title");
|
|
var rules = passive.Q<Label>(className: "collection-detail-description");
|
|
Assert.That(description.text, Is.EqualTo(unit.Description), context);
|
|
Assert.That(passiveTitle.text, Is.EqualTo(unit.Passive.Name), context);
|
|
if (unit.Id == "nixos")
|
|
Assert.That(rules.text, Does.Contain("performance: +20% card damage").And.Contain("powersave: -20% card damage").And.Contain("schedutil: +5% card damage"), context);
|
|
else
|
|
Assert.That(rules.text, Is.EqualTo(unit.Passive.RulesText), context);
|
|
float copyGap = density == UILayoutDensity.Comfortable ? 14 : 10;
|
|
float sectionGap = density == UILayoutDensity.Comfortable ? 31.5f : 23.5f;
|
|
AssertVerticalGap(identityTitle, languages, copyGap, context + ": identity heading");
|
|
AssertVerticalGap(languages, description, copyGap, context + ": language row");
|
|
AssertVerticalGap(description, passiveTitle, sectionGap, context + ": passive section");
|
|
AssertVerticalGap(passiveTitle, rules, copyGap, context + ": passive heading");
|
|
Assert.That(rules.resolvedStyle.fontSize, Is.EqualTo(textSize == UITextSize.Large ? 17 : 15), context);
|
|
|
|
foreach (var section in new[] { identity, passive })
|
|
{
|
|
AssertContainedHorizontally(section, scroll.contentViewport, context);
|
|
foreach (var label in section.Query<Label>().ToList())
|
|
{
|
|
AssertContainedHorizontally(label, section, context + ": " + label.text);
|
|
AssertTextFits(label, context);
|
|
Assert.That(label.worldBound.yMin, Is.GreaterThanOrEqualTo(section.worldBound.yMin - 1), context);
|
|
Assert.That(label.worldBound.yMax, Is.LessThanOrEqualTo(section.worldBound.yMax + 1), context);
|
|
}
|
|
}
|
|
var flavor = passive.Q<Label>(className: "flavor-text");
|
|
if (flavor != null) AssertVerticalGap(rules, flavor, copyGap, context + ": flavor text");
|
|
|
|
// On a short viewport the document must scroll, retaining the same gaps and text height.
|
|
if (size.y == 400)
|
|
{
|
|
Assert.That(scroll.contentContainer.layout.height,
|
|
Is.GreaterThan(scroll.contentViewport.layout.height), context + ": scrollable content");
|
|
Assert.That(scroll.verticalScroller.highValue, Is.GreaterThan(0), context);
|
|
scroll.ScrollTo(passive);
|
|
for (int frame = 0; frame < 2; frame++) yield return null;
|
|
Assert.That(scroll.scrollOffset.y, Is.GreaterThan(0), context);
|
|
AssertVerticalGap(passiveTitle, rules, copyGap, context + ": scrolled passive heading");
|
|
AssertTextFits(rules, context);
|
|
}
|
|
}
|
|
LogAssert.NoUnexpectedReceived();
|
|
}
|
|
finally
|
|
{
|
|
window.Close();
|
|
UIPreferences.TextSize = originalTextSize;
|
|
UIPreferences.Density = originalDensity;
|
|
}
|
|
}
|
|
|
|
private static void AssertVerticalGap(VisualElement above, VisualElement below, float minimum, string context)
|
|
{
|
|
Assert.That(below.worldBound.yMin - above.worldBound.yMax, Is.GreaterThanOrEqualTo(minimum), context);
|
|
}
|
|
|
|
private static void AssertContainedHorizontally(VisualElement child, VisualElement parent, string context)
|
|
{
|
|
Assert.That(child.worldBound.xMin, Is.GreaterThanOrEqualTo(parent.worldBound.xMin - 1), context);
|
|
Assert.That(child.worldBound.xMax, Is.LessThanOrEqualTo(parent.worldBound.xMax + 1), context);
|
|
}
|
|
|
|
private static void AssertTextFits(Label label, string context)
|
|
{
|
|
Assert.That(label.contentRect.width, Is.GreaterThan(0), context + ": text width");
|
|
float requiredHeight = label.MeasureTextSize(label.text, label.contentRect.width,
|
|
VisualElement.MeasureMode.Exactly, 0, VisualElement.MeasureMode.Undefined).y;
|
|
Assert.That(label.contentRect.height, Is.GreaterThanOrEqualTo(requiredHeight - 1),
|
|
context + ": clipped text: " + label.text);
|
|
}
|
|
}
|