using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.VisualTree;
using NewLife.XCoder.Shell;
using Xunit;
namespace XCoder.App.UiTests;
/// <summary>页面排版约定:按钮文字居中、页容器留白不吞掉底部内容</summary>
/// <remarks>
/// 两个回归点:
/// 1. Button 继承 ContentControl 的 Stretch 默认,宽度大于文字时文字贴左,看起来没对齐;
/// 2. 页容器的 ScrollViewer 用 Padding 时,<c>MinHeight</c> 绑定的 Viewport 高度不含内边距,
/// 内容总是多出 40px,页面底部操作行被折到滚动视图外。
/// </remarks>
public class PageLayoutTests
{
private static Task RunOnUi(Action action) => UiSession.Run(action);
[Fact]
[DisplayName("按钮内容默认水平垂直居中,导航类按钮保持左对齐")]
public async Task Buttons_CenterContent()
{
await RunOnUi(() =>
{
var plain = new Button { Content = "text", Width = 120 };
var primary = new Button { Content = "text", Classes = { "primary" }, Width = 120 };
var secondary = new Button { Content = "text", Classes = { "secondary" }, Width = 120 };
var action = new Button { Content = "text", Classes = { "ActionButton" }, Width = 120 };
var nav = new Button { Content = "text", Classes = { "nav" }, Width = 120 };
var cmd = new Button { Content = "text", Classes = { "cmd" }, Width = 120 };
var window = new Window
{
Width = 240,
Height = 400,
Content = new StackPanel { Children = { plain, primary, secondary, action, nav, cmd } },
};
window.Show();
try
{
foreach (var button in new[] { plain, primary, secondary, action })
{
Assert.Equal(Avalonia.Layout.HorizontalAlignment.Center, button.HorizontalContentAlignment);
Assert.Equal(Avalonia.Layout.VerticalAlignment.Center, button.VerticalContentAlignment);
}
// 侧栏与命令栏是整行左对齐的导航项,不能被居中
foreach (var button in new[] { nav, cmd })
{
Assert.Equal(Avalonia.Layout.HorizontalAlignment.Left, button.HorizontalContentAlignment);
}
}
finally
{
window.Close();
}
});
}
[Fact]
[DisplayName("页容器不再用 ScrollViewer 内边距,底部内容不被折到可视区外")]
public async Task PageScrolls_DoNotOverflow()
{
await RunOnUi(() =>
{
var failures = new List<String>();
foreach (var tool in ToolRegistry.Tools.Where(e => e.ViewType != null))
{
var view = CreateView(tool.ViewType!);
var scroll = view.GetVisualDescendants().OfType<ScrollViewer>()
.FirstOrDefault(e => e.Name == "PageScroll");
if (scroll == null) continue;
if (scroll.Padding.Top != 0 || scroll.Padding.Bottom != 0)
failures.Add($"{tool.Key}: ScrollViewer 仍在用内边距({scroll.Padding}),会使 MinHeight 多算一截");
if (scroll.Margin.Top <= 0)
failures.Add($"{tool.Key}: 页容器缺少外边距");
}
Assert.True(failures.Count == 0, "以下页面排版未通过:" + Environment.NewLine + String.Join(Environment.NewLine, failures));
});
}
[Fact]
[DisplayName("内容放得下时编辑框页面不出现滚动,底部操作行完整可见")]
public async Task ShortPage_FitsWithoutScroll()
{
await RunOnUi(() =>
{
var tool = ToolRegistry.Find("md5");
Assert.NotNull(tool);
var view = CreateView(tool!.ViewType!);
var window = new Window
{
Width = 1200,
Height = 800,
Content = new Border
{
Margin = new Thickness(16, 0, 16, 16),
Classes = { "panel" },
Child = view,
},
};
window.Show();
try
{
var scroll = view.GetVisualDescendants().OfType<ScrollViewer>().First(e => e.Name == "PageScroll");
var root = view.GetVisualDescendants().OfType<Control>().First(e => e.Name == "PageRoot");
// 内容高度等于可视高度时,不应再多出一段滚动
Assert.True(root.Bounds.Height <= scroll.Viewport.Height + 0.5,
$"页根高度 {root.Bounds.Height} 超过可视高度 {scroll.Viewport.Height}");
Assert.True(scroll.Extent.Height <= scroll.Viewport.Height + 0.5,
$"页面仍溢出:extent {scroll.Extent.Height} > viewport {scroll.Viewport.Height}");
}
finally
{
window.Close();
}
});
}
/// <summary>构造工具视图;窗口型工具把内容与窗口级样式搬出来,与外壳行为一致</summary>
/// <param name="viewType">视图类型</param>
/// <returns>可放入窗口的内容控件</returns>
private static Control CreateView(Type viewType)
{
var instance = (Control)Activator.CreateInstance(viewType)!;
if (instance is not Window source) return instance;
var view = source.Content as Control ?? new Panel();
source.Content = null;
var styles = source.Styles.ToArray();
if (styles.Length > 0)
{
source.Styles.Clear();
foreach (var style in styles) view.Styles.Add(style);
}
try { source.Close(); } catch { }
return view;
}
}
|