diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/BookControllerTest.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/BookControllerTest.cs index 2ae1d1c8..5bcebe14 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/BookControllerTest.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/BookControllerTest.cs @@ -1,5 +1,4 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Waf.Applications; using System.Waf.UnitTesting; using Test.BookLibrary.Library.Applications.Services; using Test.BookLibrary.Library.Applications.Views; diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/ModuleControllerTest.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/ModuleControllerTest.cs index e8f907b5..bb51a557 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/ModuleControllerTest.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Controllers/ModuleControllerTest.cs @@ -1,5 +1,4 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.Waf.Applications; using System.Waf.Applications.Services; using System.Waf.UnitTesting.Mocks; using Test.BookLibrary.Library.Applications.Services; diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Integration/IntegrationTest.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Integration/IntegrationTest.cs new file mode 100644 index 00000000..787abceb --- /dev/null +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Integration/IntegrationTest.cs @@ -0,0 +1,102 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Test.BookLibrary.Library.Applications.Services; +using Test.BookLibrary.Library.Applications.Views; +using Waf.BookLibrary.Library.Applications.Controllers; +using Waf.BookLibrary.Library.Domain; + +namespace Test.BookLibrary.Library.Applications.Integration; + +[TestClass] +public class IntegrationTest : ApplicationsTest +{ + protected override void OnInitialize() + { + base.OnInitialize(); + var dbContextService = Get(); + dbContextService.ContextCreated = x => + { + var harry = new Person { Firstname = "Harry", Lastname = "Potter" }; + var ron = new Person { Firstname = "Ron", Lastname = "Weasley" }; + x.Set().Add(harry); + x.Set().Add(ron); + x.Set().Add(new Book { Title = "The Lord of the Rings - The Fellowship of the Ring", Author = "J.R.R. Tolkien", LendTo = harry }); + x.Set().Add(new Book { Title = "Star Wars - Heir to the Empire", Author = "Timothy Zahn", LendTo = ron }); + x.Set().Add(new Book { Title = "Serenity, Vol 1: Those Left Behind", Author = "Joss Whedon, Brett Matthews, Will Conrad" }); + }; + } + + [TestMethod] + public void SortFilterAndChangeLendToStatusTest() + { + var moduleController = Get(); + moduleController.Initialize(); + moduleController.Run(); + + var shellView = Get(); + Assert.IsTrue(shellView.IsVisible); + var bookListView = (MockBookListView)shellView.ViewModel.ShellService.BookListView!; + var bookView = (MockBookView)shellView.ViewModel.ShellService.BookView!; + + var books = bookListView.ViewModel.Books!; + bookListView.SingleSelect(books[0]); + + // Sort Book list by Title + Assert.IsTrue(books[0].Book.Title.StartsWith("The Lord")); + bookListView.ViewModel.Sort = x => x.OrderBy(y => y.Book.Title); + Assert.IsTrue(books[0].Book.Title.StartsWith("Serenity")); + + // Filter Book list by search text + bookListView.ViewModel.FilterText = "Star"; + var book = books.Single().Book; + Assert.IsTrue(book.Title.StartsWith("Star")); + bookListView.SingleSelect(books[0]); + + Assert.AreSame(book, bookView.ViewModel.Book); + + // Check that the Star Wars book was lend to Ron. Then change the status to not lend to anyone. + var ron = book.LendTo!; + Assert.AreEqual("Ron", ron.Firstname); + MockLendToView.ShowDialogAction = v => + { + Assert.AreSame(book, v.ViewModel.Book); + Assert.AreSame(ron, v.ViewModel.SelectedPerson); + Assert.IsTrue(v.ViewModel.IsLendTo); + v.ViewModel.IsLendTo = false; + v.ViewModel.OkCommand.Execute(null); + }; + bookView.ViewModel.LendToCommand!.Execute(book); + Assert.IsNull(book.LendTo); + + // Check that The Lord of the Rings book was lend to Harry. + bookListView.ViewModel.FilterText = ""; + var book2 = books.Select(x => x.Book).First(x => x.Title.StartsWith("The Lord")); + var harry = book2.LendTo!; + bookListView.SingleSelect(books.Single(x => x.Book == book2)); + Assert.AreEqual("Harry", bookView.ViewModel.Book?.LendTo?.Firstname); + + // Switch to Persons(List)View and select Harry + var personListView = (MockPersonListView)shellView.ViewModel.ShellService.PersonListView!; + var personView = (MockPersonView)shellView.ViewModel.ShellService?.PersonView!; + var persons = personListView.ViewModel.Persons!; + Assert.AreEqual(2, persons.Count); + Assert.AreSame(harry, persons.Single(x => x.Firstname == "Harry")); + personListView.SingleSelect(harry); + Assert.AreSame(harry, personView.ViewModel.Person); + + // Select all Persons and remove all of them + foreach (var x in persons.Where(x => x != harry)) personListView.ViewModel.AddSelectedPerson(x); + personListView.ViewModel.RemoveCommand!.Execute(null); + Assert.AreEqual(0, persons.Count); + + // Check that The Lord of the Rings book is not lend to anyone. + Assert.IsNull(book2.LendTo); + + // Use a search string which does not exist -> Books list is empty now + bookListView.ViewModel.FilterText = "Star Trek"; + Assert.AreEqual(0, books.Count); + bookListView.SingleSelect(null); + Assert.IsNull(bookView.ViewModel.Book); + + moduleController.Shutdown(); + } +} diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Services/MockDBContextService.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Services/MockDBContextService.cs index f51e2617..411553d3 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Services/MockDBContextService.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Services/MockDBContextService.cs @@ -10,6 +10,8 @@ namespace Test.BookLibrary.Library.Applications.Services; [Export, Export(typeof(IDBContextService))] public class MockDBContextService : IDBContextService { + public Action? ContextCreated { get; set; } + public DbContext GetBookLibraryContext(out string dataSourcePath) { dataSourcePath = @"C:\Test.db"; @@ -19,6 +21,7 @@ public DbContext GetBookLibraryContext(out string dataSourcePath) modelBuilder.Entity().Ignore(x => x.Errors).Ignore(x => x.HasErrors); modelBuilder.Entity().Ignore(x => x.Errors).Ignore(x => x.HasErrors); }); + ContextCreated?.Invoke(context); return context; } } diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockBookListView.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockBookListView.cs index 7b7d76c8..a310ffcb 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockBookListView.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockBookListView.cs @@ -1,5 +1,6 @@ using System.ComponentModel.Composition; using System.Waf.UnitTesting.Mocks; +using Waf.BookLibrary.Library.Applications.DataModels; using Waf.BookLibrary.Library.Applications.ViewModels; using Waf.BookLibrary.Library.Applications.Views; @@ -11,4 +12,11 @@ public class MockBookListView : MockView, IBookListView public bool FirstCellHasFocus { get; set; } public void FocusFirstCell() => FirstCellHasFocus = true; + + public void SingleSelect(BookDataModel? book) + { + ViewModel.SelectedBook = book; + foreach (var x in ViewModel.SelectedBooks.ToArray()) ViewModel.RemoveSelectedBook(x); + if (book is not null) ViewModel.AddSelectedBook(book); + } } diff --git a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockPersonListView.cs b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockPersonListView.cs index 2990d70a..d26eb5ab 100644 --- a/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockPersonListView.cs +++ b/src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications.Test/Views/MockPersonListView.cs @@ -2,6 +2,7 @@ using System.Waf.UnitTesting.Mocks; using Waf.BookLibrary.Library.Applications.ViewModels; using Waf.BookLibrary.Library.Applications.Views; +using Waf.BookLibrary.Library.Domain; namespace Test.BookLibrary.Library.Applications.Views; @@ -11,4 +12,11 @@ public class MockPersonListView : MockView, IPersonListView public bool FirstCellHasFocus { get; set; } public void FocusFirstCell() => FirstCellHasFocus = true; + + public void SingleSelect(Person? person) + { + ViewModel.SelectedPerson = person; + foreach (var x in ViewModel.SelectedPersons.ToArray()) ViewModel.RemoveSelectedPerson(x); + if (person is not null) ViewModel.AddSelectedPerson(person); + } }