using Annulus; using Azimuth; using Clave; using System; using System.Collections.Generic; using UnitTestSharp; using Listener = Glass.UnitTests.FileDialogModelTests.Listener; namespace Glass.UnitTests { public class FileDialogTests : TestFixture { public static readonly FileFilter TextFiles = new FileFilter("Text files", new[] { ".txt" }); public static readonly FileFilter MarkdownFiles = new FileFilter("Markdown files", new[] { ".md" }); public static readonly ButtonMaterial Row = DropdownTests.Arrow(1, 1); public static readonly ButtonMaterial SelectedRow = DropdownTests.Arrow(2, 2); /// /// One line of this is 20 high. /// public static readonly TextStyle LabelStyle = TextStyleTests.WithFont() with { VerticalMargin = 8 }; /// /// Buttons of 50 by 20, each a 46 by 16 body inside a 2 thick border. /// public static readonly FileDialogMaterial Measurements = new FileDialogMaterial { Spacing = 4, UpButton = ButtonOf(46, 16), NewFolderButton = ButtonOf(46, 16), AcceptButton = ButtonOf(46, 16), CancelButton = ButtonOf(46, 16), EntryRow = Row, SelectedEntryRow = SelectedRow, TextStyle = LabelStyle, OverwriteQuestionFormat = "'{0}' already exists. Replace it?", OverwriteBox = new MessageBoxMaterial { TextStyle = LabelStyle, Spacing = 4 } .WithButton(FileDialog.OverwriteAnswer.Replace, ButtonOf(46, 16)) .WithButton(FileDialog.OverwriteAnswer.Cancel, ButtonOf(46, 16)), MinimumEntryRows = 3, }; /// /// Everything that gets built for a dialog, in the order it was asked for. /// public class Built { public readonly List Entries = new List(); } /// /// The size the dialog's text controls give in the label style. /// public static Vector TextSize(string text) { return new TextMaterial { Text = text, Style = LabelStyle }.PreferredSize(Constraints.Unbounded); } public static string QuestionText(string name) { return $"'{name}' already exists. Replace it?"; } [IgnoreTest] public static string AskedText(FileDialog dialog) { return ((Label)MessageBoxTests.MessageOf(dialog.OverwriteBox)).Text.ToString(); } [IgnoreTest] public static Button AnswerButton(FileDialog dialog, FileDialog.OverwriteAnswer answer) { return dialog.OverwriteBox.ButtonFor(answer); } [IgnoreTest] public static string[] TextsOf(Dropdown dropdown) { var returnMe = new List(); foreach (Control row in DropdownTests.Rows(dropdown)) { returnMe.Add(((TextMaterial)DropdownTests.ItemOf(row).Material).Text.ToString()); } return returnMe.ToArray(); } public static Control Sized(Scalar width, Scalar height) { return new Panel { Bounds = Box(0, 0, width, height) }; } /// /// A button whose body wants exactly this much room, inside a border of the given thickness. /// public static ButtonMaterial ButtonOf(Scalar width, Scalar height, int border = 2) { Material body = new ScrollViewTests.SizedSolidMaterial(new Vector(width, height)); return new ButtonMaterial(ScrollViewTests.Bordered(border, "button").Framing(body)); } public static FileDialogMaterial WithEveryButton(FileDialogMaterial material, ButtonMaterial button) { return material with { UpButton = button, NewFolderButton = button, AcceptButton = button, CancelButton = button, }; } /// /// Builds a dialog whose entries are 10 by 20, unless told otherwise. /// public static FileDialog NewDialog( FileDialogMode mode = FileDialogMode.Open, IReadOnlyList filters = null, Built built = null, string startDirectory = null, FileDialogMaterial? material = null, Func entrySize = null) { built = built ?? new Built(); FileDialogModel model = FileDialogModel.Create(InMemoryFileSystemTests.Sample(), mode, filters, startDirectory).Value; var dialog = new FileDialog( model, entry => { built.Entries.Add(entry.Name); return Sized(entrySize?.Invoke(entry.Name) ?? new Vector(10, 20)); }, DialogBehavior.Default); dialog.FileDialogMaterial = material ?? Measurements; dialog.Bounds = Rect.FromPositionAndSize(Vector.Zero, new Vector(300, 300)); return dialog; } public static Control Sized(Vector size) { return Sized(size.X, size.Y); } public static Rect Box(Scalar x, Scalar y, Scalar width, Scalar height) { return Rect.FromPositionAndSize(new Vector(x, y), new Vector(width, height)); } public static void Click(Button button, int clickCount = 1) { button.Click(default, MouseButtonFlags.Left, clickCount); } [IgnoreTest] public static Button[] RowsOf(FileDialog dialog) { var returnMe = new List