Browse Source

Add more tests

Max Katz 4 years ago
parent
commit
9e2d104739

+ 105 - 5
tests/Avalonia.Controls.UnitTests/ContextMenuTests.cs

@@ -6,7 +6,6 @@ using Avalonia.Markup.Xaml;
 using Avalonia.Platform;
 using Avalonia.Rendering;
 using Avalonia.UnitTests;
-using Avalonia.VisualTree;
 
 using Moq;
 using Xunit;
@@ -18,6 +17,107 @@ namespace Avalonia.Controls.UnitTests
         private Mock<IPopupImpl> popupImpl;
         private MouseTestHelper _mouse = new MouseTestHelper();
 
+        [Fact]
+        public void ContextRequested_Opens_ContextMenu()
+        {
+            using (Application())
+            {
+                var sut = new ContextMenu();
+                var target = new Panel
+                {
+                    ContextMenu = sut
+                };
+
+                var window = new Window { Content = target };
+                window.ApplyTemplate();
+                window.Presenter.ApplyTemplate();
+
+                int openedCount = 0;
+
+                sut.MenuOpened += (sender, args) =>
+                {
+                    openedCount++;
+                };
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(sut.IsOpen);
+                Assert.Equal(1, openedCount);
+            }
+        }
+
+        [Fact]
+        public void ContextMenu_Is_Opened_When_ContextFlyout_Is_Also_Set()
+        {
+            // We have this test for backwards compatability with the code that already sets custom ContextMenu.
+            using (Application())
+            {
+                var sut = new ContextMenu();
+                var flyout = new Flyout();
+                var target = new Panel
+                {
+                    ContextMenu = sut,
+                    ContextFlyout = flyout
+                };
+
+                var window = new Window { Content = target };
+                window.ApplyTemplate();
+                window.Presenter.ApplyTemplate();
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(sut.IsOpen);
+                Assert.False(flyout.IsOpen);
+            }
+        }
+
+        [Fact]
+        public void KeyUp_Raised_On_Target_Opens_ContextFlyout()
+        {
+            using (Application())
+            {
+                var sut = new ContextMenu();
+                var target = new Panel
+                {
+                    ContextMenu = sut
+                };
+                var contextRequestedCount = 0;
+                target.AddHandler(Control.ContextRequestedEvent, (s, a) => contextRequestedCount++, Interactivity.RoutingStrategies.Tunnel);
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                target.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
+
+                Assert.True(sut.IsOpen);
+                Assert.Equal(1, contextRequestedCount);
+            }
+        }
+
+        [Fact]
+        public void KeyUp_Raised_On_Flyout_Closes_Opened_ContextMenu()
+        {
+            using (Application())
+            {
+                var sut = new ContextMenu();
+                var target = new Panel
+                {
+                    ContextMenu = sut
+                };
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(sut.IsOpen);
+
+                sut.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
+
+                Assert.False(sut.IsOpen);
+            }
+        }
+
         [Fact]
         public void Opening_Raises_Single_Opened_Event()
         {
@@ -225,8 +325,7 @@ namespace Avalonia.Controls.UnitTests
                 };
 
                 var window = PreparedWindow(target);
-                window.ApplyTemplate();
-                window.Presenter.ApplyTemplate();
+                window.Show();
                 var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
 
                 _mouse.Click(target, MouseButton.Right);
@@ -257,8 +356,8 @@ namespace Avalonia.Controls.UnitTests
                 };
 
                 var window = PreparedWindow(target);
-                window.ApplyTemplate();
-                window.Presenter.ApplyTemplate();
+                window.Show();
+
                 var overlay = LightDismissOverlayLayer.GetLightDismissOverlayLayer(window);
 
                 _mouse.Click(target, MouseButton.Right);
@@ -475,6 +574,7 @@ namespace Avalonia.Controls.UnitTests
 
             var w = new Window(windowImpl.Object) { Content = content };
             w.ApplyTemplate();
+            w.Presenter.ApplyTemplate();
             return w;
         }
 

+ 103 - 0
tests/Avalonia.Controls.UnitTests/FlyoutTests.cs

@@ -329,6 +329,109 @@ namespace Avalonia.Controls.UnitTests
             }
         }
 
+        [Fact]
+        public void ContextRequested_Opens_ContextFlyout()
+        {
+            using (CreateServicesWithFocus())
+            {
+                var flyout = new Flyout();
+                var target = new Panel
+                {
+                    ContextFlyout = flyout
+                };
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                int openedCount = 0;
+
+                flyout.Opened += (sender, args) =>
+                {
+                    openedCount++;
+                };
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(flyout.IsOpen);
+                Assert.Equal(1, openedCount);
+            }
+        }
+
+        [Fact]
+        public void KeyUp_Raised_On_Target_Opens_ContextFlyout()
+        {
+            using (CreateServicesWithFocus())
+            {
+                var flyout = new Flyout();
+                var target = new Panel
+                {
+                    ContextFlyout = flyout
+                };
+                var contextRequestedCount = 0;
+                target.AddHandler(Control.ContextRequestedEvent, (s, a) => contextRequestedCount++, Interactivity.RoutingStrategies.Tunnel);
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                target.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
+
+                Assert.True(flyout.IsOpen);
+                Assert.Equal(1, contextRequestedCount);
+            }
+        }
+
+        [Fact]
+        public void KeyUp_Raised_On_Target_Closes_Opened_ContextFlyout()
+        {
+            using (CreateServicesWithFocus())
+            {
+                var flyout = new Flyout();
+                var target = new Panel
+                {
+                    ContextFlyout = flyout
+                };
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(flyout.IsOpen);
+
+                target.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
+
+                Assert.False(flyout.IsOpen);
+            }
+        }
+
+        [Fact]
+        public void KeyUp_Raised_On_Flyout_Closes_Opened_ContextFlyout()
+        {
+            using (CreateServicesWithFocus())
+            {
+                var flyoutContent = new Button();
+                var flyout = new Flyout()
+                {
+                    Content = flyoutContent
+                };
+                var target = new Panel
+                {
+                    ContextFlyout = flyout
+                };
+
+                var window = PreparedWindow(target);
+                window.Show();
+
+                target.RaiseEvent(new ContextRequestedEventArgs());
+
+                Assert.True(flyout.IsOpen);
+
+                flyoutContent.RaiseEvent(new KeyEventArgs { RoutedEvent = InputElement.KeyUpEvent, Key = Key.Apps, Source = window });
+
+                Assert.False(flyout.IsOpen);
+            }
+        }
+
         [Fact]
         public void ContextFlyout_Can_Be_Set_In_Styles()
         {