using System.Collections.Generic; using System.Linq; using BlaxLaws; using RimWorld; using Verse; namespace BlaxLawsTests { /// /// The Laws tab, the act picker, the case log and the letters (design: "Lawbook tab", Tests). Headless /// runs have no GUI, so these drive the logic behind the widgets: what each column's dropdown offers and /// what picking it writes, what a pick from the act picker adds, the picker's tree and search, the /// overlap icon, the delete confirmation, the case log's lines and "Arrest now", and the letters. /// Drawing is checked by hand. /// public static class UiTests { private const string Binge = "Break/@anyDrugBinge"; private const string Insult = "Interaction/Insult"; private const string MissingKey = "Ate/BlaxLawsTest_UiNoSuchFood"; // ---- helpers ----------------------------------------------------------------------------- private static ActEntry Entry(string key) { ActEntry e = ActCatalogue.Get(key); T.NotNull(e, "catalogue entry " + key); return e; } /// A fresh row through the picker's "Add law" path; fails when the book had one. private static Law Add(string key) { Entry(key); Law row = LawbookUi.AddFromPicker(key, out Law existing); T.Check(row != null, "Add law for " + key + " added nothing, existing " + existing); return row; } private static List Names(List choices) => choices.Select(c => c.payload is Def d ? d.defName : c.payload?.ToString()).ToList(); private static string Show(List choices) => string.Join(" | ", choices.Select(c => c.ToString())); private static RowChoice Choice(List choices, string name) { RowChoice c = choices.FirstOrDefault(x => (x.payload is Def d ? d.defName : x.payload?.ToString()) == name); T.NotNull(c, "menu entry " + name + " in " + Show(choices)); return c; } /// Turns the other rows of a category off for this test, so its watch flag reads only this test's rows. private static void SilenceOtherRows(string categoryId) { foreach (Law l in Lawbook.Laws.Where(l => l.enabled && l.CategoryId == categoryId).ToList()) { Lawbook.SetEnabled(l, false); Law kept = l; Fixtures.OnCleanup(() => Lawbook.SetEnabled(kept, true)); } } /// Removes the letters a test sent from the stack and the archive, so the save never holds their targets. private static void RemoveNewLettersAfterTest() { var before = new HashSet(Find.LetterStack.LettersListForReading); Fixtures.OnCleanup(() => { foreach (Letter l in Find.LetterStack.LettersListForReading.Where(l => !before.Contains(l)).ToList()) { Find.LetterStack.RemoveLetter(l); Find.Archive.Remove(l); } }); } private static List LettersSince(HashSet before) => Find.LetterStack.LettersListForReading.Where(l => !before.Contains(l)).ToList(); private static LawCase OpenCase(Law law, Pawn p) => CaseFiler.Open(law, new ActEvent(ActCategories.Ate, ThingDefOf.MealSimple, p), law.stage); // ---- the tab ----------------------------------------------------------------------------- [BlaxTest("ui: the Laws button opens MainTabWindow_Laws after Ideoligions, also without a map; Show Laws tab writes buttonVisible")] public static void MainButton() { MainButtonDef def = BlaxLawsDefOf.BlaxLaws_Laws; T.NotNull(def, "MainButtonDef BlaxLaws_Laws"); T.Equal(typeof(MainTabWindow_Laws), def.tabWindowClass, "tab window class"); T.Equal(105, def.order, "button order"); T.Check(def.validWithoutMap, "the Laws tab is not valid without a map"); T.Check(def.TabWindow is MainTabWindow_Laws, "TabWindow is " + def.TabWindow?.GetType().Name); Fixtures.Settings.showLawsTab = false; BlaxLawsMod.ApplyTabVisibility(); T.Check(!def.buttonVisible, "Show Laws tab off left the button visible"); Fixtures.Settings.showLawsTab = true; BlaxLawsMod.ApplyTabVisibility(); T.Check(def.buttonVisible, "Show Laws tab on left the button hidden"); } [BlaxTest("ui: Add law gives a row at the entry's first stage, Moderate, Warn, 3 days and On; a binge row gets Any drug")] public static void AddLawDefaults() { Law binge = Add(Binge); T.Equal(LawStage.Planning, binge.stage, "stage of a new binge row"); T.Equal(LawSeverity.Moderate, binge.severity, "severity of a new row"); T.Equal("Warn", binge.punishment, "punishment of a new row"); T.Equal(3, binge.days, "days of a new row"); T.Check(binge.enabled, "a new row is off"); T.Equal(DrugOptions.Any, DrugOptions.Normalize(binge.option), "option of a new binge row"); T.Check(LawbookUi.HasOption(binge), "a binge row has no Drug column"); T.Equal(binge.id, LawbookUi.FocusLawId, "the tab doesn't show the new row"); Law insult = Add(Insult); T.Equal(LawStage.DidIt, insult.stage, "stage of a new Insult row (no planning outside the breaks)"); T.Check(insult.option == null && !LawbookUi.HasOption(insult), "an Insult row has a drug option: " + insult.option); T.Equal(0, LawbookUi.OptionChoices(insult).Count, "drug options of an Insult row"); T.Check(insult.savedLabel == Entry(Insult).label && insult.savedMod == "Core", "the row didn't save the entry's label and mod: " + insult.savedLabel + ", " + insult.savedMod); } [BlaxTest("ui: Add law on an entry whose stages all have rows adds nothing and shows the existing row")] public static void AddLawDuplicateShowsRow() { Law planning = Add(Binge); ActEntry e = Entry(Binge); T.Check(e.HasStage(LawStage.DidIt), "the any drug binge entry has no Did it stage (break link missing)"); Law didIt = Add(Binge); T.Equal(LawStage.DidIt, didIt.stage, "stage of the second binge row"); int count = Lawbook.Laws.Count; LawbookUi.ClearFocus(); Law third = LawbookUi.AddFromPicker(Binge, out Law existing); T.Check(third == null, "a third binge row was added: " + third); T.Equal(count, Lawbook.Laws.Count, "rows after the refused pick"); T.Check(existing == planning, "the refusal names " + existing + ", not the planning row"); T.Equal(planning.id, LawbookUi.FocusLawId, "the tab doesn't jump to the existing row"); T.Check(LawbookUi.ScrollPending, "the tab isn't asked to scroll to the existing row"); Law insult = Add(Insult); T.Check(!LawbookUi.ChangeAction(didIt, Insult), "changing an action to a key in the book was allowed"); T.Equal(Binge, didIt.actKey, "the refused action change still changed the row"); T.Equal(insult.id, LawbookUi.FocusLawId, "the refused action change doesn't show the existing row"); } [BlaxTest("ui: an any drug binge row turns the Break flag on and watches both binge breaks; deleting it turns them off")] public static void BingeRowWatches() { SilenceOtherRows(ActCategories.Break); MentalStateDef extreme = DefDatabase.GetNamed("Binging_DrugExtreme"); MentalStateDef major = DefDatabase.GetNamed("Binging_DrugMajor"); T.Check(!Watch.Break, "the Break flag is on before the row"); Law row = Add(Binge); T.Check(Watch.Break && Watch.Any, "the Break flag is off after adding the row"); T.Check(Watch.IsWatched(ActCategories.Break, extreme), "Binging_DrugExtreme is not watched"); T.Check(Watch.IsWatched(ActCategories.Break, major), "Binging_DrugMajor is not watched"); T.Check(!Watch.IsWatched(ActCategories.Break, DefDatabase.GetNamed("FireStartingSpree")), "a binge row watches the fire-starting spree"); Lawbook.SetEnabled(row, false); T.Check(!Watch.Break, "the Break flag is on with the row off"); Lawbook.SetEnabled(row, true); Lawbook.Delete(row); T.Check(!Watch.Break, "the Break flag is on after deleting the row"); T.Check(!Watch.IsWatched(ActCategories.Break, extreme) && !Watch.IsWatched(ActCategories.Break, major), "a binge break is still watched after the delete"); } // ---- column dropdowns ------------------------------------------------------------------ [BlaxTest("ui: a Planning row never offers Reprimand, Banish or Execute; a Berserk row never offers Subdue; a Tantrum row does")] public static void PunishmentMenus() { Law planning = Add(Binge); List p = Names(LawbookUi.PunishmentChoices(planning)); T.Check(p.Contains("Warn") && p.Contains("Subdue") && p.Contains("Jail"), "a binge planning row lacks Warn, Subdue or Jail: " + string.Join(", ", p)); foreach (string never in new[] { "Reprimand", "Banish", "Execute", "Guilty", "SubdueThenJail" }) T.Check(!p.Contains(never), "a binge planning row offers " + never); Law didIt = Add(Binge); List d = Names(LawbookUi.PunishmentChoices(didIt)); foreach (string must in new[] { "Warn", "Guilty", "Subdue", "Jail", "SubdueThenJail" }) T.Check(d.Contains(must), "a binge did-it row lacks " + must + ": " + string.Join(", ", d)); Law berserk = Add("Break/Berserk"); List b = Names(LawbookUi.PunishmentChoices(berserk)); T.Check(Entry("Break/Berserk").hostileBreak, "the Berserk entry isn't marked hostile"); T.Check(!b.Contains("Subdue") && !b.Contains("SubdueThenJail"), "a Berserk row offers a subdue: " + string.Join(", ", b)); T.Check(b.Contains("Warn") && b.Contains("Guilty") && b.Contains("Jail"), "a Berserk row lacks Warn, Guilty or Jail: " + string.Join(", ", b)); Law tantrum = Add("Break/Tantrum"); T.Check(Names(LawbookUi.PunishmentChoices(tantrum)).Contains("Subdue"), "a Tantrum row doesn't offer Subdue"); Law insult = Add(Insult); List a = Names(LawbookUi.PunishmentChoices(insult)); T.Check(a.Contains("Warn") && a.Contains("Guilty") && a.Contains("Jail") && !a.Contains("Subdue") && !a.Contains("SubdueThenJail"), "an Insult row offers " + string.Join(", ", a) + " (expected Warn, Guilty, Jail, no subdue)"); Choice(LawbookUi.PunishmentChoices(insult), "Jail").action(); T.Equal("Jail", insult.punishment, "picking Jail in the menu"); T.Check(LawbookUi.UsesDays(insult), "a Jail row shows no Days field"); Choice(LawbookUi.PunishmentChoices(insult), "Warn").action(); T.Check(!LawbookUi.UsesDays(insult), "a Warn row shows a Days field"); } [BlaxTest("ui: switching a Subdue row's action to Ate: any meat resets it to Warn and Did it; Severity moves untouched days")] public static void ActionAndSeverityResets() { Law row = Add(Binge); Choice(LawbookUi.PunishmentChoices(row), "Subdue").action(); T.Equal("Subdue", row.punishment, "picking Subdue in the menu"); T.Check(LawbookUi.ChangeAction(row, "Ate/@anyMeat"), "changing the action to Ate: any meat was refused"); T.Equal("Ate/@anyMeat", row.actKey, "act key after the change"); T.Equal("Warn", row.punishment, "punishment after switching a Subdue row to Ate: any meat"); T.Equal(LawStage.DidIt, row.stage, "stage after switching to an entry without Planning"); T.Check(row.option == null, "the option stayed after switching away from a binge: " + row.option); T.Check(row.savedLabel == Entry("Ate/@anyMeat").label, "the row didn't save the new entry's label: " + row.savedLabel); Choice(LawbookUi.SeverityChoices(row), nameof(LawSeverity.Serious)).action(); T.Equal(5, row.days, "untouched days after Moderate to Serious in the menu"); Lawbook.SetDays(row, 8); Choice(LawbookUi.SeverityChoices(row), nameof(LawSeverity.Grave)).action(); T.Equal(8, row.days, "typed days after Serious to Grave in the menu"); T.Check(LawbookUi.SeverityChoices(row).Single(c => c.selected).payload.Equals(LawSeverity.Grave), "the severity menu marks another value as selected"); // The first binge row became Ate: any meat, so the binge's planning key is free again. Law planning = Add(Binge); T.Equal(LawStage.Planning, planning.stage, "stage of the next binge row"); Law didIt = Add(Binge); T.Equal(LawStage.DidIt, didIt.stage, "stage of the binge row after that"); Choice(LawbookUi.PunishmentChoices(didIt), "SubdueThenJail").action(); T.Equal("SubdueThenJail", didIt.punishment, "picking Subdue then jail in the menu"); T.Check(!LawbookUi.ChangeStage(didIt, LawStage.Planning), "the stage menu made a second binge planning row"); T.Equal(planning.id, LawbookUi.FocusLawId, "the refused stage change doesn't show the existing row"); Lawbook.Delete(planning); Choice(LawbookUi.StageChoices(didIt), nameof(LawStage.Planning)).action(); T.Equal(LawStage.Planning, didIt.stage, "stage after picking Planning"); T.Equal("Warn", didIt.punishment, "Subdue then jail, which planning doesn't offer, didn't reset to Warn on the stage change"); Choice(LawbookUi.PunishmentChoices(didIt), "Jail").action(); Choice(LawbookUi.StageChoices(didIt), nameof(LawStage.DidIt)).action(); T.Equal("Jail", didIt.punishment, "a Jail row that still fits lost its punishment on the stage change"); } [BlaxTest("ui: the drug option list holds WakeUp and no non-drug chemical; picking an option keeps the key unique")] public static void DrugOptionMenu() { Law row = Add(Binge); List choices = LawbookUi.OptionChoices(row); List names = Names(choices); T.Check(names.Count > 3 && names[0] == DrugOptions.Any && names[1] == DrugOptions.Hard && names[2] == DrugOptions.Social, "the option menu doesn't start with Any, Hard, Social: " + string.Join(", ", names.Take(4))); T.Check(names.Contains("WakeUp"), "the option menu lacks the wake-up chemical: " + string.Join(", ", names)); var drugChemicals = new HashSet(); foreach (ThingDef t in DefDatabase.AllDefsListForReading) { CompProperties_Drug drug = t.GetCompProperties(); if (drug?.chemical != null) drugChemicals.Add(drug.chemical.defName); } foreach (string n in names.Skip(3)) T.Check(drugChemicals.Contains(n), "the option menu lists " + n + ", a chemical no drug names"); T.Equal(names.Count, names.Distinct().Count(), "options listed twice"); List others = DefDatabase.AllDefsListForReading.Select(c => c.defName).Where(n => !drugChemicals.Contains(n)).ToList(); T.Info("chemicals no drug names (left out): " + (others.Count == 0 ? "none" : string.Join(", ", others))); Choice(choices, "WakeUp").action(); T.Equal("WakeUp", row.option, "option after picking wake-up"); T.Equal(DrugOptions.Label("WakeUp"), DefDatabase.GetNamed("WakeUp").LabelCap.ToString(), "the wake-up option's label"); // The planning key with Any drug is free again, so the next pick adds a planning row with Any drug. Law second = Add(Binge); T.Equal(LawStage.Planning, second.stage, "stage of the second binge row"); T.Equal(DrugOptions.Any, DrugOptions.Normalize(second.option), "option of the second binge row"); T.Check(!LawbookUi.ChangeOption(second, "WakeUp"), "a second planning row with the wake-up option was allowed"); T.Equal(row.id, LawbookUi.FocusLawId, "the refused option doesn't show the existing row"); T.Check(LawbookUi.ChangeOption(second, DrugOptions.Hard), "the hard drugs option was refused"); T.Equal(DrugOptions.Hard, second.option, "option after picking hard drugs"); } [BlaxTest("ui: a row whose entry is missing shows its saved label and why, keeps its stage and punishment and offers nothing else")] public static void MissingEntryRow() { Law row = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Moderate, "Jail", 4, "Ideology", "ludeon.rimworld.ideology"); T.Check(LawbookUi.ActionLabel(row).Contains(ActKey.NameOf(MissingKey)), "the action label lacks the saved label: " + LawbookUi.ActionLabel(row)); T.Check(LawbookUi.ActionTooltip(row).Contains("Needs Ideology"), "the action tooltip doesn't say Needs Ideology: " + LawbookUi.ActionTooltip(row)); List stages = LawbookUi.StageChoices(row); T.Check(stages.Count == 1 && stages[0].action == null && stages[0].selected && stages[0].reason == "Needs Ideology", "stage menu of a missing row: " + Show(stages)); List punishments = LawbookUi.PunishmentChoices(row); T.Check(punishments.Count == 1 && punishments[0].action == null && Equals(punishments[0].payload, BlaxLawsDefOf.Jail), "punishment menu of a missing row: " + Show(punishments)); T.Check(LawbookUi.UsesDays(row), "a missing Jail row lost its Days field"); T.Equal(0, ActOverlaps.Of(row).Count, "overlaps of a missing row"); } // ---- act picker -------------------------------------------------------------------------- [BlaxTest("ui: the picker's search for 'wake' lists Wake-up under Ate and make wake-up under Bills; 'insult' lists Insult and the insulting sprees")] public static void PickerSearch() { var tree = new ActPickerTree { SearchText = "wake" }; List rows = tree.Rows.ToList(); List entries = rows.Where(r => !r.IsHeader).ToList(); T.Check(entries.Any(r => r.category.defName == ActCategories.Ate && r.entry.def?.defName == "WakeUp"), "'wake' doesn't list Wake-up under Ate"); T.Check(entries.Any(r => r.category.defName == ActCategories.Bill && r.entry.def is RecipeDef rd && rd.ProducedThingDef?.defName == "WakeUp"), "'wake' doesn't list make wake-up under Bills"); T.Check(entries.All(r => tree.Matches(r.entry)), "a listed entry doesn't match 'wake'"); T.Check(!entries.Any(r => r.entry.def?.defName == "Beer"), "'wake' lists Beer"); foreach (ActPickerRow h in rows.Where(r => r.IsHeader)) { T.Check(h.open && h.count > 0, "searching shows the category " + h.category.defName + " closed or empty"); T.Equal(h.count, entries.Count(r => r.category == h.category), "entries listed under " + h.category.defName); } T.Equal(entries.Count, tree.Hits, "hits of 'wake'"); tree.SearchText = "insult"; entries = tree.Rows.Where(r => !r.IsHeader).ToList(); T.Check(entries.Any(r => r.category.defName == ActCategories.Interaction && r.entry.def?.defName == "Insult"), "'insult' doesn't list Insult under interactions"); T.Check(entries.Any(r => r.category.defName == ActCategories.Break && r.entry.def?.defName == "InsultingSpree"), "'insult' doesn't list the insulting spree under breaks"); T.Check(entries.Any(r => r.category.defName == ActCategories.Break && r.entry.def?.defName == "TargetedInsultingSpree"), "'insult' doesn't list the targeted insulting spree under breaks"); tree.SearchText = "blaxlawstest no act is called this"; T.Equal(0, tree.Rows.Count, "lines for a search without hits"); T.Equal(0, tree.Hits, "hits for a search without hits"); } [BlaxTest("ui: the picker opens with every category closed, opens one on a click, groups first; entries in the book show a tick")] public static void PickerTree() { var tree = new ActPickerTree(); IReadOnlyList cats = ActCatalogue.Categories; T.Equal(cats.Count, tree.Rows.Count, "lines with every category closed"); T.Check(tree.Rows.All(r => r.IsHeader && !r.open), "a closed tree shows an entry"); T.Check(cats.Select(c => c.defName).SequenceEqual(ActCategories.All.Where(id => cats.Any(c => c.defName == id))), "categories not in picker order: " + string.Join(", ", cats.Select(c => c.defName))); tree.Toggle(ActCategories.Break); List rows = tree.Rows.ToList(); List breaks = rows.Where(r => !r.IsHeader).ToList(); T.Equal(ActCatalogue.EntriesOf(ActCategories.Break).Count, breaks.Count, "entries under an opened Mental breaks"); T.Check(breaks.Count > 0 && breaks.All(r => r.category.defName == ActCategories.Break), "an opened category lists another category's entries"); int firstDef = breaks.FindIndex(r => !r.entry.IsGroup); T.Check(firstDef > 0 && breaks.Skip(firstDef).All(r => !r.entry.IsGroup), "groups don't come first under Mental breaks"); T.Equal(1, rows.Count(r => r.IsHeader && r.open), "open categories after one click"); Law insult = Add(Insult); tree.SearchText = "insult"; ActPickerRow insultLine = tree.Rows.First(r => !r.IsHeader && r.entry.key == Insult); T.Check(insultLine.inBook, "Insult has a row but shows no tick"); T.Check(tree.Rows.Where(r => !r.IsHeader && r.entry.key != Insult).All(r => !r.inBook), "an entry without a row shows a tick"); T.Check(Dialog_PickAct.EntryTooltip(insultLine.entry, true).Contains("Core"), "the entry tooltip lacks its source mod"); Dialog_PickAct forRow = Dialog_PickAct.ForRow(insult); T.Check(forRow.Tree.IsOpen(ActCategories.Interaction), "the picker for a row doesn't open the row's category"); tree.SearchText = ""; tree.Toggle(ActCategories.Break); T.Equal(cats.Count, tree.Rows.Count, "lines after closing Mental breaks again"); } // ---- overlap icon ------------------------------------------------------------------------ [BlaxTest("ui: Ate: wake-up next to Recorded acts: ingested a drug shows the overlap icon on both; a cook's bill overlaps meals cooked")] public static void OverlapIcon() { Law ate = Fixtures.LawRow("Ate/WakeUp", null, LawStage.DidIt, LawSeverity.Moderate, "Warn", 3); Law recorded = Fixtures.LawRow("RecordedAct/IngestedDrug", null, LawStage.DidIt, LawSeverity.Moderate, "Warn", 3); Law insult = Fixtures.LawRow(Insult, null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); T.Check(ate.Resolves && recorded.Resolves && insult.Resolves, "an overlap row doesn't resolve: " + ate.MissingText + ", " + recorded.MissingText + ", " + insult.MissingText); T.Check(ActOverlaps.Of(ate).Contains(recorded), "Ate: wake-up shows no overlap with Recorded acts: ingested a drug"); T.Check(ActOverlaps.Of(recorded).Contains(ate), "Recorded acts: ingested a drug shows no overlap with Ate: wake-up"); T.Equal(0, ActOverlaps.Of(insult).Count, "overlaps of an Insult row"); T.Check(!ActOverlaps.Of(ate).Contains(insult), "Ate: wake-up overlaps Insult"); Lawbook.SetEnabled(recorded, false); T.Equal(0, ActOverlaps.Of(ate).Count, "overlaps of Ate: wake-up with the recorded-act row off"); Lawbook.SetEnabled(recorded, true); T.Check(ActOverlaps.Of(ate).Contains(recorded), "the overlap didn't come back with the row on again"); Law meal = Fixtures.LawRow("Ate/MealSimple", null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); T.Check(!ActOverlaps.Of(meal).Contains(recorded), "a simple meal overlaps ingested a drug"); Law cook = Fixtures.LawRow("Bill/CookMealSimple", null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); Law cooked = Fixtures.LawRow("Record/MealsCooked", null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); T.Check(cook.Resolves && cooked.Resolves, "the bill or record row doesn't resolve: " + cook.MissingText + ", " + cooked.MissingText); T.Check(ActOverlaps.Of(cook).Contains(cooked) && ActOverlaps.Of(cooked).Contains(cook), "cooking a simple meal shows no overlap with meals cooked"); T.Check(!ActOverlaps.Of(cooked).Contains(ate), "meals cooked overlaps eating wake-up"); } // ---- delete and starter ------------------------------------------------------------------ [BlaxTest("ui: deleting a law with open cases asks first and names the count; without open cases it deletes at once")] public static void DeleteAsksFirst() { Law quiet = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); T.Check(LawbookUi.DeleteConfirmText(quiet) == null, "a row without cases asks before the delete"); LawbookUi.RequestDelete(quiet); T.Check(!Lawbook.Laws.Contains(quiet), "a row without cases wasn't deleted at once"); Law busy = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); Pawn p = Fixtures.Colonist(); LawCase c1 = OpenCase(busy, p); LawCase c2 = OpenCase(busy, Fixtures.Colonist()); string text = LawbookUi.DeleteConfirmText(busy); T.Check(text != null && text.Contains("(2)"), "the confirmation doesn't name 2 open cases: " + text); int windows = Find.WindowStack.Count; LawbookUi.RequestDelete(busy); Dialog_MessageBox box = Find.WindowStack.WindowOfType(); T.Check(box != null && Find.WindowStack.Count == windows + 1, "no confirmation opened"); T.Check(Lawbook.Laws.Contains(busy) && !c1.IsClosed, "the row was deleted before the confirmation"); Find.WindowStack.TryRemove(box, doCloseSound: false); T.Check(Lawbook.Laws.Contains(busy), "closing the confirmation deleted the row"); T.Equal(2, Lawbook.Delete(busy), "cases the confirmed delete closed"); T.Check(c1.outcome == CaseOutcomes.Repealed && c2.outcome == CaseOutcomes.Repealed, "the cases didn't close as repealed: " + c1 + "; " + c2); } [BlaxTest("ui: the Starter lawbook button adds 7 rows; pressing it again adds none")] public static void StarterButton() { foreach (Lawbook.StarterRow s in Lawbook.StarterRows) T.Check(Lawbook.Find(s.actKey, s.option, s.stage) == null, "setup: starter row " + s.actKey + " is already in the book"); T.Equal(7, LawbookUi.PressStarter(), "rows the button added"); T.Equal(0, LawbookUi.PressStarter(), "rows a second press added"); foreach (Lawbook.StarterRow s in Lawbook.StarterRows) { Law l = Lawbook.Find(s.actKey, s.option, s.stage); T.Check(l != null && l.Resolves, "starter row " + s.actKey + " " + s.stage + " is missing or doesn't resolve: " + l?.MissingText); } } // ---- case log ---------------------------------------------------------------------------- [BlaxTest("ui: the case log lists newest first with who, what, stage, when, punishment and status; closed cases only when asked")] public static void CaseLogLines() { GameComponent_BlaxLaws comp = Fixtures.Comp; int now = Find.TickManager.TicksGame; Law warn = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); Law jail = Fixtures.LawRow("Ate/BlaxLawsTest_UiNoSuchDrug", null, LawStage.DidIt, LawSeverity.Grave, "Jail", 5); Pawn a = Fixtures.Colonist(); Pawn b = Fixtures.Colonist(); LawCase older = OpenCase(warn, a); older.openTick = older.lastActTick = now - 3 * GenDate.TicksPerDay; LawCase newer = OpenCase(jail, b); newer.count = 3; newer.notes = CaseNotes.OnTable; List rows = CaseLog.Rows(true).Where(c => c == older || c == newer).ToList(); T.Check(rows.Count == 2 && rows[0] == newer && rows[1] == older, "the log isn't newest first: " + string.Join("; ", rows)); T.Equal(a.LabelShort, CaseLog.Who(older), "who"); T.Check(CaseLog.What(newer).Contains("3 times") && CaseLog.What(newer).Contains("on a table"), "what lacks the count or the note: " + CaseLog.What(newer)); T.Check(CaseLog.What(older).Contains(ActKey.NameOf(MissingKey)), "what lacks the row's label: " + CaseLog.What(older)); T.Equal("Did it", CaseLog.Stage(older), "stage"); T.Check(CaseLog.When(older).Contains("3 days") && CaseLog.When(older).Contains("ago"), "when: " + CaseLog.When(older)); T.Check(CaseLog.Punishment(newer).Contains("5"), "punishment lacks the days: " + CaseLog.Punishment(newer)); T.Equal("Open", CaseLog.Status(older), "status of an open case"); newer.status = CaseStatus.Jailed; newer.releaseTick = now + 2 * GenDate.TicksPerDay; T.Check(CaseLog.Status(newer).Contains("2 days"), "a jailed case doesn't count down: " + CaseLog.Status(newer)); newer.status = CaseStatus.Open; newer.waitReason = WaitReasons.Danger; T.Check(CaseLog.Status(newer).Contains("danger"), "a waiting case doesn't say why: " + CaseLog.Status(newer)); comp.CloseCase(older, CaseOutcomes.Warned); T.Equal("Warned", CaseLog.Status(older), "status of a closed case"); T.Check(!CaseLog.Rows(false).Contains(older), "a closed case shows with closed cases hidden"); T.Check(CaseLog.Rows(false).Contains(newer), "an open case is hidden"); T.Check(CaseLog.CanJump(newer), "no jump button for a spawned offender"); } [BlaxTest("ui: the Cases tab refills one list in place, newest first, then severity, then id within a tick, and follows closing and severity changes")] public static void CaseRowsFillInPlace() { GameComponent_BlaxLaws comp = Fixtures.Comp; int now = Find.TickManager.TicksGame; Law minor = Fixtures.LawRow("Ate/BlaxLawsTest_UiRowOrderMinor", null, LawStage.DidIt, LawSeverity.Minor, "Warn", 1); Law moderate = Fixtures.LawRow("Ate/BlaxLawsTest_UiRowOrderModerate", null, LawStage.DidIt, LawSeverity.Moderate, "Warn", 1); Law grave = Fixtures.LawRow("Ate/BlaxLawsTest_UiRowOrderGrave", null, LawStage.DidIt, LawSeverity.Grave, "Warn", 1); LawCase older = OpenCase(grave, Fixtures.Colonist()); older.openTick = older.lastActTick = now - GenDate.TicksPerDay; LawCase m = OpenCase(moderate, Fixtures.Colonist()); LawCase b = OpenCase(minor, Fixtures.Colonist()); LawCase g = OpenCase(grave, Fixtures.Colonist()); LawCase b2 = OpenCase(minor, Fixtures.Colonist()); var mine = new HashSet { older, m, b, g, b2 }; string Order(List l) => string.Join("; ", l.Where(mine.Contains).Select(x => x.id + "/" + x.severity)); var rows = new List(); CaseLog.FillRows(rows, true); List got = rows.Where(mine.Contains).ToList(); T.Check(got.SequenceEqual(new[] { g, m, b2, b, older }), "not newest first, then severity, then id: " + Order(rows)); T.Check(CaseLog.Rows(true).SequenceEqual(rows), "Rows and FillRows disagree"); int capacity = rows.Capacity; CaseLog.FillRows(rows, true); T.Equal(capacity, rows.Capacity, "a second fill of the same cases grew the list"); b.SetLaw(LawStage.Planning, grave); // raises b to Grave; same tick as g, lower id CaseLog.FillRows(rows, true); T.Check(rows.Where(mine.Contains).SequenceEqual(new[] { g, b, m, b2, older }), "a raised severity didn't reorder the rows: " + Order(rows)); comp.CloseCase(m, CaseOutcomes.Warned); CaseLog.FillRows(rows, false); T.Check(!rows.Contains(m) && rows.Contains(g) && rows.Contains(older), "closing a case didn't hide only it: " + Order(rows)); CaseLog.FillRows(rows, true); T.Check(rows.Contains(m), "a closed case is missing with closed cases shown"); m.status = CaseStatus.Open; m.outcome = null; m.closeTick = -1; CaseLog.FillRows(rows, false); T.Check(rows.Contains(m), "a reopened case stays hidden"); foreach (LawCase c in mine) comp.CloseCase(c, CaseOutcomes.Warned); } [BlaxTest("ui: Arrest now shows only for a held case and the Cases tab says it waits for it; not for another wait or a closed case")] public static void ArrestNowShowsForHold() { Law jail = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Serious, "Jail", 5); Pawn p = Fixtures.Colonist(); LawCase c = OpenCase(jail, p); try { c.activePunishment = "Jail"; c.activeStage = LawStage.DidIt; T.Check(!CaseLog.CanArrestNow(c), "Arrest now shows for a case that doesn't wait"); WaitReasons.Set(c, WaitReasons.Danger); T.Check(!CaseLog.CanArrestNow(c), "Arrest now shows for a case that waits for the danger to pass"); T.Check(CaseLog.Status(c).Contains("danger"), "the status of a danger wait doesn't say why: " + CaseLog.Status(c)); // The hold JailFlow sets for a role holder or a mechanitor (JailTests drive the real hold and its letter). WaitReasons.Set(c, WaitReasons.Hold); T.Check(CaseLog.CanArrestNow(c), "Arrest now doesn't show for a held case"); T.Check(CaseLog.Status(c).Contains("Arrest now"), "the status of a held case doesn't say it waits for Arrest now: " + CaseLog.Status(c)); T.Check(!CaseLog.HoldReason(p).NullOrEmpty(), "no hold reason for the tooltip"); Fixtures.Comp.CloseCase(c, CaseOutcomes.NotEnforced); T.Check(!CaseLog.CanArrestNow(c), "Arrest now shows for a closed case"); } finally { c.activePunishment = null; } } [BlaxTest("ui: the jail letter names the offender, the law, the days and the release day; none with letters off")] public static void JailLetter() { RemoveNewLettersAfterTest(); var before = new HashSet(Find.LetterStack.LettersListForReading); Law jail = Fixtures.LawRow(MissingKey, null, LawStage.DidIt, LawSeverity.Serious, "Jail", 5); Pawn p = Fixtures.Colonist(); LawCase c = OpenCase(jail, p); c.activeStage = LawStage.DidIt; c.captureTick = Find.TickManager.TicksGame; c.releaseTick = c.captureTick + 5 * GenDate.TicksPerDay; CaseLetters.Jailed(c); List sent = LettersSince(before); T.Equal(1, sent.Count, "jail letters"); string text = (sent[0] as ChoiceLetter)?.Text.ToString() ?? ""; T.Check(text.Contains(p.LabelShort) && text.Contains("5 days") && text.Contains(ActKey.NameOf(MissingKey)), "the jail letter: " + text); T.Check(text.Contains(Sentences.ReleaseDate(c)) && !Sentences.ReleaseDate(c).NullOrEmpty(), "the jail letter lacks the release day: " + text); Fixtures.Settings.jailLetters = false; CaseLetters.Jailed(c); T.Equal(1, LettersSince(before).Count, "jail letters with letters off"); } } }