using System; using System.Collections.Generic; using Amphora; using Annulus; using Azimuth; using Blacklight.Core; using Glass; using Glass.Blacklight; using Prism; using Prism.Glass; using UnitTestSharp; namespace Konis.Testbed.UnitTests { public class GradientWindowTests : TestFixture { public static GradientWindow BuiltWindow() { new BlacklightRenderer().RegisterAllFonts(new NullWindowHandle()); return new GradientWindow(TestbedSkinTests.NewSkin(), new NullWindowHandle()); } public static Gradient WhiteToWhite() { return new Gradient( GradientSpace.Hsv, HueDirection.Shortest, new GradientStop(0, Colors.White), new GradientStop(1, Colors.White)); } public static Func Recording(List edits) { return gradient => { edits.Add(gradient); return Status.Ok; }; } public static Status Refuse(Gradient gradient) { return TunableFailures.Refused.Fail("Too bright."); } public static ControlType Child(GradientWindow window) where ControlType : Control { foreach (Control child in window.Frame.CanvasChildren.BackToFront()) { if (child is ControlType found) { return found; } } throw new InvalidOperationException($"The window has no {typeof(ControlType).Name}."); } public static GradientEditor EditorOf(GradientWindow window) { return Child(window); } /// /// Presses the middle of the editor's bar, which adds a stop there. /// public static void AddAStopInTheMiddle(GradientWindow window) { Rect bar = EditorOf(window).Bar.Bounds; var middle = new Vector(bar.Left + bar.Width / 2, bar.Top + bar.Height / 2); EditorOf(window).Bar.MouseDown( new MouseState { Position = middle }, MouseButtonFlags.Left, default, new KeyboardManager()); } public class CtorTests : TestFixture { public void StartsClosed() { GradientWindow window = BuiltWindow(); CheckFalse(window.IsOpen); CheckFalse(window.Frame.Visible); } } public class EditTests : TestFixture { public void OpensTheWindowUnderTheTitle() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Recording(new List()), "tint"); CheckTrue(window.IsOpen); CheckEqual("tint", window.Frame.Title); } public void LoadsTheGradientIntoTheEditor() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Recording(new List()), "tint"); CheckEqual(2, EditorOf(window).Model.StopCount); } public void LoadingTheGradient_IsNotAskedAboutAsAnEdit() { GradientWindow window = BuiltWindow(); var edits = new List(); window.Edit(WhiteToWhite(), Recording(edits), "tint"); CheckEqual(0, edits.Count); } public void AnEditInTheWindow_IsAskedAboutWithTheNewGradient() { GradientWindow window = BuiltWindow(); var edits = new List(); window.Edit(WhiteToWhite(), Recording(edits), "tint"); AddAStopInTheMiddle(window); CheckEqual(1, edits.Count); CheckEqual(3, edits[0].StopCount); } public void EditingAnotherGradient_AsksTheNewListenerOnly() { GradientWindow window = BuiltWindow(); var first = new List(); var second = new List(); window.Edit(WhiteToWhite(), Recording(first), "tint"); window.Edit(WhiteToWhite(), Recording(second), "other"); AddAStopInTheMiddle(window); CheckEqual(0, first.Count); CheckEqual(1, second.Count); CheckEqual("other", window.Frame.Title); } public void WithoutAListener_Throws() { CheckThrow(typeof(ArgumentNullException)); BuiltWindow().Edit(WhiteToWhite(), null, "tint"); } } public class ProblemTests : TestFixture { public void Starts_Empty() { CheckEqual("", BuiltWindow().Problem); } public void AnEditTheDefRefuses_SaysWhyItDid() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Refuse, "tint"); AddAStopInTheMiddle(window); CheckEqual("Too bright.", window.Problem); } public void AnEditTheDefRefuses_IsNotMade() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Refuse, "tint"); AddAStopInTheMiddle(window); CheckEqual(2, EditorOf(window).Model.StopCount); } public void ASpaceTheDefRefuses_PutsTheDropdownBackAndSaysWhy() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Refuse, "tint"); BlendPicker blend = Child(window); blend.SpaceDropdown.Select((int)GradientSpace.Oklab); CheckEqual((int)GradientSpace.Hsv, blend.SpaceDropdown.SelectedIndex); CheckEqual(GradientSpace.Hsv, EditorOf(window).Model.Space); CheckEqual("Too bright.", window.Problem); } public void AfterARefusal_AnEditTheDefTakesClearsIt() { GradientWindow window = BuiltWindow(); bool refusing = true; window.Edit(WhiteToWhite(), gradient => refusing ? Refuse(gradient) : Status.Ok, "tint"); AddAStopInTheMiddle(window); refusing = false; AddAStopInTheMiddle(window); CheckEqual("", window.Problem); CheckEqual(3, EditorOf(window).Model.StopCount); } public void OpeningAnotherGradient_ClearsIt() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Refuse, "tint"); AddAStopInTheMiddle(window); window.Edit(WhiteToWhite(), Recording(new List()), "other"); CheckEqual("", window.Problem); } } public class CloseTests : TestFixture { public void HidesTheWindow() { GradientWindow window = BuiltWindow(); window.Edit(WhiteToWhite(), Recording(new List()), "tint"); window.Close(); CheckFalse(window.IsOpen); } } } }