Files
LanMountainDesktop/LanMountainDesktop.Tests/DesktopComponentRenderModeTests.cs
lincube eb066b53f1 Introduce render mode & static component previews
Add DesktopComponentRenderMode and thread the render mode through runtime context and creation APIs so controls can be created for library previews. Replace image-based preview system with static preview Controls: viewmodels and ComponentLibraryWindow now use PreviewControl, and ComponentPreviewImageService/related types and tests were removed. Add ComponentPreviewRuntimeQuiescer to attach/detach preview controls (stop timers, disable input) for safe static previews. Simplify component-library collapse state/presenter by removing transient expanded opacity handling. Update runtime registry, services, views and tests to support the new flow.
2026-04-29 19:43:29 +08:00

136 lines
4.5 KiB
C#

using Avalonia.Controls;
using LanMountainDesktop.ComponentSystem;
using LanMountainDesktop.Services;
using LanMountainDesktop.Services.Settings;
using LanMountainDesktop.Views.Components;
using Xunit;
namespace LanMountainDesktop.Tests;
public sealed class DesktopComponentRenderModeTests
{
private const string ComponentId = "RenderModeProbe";
[Fact]
public void DescriptorCreateControl_DefaultsToLiveRenderMode()
{
var descriptor = CreateDescriptor();
var control = (ProbeControl)descriptor.CreateControl(
cellSize: 64,
CreateTimeZoneService(),
CreateWeatherInfoService(),
new RecommendationDataService(),
new CalculatorDataService(),
CreateSettingsFacade(),
placementId: "desktop-placement");
Assert.Equal(DesktopComponentRenderMode.Live, control.RuntimeContext?.RenderMode);
Assert.Equal("desktop-placement", control.RuntimeContext?.PlacementId);
}
[Fact]
public void DescriptorCreateControl_CanCreateLibraryPreviewRenderModeWithoutPlacement()
{
var descriptor = CreateDescriptor();
var control = (ProbeControl)descriptor.CreateControl(
cellSize: 64,
CreateTimeZoneService(),
CreateWeatherInfoService(),
new RecommendationDataService(),
new CalculatorDataService(),
CreateSettingsFacade(),
placementId: null,
renderMode: DesktopComponentRenderMode.LibraryPreview);
Assert.Equal(DesktopComponentRenderMode.LibraryPreview, control.RuntimeContext?.RenderMode);
Assert.Null(control.RuntimeContext?.PlacementId);
}
[Fact]
public void ComponentLibraryService_CreatesLibraryPreviewRenderMode()
{
var service = new ComponentLibraryService(
CreateComponentRegistry(),
CreateRuntimeRegistry());
var created = service.TryCreateControl(
ComponentId,
new ComponentLibraryCreateContext(
64,
CreateTimeZoneService(),
CreateWeatherInfoService(),
new RecommendationDataService(),
new CalculatorDataService(),
CreateSettingsFacade(),
PlacementId: null,
RenderMode: DesktopComponentRenderMode.LibraryPreview),
out var control,
out var exception);
Assert.True(created, exception?.ToString());
var probe = Assert.IsType<ProbeControl>(control);
Assert.Equal(DesktopComponentRenderMode.LibraryPreview, probe.RuntimeContext?.RenderMode);
Assert.Null(probe.RuntimeContext?.PlacementId);
}
private static DesktopComponentRuntimeDescriptor CreateDescriptor()
{
Assert.True(CreateRuntimeRegistry().TryGetDescriptor(ComponentId, out var descriptor));
return descriptor;
}
private static DesktopComponentRuntimeRegistry CreateRuntimeRegistry()
{
return new DesktopComponentRuntimeRegistry(
CreateComponentRegistry(),
[
new DesktopComponentRuntimeRegistration(
ComponentId,
displayNameLocalizationKey: null,
_ => new ProbeControl(),
cornerRadiusResolver: (System.Func<double, double>?)null)
]);
}
private static ComponentRegistry CreateComponentRegistry()
{
return new ComponentRegistry(
[
new DesktopComponentDefinition(
ComponentId,
"Render Mode Probe",
"Apps",
"Test",
MinWidthCells: 1,
MinHeightCells: 1,
AllowStatusBarPlacement: false,
AllowDesktopPlacement: true)
]);
}
private static ISettingsFacadeService CreateSettingsFacade()
{
return HostSettingsFacadeProvider.GetOrCreate();
}
private static TimeZoneService CreateTimeZoneService()
{
return CreateSettingsFacade().Region.GetTimeZoneService();
}
private static IWeatherInfoService CreateWeatherInfoService()
{
return CreateSettingsFacade().Weather.GetWeatherInfoService();
}
private sealed class ProbeControl : Control, IComponentRuntimeContextAware
{
public DesktopComponentRuntimeContext? RuntimeContext { get; private set; }
public void SetComponentRuntimeContext(DesktopComponentRuntimeContext context)
{
RuntimeContext = context;
}
}
}