From 20c57781c54261963b23688c52791ed5857c0d5b Mon Sep 17 00:00:00 2001 From: jbe2277 Date: Sat, 30 Nov 2024 18:46:25 +0100 Subject: [PATCH] UITest/BookLib: add LoadCorruptDatabaseTest (#60) * UITest/BookLib: add LoadCorruptDatabaseTest * UITest/BookLib: improve LoadCorruptDatabaseTest --- .../BookLibrary.Test/Tests/GeneralTest.cs | 29 ++++++++++++++++++ src/Samples.UITest/BookLibrary.Test/UITest.cs | 30 ++++++++++++------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs b/src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs index 74226de8..525d4cc9 100644 --- a/src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs +++ b/src/Samples.UITest/BookLibrary.Test/Tests/GeneralTest.cs @@ -29,4 +29,33 @@ public void AboutTest() => Run(() => dataMenu.Click(); dataMenu.ExitMenuItem.Click(); }); + + [Fact] + public void LoadCorruptDatabaseTest() => Run(() => + { + if (File.Exists(AppInfo.DatabaseFile)) File.Delete(AppInfo.DatabaseFile); + Directory.CreateDirectory(Path.GetDirectoryName(AppInfo.DatabaseFile)!); + File.AppendAllText(AppInfo.DatabaseFile, "42"); + + Launch(resetDatabase: false); + var window = GetShellWindow(); + var bookListView = window.TabControl.BookLibraryTabItem.BookListView; + Assert.Equal(0, bookListView.BookDataGrid.RowCount); + + var messageBox = window.FirstModalWindow().As(); + Assert.Equal("Could not load the Books from the database.", messageBox.Message); + messageBox.Buttons[0].Click(); + + messageBox = window.FirstModalWindow().As(); + Assert.Equal("Could not load the Persons from the database.", messageBox.Message); + messageBox.Buttons[0].Click(); + + Assert.Equal(0, bookListView.BookDataGrid.RowCount); + + window.TabControl.AddressBookTabItem.Select(); + var personListView = window.TabControl.AddressBookTabItem.PersonListView; + Assert.Equal(0, personListView.PersonDataGrid.RowCount); + + window.Close(); + }); } diff --git a/src/Samples.UITest/BookLibrary.Test/UITest.cs b/src/Samples.UITest/BookLibrary.Test/UITest.cs index cf01e735..b0653e6f 100644 --- a/src/Samples.UITest/BookLibrary.Test/UITest.cs +++ b/src/Samples.UITest/BookLibrary.Test/UITest.cs @@ -9,27 +9,35 @@ namespace UITest.BookLibrary; -public abstract class UITest(ITestOutputHelper log) : UITestBase(log, "BookLibrary.exe", +public record AppInfo(string CompanyName, string ProductName, string SettingsFile, string DatabaseFile); + +public abstract class UITest : UITestBase +{ + protected UITest(ITestOutputHelper log) : base(log, "BookLibrary.exe", Environment.GetEnvironmentVariable("UITestExePath") ?? "out/BookLibrary/Release/net8.0-windows/", Environment.GetEnvironmentVariable("UITestOutputPath") ?? "out/Samples.UITest/BookLibrary/") -{ + { + var companyName = FileVersionInfo.GetVersionInfo(Executable).CompanyName ?? throw new InvalidOperationException("Could not read the CompanyName from the exe."); + var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe."); + AppInfo = new(companyName, productName, + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName, "Settings", "Settings.xml"), + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), companyName, productName, "Resources", "BookLibrary.db")); + } + + protected AppInfo AppInfo { get; } + public Application Launch(LaunchArguments? arguments = null, bool resetSettings = true, bool resetDatabase = true) { Log.WriteLine(""); if (resetSettings) { - var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe."); - var settingsFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), productName, "Settings", "Settings.xml"); - if (File.Exists(settingsFile)) File.Delete(settingsFile); - Log.WriteLine($"Delete settings: {settingsFile}"); + if (File.Exists(AppInfo.SettingsFile)) File.Delete(AppInfo.SettingsFile); + Log.WriteLine($"Delete settings: {AppInfo.SettingsFile}"); } if (resetDatabase) { - var companyName = FileVersionInfo.GetVersionInfo(Executable).CompanyName ?? throw new InvalidOperationException("Could not read the CompanyName from the exe."); - var productName = FileVersionInfo.GetVersionInfo(Executable).ProductName ?? throw new InvalidOperationException("Could not read the ProductName from the exe."); - var dbFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), companyName, productName, "Resources", "BookLibrary.db"); - if (File.Exists(dbFile)) File.Delete(dbFile); - Log.WriteLine($"Delete database: {dbFile}"); + if (File.Exists(AppInfo.DatabaseFile)) File.Delete(AppInfo.DatabaseFile); + Log.WriteLine($"Delete database: {AppInfo.DatabaseFile}"); } var args = (arguments ?? new LaunchArguments()).ToArguments(); Log.WriteLine($"Launch: {args}");