async Task Ex04(string key, object item) { TestElement element = null; TestElement container = null; await RunAsync(() => { element = new TestElement(); container = new TestElement { Content = element }; Given("a data template in a container resource", () => container.Resources[key] = ExpectedTemplate = new DataTemplate()); }); await SetWindowContent(container); await RunAsync(() => { When("to select a template by specifying the data and the container", () => ActualTemplate = new DataTypeDataTemplateSelector().SelectTemplate(item, container)); Then("the template should be selected", () => ActualTemplate == ExpectedTemplate); }); await RunAsync(() => { When("to select a template by specifying the data and the element whose parent is the container", () => ActualTemplate = new DataTypeDataTemplateSelector().SelectTemplate(item, element)); Then("the template should be selected", () => ActualTemplate == ExpectedTemplate); }); }
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); }
async Task Ex07() { var dataContextChangedHandled = new[] { false, false, false }; var loadedEventHandled = new[] { false, false, false }; var changedEventHandled = new[] { false, false, false }; await RunAsync(() => { Given("an element that contains the data context", () => Element = new TestElement { Name = "Element", DataContext = new TestUwpControllers.MultiTestDataContext() }); When("the UwpController is enabled for the element", () => { UwpController.SetIsEnabled(Element, true); GetController <TestUwpControllers.MultiTestUwpControllerA>(Element).DataContextChangedAssertionHandler = () => dataContextChangedHandled[0] = true; GetController <TestUwpControllers.MultiTestUwpControllerA>(Element).LoadedAssertionHandler = () => loadedEventHandled[0] = true; GetController <TestUwpControllers.MultiTestUwpControllerA>(Element).ChangedAssertionHandler = () => changedEventHandled[0] = true; GetController <TestUwpControllers.MultiTestUwpControllerB>(Element).DataContextChangedAssertionHandler = () => dataContextChangedHandled[1] = true; GetController <TestUwpControllers.MultiTestUwpControllerB>(Element).LoadedAssertionHandler = () => loadedEventHandled[1] = true; GetController <TestUwpControllers.MultiTestUwpControllerB>(Element).ChangedAssertionHandler = () => changedEventHandled[1] = true; GetController <TestUwpControllers.MultiTestUwpControllerC>(Element).DataContextChangedAssertionHandler = () => dataContextChangedHandled[2] = true; GetController <TestUwpControllers.MultiTestUwpControllerC>(Element).LoadedAssertionHandler = () => loadedEventHandled[2] = true; GetController <TestUwpControllers.MultiTestUwpControllerC>(Element).ChangedAssertionHandler = () => changedEventHandled[2] = true; }); Then("the data context of the controller should be set", () => UwpController.GetControllers(Element).Cast <TestUwpControllers.TestUwpControllerBase>().All(controller => controller.DataContext == Element.DataContext)); }); When("the element is set for the content of the window", async() => await SetWindowContent(Element)); await RunAsync(() => { Then("the element of the controller should be set", () => UwpController.GetControllers(Element).Cast <TestUwpControllers.TestUwpControllerBase>().All(controller => controller.Element == Element)); Then("the DataContextChanged event should be handled", () => dataContextChangedHandled.All(h => h)); Then("the Loaded event should be handled", () => loadedEventHandled.All(h => h)); When("the Changed event of the element is raised", () => Element.RaiseChanged()); Then("the Changed event should be handled", () => changedEventHandled.All(h => h)); }); When("the content of the window is cleared in order to unload the element", async() => await ClearWindowContent()); await RunAsync(() => { Then("the data context of the controller should be null", () => UwpController.GetControllers(Element).Cast <TestUwpControllers.TestUwpControllerBase>().All(controller => controller.DataContext == null)); Then("the element of the controller should be null", () => UwpController.GetControllers(Element).Cast <TestUwpControllers.TestUwpControllerBase>().All(controller => controller.Element == null)); for (int index = 0; index < changedEventHandled.Length; index++) { changedEventHandled[index] = false; } When("the Changed event of the element is raised", () => Element.RaiseChanged()); Then("the Changed event should not be handled", () => changedEventHandled.All(h => !h)); }); }
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 Ex04() { Given("an element that contains the data context", () => Element = new TestElement { DataContext = new TestDataContexts.AttachingTestDataContext() }); When("the AvaloniaController is enabled for the element", () => AvaloniaController.SetIsEnabled(Element, true)); Then("the data context of the controller should be set", () => Element.GetController <TestAvaloniaControllers.TestController>().DataContext == Element.DataContext); When("another data context is set for the element", () => Element.DataContext = new object()); Then("the data context of the controller should be set", () => Element.GetController <TestAvaloniaControllers.TestController>().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) ); }
public AvaloniaControllerSpec_EventHandlerInjectionForAttachedEvent() { Element = new TestElement { Name = "Element", Content = new TestElement { Content = Button }, DataContext = new TestDataContexts.TestDataContext() }; Element.EnsureVisual(); }
void Ex02(TestAvaloniaControllers.ITestAvaloniaController controller) { Given("a child element", () => ChildElement = new Button { Name = "ChildElement" }); Given("an element that has the child element", () => Element = new TestElement { Name = "element", Content = ChildElement }); When("the child element is set to the controller using the AvaloniaController", () => AvaloniaController.SetElement(ChildElement, controller, true)); When("the element is set to the controller using the AvaloniaController", () => AvaloniaController.SetElement(Element, controller, true)); Then("the element should be set to the controller", () => controller.Element == Element); Then("the child element should be set to the controller", () => controller.ChildElement == ChildElement); }
async Task Ex02(object dataContext, Type[] expectedControllerTypes) { await RunAsync(() => { Given("an element that does not contain the data context", () => Element = new TestElement()); When("the UwpController is enabled for the element", () => UwpController.SetIsEnabled(Element, true)); When("a data context of the element is set", () => Element.DataContext = dataContext); Then("the controller should be attached to the element", () => UwpController.GetControllers(Element).Select(controller => controller.GetType()).SequenceEqual(expectedControllerTypes) && UwpController.GetControllers(Element).Cast <TestController>().All(controller => controller.DataContext == dataContext) ); }); }
async Task Ex06() { var dataContextChangedHandled = false; var loadedEventHandled = false; var changedEventHandled = false; TestUwpControllers.TestUwpController controller = null; await RunAsync(() => { Given("an element that contains the data context", () => Element = new TestElement { Name = "Element", DataContext = new TestUwpControllers.TestDataContext() }); When("the UwpController is enabled for the element", () => { UwpController.SetIsEnabled(Element, true); controller = GetController <TestUwpControllers.TestUwpController>(Element); controller.DataContextChangedAssertionHandler = () => dataContextChangedHandled = true; controller.LoadedAssertionHandler = () => loadedEventHandled = true; controller.ChangedAssertionHandler = () => changedEventHandled = true; }); Then("the data context of the controller should be set", () => GetController <TestUwpControllers.TestUwpController>(Element).DataContext == Element.DataContext); }); When("the element is set for the content of the window", async() => await SetWindowContent(Element)); await RunAsync(() => { Then("the element of the controller should be set", () => GetController <TestUwpControllers.TestUwpController>(Element).Element == Element); Then("the DataContextChanged event should be handled", () => dataContextChangedHandled); Then("the Loaded event should be handled", () => loadedEventHandled); When("the UwpController is disabled for the element", () => UwpController.SetIsEnabled(Element, false)); Then("the controller should be detached", () => !UwpController.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 Changed event of the element is raised", () => Element.RaiseChanged()); Then("the Changed event should not be handled", () => !changedEventHandled); }); When("the content of the window is cleared in order to unload the element", async() => await ClearWindowContent()); When("the element is set for the content of the window", async() => await SetWindowContent(Element)); await RunAsync(() => { Then("the controller should not be attached", () => !UwpController.GetControllers(Element).Any()); }); }
async Task Ex04() { await RunAsync(() => { Given("a data context", () => DataContext = new AttachingTestDataContext()); Given("an element that contains the data context", () => Element = new TestElement { DataContext = DataContext }); When("the UwpController is enabled for the element", () => UwpController.SetIsEnabled(Element, true)); Then("the data context of the controller should be set", () => GetController <TestController>(Element).DataContext == DataContext); When("another data context is set for the element", () => Element.DataContext = AnotherDataContext = new object()); Then("the data context of the controller should be changed", () => GetController <TestController>(Element).DataContext == AnotherDataContext); }); }
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); }
async Task Ex02(TestUwpControllers.ITestUwpController controller) { await RunAsync(() => { Given("a child element", () => ChildElement = new TestElement { Name = "childElement" }); Given("an element", () => Element = new TestElement { Name = "element" }); When("the child element is set to the controller using the UwpController", () => UwpController.SetElement(ChildElement, controller, true)); When("the element is set to the controller using the UwpController", () => UwpController.SetElement(Element, controller, true)); Then("the element should be set to the controller", () => controller.Element == Element); Then("the child element should be set to the controller", () => controller.ChildElement == ChildElement); }); }
async Task Ex03() { await RunAsync(() => { Given("a data context", () => DataContext = new KeyAttachingTestDataContext()); Given("an element that contains the data context", () => Element = new TestElement { DataContext = DataContext }); When("the key of the element is set using the UwpController", () => UwpController.SetKey(Element, "TestElement")); Then("the UwpController should be enabled for the element", () => UwpController.GetIsEnabled(Element)); Then("the controller should be attached to the element", () => UwpController.GetControllers(Element).Count == 1 && UwpController.GetControllers(Element)[0].GetType() == typeof(KeyTestDataContextController) && (UwpController.GetControllers(Element)[0] as KeyTestDataContextController).DataContext == DataContext ); }); }
async Task Ex01() { HandledException = true; await RunAsync(() => { Given("a controller that has an event handler that throws an exception", () => Controller = new TestUwpControllers.ExceptionTestUwpController()); Given("an element", () => Element = new TestElement()); When("the controller is added", () => UwpController.GetControllers(Element).Add(Controller)); When("the controller is attached to the element", () => UwpController.GetControllers(Element).AttachTo(Element)); }); When("the element is set to the content of the window", async() => await SetWindowContent(Element)); await RunAsync(() => { When("the Changed event of the element is raised", () => Element.RaiseChanged()); Then("the unhandled exception should be handled", () => UnhandledException != null); }); }
async Task Ex01() { Extension = Substitute.For <IUwpControllerExtension>(); await RunAsync(() => { Given("an element that contains a data context", () => Element = new TestElement { DataContext = DataContext }); When("the UwpController is enabled for the element", () => { UwpController.SetIsEnabled(Element, true); Controller = UwpController.GetControllers(Element)[0] as TestUwpControllers.TestUwpController; }); When("the extension is added", () => UwpController.AddExtension(Extension)); }); When("the element is set for the content of the window", async() => await SetWindowContent(Element)); Then("the extension should be attached", () => Extension.Received().Attach(Controller, Element)); When("the content of the window is cleared in order to unload the element", async() => await ClearWindowContent()); Then("the extension should be detached", () => Extension.Received().Detach(Controller, Element)); }
async Task Ex01(TestUwpControllers.ITestUwpController controller) { await RunAsync(() => { Given("a data context", () => DataContext = new object()); Given("a child element", () => ChildElement = new TestElement { Name = "childElement" }); Given("an element that has the child element", () => Element = new TestElement { Name = "element", Content = ChildElement, DataContext = DataContext }); When("the controller is added", () => UwpController.GetControllers(Element).Add(controller)); When("the controller is attached to the element", () => UwpController.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); }); EventHandled = false; When("the element is set to the content of the window", async() => await SetWindowContent(Element)); 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); Then("the event should be handled", () => EventHandled); await RunAsync(() => { EventHandled = false; When("the Changed event of the child element is raised", () => ChildElement.RaiseChanged()); Then("the Changed event should be handled", () => EventHandled); EventHandled = false; When("the data context of the child element should be set", () => ChildElement.DataContext = new object()); Then("the DataContextChanged event should be handled", () => EventHandled); }); }
void Ex07() { Given("an element that contains the data context", () => Element = new TestElement { Name = "Element", DataContext = new TestDataContexts.MultiTestDataContext() }); When("the AvaloniaController is enabled for the element", () => { AvaloniaController.SetIsEnabled(Element, true); Controllers = new TestAvaloniaControllers.TestAvaloniaControllerBase[] { Element.GetController <TestAvaloniaControllers.MultiTestAvaloniaControllerA>(), Element.GetController <TestAvaloniaControllers.MultiTestAvaloniaControllerB>(), Element.GetController <TestAvaloniaControllers.MultiTestAvaloniaControllerC>() }; AttachedToLogicalTreeEventsHandled = new[] { false, false, false }; ChangedEventsHandled = new[] { false, false, false }; }); Then("the data context of the controller should be set", () => Controllers.All(controller => controller.DataContext == Element.DataContext)); When("the element is attached to the logical tree", () => { for (var index = 0; index < Controllers.Length; index++) { var eventHandledIndex = index; Controllers[index].AttachedToLogicalTreeAssertionHandler = () => AttachedToLogicalTreeEventsHandled[eventHandledIndex] = true; } Element.AttachToLogicalTree(); }); Then("the element of the controller should be set", () => Controllers.All(controller => controller.Element == Element)); Then("the AttachedToLogicalTree event should be handled", () => AttachedToLogicalTreeEventsHandled.All(handled => handled)); When("the Changed event is raised", () => { for (var index = 0; index < Controllers.Length; index++) { var eventHandledIndex = index; Controllers[index].ChangedAssertionHandler = () => ChangedEventsHandled[eventHandledIndex] = true; } Element.RaiseChanged(); }); Then("the Changed event should be handled", () => ChangedEventsHandled.All(handled => handled)); When("the element is detached from the logical tree", () => Element.DetachFromLogicalTree()); Then("the data context of the controller should be null", () => Controllers.All(controller => controller.DataContext == null)); Then("the element of the controller should be null", () => Controllers.All(controller => controller.Element == null)); When("the element is attached to the logical tree", () => { for (var index = 0; index < AttachedToLogicalTreeEventsHandled.Length; index++) { AttachedToLogicalTreeEventsHandled[index] = false; } Element.AttachToLogicalTree(); }); Then("the AttachedToLogicalTree event should not be handled", () => AttachedToLogicalTreeEventsHandled.All(handled => !handled)); When("the Changed event is raised", () => { for (var index = 0; index < ChangedEventsHandled.Length; index++) { ChangedEventsHandled[index] = false; } Element.RaiseChanged(); }); Then("the Changed event should not be handled", () => ChangedEventsHandled.All(handled => !handled)); }