Skip to content

Commit

Permalink
Merge pull request #8232 from AvaloniaUI/feature/window-integration-t…
Browse files Browse the repository at this point in the history
…ests

Feature/window integration tests
  • Loading branch information
grokys authored Jun 28, 2022
2 parents c10f144 + 28f5e4b commit b74a2bd
Show file tree
Hide file tree
Showing 18 changed files with 659 additions and 52 deletions.
20 changes: 20 additions & 0 deletions samples/IntegrationTestApp/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="IntegrationTestApp.MainWindow"
Name="MainWindow"
Title="IntegrationTestApp">
<NativeMenu.Menu>
<NativeMenu>
Expand Down Expand Up @@ -94,6 +95,25 @@
</StackPanel>
</DockPanel>
</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>
<Button Name="ShowWindow">Show Window</Button>
<Button Name="SendToBack">Send to Back</Button>
<Button Name="ExitFullscreen">Exit Fullscreen</Button>
</StackPanel>
</TabItem>
</TabControl>
</DockPanel>
</Window>
53 changes: 53 additions & 0 deletions samples/IntegrationTestApp/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.VisualTree;

namespace IntegrationTestApp
{
Expand Down Expand Up @@ -46,6 +48,51 @@ private void InitializeViewMenu()
}
}

private void ShowWindow()
{
var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
var owner = (Window)this.GetVisualRoot()!;

var window = new ShowWindowTest
{
WindowStartupLocation = (WindowStartupLocation)locationComboBox.SelectedIndex,
};

if (size.HasValue)
{
window.Width = size.Value.Width;
window.Height = size.Value.Height;
}

sizeTextBox.Text = string.Empty;

switch (modeComboBox.SelectedIndex)
{
case 0:
window.Show();
break;
case 1:
window.Show(owner);
break;
case 2:
window.ShowDialog(owner);
break;
}
}

private void SendToBack()
{
var lifetime = (ClassicDesktopStyleApplicationLifetime)Application.Current!.ApplicationLifetime!;

foreach (var window in lifetime.Windows)
{
window.Activate();
}
}

private void MenuClicked(object? sender, RoutedEventArgs e)
{
var clickedMenuItemTextBlock = this.FindControl<TextBlock>("ClickedMenuItem");
Expand All @@ -64,6 +111,12 @@ private void OnButtonClick(object? sender, RoutedEventArgs e)
this.FindControl<ListBox>("BasicListBox").SelectedIndex = -1;
if (source?.Name == "MenuClickedMenuItemReset")
this.FindControl<TextBlock>("ClickedMenuItem").Text = "None";
if (source?.Name == "ShowWindow")
ShowWindow();
if (source?.Name == "SendToBack")
SendToBack();
if (source?.Name == "ExitFullscreen")
WindowState = WindowState.Normal;
}
}
}
25 changes: 25 additions & 0 deletions samples/IntegrationTestApp/ShowWindowTest.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="IntegrationTestApp.ShowWindowTest"
Name="SecondaryWindow"
Title="Show Window Test">
<Grid ColumnDefinitions="Auto,Auto" RowDefinitions="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"/>

<Label Grid.Column="0" Grid.Row="2">Frame Size</Label>
<TextBox Name="FrameSize" Grid.Column="1" Grid.Row="2" IsReadOnly="True"/>

<Label Grid.Column="0" Grid.Row="3">Position</Label>
<TextBox Name="Position" Grid.Column="1" Grid.Row="3" IsReadOnly="True"/>

<Label Grid.Column="0" Grid.Row="4">Owner Rect</Label>
<TextBox Name="OwnerRect" Grid.Column="1" Grid.Row="4" IsReadOnly="True"/>

<Label Grid.Column="0" Grid.Row="5">Screen Rect</Label>
<TextBox Name="ScreenRect" Grid.Column="1" Grid.Row="5" IsReadOnly="True"/>

<Label Grid.Column="0" Grid.Row="6">Scaling</Label>
<TextBox Name="Scaling" Grid.Column="1" Grid.Row="6" IsReadOnly="True"/>
</Grid>
</Window>
38 changes: 38 additions & 0 deletions samples/IntegrationTestApp/ShowWindowTest.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Avalonia.Rendering;

namespace IntegrationTestApp
{
public class ShowWindowTest : Window
{
public ShowWindowTest()
{
InitializeComponent();
}

private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}

protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
this.GetControl<TextBox>("ClientSize").Text = $"{Width}, {Height}";
this.GetControl<TextBox>("FrameSize").Text = $"{FrameSize}";
this.GetControl<TextBox>("Position").Text = $"{Position}";
this.GetControl<TextBox>("ScreenRect").Text = $"{Screens.ScreenFromVisual(this)?.WorkingArea}";
this.GetControl<TextBox>("Scaling").Text = $"{PlatformImpl?.DesktopScaling}";

if (Owner is not null)
{
var ownerRect = this.GetControl<TextBox>("OwnerRect");
var owner = (Window)Owner;
ownerRect.Text = $"{owner.Position}, {owner.FrameSize}";
}
}
}
}
6 changes: 5 additions & 1 deletion src/Avalonia.Controls/TopLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,11 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
/// Raises the <see cref="Opened"/> event.
/// </summary>
/// <param name="e">The event args.</param>
protected virtual void OnOpened(EventArgs e) => Opened?.Invoke(this, e);
protected virtual void OnOpened(EventArgs e)
{
FrameSize = PlatformImpl?.FrameSize;
Opened?.Invoke(this, e);
}

/// <summary>
/// Raises the <see cref="Closed"/> event.
Expand Down
48 changes: 24 additions & 24 deletions src/Avalonia.Controls/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,10 @@ Owner is Window ownerWindow &&

