Files
2026-11-18 19:42:44 +01:00

215 lines
14 KiB
C#

using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using KernelPanic.Meta;
using KernelPanic.Data;
using KernelPanic.UI;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UIElements;
public class DailyQuestLayoutTests
{
[Test]
public void WidgetAndDetailsDistinguishAllThreeStatesWithoutAdditionalFocusTargets()
{
var state = new DailyQuestState
{
selectedQuests = new[]
{
DailyQuestKind.Cards, DailyQuestKind.Damage, DailyQuestKind.Waves,
DailyQuestKind.BlockDamage, DailyQuestKind.NativeCards
},
cards = 15, damage = 200, waves = 3, claimedMask = 1 << (int)DailyQuestKind.Waves,
refreshAtUtcTicks = DateTime.UtcNow.AddHours(12).Ticks
};
var widget = DailyQuestView.BuildWidget(state, DateTime.UtcNow, () => { });
Assert.That(widget.tooltip, Is.Empty, "The terminal widget must not spawn a native floating tooltip over the menu.");
Assert.That(widget.Q<Label>("DailyQuestRewardReady").text, Is.EqualTo("1 reward ready"));
Assert.That(widget.Q<Label>("DailyQuestCompleted").text, Is.EqualTo("2/5 completed"));
Assert.That(widget.Q<Label>("DailyQuestClaimed").text, Is.EqualTo("1 claimed"));
foreach (var child in widget.Query<VisualElement>().ToList().Where(child => child != widget))
Assert.That(child.pickingMode, Is.EqualTo(PickingMode.Ignore), "Every part of the widget belongs to its button.");
var overlay = DailyQuestView.BuildOverlay(state, DateTime.UtcNow, (_, _) => { }, () => { });
Assert.That(overlay.Q<Button>("ClaimDailyQuestCards").text, Is.EqualTo("[ in progress ]"));
Assert.That(overlay.Q<Button>("ClaimDailyQuestDamage").text, Is.EqualTo("> CLAIM REWARD"));
Assert.That(overlay.Q<Button>("ClaimDailyQuestWaves").text, Is.EqualTo("[ CLAIMED ]"));
Assert.That(overlay.Q<Button>("ClaimDailyQuestCards").enabledSelf, Is.False);
Assert.That(overlay.Q<Button>("ClaimDailyQuestDamage").enabledSelf, Is.True);
Assert.That(overlay.Q<Button>("ClaimDailyQuestWaves").enabledSelf, Is.False);
Assert.That(overlay.Q<Label>("DailyQuestCompletionBonusStatus").text, Does.Contain("waiting for all five jobs to exit 0"));
Assert.That(overlay.Q<Label>(className: "daily-service-title").text, Is.EqualTo("[ crontab -l ]"));
Assert.That(overlay.Q("DailyQuestCards").Q<Label>(className: "daily-cron-entry").text,
Is.EqualTo("0 0 * * * /etc/cron.daily/play-cards"));
Assert.That(overlay.Q("DailyQuestDamage").Q<Label>(className: "daily-exit-status").text,
Does.Contain("exit=0").And.Contain("reward=pending"));
Assert.That(overlay.Q("DailyQuestCompletionBonus").Query<Label>().ToList().Select(label => label.text),
Does.Contain($"+{DailyQuests.CompletionBonusBandwidth} Bandwidth / +{DailyQuests.CompletionBonusEntropy} Entropy / +{DailyQuests.CompletionBonusCommits} Commits"));
Assert.That(overlay.Q("DailyQuestCards").Q<Label>(className: "daily-detail-progress").text, Is.EqualTo("[######....]"));
foreach (var label in overlay.Query<Label>(className: "daily-detail-progress").ToList())
Assert.That(label.text.Length, Is.EqualTo(12));
state.claimedMask |= 1 << (int)DailyQuestKind.Damage;
widget = DailyQuestView.BuildWidget(state, DateTime.UtcNow, () => { });
Assert.That(widget.Q("DailyQuestRewardReady").style.display.value, Is.EqualTo(DisplayStyle.None));
Assert.That(widget.Q<Label>("DailyQuestInspect").text, Is.EqualTo("> inspect tasks"));
}
[UnityTest]
public IEnumerator MenuQuestsWrapAndClaimsRemainReachableAcrossViewports()
{
if (SystemInfo.graphicsDeviceType == UnityEngine.Rendering.GraphicsDeviceType.Null)
Assert.Ignore("Requires a rendered editor panel.");
var window = ScriptableObject.CreateInstance<TerminalFocusTestWindow>();
var previousSize = UIPreferences.TextSize;
try
{
window.minSize = new Vector2(100, 100);
window.maxSize = new Vector2(4000, 3000);
window.Show();
var root = window.rootVisualElement;
// Match UIDocument stylesheet scope and order, including the later readability sheet.
AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/MainMenu.uxml").CloneTree(root);
var font = typeof(MainMenuController).Assembly.GetType("KernelPanic.UI.TerminalFontResolver")
.GetMethod("Resolve").Invoke(null, new object[] { null }) as UnityEngine.TextCore.Text.FontAsset;
root.style.unityFontDefinition = new StyleFontDefinition(font);
typeof(MainMenuController).Assembly.GetType("KernelPanic.UI.TerminalTheme")
.GetMethod("Attach", BindingFlags.Public | BindingFlags.Static).Invoke(null, new object[] { root });
root.Q("BootIntroPanel").style.display = DisplayStyle.None;
root.Q("ShellRoot").style.opacity = 1;
var featured = new FeaturedUnitPanel();
featured.Bind(root, null);
featured.Refresh(new[] { AssetDatabase.LoadAssetAtPath<DistroDefinition>("Assets/Data/Distros/Ubuntu.asset") });
Assert.That(root.Q("FeaturedUnitPanel").tooltip, Is.Empty,
"The neofetch preview must not spawn a native floating tooltip over the menu.");
var scroll = root.Q<ScrollView>(className: "home-scroll");
// 1024x576 also covers 1280x720 with the player's 1.25 panel scale.
foreach (var size in new[] { new Vector2(1920, 1080), new Vector2(1366, 768), new Vector2(1280, 720), new Vector2(1024, 576), new Vector2(390, 844), new Vector2(960, 400) })
foreach (var textSize in new[] { UITextSize.Standard, UITextSize.Large })
foreach (int group in new[] { 0, 3, 6 })
{
UIPreferences.TextSize = textSize;
window.position = new Rect(20, 20, size.x, size.y);
root.style.width = size.x;
root.style.height = size.y;
var data = SaveData.CreateDefault();
var now = DateTime.UtcNow;
DailyQuests.Refresh(data.dailyQuests, now);
data.dailyQuests.selectedQuests = Enumerable.Range(0, DailyQuests.Count)
.Select(offset => DailyQuests.Pool[(group + offset) % DailyQuests.Pool.Count]).ToArray();
foreach (var kind in DailyQuests.Active(data.dailyQuests))
DailyQuests.Record(data.dailyQuests, kind, int.MaxValue, now);
var host = root.Q("DailyQuestsHost");
host.Clear();
VisualElement overlay = null;
void RefreshWidget()
{
host.Clear();
host.Add(DailyQuestView.BuildWidget(data.dailyQuests, now, () =>
{
overlay = DailyQuestView.BuildOverlay(data.dailyQuests, now, (kind, period) =>
{
Assert.That(DailyQuests.Claim(data, kind, period, now), Is.True);
RefreshWidget();
}, () => overlay.RemoveFromHierarchy());
root.Add(overlay);
}));
}
RefreshWidget();
scroll.scrollOffset = Vector2.zero;
for (int frame = 0; frame < 6; frame++) yield return null;
string context = $"{size}, {textSize}";
Assert.That(host.Query(className: "daily-service-entry").ToList().Count, Is.EqualTo(DailyQuests.Count), context);
Assert.That(host.Q<ProgressBar>(), Is.Null);
Assert.That(host.parent, Is.SameAs(root.Q("FeaturedUnitPanel").parent));
Assert.That(host.worldBound.yMin, Is.GreaterThanOrEqualTo(root.Q("FeaturedUnitPanel").worldBound.yMax - 1), context);
if (size.x >= 1280)
{
Assert.That(host.worldBound.yMax, Is.LessThanOrEqualTo(scroll.contentViewport.worldBound.yMax + 1), context + ": fits below neofetch");
Assert.That(host.worldBound.xMin, Is.GreaterThan(root.Q(className: "command-column").worldBound.xMin + 300), context);
}
foreach (var label in host.Query<Label>().ToList())
{
Assert.That(label.worldBound.xMin, Is.GreaterThanOrEqualTo(scroll.contentViewport.worldBound.xMin - 1), context);
Assert.That(label.worldBound.xMax, Is.LessThanOrEqualTo(scroll.contentViewport.worldBound.xMax + 1), context);
float needed = label.MeasureTextSize(label.text, label.contentRect.width,
VisualElement.MeasureMode.AtMost, 0, VisualElement.MeasureMode.Undefined).y;
Assert.That(label.contentRect.height + 2, Is.GreaterThanOrEqualTo(needed), context + ": " + label.text);
}
var before = root.Q(className: "content-row").worldBound;
using (var submit = NavigationSubmitEvent.GetPooled())
{
var widget = host.Q<Button>("DailyQuestWidget");
submit.target = widget;
widget.SendEvent(submit);
}
for (int frame = 0; frame < 6; frame++) yield return null;
Assert.That(root.Q(className: "content-row").worldBound.yMin, Is.EqualTo(before.yMin).Within(1), "Opening details cannot push the menu downward.");
Assert.That(root.Q(className: "content-row").worldBound.height, Is.EqualTo(before.height).Within(1), "Details must not add menu height.");
var dialog = overlay.Q("DailyQuestDialog");
Assert.That(dialog.worldBound.xMin, Is.GreaterThanOrEqualTo(0), context);
Assert.That(dialog.worldBound.xMax, Is.LessThanOrEqualTo(size.x + 1), context);
Assert.That(dialog.worldBound.yMin, Is.GreaterThanOrEqualTo(0), context);
Assert.That(dialog.worldBound.yMax, Is.LessThanOrEqualTo(size.y + 1), context);
Assert.That(dialog.worldBound.center.x, Is.EqualTo(overlay.worldBound.center.x).Within(1),
context + ": dialog is horizontally centered");
Assert.That(dialog.worldBound.center.y, Is.EqualTo(overlay.worldBound.center.y).Within(1),
context + ": dialog is vertically centered");
var details = overlay.Q<ScrollView>("DailyQuestDetailScroll");
// Include the two-line confirmation in the desktop fit requirement.
var feedback = overlay.Q<Label>("DailyQuestFeedback");
feedback.text = "[ OK ] Daily task completed.\n[ OK ] +250 Bandwidth, +125 Entropy deposited.";
feedback.style.display = DisplayStyle.Flex;
for (int frame = 0; frame < 3; frame++) yield return null;
if (size.y <= 400)
Assert.That(details.contentContainer.worldBound.height, Is.GreaterThan(details.contentViewport.worldBound.height), context + ": short viewports scroll");
var headerBounds = overlay.Q("DailyQuestHeader").worldBound;
foreach (var kind in DailyQuests.Active(data.dailyQuests))
{
var reward = DailyQuests.Reward(kind);
Assert.That(details.Q("DailyQuest" + kind).Query<Label>().ToList().Select(label => label.text),
Has.Some.StartsWith($"+{reward.Bandwidth} Bandwidth / +{reward.Entropy} Entropy"));
}
foreach (var label in details.Query<Label>().ToList())
{
Assert.That(label.worldBound.xMax, Is.LessThanOrEqualTo(details.contentViewport.worldBound.xMax + 1), context + label.text);
float needed = label.MeasureTextSize(label.text, label.contentRect.width,
VisualElement.MeasureMode.AtMost, 0, VisualElement.MeasureMode.Undefined).y;
Assert.That(label.contentRect.height + 2, Is.GreaterThanOrEqualTo(needed), context + label.text);
}
foreach (var button in details.Query<Button>().ToList())
{
details.ScrollTo(button);
for (int frame = 0; frame < 3; frame++) yield return null;
Assert.That(overlay.Q("DailyQuestHeader").worldBound, Is.EqualTo(headerBounds), "The header stays fixed while scrolling.");
Assert.That(button.worldBound.yMin, Is.GreaterThanOrEqualTo(details.contentViewport.worldBound.yMin - 1), context);
Assert.That(button.worldBound.yMax, Is.LessThanOrEqualTo(details.contentViewport.worldBound.yMax + 1), context);
using var submit = NavigationSubmitEvent.GetPooled();
submit.target = button;
button.SendEvent(submit);
}
Assert.That(data.bandwidthBalance, Is.EqualTo(DailyQuests.Active(data.dailyQuests).Sum(kind => DailyQuests.Reward(kind).Bandwidth)
+ DailyQuests.CompletionBonusBandwidth));
Assert.That(data.entropyBalance, Is.EqualTo(DailyQuests.Active(data.dailyQuests).Sum(kind => DailyQuests.Reward(kind).Entropy)
+ DailyQuests.CompletionBonusEntropy));
Assert.That(host.Q<Label>("DailyQuestCompleted").text, Is.EqualTo("5/5 completed"));
Assert.That(host.Q("DailyQuestRewardReady").style.display.value, Is.EqualTo(DisplayStyle.None));
using (var escape = KeyDownEvent.GetPooled(new Event { type = EventType.KeyDown, keyCode = KeyCode.Escape }))
{
escape.target = overlay.Q<Button>("CloseDailyQuests");
overlay.Q<Button>("CloseDailyQuests").SendEvent(escape);
}
Assert.That(root.Q("DailyQuestOverlay"), Is.Null);
LogAssert.NoUnexpectedReceived();
}
}
finally
{
UIPreferences.TextSize = previousSize;
window.Close();
}
}
}