rimworld-blax-castes/Tests/BlaxCastesTests/Source/UiTests.cs

565 lines
35 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BlaxCastes;
using HarmonyLib;
using RimWorld;
using Verse;
namespace BlaxCastesTests
{
/// <summary>
/// The Caste column in the Assign tab and the caste manager window. Headless runs have no GUI, so
/// these drive the logic behind the widgets: the table def, the cell menu, labels, sorting and the
/// manager's overrides. Drawing is checked by hand.
/// </summary>
public static class UiTests
{
// ---- helpers -----------------------------------------------------------------------------
private static PawnColumnDef NeedColumn()
{
PawnColumnDef col = CasteColumns.Column;
T.NotNull(col, "PawnColumnDef " + PawnColumnWorker_BlaxCaste.DefName);
return col;
}
private static string ColumnList(PawnTableDef table) => string.Join(", ", table.columns.Select(c => c?.defName ?? "null"));
private static void CheckRightBeforeHostility(PawnTableDef table, PawnColumnDef col)
{
int at = table.columns.IndexOf(col);
int hostility = table.columns.FindIndex(c => c != null && c.defName == CasteColumns.AnchorColumn);
T.Check(at >= 0, "Caste column not in the Assign table: " + ColumnList(table));
T.Check(hostility >= 0, "HostilityResponse not in the Assign table: " + ColumnList(table));
T.Equal(hostility - 1, at, "Caste column index (columns: " + ColumnList(table) + ")");
T.Equal(1, table.columns.Count(c => c == col), "times the Caste column is listed");
}
private static FoodPolicy Simple => Fixtures.Need(Fixtures.Food("FoodRestrictionSimple"), "Simple food");
private static FoodPolicy Lavish => Fixtures.Need(Fixtures.Food("FoodRestrictionLavish"), "Lavish food");
private static Caste Full(string label)
{
Caste c = Fixtures.NewCaste(label);
CasteManager.SetSlot(c, CasteSlot.Apparel, Fixtures.Need(Fixtures.Outfit("OutfitNudist"), "Nudist outfit"));
CasteManager.SetSlot(c, CasteSlot.Food, Simple);
CasteManager.SetSlot(c, CasteSlot.Drug, Fixtures.Need(Fixtures.Drug("NoDrugs"), "No drugs"));
CasteManager.SetSlot(c, CasteSlot.Reading, Fixtures.Need(Fixtures.Reading("NoneReadingPolicy"), "None reading"));
CasteManager.SetSlot(c, CasteSlot.MedCare, MedicalCareCategory.HerbalOrWorse);
return c;
}
private static string Policies(Pawn p) =>
string.Join(", ", CasteUtility.AllSlots.Select(s => s + "=" + CasteUtility.ValueLabel(s, CasteUtility.CurrentValue(p, s))));
private static string Labels(List<CasteMenuEntry> menu) => string.Join(" | ", menu.Select(e => e.Label));
/// <summary>Calls one of the manager's protected overrides, the way the base class does.</summary>
private static TR Call<TR>(Dialog_ManageCastes dialog, string method, params object[] args)
{
MethodInfo m = AccessTools.Method(typeof(Dialog_ManageCastes), method);
T.NotNull(m, "Dialog_ManageCastes." + method);
return (TR)m.Invoke(dialog, args);
}
// ---- Assign table ------------------------------------------------------------------------
[BlaxTest("ui: at startup the Assign table has the Caste column right before HostilityResponse")]
public static void ColumnPlaced()
{
if (!Fixtures.Settings.showAssignColumn)
T.Skip("showAssignColumn is off in this run");
PawnColumnDef col = NeedColumn();
T.Equal(typeof(PawnColumnWorker_BlaxCaste), col.workerClass, "worker class");
T.Check(col.Worker is PawnColumnWorker_BlaxCaste, "Worker is " + col.Worker?.GetType().Name);
T.Check(col.sortable, "the column is not sortable");
CheckRightBeforeHostility(PawnTableDefOf.Assign, col);
T.Info("Ideology " + ModsConfig.IdeologyActive + ", Biotech " + ModsConfig.BiotechActive + ", columns: " + ColumnList(PawnTableDefOf.Assign));
}
[BlaxTest("ui: showAssignColumn off removes the column at startup; on puts it back before HostilityResponse")]
public static void ColumnToggle()
{
PawnTableDef table = PawnTableDefOf.Assign;
PawnColumnDef col = NeedColumn();
bool had = table.columns.Contains(col);
try
{
CasteColumns.Apply(false);
T.Check(!table.columns.Contains(col), "Apply(false) left the column in the table");
T.Check(!CasteColumns.Apply(false), "removing a missing column reported a change");
T.Check(CasteColumns.Apply(true), "Apply(true) did not add the column back");
CheckRightBeforeHostility(table, col);
T.Check(!CasteColumns.Apply(true), "adding a present column reported a change");
CheckRightBeforeHostility(table, col);
}
finally
{
CasteColumns.Apply(had);
}
}
// ---- cell menu ---------------------------------------------------------------------------
[BlaxTest("ui: the cell menu has No caste, one entry per caste, Reset and Edit; picking an entry assigns")]
public static void MenuAssigns()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
Fixtures.NewCaste("Menu A");
Caste b = Fixtures.NewCaste("Menu B");
CasteManager.SetSlot(b, CasteSlot.Food, Simple);
Pawn p = Fixtures.Colonist();
p.foodRestriction.CurrentFoodPolicy = Lavish;
List<CasteMenuEntry> menu = PawnColumnWorker_BlaxCaste.MenuEntries(p);
T.Equal(comp.Castes.Count + 3, menu.Count, "menu entries (" + Labels(menu) + ")");
T.Equal(CasteChoiceKind.Casteless, menu[0].Choice.Kind, "first entry");
T.Equal((string)"BlaxCastes_NoCaste".Translate(), menu[0].Label, "first entry label");
foreach (Caste c in comp.Castes)
T.Equal(1, menu.Count(e => e.Choice.Kind == CasteChoiceKind.Caste && e.Choice.Caste == c), "entries for caste " + c.label);
List<string> casteLabels = menu.Where(e => e.Choice.Kind == CasteChoiceKind.Caste).Select(e => e.Label).ToList();
T.Check(casteLabels.SequenceEqual(casteLabels.OrderBy(l => l, StringComparer.CurrentCultureIgnoreCase)), "castes not sorted by label: " + string.Join(", ", casteLabels));
T.Equal(CasteChoiceKind.Reset, menu[menu.Count - 2].Choice.Kind, "second to last entry");
T.Equal(CasteChoiceKind.Edit, menu[menu.Count - 1].Choice.Kind, "last entry");
T.Check(menu[menu.Count - 2].Action == null, "Reset is enabled for a pawn without a caste");
T.Check(menu.Where(e => e.Choice.Kind != CasteChoiceKind.Reset).All(e => e.Action != null), "an entry other than Reset is disabled: " + Labels(menu));
menu.Single(e => e.Choice.Caste == b).Action();
CasteMembership m = comp.MembershipOf(p);
T.Check(m != null && m.caste == b && !m.auto && m.reason == JoinReason.Manual,
$"after picking B: caste={m?.caste}, auto={m?.auto}, reason={m?.reason}");
T.Check(p.foodRestriction.CurrentFoodPolicy == Simple, "picking B did not set its food policy: " + Policies(p));
p.foodRestriction.CurrentFoodPolicy = Lavish;
CasteMenuEntry reset = PawnColumnWorker_BlaxCaste.MenuEntries(p).Single(e => e.Choice.Kind == CasteChoiceKind.Reset);
T.NotNull(reset.Action, "Reset action for a member");
reset.Action();
T.Check(p.foodRestriction.CurrentFoodPolicy == Simple, "Reset did not restore the caste's food policy: " + Policies(p));
string before = Policies(p);
PawnColumnWorker_BlaxCaste.MenuEntries(p)[0].Action();
m = comp.MembershipOf(p);
T.Check(m != null && m.caste == null && !m.auto && m.reason == JoinReason.Manual, $"after No caste: caste={m?.caste}, auto={m?.auto}, reason={m?.reason}");
T.Equal(before, Policies(p), "policies after No caste");
}
[BlaxTest("ui: painting copies the source cell's caste; a pawn without a record paints nothing")]
public static void PaintPayload()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
Caste a = Fixtures.NewCaste("Paint A");
Pawn member = Fixtures.Colonist();
Pawn casteless = Fixtures.Colonist();
Pawn fresh = Fixtures.Colonist();
Pawn target = Fixtures.Colonist();
CasteManager.Assign(member, a, false, JoinReason.Manual);
CasteManager.MakeCasteless(casteless);
T.Equal(new CasteChoice(CasteChoiceKind.Caste, a), PawnColumnWorker_BlaxCaste.ChoiceOf(member), "member's payload");
T.Equal(new CasteChoice(CasteChoiceKind.Casteless), PawnColumnWorker_BlaxCaste.ChoiceOf(casteless), "casteless pawn's payload");
T.Equal(new CasteChoice(CasteChoiceKind.None), PawnColumnWorker_BlaxCaste.ChoiceOf(fresh), "unassigned pawn's payload");
// Widgets.Dropdown matches the painted payload with object.Equals against each menu entry.
List<CasteMenuEntry> menu = PawnColumnWorker_BlaxCaste.MenuEntries(target);
object none = PawnColumnWorker_BlaxCaste.ChoiceOf(fresh);
T.Check(!menu.Any(e => Equals(e.Choice, none)), "an unassigned pawn's payload matches a menu entry");
foreach (CasteMenuEntry e in menu.Where(e => e.Choice.Kind == CasteChoiceKind.Reset || e.Choice.Kind == CasteChoiceKind.Edit))
{
foreach (Pawn source in new[] { member, casteless, fresh })
T.Check(!Equals(e.Choice, PawnColumnWorker_BlaxCaste.ChoiceOf(source)), e.Label + " matches the payload of " + source.LabelShort);
}
object painted = PawnColumnWorker_BlaxCaste.ChoiceOf(member);
CasteMenuEntry hit = menu.FirstOrDefault(e => Equals(e.Choice, painted));
T.NotNull(hit, "menu entry for the painted caste");
hit.Action();
T.Check(comp.CasteOf(target) == a, "painting caste A did not assign it");
painted = PawnColumnWorker_BlaxCaste.ChoiceOf(casteless);
hit = PawnColumnWorker_BlaxCaste.MenuEntries(target).FirstOrDefault(e => Equals(e.Choice, painted));
T.NotNull(hit, "menu entry for the painted No caste");
hit.Action();
T.Check(comp.MembershipOf(target) != null && comp.MembershipOf(target).caste == null, "painting No caste did not make the pawn casteless");
}
[BlaxTest("ui: painting skips pawns already in the painted caste, so their hand changes stay; a click still rewrites")]
public static void PaintKeepsOverrides()
{
Caste a = Fixtures.NewCaste("Paint keep A");
Caste b = Fixtures.NewCaste("Paint keep B");
CasteManager.SetSlot(a, CasteSlot.Food, Simple);
Pawn member = Fixtures.Colonist();
Pawn other = Fixtures.Colonist();
Pawn casteless = Fixtures.Colonist();
CasteManager.Assign(member, a, false, JoinReason.Manual);
CasteManager.Assign(other, b, false, JoinReason.Manual);
CasteManager.MakeCasteless(casteless);
member.foodRestriction.CurrentFoodPolicy = Lavish; // by hand
var paintA = new CasteChoice(CasteChoiceKind.Caste, a);
var paintNone = new CasteChoice(CasteChoiceKind.Casteless);
T.Check(PawnColumnWorker_BlaxCaste.SkipWhilePainting(member, paintA, true), "painting A over a member of A is not skipped");
T.Check(!PawnColumnWorker_BlaxCaste.SkipWhilePainting(other, paintA, true), "painting A over a member of B is skipped");
T.Check(!PawnColumnWorker_BlaxCaste.SkipWhilePainting(member, paintA, false), "a click on A is skipped for a member of A");
T.Check(PawnColumnWorker_BlaxCaste.SkipWhilePainting(casteless, paintNone, true), "painting No caste over a casteless pawn is not skipped");
T.Check(!PawnColumnWorker_BlaxCaste.SkipWhilePainting(member, paintNone, true), "painting No caste over a member is skipped");
// What the paint stroke does to each cell it passes, start cell included.
foreach (Pawn p in new[] { member, other })
{
CasteMenuEntry hit = PawnColumnWorker_BlaxCaste.MenuEntries(p).First(e => Equals(e.Choice, paintA));
if (!PawnColumnWorker_BlaxCaste.SkipWhilePainting(p, paintA, true))
hit.Action();
}
T.Check(member.foodRestriction.CurrentFoodPolicy == Lavish, "painting wiped the member's hand-set food: " + Policies(member));
T.Check(Fixtures.Comp.CasteOf(other) == a && other.foodRestriction.CurrentFoodPolicy == Simple, "painting did not move the other pawn into A: " + Policies(other));
}
[BlaxTest("ui: sorting orders by caste label, pawns without a caste last")]
public static void CompareOrder()
{
Caste zeta = Fixtures.NewCaste("Zeta sort");
Caste alpha = Fixtures.NewCaste("alpha sort");
Pawn z = Fixtures.Colonist();
Pawn a = Fixtures.Colonist();
Pawn none = Fixtures.Colonist();
Pawn casteless = Fixtures.Colonist();
CasteManager.Assign(z, zeta, false, JoinReason.Manual);
CasteManager.Assign(a, alpha, false, JoinReason.Manual);
CasteManager.MakeCasteless(casteless);
var worker = (PawnColumnWorker_BlaxCaste)NeedColumn().Worker;
var list = new List<Pawn> { none, z, casteless, a };
list.Sort(worker.Compare);
T.Check(list[0] == a && list[1] == z, "castes not first and by label: " + string.Join(", ", list.Select(p => p.LabelShort + "=" + PawnColumnWorker_BlaxCaste.CellLabel(p))));
T.Check(list.Skip(2).All(p => p == none || p == casteless), "pawns without a caste are not last");
T.Equal(0, worker.Compare(none, casteless), "two pawns without a caste");
T.Check(worker.Compare(a, none) < 0 && worker.Compare(none, a) > 0, "a caste does not sort before no caste");
}
// ---- cell label and tooltip --------------------------------------------------------------
[BlaxTest("ui: the cell shows the caste, * with a tooltip naming overrides, No caste and (pending)")]
public static void CellLabels()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
Caste c = Fixtures.NewCaste("Label caste");
CasteManager.SetSlot(c, CasteSlot.Food, Simple);
Pawn p = Fixtures.Colonist();
int stray = comp.JoinQueue.RemoveAll(q => q != null && q.pawn == p);
if (stray > 0)
T.Info("a fresh colonist fixture was in the join queue; removed it");
T.Equal((string)"BlaxCastes_CellNone".Translate(), PawnColumnWorker_BlaxCaste.CellLabel(p), "label without a record");
CasteManager.Assign(p, c, false, JoinReason.Manual);
T.Equal(c.label, PawnColumnWorker_BlaxCaste.CellLabel(p), "label of a member");
p.foodRestriction.CurrentFoodPolicy = Lavish;
T.Equal(c.label + "*", PawnColumnWorker_BlaxCaste.CellLabel(p), "label with an override");
string tip = PawnColumnWorker_BlaxCaste.CellTip(p);
T.Check(tip.Contains(CasteUtility.SlotLabel(CasteSlot.Food)) && tip.Contains(Lavish.label) && tip.Contains(Simple.label),
"tooltip does not name the slot and both values: " + tip);
CasteManager.MakeCasteless(p);
T.Equal((string)"BlaxCastes_NoCaste".Translate(), PawnColumnWorker_BlaxCaste.CellLabel(p), "label when casteless by choice");
// "(pending)" only while the queued join will change the caste.
CasteManager.EndMembership(p);
var queued = new QueuedJoin(p, JoinReason.NewColonist, Find.TickManager.TicksGame);
comp.JoinQueue.Add(queued);
try
{
T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.SpecificCaste, c), "setup: SetJoinRow failed");
T.Check(PawnColumnWorker_BlaxCaste.IsPending(p), "IsPending false for a queued pawn whose row gives a caste");
T.Equal((string)"BlaxCastes_CellPending".Translate(), PawnColumnWorker_BlaxCaste.CellLabel(p), "label while queued");
T.Check(CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.NoCaste), "setup: SetJoinRow failed");
T.Check(!PawnColumnWorker_BlaxCaste.IsPending(p), "IsPending true for a queued pawn without a caste whose row is No caste");
T.Equal((string)"BlaxCastes_CellNone".Translate(), PawnColumnWorker_BlaxCaste.CellLabel(p), "label while a join that changes nothing is queued");
CasteManager.Assign(p, c, true, JoinReason.NewColonist);
T.Check(PawnColumnWorker_BlaxCaste.IsPending(p), "IsPending false for a row-set member whose row is now No caste (it will leave)");
CasteManager.Assign(p, c, false, JoinReason.Manual);
T.Check(!PawnColumnWorker_BlaxCaste.IsPending(p), "IsPending true for a caste picked by hand after the join was queued");
}
finally
{
comp.JoinQueue.Remove(queued);
}
T.Check(!PawnColumnWorker_BlaxCaste.IsPending(p), "IsPending true after the queue entry left");
// A caste deleted from under a pawn has its own tooltip.
Caste doomed = Fixtures.NewCaste("Label doomed caste");
CasteManager.Assign(p, doomed, false, JoinReason.Manual);
CasteManager.DeleteCaste(doomed);
tip = PawnColumnWorker_BlaxCaste.CellTip(p);
T.Check(tip.Contains("BlaxCastes_CellCasteDeletedTip".Translate()), "tooltip after the caste was deleted: " + tip);
}
[BlaxTest("ui: a quest lodger's cell says Unchangeable")]
public static void LodgerLabel()
{
Pawn lodger = Fixtures.QuestLodger();
T.Equal((string)"Unchangeable".Translate(), PawnColumnWorker_BlaxCaste.CellLabel(lodger), "lodger label");
}
// ---- caste manager -----------------------------------------------------------------------
[BlaxTest("ui: manager CreateNewPolicy gives unique ids, labels Caste N and manages nothing")]
public static void ManagerCreate()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
var dialog = new Dialog_ManageCastes();
var made = new List<Caste>();
try
{
for (int i = 0; i < 3; i++)
made.Add(Call<Caste>(dialog, "CreateNewPolicy"));
T.Check(made.All(c => c != null && comp.HasCaste(c)), "a new caste is null or not in the component");
T.Equal(3, made.Select(c => c.id).Distinct().Count(), "distinct ids among " + string.Join(", ", made.Select(c => c.id)));
T.Equal(comp.Castes.Count, comp.Castes.Select(c => c.id).Distinct().Count(), "distinct ids in the component");
foreach (Caste c in made)
{
T.Equal((string)"BlaxCastes_NewCasteLabel".Translate(c.id), c.label, "label of caste " + c.id);
T.Check(!c.AnyManaged(), c.label + " manages a slot");
}
T.Check(ReferenceEquals(Call<List<Caste>>(dialog, "GetPolicies"), comp.Castes), "GetPolicies is not the component's caste list");
}
finally
{
foreach (Caste c in made)
CasteManager.DeleteCaste(c);
}
}
[BlaxTest("ui: manager duplicate (CreateNewPolicy then CopyFrom, as the base button does) copies every slot")]
public static void ManagerDuplicate()
{
Caste src = Full("Dialog duplicate source");
Pawn member = Fixtures.Colonist();
CasteManager.Assign(member, src, false, JoinReason.Manual);
var dialog = new Dialog_ManageCastes(src);
Caste copy = Call<Caste>(dialog, "CreateNewPolicy");
try
{
copy.CopyFrom(src);
T.Check(copy.id != src.id, "duplicate has the source's id");
foreach (CasteSlot slot in CasteUtility.AllSlots)
T.Check(CasteUtility.SameValue(src.GetSlot(slot), copy.GetSlot(slot)),
$"{slot}: source {CasteUtility.ValueLabel(slot, src.GetSlot(slot))}, duplicate {CasteUtility.ValueLabel(slot, copy.GetSlot(slot))}");
T.Equal(0, Fixtures.Comp.MembersOf(copy).Count, "members of the duplicate");
T.Check(Fixtures.Comp.CasteOf(member) == src, "the source's member moved");
}
finally
{
CasteManager.DeleteCaste(copy);
}
}
[BlaxTest("ui: manager TryDeletePolicy removes the caste, empties its memberships, resets its rows, keeps policies")]
public static void ManagerDelete()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
Caste doomed = Full("Dialog doomed");
Pawn a = Fixtures.Colonist();
CasteManager.Assign(a, doomed, false, JoinReason.Manual);
CasteManager.SetJoinRow(JoinReason.BoughtSlave, JoinRowMode.SpecificCaste, doomed);
CasteManager.SetJoinRow(JoinReason.NewColonist, JoinRowMode.DefaultCaste);
CasteManager.SetDefault(doomed);
string before = Policies(a);
var dialog = new Dialog_ManageCastes(doomed);
AcceptanceReport report = Call<AcceptanceReport>(dialog, "TryDeletePolicy", doomed);
T.Check(report.Accepted, "TryDeletePolicy refused: " + report.Reason);
T.Check(!comp.HasCaste(doomed), "caste still in the component");
T.Equal(0, comp.MembersOf(doomed).Count, "members left in the deleted caste");
CasteMembership m = comp.MembershipOf(a);
T.Check(m != null && m.caste == null && m.reason == JoinReason.CasteDeleted, $"member record: caste={m?.caste}, reason={m?.reason}");
T.Equal(before, Policies(a), "member's policies after delete");
T.Equal(JoinRowMode.NoCaste, comp.GetRow(JoinReason.BoughtSlave).mode, "row that pointed at the caste");
T.Check(Call<Caste>(dialog, "GetDefaultPolicy") == null, "the deleted caste is still the default");
T.Check(comp.ResolveRow(JoinReason.NewColonist) == null, "a Default caste row still resolves");
AcceptanceReport again = Call<AcceptanceReport>(dialog, "TryDeletePolicy", doomed);
T.Check(!again.Accepted, "deleting twice was accepted");
T.Check(!again.Reason.NullOrEmpty(), "a refused delete gives no reason");
}
[BlaxTest("ui: manager Get/SetDefaultPolicy change what Default caste rows resolve to")]
public static void ManagerDefault()
{
GameComponent_BlaxCastes comp = Fixtures.Comp;
Caste one = Fixtures.NewCaste("Dialog default one");
Caste two = Fixtures.NewCaste("Dialog default two");
CasteManager.SetJoinRow(JoinReason.NewSlave, JoinRowMode.DefaultCaste);
var dialog = new Dialog_ManageCastes();
Call<object>(dialog, "SetDefaultPolicy", one);
T.Check(Call<Caste>(dialog, "GetDefaultPolicy") == one && comp.DefaultCaste == one, "default after SetDefaultPolicy(one)");
T.Check(comp.ResolveRow(JoinReason.NewSlave) == one, "Default caste row does not resolve to one");
Call<object>(dialog, "SetDefaultPolicy", two);
T.Check(comp.ResolveRow(JoinReason.NewSlave) == two, "Default caste row does not resolve to two");
}
[BlaxTest("ui: manager managed checkbox sets the database default; unticking changes no pawn")]
public static void ManagedCheckbox()
{
Game game = Current.Game;
Caste c = Fixtures.NewCaste("Checkbox caste");
Pawn p = Fixtures.Colonist();
CasteManager.Assign(p, c, false, JoinReason.Manual);
p.foodRestriction.CurrentFoodPolicy = Lavish;
FoodPolicy dbDefault = game.foodRestrictionDatabase.DefaultFoodRestriction();
Dialog_ManageCastes.SetManaged(c, CasteSlot.Food, true);
T.Check(c.FoodPolicy == dbDefault, "ticking Food set " + CasteUtility.ValueLabel(CasteSlot.Food, c.FoodPolicy) + ", not the database default " + dbDefault?.label);
T.Check(p.foodRestriction.CurrentFoodPolicy == dbDefault, "a member did not follow the newly managed slot: " + Policies(p));
Dialog_ManageCastes.SetManaged(c, CasteSlot.MedCare, true);
T.Equal((int)Find.PlaySettings.defaultCareForColonist, c.MedCare, "ticking medical care");
p.foodRestriction.CurrentFoodPolicy = Simple;
Dialog_ManageCastes.SetManaged(c, CasteSlot.Food, false);
T.Check(c.FoodPolicy == null && !c.IsManaged(CasteSlot.Food), "unticking Food left it managed");
T.Check(p.foodRestriction.CurrentFoodPolicy == Simple, "unticking changed a member's food policy");
T.Equal(0, Dialog_ManageCastes.SetManaged(c, CasteSlot.Food, false), "unticking twice moved pawns");
T.Check(Dialog_ManageCastes.PoliciesFor(CasteSlot.Apparel).SequenceEqual(game.outfitDatabase.AllOutfits.Cast<Policy>()), "apparel dropdown is not the outfit database");
T.Check(Dialog_ManageCastes.PoliciesFor(CasteSlot.Reading).SequenceEqual(game.readingPolicyDatabase.AllReadingPolicies.Cast<Policy>()), "reading dropdown is not the reading database");
T.Check(!Dialog_ManageCastes.PoliciesFor(CasteSlot.MedCare).Any(), "medical care lists policies");
}
[BlaxTest("ui: every string the manager, the column and the settings buttons use is in Keyed")]
public static void UiStrings()
{
string[] keys =
{
"BlaxCastes_ManageCastesTip", "BlaxCastes_ManagedTip", "BlaxCastes_ManagerHint", "BlaxCastes_DeleteFailed",
"BlaxCastes_FirstLoadMessage", "BlaxCastes_ManageCastesButton", "BlaxCastes_ManageCastesButtonTip",
"BlaxCastes_JoinDefaultsButton", "BlaxCastes_JoinDefaultsButtonTip", "BlaxCastes_CellNone", "BlaxCastes_CellNoneTip",
"BlaxCastes_CellCastelessTip", "BlaxCastes_CellPending", "BlaxCastes_CellPendingTip", "BlaxCastes_CellLodgerTip",
"BlaxCastes_CellCasteTip", "BlaxCastes_CellOverrides", "BlaxCastes_CellOverrideLine", "BlaxCastes_CellHint",
"BlaxCastes_MenuReset", "BlaxCastes_MenuEdit", "BlaxCastes_SettingOpenWindows",
"BlaxCastes_CellCasteDeletedTip", "BlaxCastes_MakeDefaultTip", "BlaxCastes_MemberCount", "BlaxCastes_ApplyToAll",
"BlaxCastes_ApplyToAllTip", "BlaxCastes_ApplyToAllConfirm", "BlaxCastes_AssignByStatus", "BlaxCastes_AssignByStatusTip",
"BlaxCastes_AssignByStatusDone", "BlaxCastes_AssignByStatusConfirm", "BlaxCastes_SlaveOutfitCold",
"BlaxCastes_StarterSeededMessage", "BlaxCastes_StarterSeededMessageNoColumn",
"Unchangeable", "ManageApparelPolicies", "ManageFoodPolicies", "ManageDrugPolicies", "ManageReadingPolicies",
};
var missing = keys.Where(k => !k.CanTranslate()).ToList();
T.Check(missing.Count == 0, "strings missing from Keyed: " + string.Join(", ", missing));
}
[BlaxTest("ui: texts with a count read right for one; Assign by status asks first and names the count")]
public static void CountTextsForOne()
{
Caste c = Fixtures.NewCaste("Count of one");
OverlapMod mod = OverlapWarnings.Mods[0];
var texts = new List<string>
{
Dialog_ManageCastes.ApplyToAllConfirmText(c, 1),
Dialog_ManageCastes.AssignByStatusConfirmText(1),
"BlaxCastes_AssignByStatusTip".Translate(1),
"BlaxCastes_AssignByStatusDone".Translate(1),
OverlapWarnings.MessageText(new List<OverlapMod> { mod }),
"BlaxCastes_SettingFormerCasteDays".Translate(1),
};
foreach (string text in texts)
{
T.Check(!System.Text.RegularExpressions.Regex.IsMatch(text, @"\b1 (members|pawns|days)\b") && !text.Contains("all 1"), "reads wrong for one: " + text);
}
T.Check(!texts[4].Contains(mod.Name + " also set"), "the overlap message conjugates for many mods: " + texts[4]);
string confirm = Dialog_ManageCastes.AssignByStatusConfirmText(7);
T.Check(confirm.Contains("7"), "the Assign by status confirm does not name the count: " + confirm);
}
[BlaxTest("ui: a new game posts the first-load message once and stores that in the save")]
public static void FirstLoadMessageOnNewGame()
{
if (!Fixtures.Settings.showAssignColumn)
T.Skip("showAssignColumn is off in this run, so the message waits");
T.Check(Fixtures.Comp.FirstLoadMessageShown, "StartedNewGame did not post the first-load message");
T.Equal(1, FirstLoadMessages(), "first-load messages in the history of a new game");
}
[BlaxTest("ui: the first-load message points to Manage castes, and waits while the column is not there as the game started, whatever the setting says now")]
public static void FirstLoadMessage()
{
if (!CasteColumns.Shown)
T.Skip("the Caste column is not in the Assign tab in this run");
// Vanilla drops a message whose text is still on screen (the new game's copy), so clear the live ones first.
Messages.Clear();
int before = FirstLoadMessages();
T.Check(Dialog_ManageCastes.ShowFirstLoadMessage(), "returned false with the column on");
T.Equal(before + 1, FirstLoadMessages(), "first-load messages in the history after posting one");
// The setting turned off in this session waits for a restart: the column and its button are still there.
Fixtures.Settings.showAssignColumn = false;
Messages.Clear();
T.Check(Dialog_ManageCastes.ShowFirstLoadMessage(), "returned false while the column is still shown (the setting waits for a restart)");
T.Equal(before + 2, FirstLoadMessages(), "first-load messages after a call with the setting off and the column shown");
// The game started without the column: the message waits, though the setting is on again.
Fixtures.Settings.showAssignColumn = true;
CasteColumns.Apply(false);
try
{
T.Check(!CasteColumns.Shown, "setup: the column is still in the Assign table");
T.Check(!Dialog_ManageCastes.ShowFirstLoadMessage(), "returned true without the column (setting on, waiting for a restart)");
}
finally
{
CasteColumns.Apply(true);
}
T.Equal(before + 2, FirstLoadMessages(), "first-load messages after a call without the column");
T.Carry["firstLoadMessages"] = FirstLoadMessages().ToString();
}
[BlaxTest("ui: the schedule grid paints only after a press that started on it, never while the scrollbar is dragged over it")]
public static void SchedulePaintNeedsPressOnGrid()
{
bool started = false;
// A press on the scrollbar (not over the grid; the scrollbar may also use the event), then a drag over the grid.
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseDown, 0, false, true), "a press beside the grid paints");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.Used, 0, true, true), "a press another control used paints");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseDrag, 0, true, true), "dragging onto the grid after a press beside it paints");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.Repaint, 0, true, true), "a repaint over the grid during that drag paints");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseUp, 0, true, false), "the release over the grid paints");
// A press on the grid paints, and goes on while the button is held.
T.Check(Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseDown, 0, true, true), "a press on the grid does not paint");
T.Check(Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseDrag, 0, true, true), "dragging along the grid does not paint");
T.Check(Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.Repaint, 0, true, true), "a repaint while dragging along the grid does not paint");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseUp, 0, true, false), "the release paints");
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.Repaint, 0, true, false), "a repaint after the release paints");
// A right press on the grid starts nothing.
T.Check(!Dialog_ManageCastes.SchedulePaintAllowed(ref started, UnityEngine.EventType.MouseDown, 1, true, false), "a right press on the grid paints");
}
[BlaxTest("ui: the Caste column setting's tooltip names where the column sits in each tab")]
public static void ColumnSettingTip()
{
string tip = "BlaxCastes_SettingShowAssignColumnTip".Translate();
T.Check(tip.Contains("right of the timetable"), "the tooltip does not say the column sits right of the timetable in the Schedule tab: " + tip);
T.Check(!tip.Contains("left of the policy columns, the schedule"), "the tooltip still puts the column left of the schedule: " + tip);
}
private static int FirstLoadMessages()
{
string text = ((string)"BlaxCastes_FirstLoadMessage".Translate()).CapitalizeFirst();
return Find.Archive.ArchivablesListForReading.OfType<Message>().Count(m => m.text == text);
}
[BlaxTest("ui: after the reload the first-load flag is still set and no second message was posted", phase: 2)]
public static void FirstLoadMessageNotRepeated()
{
T.Check(Fixtures.Comp.FirstLoadMessageShown, "the first-load flag was not saved");
if (!T.Carry.TryGetValue("firstLoadMessages", out string carried))
T.Skip("phase 1 did not record the message count");
int expected = int.Parse(carried);
T.Check(FirstLoadMessages() <= expected, "first-load messages after the reload: " + FirstLoadMessages() + ", before: " + expected);
}
}
}