using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Headless.XUnit;
using Avalonia.Media;
using Avalonia.Threading;
using Avalonia.VisualTree;
using IFSRenderer.AvaloniaShell.Controls;
using Xunit;
namespace IFSRenderer.AvaloniaShell.Tests;
/// <summary>
/// The background colour control: "the background RGB sliders are too large", and separately
/// "those sliders are still normal sliders??".
/// </summary>
/// <remarks>
/// Both complaints had the same root: three stock 0-255 Sliders that the canonical does not have
/// at all. MainWindow.xaml:171 is an xctk:ColorPicker — a swatch that opens a picker. The
/// read-outs beside our sliders were one-way StringFormat, so an exact colour could not be
/// entered by any means.
/// </remarks>
public class BackgroundColourTests
{
private static MainWindow Ready()
{
(MainWindow window, _) = MainWindowHarness.Start();
window.Measure(new Size(1500, 900));
window.Arrange(new Rect(0, 0, 1500, 900));
Dispatcher.UIThread.RunJobs();
return window;
}
/// <summary>
/// The whole app should contain no stock Slider. The canonical has exactly two, both inside
/// PaletteSlider.xaml and fully retemplated into a gradient bar.
/// </summary>
[AvaloniaFact]
public void No_stock_sliders_remain_in_the_main_window()
{
MainWindow window = Ready();
var stock = window.GetVisualDescendants().OfType<Slider>().ToList();
Assert.True(stock.Count == 0,
$"{stock.Count} stock Slider(s) in the window; the canonical has none outside "
+ "PaletteSlider.xaml. Use ValueSlider.");
}
/// <summary>An exact colour must be reachable — the capability the sliders never had.</summary>
[AvaloniaFact]
public void A_hex_colour_can_be_typed_and_reaches_the_fractal()
{
(MainWindow window, Fakes.FakeRenderQueue queue) = MainWindowHarness.Start();
Dispatcher.UIThread.RunJobs();
var hex = window.FindControl<TextBox>("BgHex");
Assert.NotNull(hex);
hex.Text = "#102030";
Dispatcher.UIThread.RunJobs();
MainWindowHarness.Settle(queue);
Assert.Equal(0x10, queue.Current.BackgroundColor.R);
Assert.Equal(0x20, queue.Current.BackgroundColor.G);
Assert.Equal(0x30, queue.Current.BackgroundColor.B);
}
/// <summary>Dragging a channel writes back to the hex box, so the two never disagree.</summary>
[AvaloniaFact]
public void Channel_edits_are_reflected_in_the_hex_box()
{
(MainWindow window, Fakes.FakeRenderQueue queue) = MainWindowHarness.Start();
Dispatcher.UIThread.RunJobs();
var hex = window.FindControl<TextBox>("BgHex");
var red = window.FindControl<ValueSlider>("BgR");
Assert.NotNull(red);
hex.Text = "#000000";
Dispatcher.UIThread.RunJobs();
MainWindowHarness.Click(red, "UpButton");
MainWindowHarness.Settle(queue);
Assert.Equal("#010000", hex.Text);
Assert.Equal(1, queue.Current.BackgroundColor.R);
}
}