var scaling = owner?.DesktopScaling ?? PlatformImpl?.DesktopScaling ?? 1;

// TODO: We really need non-client size here.
var rect = new PixelRect(
PixelPoint.Origin,
PixelSize.FromSize(ClientSize, scaling));
// Use frame size, falling back to client size if the platform can't give it to us.
var rect = FrameSize.HasValue ?
new PixelRect(PixelSize.FromSize(FrameSize.Value, scaling)) :
new PixelRect(PixelSize.FromSize(ClientSize, scaling));

if (startupLocation == WindowStartupLocation.CenterScreen)
{
Expand Down Expand Up @@ -991,28 +991,28 @@ protected sealed override void HandleClosed()
/// <inheritdoc/>
protected sealed override void HandleResized(Size clientSize, PlatformResizeReason reason)
{
if (ClientSize == clientSize)
return;

var sizeToContent = SizeToContent;

// If auto-sizing is enabled, and the resize came from a user resize (or the reason was
// unspecified) then turn off auto-resizing for any window dimension that is not equal
// to the requested size.
if (sizeToContent != SizeToContent.Manual &&
CanResize &&
reason == PlatformResizeReason.Unspecified ||
reason == PlatformResizeReason.User)
if (ClientSize != clientSize || double.IsNaN(Width) || double.IsNaN(Height))
{
if (clientSize.Width != ClientSize.Width)
sizeToContent &= ~SizeToContent.Width;
if (clientSize.Height != ClientSize.Height)
sizeToContent &= ~SizeToContent.Height;
SizeToContent = sizeToContent;
}
var sizeToContent = SizeToContent;

// If auto-sizing is enabled, and the resize came from a user resize (or the reason was
// unspecified) then turn off auto-resizing for any window dimension that is not equal
// to the requested size.
if (sizeToContent != SizeToContent.Manual &&
CanResize &&
reason == PlatformResizeReason.Unspecified ||
reason == PlatformResizeReason.User)
{
if (clientSize.Width != ClientSize.Width)
sizeToContent &= ~SizeToContent.Width;
if (clientSize.Height != ClientSize.Height)
sizeToContent &= ~SizeToContent.Height;
SizeToContent = sizeToContent;
}

Width = clientSize.Width;
Height = clientSize.Height;
Width = clientSize.Width;
Height = clientSize.Height;
}

base.HandleResized(clientSize, reason);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Avalonia.Controls/WindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ protected override void HandleClosed()
/// <param name="reason">The reason for the resize.</param>
protected override void HandleResized(Size clientSize, PlatformResizeReason reason)
{
ClientSize = clientSize;
FrameSize = PlatformImpl?.FrameSize;
LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize);

if (ClientSize != clientSize)
{
ClientSize = clientSize;
LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize);
}
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/Avalonia.Controls.UnitTests/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,31 @@ public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window
}
}

[Fact]
public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var child = new Canvas
{
Width = 400,
Height = 800,
};

var target = new Window()
{
SizeToContent = SizeToContent.Manual,
Content = child
};

Show(target);

// Values come from MockWindowingPlatform defaults.
Assert.Equal(800, target.Width);
Assert.Equal(600, target.Height);
}
}

[Fact]
public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight()
{
Expand All @@ -712,6 +737,8 @@ public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAn
Content = child
};

target.GetObservable(Window.WidthProperty).Subscribe(x => { });

Show(target);

Assert.Equal(400, target.Width);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Avalonia.Controls\Avalonia.Controls.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="4.3.1" />
<PackageReference Include="Xunit.Extensions.Ordering" Version="1.4.5" />
Expand Down
2 changes: 1 addition & 1 deletion tests/Avalonia.IntegrationTests.Appium/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void ButtonWithTextBlock()
Assert.Equal("Button with TextBlock", button.Text);
}

[PlatformFact(SkipOnOSX = true)]
[PlatformFact(TestPlatforms.Windows)]
public void ButtonWithAcceleratorKey()
{
var button = _session.FindElementByAccessibilityId("ButtonWithAcceleratorKey");
Expand Down
6 changes: 3 additions & 3 deletions tests/Avalonia.IntegrationTests.Appium/ComboBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void Can_Change_Selection_From_Unselected_Using_Mouse()
Assert.Equal("Item 0", comboBox.GetComboBoxValue());
}

[PlatformFact(SkipOnOSX = true)]
[PlatformFact(TestPlatforms.Windows)]
public void Can_Change_Selection_With_Keyboard()
{
var comboBox = _session.FindElementByAccessibilityId("BasicComboBox");
Expand All @@ -63,7 +63,7 @@ public void Can_Change_Selection_With_Keyboard()
Assert.Equal("Item 1", comboBox.GetComboBoxValue());
}

[PlatformFact(SkipOnOSX = true)]
[PlatformFact(TestPlatforms.Windows)]
public void Can_Change_Selection_With_Keyboard_From_Unselected()
{
var comboBox = _session.FindElementByAccessibilityId("BasicComboBox");
Expand All @@ -80,7 +80,7 @@ public void Can_Change_Selection_With_Keyboard_From_Unselected()
Assert.Equal("Item 0", comboBox.GetComboBoxValue());
}

[PlatformFact(SkipOnOSX = true)]
[PlatformFact(TestPlatforms.Windows)]
public void Can_Cancel_Keyboard_Selection_With_Escape()
{
var comboBox = _session.FindElementByAccessibilityId("BasicComboBox");
Expand Down
Loading

0 comments on commit b74a2bd

Please sign in to comment.