void Ex01(TestAvaloniaControllers.ITestAvaloniaController controller) { Given("a data context", () => DataContext = new object()); Given("a child element", () => ChildElement = new Button { Name = "ChildElement" }); Given("an element that has the child element", () => Element = new TestElement { Name = "element", Content = ChildElement, DataContext = DataContext }); When("the controller is added", () => AvaloniaController.GetControllers(Element).Add(controller)); When("the controller is attached to the element", () => AvaloniaController.GetControllers(Element).AttachTo(Element)); Then("the data context of the controller should be set", () => controller.DataContext == DataContext); Then("the element of the controller should be null", () => controller.Element == null); Then("the child element of the controller should be null", () => controller.ChildElement == null); When("the element is attached to the logical tree", () => Element.AttachToLogicalTree()); Then("the data context of the controller should be set", () => controller.DataContext == DataContext); Then("the element of the controller should be set", () => controller.Element == Element); Then("the child element of the controller should be set", () => controller.ChildElement == ChildElement); EventHandled = false; When("the Click event of the child element is raised", () => ChildElement.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, ChildElement))); Then("the Click event should be handled", () => EventHandled); }
void Ex02(object dataContext, IEnumerable <Type> expectedControllerTypes) { Given("an element that does not contain the data context", () => Element = new TestElement()); When("the AvaloniaController is enabled for the element", () => AvaloniaController.SetIsEnabled(Element, true)); When("a data context of the element is set", () => Element.DataContext = dataContext); Then("the controller should be attached to the element", () => AvaloniaController.GetControllers(Element).Select(controller => controller.GetType()).SequenceEqual(expectedControllerTypes) && AvaloniaController.GetControllers(Element).OfType <TestAvaloniaControllers.TestController>().All(controller => controller.DataContext == Element.DataContext) ); }
void Ex03() { Given("an element that contains the data context", () => Element = new TestElement { DataContext = new TestDataContexts.KeyAttachingTestDataContext() }); When("the key of the element is set using the AvaloniaController", () => AvaloniaController.SetKey(Element, "TestElement")); Then("the AvaloniaController should be enabled for the element", () => AvaloniaController.GetIsEnabled(Element)); Then("the controller should be attached to the element", () => AvaloniaController.GetControllers(Element).Select(controller => controller.GetType()).SequenceEqual(new[] { typeof(TestAvaloniaControllers.KeyTestDataContextController) }) && AvaloniaController.GetControllers(Element).OfType <TestAvaloniaControllers.TestController>().All(controller => controller.DataContext == Element.DataContext) ); }
void Ex01() { ExceptionHandled = true; When("the controller is added", () => AvaloniaController.GetControllers(Element).Add(Controller)); When("the controller is attached to the element", () => AvaloniaController.GetControllers(Element).AttachTo(Element)); When("the element is attached to the logical tree", () => Element.AttachToLogicalTree()); When("the Changed event of the element is raised", () => Element.RaiseChanged()); Then("the unhandled exception should be handled", () => UnhandledException != null); }
void Ex06() { Given("an element that contains the data context", () => Element = new TestElement { Name = "Element", DataContext = new TestDataContexts.TestDataContext() }); When("the AvaloniaController is enabled for the element", () => { AvaloniaController.SetIsEnabled(Element, true); Controller = Element.GetController <TestAvaloniaControllers.TestAvaloniaController>(); }); Then("the data context of the controller should be set", () => Controller.DataContext == Element.DataContext); When("the element is attached to the logical tree", () => { Controller.AttachedToLogicalTreeAssertionHandler = () => AttachedToLogicalTreeEventHandled = true; Element.AttachToLogicalTree(); }); Then("the element of the controller should be set", () => Controller.Element == Element); Then("the AttachedToLogicalTree event should be handled", () => AttachedToLogicalTreeEventHandled); When("the Changed event is raised", () => { Controller.ChangedAssertionHandler = () => ChangedEventHandled = true; Element.RaiseChanged(); }); Then("the Changed event should be handled", () => ChangedEventHandled); When("the element is detached from the logical tree", () => Element.DetachFromLogicalTree()); When("the AvaloniaController is disabled for the element", () => AvaloniaController.SetIsEnabled(Element, false)); Then("the controller should be detached", () => !AvaloniaController.GetControllers(Element).Any()); Then("the data context of the controller should be null", () => Controller.DataContext == null); Then("the element of the controller should be null", () => Controller.Element == null); When("the element is attached to the logical tree", () => { AttachedToLogicalTreeEventHandled = false; Element.AttachToLogicalTree(); }); Then("the AttachedToLogicalTree event should not be handled", () => !AttachedToLogicalTreeEventHandled); Then("the controller should not be attached", () => !AvaloniaController.GetControllers(Element).Any()); When("the Changed event is raised", () => { ChangedEventHandled = false; Element.RaiseChanged(); }); Then("the Changed event should not be handled", () => !ChangedEventHandled); }
public static T GetController <T>(this StyledElement element) => AvaloniaController.GetControllers(element).OfType <T>().FirstOrDefault();