159 lines
4.3 KiB
C#
159 lines
4.3 KiB
C#
using System.Windows;
|
|
using CursorLang.Services;
|
|
using CursorLang.Tests.Infrastructure;
|
|
|
|
namespace CursorLang.Tests.Services;
|
|
|
|
/// <summary>
|
|
/// The fate of the settings window: it comes and goes, and the application
|
|
/// outlives it either way.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The window has to be alive for real — showing and hiding are the point here —
|
|
/// so it is kept fully transparent and off the taskbar.
|
|
/// </remarks>
|
|
public sealed class MainWindowPresenterTests
|
|
{
|
|
[Fact]
|
|
public void The_close_button_hides_the_window_instead_of_closing_it()
|
|
{
|
|
Sta.Run(() =>
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
Open(presenter, window =>
|
|
{
|
|
window.Close();
|
|
|
|
Assert.False(window.IsVisible);
|
|
Assert.False(presenter.IsShown);
|
|
|
|
// The window is hidden rather than closed: a closed one would
|
|
// refuse to be shown again
|
|
presenter.Show();
|
|
Assert.True(window.IsVisible);
|
|
});
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void The_minimise_button_hides_the_window_as_well()
|
|
{
|
|
Sta.Run(() =>
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
Open(presenter, window =>
|
|
{
|
|
window.WindowState = WindowState.Minimized;
|
|
|
|
Assert.False(window.IsVisible);
|
|
|
|
// And the window is left in the state the next show needs:
|
|
// a minimised one would come back as an icon on the taskbar
|
|
Assert.Equal(WindowState.Normal, window.WindowState);
|
|
});
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void A_hidden_window_comes_back_on_request()
|
|
{
|
|
Sta.Run(() =>
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
Open(presenter, window =>
|
|
{
|
|
presenter.Hide();
|
|
Assert.False(presenter.IsShown);
|
|
|
|
presenter.Show();
|
|
|
|
Assert.True(window.IsVisible);
|
|
Assert.True(presenter.IsShown);
|
|
Assert.Equal(WindowState.Normal, window.WindowState);
|
|
});
|
|
});
|
|
}
|
|
|
|
// On the way out of the application the window closes for real: otherwise
|
|
// the exit from the tray menu would run into the very same refusal
|
|
[Fact]
|
|
public void The_window_closes_once_the_application_is_quitting()
|
|
{
|
|
Sta.Run(() =>
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
Open(presenter, window =>
|
|
{
|
|
presenter.AllowClose();
|
|
window.Close();
|
|
|
|
Assert.Throws<InvalidOperationException>(window.Show);
|
|
});
|
|
});
|
|
}
|
|
|
|
// Without a notification area there is no way back to a hidden window,
|
|
// so the window goes back to closing when told to close
|
|
[Fact]
|
|
public void A_window_let_go_of_behaves_the_usual_way()
|
|
{
|
|
Sta.Run(() =>
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
Open(presenter, window =>
|
|
{
|
|
presenter.Detach();
|
|
window.Close();
|
|
|
|
Assert.Throws<InvalidOperationException>(window.Show);
|
|
});
|
|
|
|
// Letting go twice is what happens when the application quits
|
|
// right after: it is no reason to fail
|
|
presenter.Detach();
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public void With_no_window_attached_nothing_happens()
|
|
{
|
|
var presenter = new MainWindowPresenter(new MainWindowPlacement());
|
|
|
|
presenter.Show();
|
|
presenter.Hide();
|
|
|
|
Assert.False(presenter.IsShown);
|
|
}
|
|
|
|
private static void Open(MainWindowPresenter presenter, Action<Window> check)
|
|
{
|
|
var window = new Window
|
|
{
|
|
// The window is needed alive, but not in sight
|
|
Opacity = 0,
|
|
ShowInTaskbar = false,
|
|
ShowActivated = false,
|
|
Width = 100,
|
|
Height = 100,
|
|
};
|
|
|
|
presenter.Attach(window);
|
|
|
|
try
|
|
{
|
|
window.Show();
|
|
check(window);
|
|
}
|
|
finally
|
|
{
|
|
presenter.AllowClose();
|
|
window.Close();
|
|
}
|
|
}
|
|
}
|