Prechádzať zdrojové kódy

Added integration tests for window transparency.

Failing on macOS, passing on win32.
Steven Kirk 2 rokov pred
rodič
commit
73cede0bf5

+ 30 - 24
samples/IntegrationTestApp/MainWindow.axaml

@@ -120,30 +120,36 @@
       </TabItem>
       
       <TabItem Header="Window">
-        <StackPanel>
-          <TextBox Name="ShowWindowSize" Watermark="Window Size"/>
-          <ComboBox Name="ShowWindowMode" SelectedIndex="0">
-            <ComboBoxItem>NonOwned</ComboBoxItem>
-            <ComboBoxItem>Owned</ComboBoxItem>
-            <ComboBoxItem>Modal</ComboBoxItem>
-          </ComboBox>
-          <ComboBox Name="ShowWindowLocation" SelectedIndex="0">
-            <ComboBoxItem>Manual</ComboBoxItem>
-            <ComboBoxItem>CenterScreen</ComboBoxItem>
-            <ComboBoxItem>CenterOwner</ComboBoxItem>
-          </ComboBox>
-          <ComboBox Name="ShowWindowState" SelectedIndex="0">
-            <ComboBoxItem Name="ShowWindowStateNormal">Normal</ComboBoxItem>
-            <ComboBoxItem Name="ShowWindowStateMinimized">Minimized</ComboBoxItem>
-            <ComboBoxItem Name="ShowWindowStateMaximized">Maximized</ComboBoxItem>
-            <ComboBoxItem Name="ShowWindowStateFullScreen">FullScreen</ComboBoxItem>
-          </ComboBox>
-          <Button Name="ShowWindow">Show Window</Button>
-          <Button Name="SendToBack">Send to Back</Button>
-          <Button Name="EnterFullscreen">Enter Fullscreen</Button>
-          <Button Name="ExitFullscreen">Exit Fullscreen</Button>
-          <Button Name="RestoreAll">Restore All</Button>
-        </StackPanel>
+        <Grid ColumnDefinitions="*,8,*">
+          <StackPanel Grid.Column="0">
+            <TextBox Name="ShowWindowSize" Watermark="Window Size"/>
+            <ComboBox Name="ShowWindowMode" SelectedIndex="0">
+              <ComboBoxItem>NonOwned</ComboBoxItem>
+              <ComboBoxItem>Owned</ComboBoxItem>
+              <ComboBoxItem>Modal</ComboBoxItem>
+            </ComboBox>
+            <ComboBox Name="ShowWindowLocation" SelectedIndex="0">
+              <ComboBoxItem>Manual</ComboBoxItem>
+              <ComboBoxItem>CenterScreen</ComboBoxItem>
+              <ComboBoxItem>CenterOwner</ComboBoxItem>
+            </ComboBox>
+            <ComboBox Name="ShowWindowState" SelectedIndex="0">
+              <ComboBoxItem Name="ShowWindowStateNormal">Normal</ComboBoxItem>
+              <ComboBoxItem Name="ShowWindowStateMinimized">Minimized</ComboBoxItem>
+              <ComboBoxItem Name="ShowWindowStateMaximized">Maximized</ComboBoxItem>
+              <ComboBoxItem Name="ShowWindowStateFullScreen">FullScreen</ComboBoxItem>
+            </ComboBox>
+            <Button Name="ShowWindow">Show Window</Button>
+            <Button Name="SendToBack">Send to Back</Button>
+            <Button Name="EnterFullscreen">Enter Fullscreen</Button>
+            <Button Name="ExitFullscreen">Exit Fullscreen</Button>
+            <Button Name="RestoreAll">Restore All</Button>
+          </StackPanel>
+          <StackPanel Grid.Column="2">
+            <Button Name="ShowTransparentWindow">Transparent Window</Button>
+            <Button Name="ShowTransparentPopup">Transparent Popup</Button>
+          </StackPanel>
+        </Grid>
       </TabItem>
     </TabControl>
   </DockPanel>

+ 91 - 0
samples/IntegrationTestApp/MainWindow.axaml.cs

@@ -7,9 +7,13 @@ using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Input;
 using Avalonia.Interactivity;
+using Avalonia.Media;
 using Avalonia.Markup.Xaml;
 using Avalonia.VisualTree;
 using Microsoft.CodeAnalysis;
+using Avalonia.Controls.Primitives;
+using Avalonia.Threading;
+using Avalonia.Controls.Primitives.PopupPositioning;
 
 namespace IntegrationTestApp
 {
@@ -103,6 +107,89 @@ namespace IntegrationTestApp
             }
         }
 
