Browse Source

Display window order in integration test app.

The previous method of relying on the order of windows returned by appium to determine window z-order proved not to be reliable. Instead use p/invoke to read `NSWindow.orderedIndex` on a timer and display it in a text box in the `ShowWindowtest` window.

This commit doesn't update the tests to use this new info though.
Steven Kirk 3 years ago
parent
commit
0cc0443d34

+ 27 - 0
samples/IntegrationTestApp/MacOSIntegration.cs

@@ -0,0 +1,27 @@
+using System;
+using System.Runtime.InteropServices;
+using Avalonia.Controls;
+
+namespace IntegrationTestApp
+{
+    public static class MacOSIntegration
+    {
+        [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "sel_registerName")]
+        private static extern IntPtr GetHandle(string name);
+        
+        [DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
+        private static extern long Int64_objc_msgSend(IntPtr receiver, IntPtr selector);
+
+        private static readonly IntPtr s_orderedIndexSelector;
+
+        static MacOSIntegration()
+        {
+            s_orderedIndexSelector = GetHandle("orderedIndex");;
+        }
+        
+        public static long GetOrderedIndex(Window window)
+        {
+            return Int64_objc_msgSend(window.PlatformImpl!.Handle.Handle, s_orderedIndexSelector);
+        }
+    }
+}

+ 6 - 2
samples/IntegrationTestApp/ShowWindowTest.axaml

@@ -3,7 +3,7 @@
         x:Class="IntegrationTestApp.ShowWindowTest"
         Name="SecondaryWindow"
         Title="Show Window Test">
-  <Grid ColumnDefinitions="Auto,Auto" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto">
+  <Grid ColumnDefinitions="Auto,Auto" RowDefinitions="Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto">
     <Label Grid.Column="0" Grid.Row="1">Client Size</Label>
     <TextBox Name="ClientSize" Grid.Column="1" Grid.Row="1" IsReadOnly="True"
              Text="{Binding ClientSize, Mode=OneWay}"/>
@@ -31,6 +31,10 @@
       <ComboBoxItem>Maximized</ComboBoxItem>
       <ComboBoxItem>Fullscreen</ComboBoxItem>
     </ComboBox>
-    <Button Name="HideButton" Grid.Row="8" Command="{Binding $parent[Window].Hide}">Hide</Button>
+
+    <Label Grid.Column="0" Grid.Row="8">Order (mac)</Label>
+    <TextBox Name="Order" Grid.Column="1" Grid.Row="8" IsReadOnly="True"/>
+
+    <Button Name="HideButton" Grid.Row="9" Command="{Binding $parent[Window].Hide}">Hide</Button>
   </Grid>
 </Window>

+ 25 - 3
samples/IntegrationTestApp/ShowWindowTest.axaml.cs

@@ -1,21 +1,32 @@
 using System;
+using System.Runtime.InteropServices;
 using Avalonia;
 using Avalonia.Controls;
-using Avalonia.Interactivity;
 using Avalonia.Markup.Xaml;
-using Avalonia.Rendering;
+using Avalonia.Threading;
 
 namespace IntegrationTestApp
 {
     public class ShowWindowTest : Window
     {
+        private readonly DispatcherTimer? _timer;
+        private readonly TextBox? _orderTextBox;
+        
         public ShowWindowTest()
         {
             InitializeComponent();
             DataContext = this;
             PositionChanged += (s, e) => this.GetControl<TextBox>("Position").Text = $"{Position}";
-        }
 
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
+            {
+                _orderTextBox = this.GetControl<TextBox>("Order");
+                _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(250) };
+                _timer.Tick += TimerOnTick;
+                _timer.Start();
+            }
+        }
+        
         private void InitializeComponent()
         {
             AvaloniaXamlLoader.Load(this);
@@ -36,5 +47,16 @@ namespace IntegrationTestApp
                 ownerRect.Text = $"{owner.Position}, {PixelSize.FromSize(owner.FrameSize!.Value, scaling)}";
             }
         }
+
+        protected override void OnClosed(EventArgs e)
+        {
+            base.OnClosed(e);
+            _timer?.Stop();
+        }
+
+        private void TimerOnTick(object? sender, EventArgs e)
+        {
+            _orderTextBox!.Text = MacOSIntegration.GetOrderedIndex(this).ToString();
+        }
     }
 }