wabbajack/Wabbajack.App.Test/FilePickerTests.cs

364 lines
14 KiB
C#
Raw Normal View History

2020-03-28 21:52:43 +00:00
using System.IO;
using System.Reactive.Linq;
using System.Threading.Tasks;
using DynamicData;
using Wabbajack.Common;
2020-03-28 21:52:43 +00:00
using Xunit;
namespace Wabbajack.Test
{
public class FilePickerTests
{
public static TempFile CreateSetFile(FilePickerVM vm)
{
var temp = new TempFile();
using (new FileStream(temp.File.FullName, FileMode.CreateNew)) { }
2020-03-28 21:52:43 +00:00
vm.TargetPath = temp.Path;
return temp;
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task Stock()
{
var vm = new FilePickerVM();
2020-03-28 21:52:43 +00:00
Assert.Equal(FilePickerVM.PathTypeOptions.Off, vm.PathType);
Assert.Equal(FilePickerVM.CheckOptions.Off, vm.ExistCheckOption);
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileNoExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileNoExistsCheck_Exists()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task ExistCheckTypeOff_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Off;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task ExistCheckTypeOff_Exists()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Off;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileIfNotEmptyCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileIfNotEmptyCheck_SetPath_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
2020-03-28 21:52:43 +00:00
vm.TargetPath = (AbsolutePath)"SomePath.jpg";
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileIfNotEmptyCheck_Exists()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileOnExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileOnExistsCheck_Exists()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderIfNotEmptyCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderIfNotEmptyCheck_SetPath_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
2020-03-28 21:52:43 +00:00
vm.TargetPath = (AbsolutePath)"SomePath.jpg";
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderIfNotEmptyCheck_Exists()
{
var vm = new FilePickerVM();
await using (await CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderOnExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderOnExistsCheck_Exists()
{
var vm = new FilePickerVM();
await using (await CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task AdditionalError_Success()
{
var vm = new FilePickerVM();
vm.AdditionalError = Observable.Return<IErrorResponse>(ErrorResponse.Succeed());
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task AdditionalError_Fail()
{
var vm = new FilePickerVM();
2019-12-15 03:55:16 +00:00
string errText = "An error";
vm.AdditionalError = Observable.Return<IErrorResponse>(ErrorResponse.Fail(errText));
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(errText, vm.ErrorTooltip);
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileExistsButSetToFolder()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FolderExistsButSetToFile()
{
var vm = new FilePickerVM();
await using (await CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileWithFilters_Passes()
{
var vm = new FilePickerVM();
await using (var file = CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*{vm.TargetPath.Extension}"));
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileWithFilters_ExistsButFails()
{
var vm = new FilePickerVM();
await using (CreateSetFile(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*{vm.TargetPath.Extension}z"));
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.DoesNotPassFiltersText, vm.ErrorTooltip);
}
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileWithFilters_PassesButDoesntExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
2020-03-28 21:52:43 +00:00
vm.TargetPath = (AbsolutePath)"SomePath.png";
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*{vm.TargetPath.Extension}"));
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
2020-03-28 21:52:43 +00:00
[Fact]
public async Task FileWithFilters_IfNotEmptyCheck_DoesntExist()
{
2020-03-28 21:52:43 +00:00
var vm = new FilePickerVM
{
PathType = FilePickerVM.PathTypeOptions.File,
ExistCheckOption = FilePickerVM.CheckOptions.Off,
FilterCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty
};
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*{vm.TargetPath.Extension}"));
await Task.Delay(250);
2020-03-28 21:52:43 +00:00
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
private static async Task<TempFolder> CreateSetFolder(FilePickerVM vm)
2020-03-28 21:52:43 +00:00
{
var temp = await TempFolder.Create();
2020-03-28 21:52:43 +00:00
vm.TargetPath = temp.Dir;
return temp;
}
}
}