+        private void ShowTransparentWindow()
+        {
+            // Show a background window to make sure the color behind the transparent window is
+            // a known color (green).
+            var backgroundWindow = new Window
+            {
+                Title = "Transparent Window Background",
+                Name = "TransparentWindowBackground",
+                Width = 300,
+                Height = 300,
+                Background = Brushes.Green,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner,
+            };
+
+            // This is the transparent window with a red circle.
+            var window = new Window
+            {
+                Title = "Transparent Window",
+                Name = "TransparentWindow",
+                SystemDecorations = SystemDecorations.None,
+                Background = Brushes.Transparent,
+                TransparencyLevelHint = WindowTransparencyLevel.Transparent,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner,
+                Width = 200,
+                Height = 200,
+                Content = new Border
+                {
+                    Background = Brushes.Red,
+                    CornerRadius = new CornerRadius(100),
+                }
+            };
+
+            window.PointerPressed += (_, _) =>
+            {
+                window.Close();
+                backgroundWindow.Close();
+            };
+
+            backgroundWindow.Show(this);
+            window.Show(backgroundWindow);
+        }
+
+        private void ShowTransparentPopup()
+        {
+            var popup = new Popup
+            {
+                WindowManagerAddShadowHint = false,
+                PlacementMode = PlacementMode.AnchorAndGravity,
+                PlacementAnchor = PopupAnchor.Top,
+                PlacementGravity = PopupGravity.Bottom,
+                Width= 200,
+                Height= 200,
+                Child = new Border
+                {
+                    Background = Brushes.Red,
+                    CornerRadius = new CornerRadius(100),
+                }
+            };
+
+            // Show a background window to make sure the color behind the transparent window is
+            // a known color (green).
+            var backgroundWindow = new Window
+            {
+                Title = "Transparent Popup Background",
+                Name = "TransparentPopupBackground",
+                Width = 200,
+                Height = 200,
+                Background = Brushes.Green,
+                WindowStartupLocation = WindowStartupLocation.CenterOwner,
+                Content = new Border
+                {
+                    Name = "PopupContainer",
+                    Child = popup,
+                    [AutomationProperties.AccessibilityViewProperty] = AccessibilityView.Content,
+                }
+            };
+
+            backgroundWindow.PointerPressed += (_, _) => backgroundWindow.Close();
+            backgroundWindow.Show(this);
+
+            popup.Open();
+        }
+
         private void SendToBack()
         {
             var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;
@@ -175,6 +262,10 @@ namespace IntegrationTestApp
                 this.Get<ListBox>("BasicListBox").SelectedIndex = -1;
             if (source?.Name == "MenuClickedMenuItemReset")
                 this.Get<TextBlock>("ClickedMenuItem").Text = "None";
+            if (source?.Name == "ShowTransparentWindow")
+                ShowTransparentWindow();
+            if (source?.Name == "ShowTransparentPopup")
+                ShowTransparentPopup();
             if (source?.Name == "ShowWindow")
                 ShowWindow();
             if (source?.Name == "SendToBack")

+ 1 - 0
tests/Avalonia.IntegrationTests.Appium/Avalonia.IntegrationTests.Appium.csproj

@@ -16,4 +16,5 @@
 
   <Import Project="..\..\build\XUnit.props" />
   <Import Project="..\..\build\Rx.props" />
+  <Import Project="..\..\build\ImageSharp.props" />
 </Project>

+ 44 - 1
tests/Avalonia.IntegrationTests.Appium/WindowTests.cs

@@ -1,11 +1,14 @@
 using System;
+using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
 using System.Threading;
 using Avalonia.Controls;
+using Avalonia.Media.Imaging;
 using OpenQA.Selenium;
 using OpenQA.Selenium.Appium;
 using OpenQA.Selenium.Interactions;
+using SixLabors.ImageSharp.PixelFormats;
 using Xunit;
 using Xunit.Sdk;
 
@@ -141,7 +144,6 @@ namespace Avalonia.IntegrationTests.Appium
             }
         }
 
-
         [Theory]
         [InlineData(ShowWindowMode.NonOwned)]
         [InlineData(ShowWindowMode.Owned)]
@@ -187,6 +189,47 @@ namespace Avalonia.IntegrationTests.Appium
             }
         }
 
+        [Fact]
+        public void TransparentWindow()
+        {
+            var showTransparentWindow = _session.FindElementByAccessibilityId("ShowTransparentWindow");
+            showTransparentWindow.Click();
+            Thread.Sleep(1000);
+
+            var window = _session.FindElementByAccessibilityId("TransparentWindow");
+            var screenshot = window.GetScreenshot();
+
+            window.Click();
+
+            var img = SixLabors.ImageSharp.Image.Load<Rgba32>(screenshot.AsByteArray);
+            var topLeftColor = img[1, 1];
+            var centerColor = img[img.Width / 2, img.Height / 2];
+
+            Assert.Equal(new Rgba32(0, 128, 0), topLeftColor);
+            Assert.Equal(new Rgba32(255, 0, 0), centerColor);
+        }
+
+        [Fact]
+        public void TransparentPopup()
+        {
+            var showTransparentWindow = _session.FindElementByAccessibilityId("ShowTransparentPopup");
+            showTransparentWindow.Click();
+            Thread.Sleep(1000);
+
+            var window = _session.FindElementByAccessibilityId("TransparentPopupBackground");
+            var container = window.FindElementByAccessibilityId("PopupContainer");
+            var screenshot = container.GetScreenshot();
+
+            window.Click();
+
+            var img = SixLabors.ImageSharp.Image.Load<Rgba32>(screenshot.AsByteArray);
+            var topLeftColor = img[1, 1];
+            var centerColor = img[img.Width / 2, img.Height / 2];
+
+            Assert.Equal(new Rgba32(0, 128, 0), topLeftColor);
+            Assert.Equal(new Rgba32(255, 0, 0), centerColor);
+        }
+
         public static TheoryData<Size?, ShowWindowMode, WindowStartupLocation> StartupLocationData()
         {
             var sizes = new Size?[] { null, new Size(400, 300) };