public static void Main() { var uiElementTest = new UIElementLayeringTests(); uiElementTest.TestAll(); var panelTest = new PanelTests(); panelTest.TestAll(); var controlTest = new ControlTests(); controlTest.TestAll(); var stackPanelTest = new StackPanelTests(); stackPanelTest.TestAll(); var canvasTest = new CanvasTests(); canvasTest.TestAll(); var contentControlTest = new ContentControlTest(); contentControlTest.TestAll(); var eventManagerTest = new EventManagerTests(); eventManagerTest.TestAll(); var routedEventArgTest = new RoutedEventArgsTest(); routedEventArgTest.TestAll(); var uiElementEventTest = new UIElementEventTests(); uiElementEventTest.TestAll(); var gridTest = new GridTests(); gridTest.TestAll(); }
public void TestAddRemoveHandler() { var testRoutedEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>("Test1", RoutingStrategy.Tunnel, typeof(UIElementLayeringTests)); var element = new UIElementLayeringTests(); // test for ArgumentNullException Assert.Throws<ArgumentNullException>(() => element.AddHandler<RoutedEventArgs>(null, TestDelegate2)); Assert.Throws<ArgumentNullException>(() => element.AddHandler(testRoutedEvent, null)); Assert.Throws<ArgumentNullException>(() => element.RemoveHandler<RoutedEventArgs>(null, TestDelegate2)); Assert.Throws<ArgumentNullException>(() => element.RemoveHandler(testRoutedEvent, null)); // test that adding and removing 2 times the same element does not throws any exceptions element.AddHandler(testRoutedEvent, TestDelegate2); element.AddHandler(testRoutedEvent, TestDelegate2); element.RemoveHandler(testRoutedEvent, TestDelegate2); element.RemoveHandler(testRoutedEvent, TestDelegate2); element.RemoveHandler(testRoutedEvent, TestDelegate2); }
public void TestDependencyProperties() { ResetElementState(); var newElement = new UIElementLayeringTests(); // check dependency property default values Assert.IsTrue(newElement.ForceNextMeasure); Assert.IsTrue(newElement.ForceNextArrange); Assert.IsTrue(newElement.IsEnabled); Assert.AreEqual(1f, newElement.Opacity); Assert.AreEqual(Visibility.Visible, newElement.Visibility); Assert.AreEqual(0f, newElement.DependencyProperties.Get(DefaultWidthPropertyKey)); Assert.AreEqual(0f, newElement.DependencyProperties.Get(DefaultHeightPropertyKey)); Assert.AreEqual(0f, newElement.DependencyProperties.Get(DefaultDepthPropertyKey)); Assert.AreEqual(float.NaN, newElement.Height); Assert.AreEqual(float.NaN, newElement.Width); Assert.AreEqual(float.NaN, newElement.Depth); Assert.AreEqual(0f, newElement.MinimumHeight); Assert.AreEqual(0f, newElement.MinimumWidth); Assert.AreEqual(0f, newElement.MinimumDepth); Assert.AreEqual(float.PositiveInfinity, newElement.MaximumHeight); Assert.AreEqual(float.PositiveInfinity, newElement.MaximumWidth); Assert.AreEqual(float.PositiveInfinity, newElement.MaximumDepth); Assert.AreEqual(HorizontalAlignment.Stretch, newElement.HorizontalAlignment); Assert.AreEqual(VerticalAlignment.Stretch, newElement.VerticalAlignment); Assert.AreEqual(DepthAlignment.Center, newElement.DepthAlignment); Assert.AreEqual(null, newElement.DependencyProperties.Get(NamePropertyKey)); Assert.AreEqual(Thickness.UniformCuboid(0), newElement.Margin); Assert.AreEqual(Matrix.Identity, newElement.LocalMatrix); ///////////////////////////////////////// // check dependency property validators // opacity validator Opacity = -1; Assert.AreEqual(0f, Opacity); Opacity = 2; Assert.AreEqual(1f, Opacity); Opacity = 0.5f; Assert.AreEqual(0.5f, Opacity); // default sizes Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultWidthPropertyKey, -1f)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultWidthPropertyKey, float.NaN)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultWidthPropertyKey, float.PositiveInfinity)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultHeightPropertyKey, -1f)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultHeightPropertyKey, float.NaN)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultHeightPropertyKey, float.PositiveInfinity)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultDepthPropertyKey, -1f)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultDepthPropertyKey, float.NaN)); Assert.Throws<ArgumentOutOfRangeException>(() => DependencyProperties.Set(DefaultDepthPropertyKey, float.PositiveInfinity)); // sizes Assert.Throws<ArgumentOutOfRangeException>(() => Width = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => Width = float.PositiveInfinity); Assert.Throws<ArgumentOutOfRangeException>(() => Height = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => Height = float.PositiveInfinity); Assert.Throws<ArgumentOutOfRangeException>(() => Depth = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => Depth = float.PositiveInfinity); // minimum sizes Assert.Throws<ArgumentOutOfRangeException>(() => MinimumWidth = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumWidth = float.NaN); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumWidth = float.PositiveInfinity); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumHeight = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumHeight = float.NaN); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumHeight = float.PositiveInfinity); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumDepth = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumDepth = float.NaN); Assert.Throws<ArgumentOutOfRangeException>(() => MinimumDepth = float.PositiveInfinity); // maximum sizes Assert.Throws<ArgumentOutOfRangeException>(() => MaximumWidth = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MaximumWidth = float.NaN); Assert.Throws<ArgumentOutOfRangeException>(() => MaximumHeight = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MaximumHeight = float.NaN); Assert.Throws<ArgumentOutOfRangeException>(() => MaximumDepth = -1f); Assert.Throws<ArgumentOutOfRangeException>(() => MaximumDepth = float.NaN); }
public void TestPropagateEvent() { // create a hierarchy of UIElements // (00) // | // (10) // / \ // (20) (21) // | / \ // (30) (31) (32) var element00 = new ContentControlTest(); var element10 = new StackPanel(); var element20 = new ContentControlTest(); var element21 = new StackPanel(); var element30 = new UIElementLayeringTests(); var element31 = new UIElementLayeringTests(); var element32 = new UIElementLayeringTests(); element00.Content = element10; element10.Children.Add(element20); element10.Children.Add(element21); element20.Content = element30; element21.Children.Add(element31); element21.Children.Add(element32); var elements = new List<UIElement> { element00, element10, element20, element21, element30, element31, element32 }; // create routed events var tunnelingEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>("TestTuneling", RoutingStrategy.Tunnel, typeof(UIElementLayeringTests)); var bubblingEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>("TestBubbling", RoutingStrategy.Bubble, typeof(UIElementLayeringTests)); var directEvent = EventManager.RegisterRoutedEvent<RoutedEventArgs>("TestDirect", RoutingStrategy.Direct, typeof(UIElementLayeringTests)); // test propagation direction, propagation bounds and sender values foreach (var uiElement in elements) { uiElement.AddHandler(tunnelingEvent, TestAddSenderToList); uiElement.AddHandler(bubblingEvent, TestAddSenderToList); uiElement.AddHandler(directEvent, TestAddSenderToList); } // tunneling test 1 senderList.Clear(); element20.RaiseEvent(new RoutedEventArgs(tunnelingEvent)); Assert.AreEqual(3, senderList.Count); Assert.AreEqual(element00, senderList[0]); Assert.AreEqual(element10, senderList[1]); Assert.AreEqual(element20, senderList[2]); // tunneling test 2 senderList.Clear(); element31.RaiseEvent(new RoutedEventArgs(tunnelingEvent)); Assert.AreEqual(4, senderList.Count); Assert.AreEqual(element00, senderList[0]); Assert.AreEqual(element10, senderList[1]); Assert.AreEqual(element21, senderList[2]); Assert.AreEqual(element31, senderList[3]); // direct test senderList.Clear(); element10.RaiseEvent(new RoutedEventArgs(directEvent)); Assert.AreEqual(1, senderList.Count); Assert.AreEqual(element10, senderList[0]); // tunneling test 1 senderList.Clear(); element30.RaiseEvent(new RoutedEventArgs(bubblingEvent)); Assert.AreEqual(4, senderList.Count); Assert.AreEqual(element30, senderList[0]); Assert.AreEqual(element20, senderList[1]); Assert.AreEqual(element10, senderList[2]); Assert.AreEqual(element00, senderList[3]); // tunneling test 2 senderList.Clear(); element20.RaiseEvent(new RoutedEventArgs(bubblingEvent)); Assert.AreEqual(3, senderList.Count); Assert.AreEqual(element20, senderList[0]); Assert.AreEqual(element10, senderList[1]); Assert.AreEqual(element00, senderList[2]); // test with another type of handler var eventMyTestHandler = EventManager.RegisterRoutedEvent<MyTestRoutedEventArgs>("TestMyTestHandler", RoutingStrategy.Direct, typeof(UIElementLayeringTests)); AddHandler(eventMyTestHandler, TestMyTestHandler); RaiseEvent(new MyTestRoutedEventArgs(eventMyTestHandler)); Assert.AreEqual(true, testMyTestHandlerCalled); // test Handled and EventHandledToo foreach (var uiElement in elements) { uiElement.RemoveHandler(bubblingEvent, TestAddSenderToList); uiElement.AddHandler(bubblingEvent, TestHandledHandler); } senderList.Clear(); element00.AddHandler(bubblingEvent, TestEventHandledHandler, true); element32.RaiseEvent(new RoutedEventArgs(bubblingEvent)); Assert.AreEqual(1, senderList.Count); Assert.AreEqual(element32, senderList[0]); Assert.AreEqual(true, testEventHandledTooCalled); // test class handlers basic working foreach (var uiElement in elements) uiElement.RemoveHandler(bubblingEvent, TestHandledHandler); EventManager.RegisterClassHandler(typeof(ContentControl), bubblingEvent, TestAddSenderToClassHandlerList); element30.RaiseEvent(new RoutedEventArgs(bubblingEvent)); Assert.AreEqual(2, classHandlerSenderList.Count); Assert.AreEqual(element20, classHandlerSenderList[0]); Assert.AreEqual(element00, classHandlerSenderList[1]); // test that class handlers are called before instance handlers + test handledEventToo for class handlers senderList.Clear(); classHandlerSenderList.Clear(); EventManager.RegisterClassHandler(typeof(ContentControl), bubblingEvent, TestClassHandlerHandled); EventManager.RegisterClassHandler(typeof(StackPanel), bubblingEvent, TestClassHandlerEventHandled, true); foreach (var uiElement in elements) uiElement.AddHandler(bubblingEvent, TestAddSenderToList); element20.RaiseEvent(new RoutedEventArgs(bubblingEvent)); Assert.AreEqual(1, classHandlerSenderList.Count); Assert.AreEqual(element20, classHandlerSenderList[0]); Assert.AreEqual(0, senderList.Count); Assert.AreEqual(true, testClassHandlerEventHandledTooCalled); }