using System; using System.Collections.Generic; using Amphora; using Annulus; using Azimuth; using Blacklight.Core; using Glass; using Glass.Blacklight; using Prism.Glass; using UnitTestSharp; namespace Konis.Testbed.UnitTests { public class FunctionWindowTests : TestFixture { public static FunctionWindow BuiltWindow() { new BlacklightRenderer().RegisterAllFonts(new NullWindowHandle()); return new FunctionWindow(TestbedSkinTests.NewSkin(), new NullWindowHandle()); } public static Func Recording(List edits) { return function => { edits.Add(function); return Status.Ok; }; } public static FunctionEditor EditorOf(FunctionWindow window) { foreach (Control child in window.Frame.CanvasChildren.BackToFront()) { if (child is FunctionEditor editor) { return editor; } } throw new InvalidOperationException("The window has no function editor."); } /// /// Presses the middle of the editor's plot, which adds a point there. /// public static void AddAPointInTheMiddle(FunctionWindow window) { FunctionEditor editor = EditorOf(window); Rect plot = editor.Plot.Bounds; var middle = new Vector(plot.Left + plot.Width / 2, plot.Top + plot.Height / 2); editor.Plot.MouseDown( new MouseState { Position = middle }, MouseButtonFlags.Left, default, new KeyboardManager()); } public class CanEditTests : TestFixture { public void AFunctionOfPoints_CanBeEdited() { CheckTrue(FunctionWindow.CanEdit(new PiecewiseLinear((0, 1)))); CheckTrue(FunctionWindow.CanEdit(new PiecewiseStep((0, 1)))); CheckTrue(FunctionWindow.CanEdit(new MonotoneSpline((0, 0), (1, 1)))); } public void NoFunction_CanBeEdited() { CheckTrue(FunctionWindow.CanEdit(null)); } public void AFunctionOfCode_CanNotBeEdited() { CheckFalse(FunctionWindow.CanEdit(new CallbackFunction(x => x))); } } public class CtorTests : TestFixture { public void StartsClosed() { FunctionWindow window = BuiltWindow(); CheckFalse(window.IsOpen); CheckFalse(window.Frame.Visible); } } public class EditTests : TestFixture { public void OpensTheWindowUnderTheTitle() { FunctionWindow window = BuiltWindow(); window.Edit(new PiecewiseLinear((0, 1), (1, 0)), _ => Status.Ok, "size"); CheckTrue(window.IsOpen); CheckEqual("size", window.Frame.Title); } public void NoFunction_OpensOnOneEverywhere() { FunctionWindow window = BuiltWindow(); window.Edit(null, _ => Status.Ok, "size"); CheckEqual(1, EditorOf(window).Model.PointCount); CheckEqual(0.0, EditorOf(window).Model.GetX(0).Value); CheckEqual(1.0, EditorOf(window).Model.GetY(0).Value); } public void LoadsTheFunctionIntoTheEditor() { FunctionWindow window = BuiltWindow(); window.Edit(new MonotoneSpline((0, 0), (0.5, 1), (1, 0)), _ => Status.Ok, "size"); CheckEqual(3, EditorOf(window).Model.PointCount); CheckEqual(FunctionMode.Smooth, EditorOf(window).Model.Mode); } public void LoadingTheFunction_IsNotReportedAsAnEdit() { FunctionWindow window = BuiltWindow(); var edits = new List(); window.Edit(new PiecewiseLinear((0, 1), (1, 0)), Recording(edits), "size"); CheckEqual(0, edits.Count); } public void AnEditInTheWindow_IsReportedWithTheNewFunction() { FunctionWindow window = BuiltWindow(); var edits = new List(); window.Edit(new PiecewiseLinear((0, 1), (1, 0)), Recording(edits), "size"); AddAPointInTheMiddle(window); CheckEqual(1, edits.Count); CheckEqual(3, edits[0].PointCount); } public void EditingAnotherFunction_ReportsToTheNewListenerOnly() { FunctionWindow window = BuiltWindow(); var first = new List(); var second = new List(); window.Edit(new PiecewiseLinear((0, 1)), Recording(first), "size"); window.Edit(new PiecewiseLinear((0, 1)), Recording(second), "alpha"); AddAPointInTheMiddle(window); CheckEqual(0, first.Count); CheckEqual(1, second.Count); CheckEqual("alpha", window.Frame.Title); } public void AFunctionOfCode_Throws() { CheckThrow(typeof(ArgumentException)); BuiltWindow().Edit(new CallbackFunction(x => x), _ => Status.Ok, "size"); } public void WithoutAListener_Throws() { CheckThrow(typeof(ArgumentNullException)); BuiltWindow().Edit(new PiecewiseLinear((0, 1)), null, "size"); } } public class ProblemTests : TestFixture { private static FunctionPointFields FieldsOf(FunctionWindow window) { foreach (Control child in window.Frame.CanvasChildren.BackToFront()) { if (child is FunctionPointFields fields) { return fields; } } throw new InvalidOperationException("The window has no point fields."); } private static FunctionWindow OpenOnATallHill() { FunctionWindow window = BuiltWindow(); window.Edit(new MonotoneSpline((0, 0), (0.2, 7e307), (1, 0)), _ => Status.Ok, "size"); FieldsOf(window).Model.Select(1); return window; } public void Starts_Empty() { CheckEqual("", OpenOnATallHill().Problem); } public void ATypedXThatSmoothRefuses_SaysWhy() { FunctionWindow window = OpenOnATallHill(); FieldsOf(window).XField.Value = 0.5m; CheckEqual(FunctionFailures.SmoothSpansTooMuchY.Message, window.Problem); } public void AnEditThatGoesThrough_ClearsIt() { FunctionWindow window = OpenOnATallHill(); FieldsOf(window).XField.Value = 0.5m; FieldsOf(window).XField.Value = 0.3m; CheckEqual("", window.Problem); } private static FunctionWindow OpenOnARampTheDefRefusesEditsOf() { FunctionWindow window = BuiltWindow(); window.Edit( new PiecewiseLinear((0, 1), (1, 0)), _ => TunableFailures.Refused.Fail("Too big."), "size"); return window; } public void AnEditTheDefRefuses_SaysWhyItDid() { FunctionWindow window = OpenOnARampTheDefRefusesEditsOf(); AddAPointInTheMiddle(window); CheckEqual("Too big.", window.Problem); } public void AnEditTheDefRefuses_IsNotMade() { FunctionWindow window = OpenOnARampTheDefRefusesEditsOf(); AddAPointInTheMiddle(window); CheckEqual(2, EditorOf(window).Model.PointCount); } public void ATypedXTheDefRefuses_PutsTheFieldBack() { FunctionWindow window = OpenOnARampTheDefRefusesEditsOf(); FieldsOf(window).XField.Value = 0.5m; CheckEqual(0m, FieldsOf(window).XField.Value); CheckEqual("Too big.", window.Problem); } public void AfterARefusal_AnEditTheDefTakesClearsIt() { FunctionWindow window = BuiltWindow(); bool refusing = true; window.Edit( new PiecewiseLinear((0, 1), (1, 0)), _ => refusing ? TunableFailures.Refused.Fail("Too big.") : Status.Ok, "size"); AddAPointInTheMiddle(window); refusing = false; AddAPointInTheMiddle(window); CheckEqual("", window.Problem); CheckEqual(3, EditorOf(window).Model.PointCount); } public void OpeningAnotherFunction_ClearsIt() { FunctionWindow window = OpenOnATallHill(); FieldsOf(window).XField.Value = 0.5m; window.Edit(new PiecewiseLinear((0, 1)), _ => Status.Ok, "alpha"); CheckEqual("", window.Problem); } } public class CloseTests : TestFixture { public void HidesTheWindow() { FunctionWindow window = BuiltWindow(); window.Edit(new PiecewiseLinear((0, 1)), _ => Status.Ok, "size"); window.Close(); CheckFalse(window.IsOpen); } } } }