179 lines
10 KiB
C#
179 lines
10 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");
|
|
|
|
window.position = new Rect(32, 32, 1280, 720);
|
|
root.style.width = 1280;
|
|
root.style.height = 720;
|
|
screen.RefreshUnits(database.AllDistros);
|
|
for (int frame = 0; frame < 5; frame++) yield return null;
|
|
var columns = scroll.Q<Label>(className: "unit-ls-header");
|
|
var firstUnit = scroll.Q<Button>(className: "unit-overview-row");
|
|
Assert.That(firstUnit.worldBound.yMin, Is.GreaterThanOrEqualTo(columns.worldBound.yMax - 1));
|
|
Assert.That(firstUnit.worldBound.yMin - columns.worldBound.yMax, Is.LessThan(12),
|
|
"The distro roster should start immediately below its ls header, not at the bottom of the pane.");
|
|
|
|
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: "unit-overview-section-label");
|
|
var facts = identity.Q(className: "unit-overview-facts");
|
|
var languages = identity.Q("UnitOverview_languages");
|
|
var uptime = identity.Q("UnitOverview_uptime");
|
|
var ram = identity.Q("UnitOverview_ram");
|
|
var cycles = identity.Q("UnitOverview_cycles");
|
|
var description = identity.Q<Label>("UnitOverview_description");
|
|
var passiveTitle = passive.Q<Label>(className: "unit-overview-section-label");
|
|
var passiveName = passive.Q<Label>("UnitPassive_name");
|
|
var rules = passive.Q<Label>("UnitPassive_rules");
|
|
Assert.That(identityTitle.text, Is.EqualTo("[host]"), context);
|
|
Assert.That(passiveTitle.text, Is.EqualTo("[passive]"), context);
|
|
Assert.That(description.text, Is.EqualTo(unit.Description), context);
|
|
Assert.That(passiveName.text, Is.EqualTo(unit.Passive.Name), context);
|
|
Assert.That(languages.Q<Label>(className: "unit-overview-fact-value").text,
|
|
Is.EqualTo(DistroPresentation.FormatLanguages(unit)), context);
|
|
Assert.That(uptime.Q<Label>(className: "unit-overview-fact-value").text,
|
|
Is.EqualTo(unit.BaseUptime.ToString()), context);
|
|
Assert.That(ram.Q<Label>(className: "unit-overview-fact-value").text,
|
|
Is.EqualTo(unit.BaseRam.ToString()), context);
|
|
Assert.That(cycles.Q<Label>(className: "unit-overview-fact-value").text,
|
|
Is.EqualTo(unit.BaseCyclesPerTurn.ToString()), context);
|
|
if (unit.Id == "nixos")
|
|
Assert.That(rules.text, Does.Contain("performance +20% damage").And.Contain("powersave -20% damage").And.Contain("schedutil +5% damage"), context);
|
|
else
|
|
Assert.That(rules.text, Is.EqualTo(unit.Passive.RulesText), context);
|
|
Assert.That(identity.Q("UnitConfig_name"), Is.Null, context + ": duplicate name");
|
|
Assert.That(identity.Q("UnitConfig_release"), Is.Null, context + ": duplicate release");
|
|
Assert.That(identity.Q("UnitConfig_version"), Is.Null, context + ": duplicate version");
|
|
Assert.That(identity.Q("UnitConfig_rarity"), Is.Null, context + ": duplicate rarity");
|
|
Assert.That(languages.worldBound.yMin, Is.GreaterThan(identityTitle.worldBound.yMin), context);
|
|
Assert.That(description.worldBound.yMin, Is.GreaterThanOrEqualTo(facts.worldBound.yMax - 1), context);
|
|
Assert.That(passiveTitle.worldBound.yMin, Is.GreaterThan(description.worldBound.yMin), context);
|
|
Assert.That(rules.worldBound.yMin, Is.GreaterThan(passiveTitle.worldBound.yMin), context);
|
|
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>("UnitPassive_flavor");
|
|
if (flavor != null)
|
|
{
|
|
Assert.That(flavor.text, Is.EqualTo("# " + unit.Passive.FlavorText), context);
|
|
Assert.That(flavor.worldBound.yMin, Is.GreaterThan(rules.worldBound.yMin), context + ": comment");
|
|
}
|
|
|
|
// On a short viewport the document must scroll, retaining the same gaps and text height.
|
|
if (size.y == 400 && scroll.contentContainer.layout.height > scroll.contentViewport.layout.height + 1)
|
|
{
|
|
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);
|
|
Assert.That(rules.worldBound.yMin, Is.GreaterThan(passiveTitle.worldBound.yMin), 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);
|
|
}
|
|
}
|