139 lines
4.5 KiB
C#
139 lines
4.5 KiB
C#
|
|
using Surveillance.Client.Application;
|
||
|
|
using Surveillance.Client.App;
|
||
|
|
using Surveillance.Client.Domain;
|
||
|
|
using Surveillance.Client.Media;
|
||
|
|
using Surveillance.Client.Protocol;
|
||
|
|
using Xunit;
|
||
|
|
|
||
|
|
namespace Surveillance.Client.App.Tests;
|
||
|
|
|
||
|
|
public sealed class MainWindowViewModelTests
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public void RemembersCredentialsByDefault()
|
||
|
|
{
|
||
|
|
var viewModel = new MainWindowViewModel(new CoordinatorStub());
|
||
|
|
|
||
|
|
Assert.True(viewModel.RememberCredentials);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task KeepsAuthenticatedSessionWhenPreviewProtocolIsUnverified()
|
||
|
|
{
|
||
|
|
var coordinator = new CoordinatorStub();
|
||
|
|
var viewModel = new MainWindowViewModel(coordinator);
|
||
|
|
|
||
|
|
await viewModel.ConnectCommand.ExecuteAsync(null);
|
||
|
|
|
||
|
|
Assert.True(viewModel.IsConnected);
|
||
|
|
Assert.True(coordinator.IsConnected);
|
||
|
|
Assert.Equal("Authenticated; live preview unavailable", viewModel.StatusMessage);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task InvalidPortDoesNotAttemptToConnect()
|
||
|
|
{
|
||
|
|
var coordinator = new CoordinatorStub();
|
||
|
|
var viewModel = new MainWindowViewModel(coordinator) { Port = "70000" };
|
||
|
|
|
||
|
|
await viewModel.ConnectCommand.ExecuteAsync(null);
|
||
|
|
|
||
|
|
Assert.Equal(0, coordinator.ConnectCount);
|
||
|
|
Assert.Contains("port", viewModel.DetailMessage, StringComparison.OrdinalIgnoreCase);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SavesCredentialsOnCloseWhenRememberIsEnabled()
|
||
|
|
{
|
||
|
|
var coordinator = new CoordinatorStub();
|
||
|
|
var viewModel = new MainWindowViewModel(coordinator)
|
||
|
|
{
|
||
|
|
Host = "camera.local",
|
||
|
|
Port = "9000",
|
||
|
|
UserName = "viewer",
|
||
|
|
Password = "secret"
|
||
|
|
};
|
||
|
|
|
||
|
|
await viewModel.SaveCredentialsOnCloseAsync();
|
||
|
|
|
||
|
|
Assert.Equal(1, coordinator.SaveCount);
|
||
|
|
Assert.Equal("camera.local", coordinator.SavedEndpoint?.Host);
|
||
|
|
Assert.Equal(9000, coordinator.SavedEndpoint?.Port);
|
||
|
|
Assert.Equal("viewer", coordinator.SavedCredentials?.UserName);
|
||
|
|
Assert.Equal("secret", coordinator.SavedCredentials?.Password);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task DoesNotSaveIncompleteOrUnrememberedCredentialsOnClose()
|
||
|
|
{
|
||
|
|
var coordinator = new CoordinatorStub();
|
||
|
|
var viewModel = new MainWindowViewModel(coordinator)
|
||
|
|
{
|
||
|
|
UserName = "viewer",
|
||
|
|
Password = "secret",
|
||
|
|
RememberCredentials = false
|
||
|
|
};
|
||
|
|
|
||
|
|
await viewModel.SaveCredentialsOnCloseAsync();
|
||
|
|
viewModel.RememberCredentials = true;
|
||
|
|
viewModel.Password = string.Empty;
|
||
|
|
await viewModel.SaveCredentialsOnCloseAsync();
|
||
|
|
|
||
|
|
Assert.Equal(0, coordinator.SaveCount);
|
||
|
|
}
|
||
|
|
|
||
|
|
private sealed class CoordinatorStub : ILiveViewCoordinator
|
||
|
|
{
|
||
|
|
public int ConnectCount { get; private set; }
|
||
|
|
|
||
|
|
public int SaveCount { get; private set; }
|
||
|
|
|
||
|
|
public DeviceEndpoint? SavedEndpoint { get; private set; }
|
||
|
|
|
||
|
|
public DeviceCredentials? SavedCredentials { get; private set; }
|
||
|
|
|
||
|
|
public bool IsConnected { get; private set; }
|
||
|
|
|
||
|
|
public bool IsPreviewing => false;
|
||
|
|
|
||
|
|
public ValueTask<AuthenticationResult> ConnectAsync(
|
||
|
|
DeviceEndpoint endpoint,
|
||
|
|
DeviceCredentials credentials,
|
||
|
|
bool rememberCredentials,
|
||
|
|
CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
ConnectCount++;
|
||
|
|
IsConnected = true;
|
||
|
|
return ValueTask.FromResult(AuthenticationResult.Success());
|
||
|
|
}
|
||
|
|
|
||
|
|
public ValueTask SaveCredentialsAsync(
|
||
|
|
DeviceEndpoint endpoint,
|
||
|
|
DeviceCredentials credentials,
|
||
|
|
CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
SaveCount++;
|
||
|
|
SavedEndpoint = endpoint;
|
||
|
|
SavedCredentials = credentials;
|
||
|
|
return ValueTask.CompletedTask;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ValueTask StartPreviewAsync(ChannelId channel, CancellationToken cancellationToken = default) =>
|
||
|
|
ValueTask.FromException(new ProtocolNotVerifiedException("Preview protocol is not verified."));
|
||
|
|
|
||
|
|
public async IAsyncEnumerable<DecodedVideoFrame> ReadFramesAsync(
|
||
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
await Task.CompletedTask;
|
||
|
|
yield break;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ValueTask StopAsync(CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
IsConnected = false;
|
||
|
|
return ValueTask.CompletedTask;
|
||
|
|
}
|
||
|
|
|
||
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
||
|
|
}
|
||
|
|
}
|