public void BookListViewModelBooksTest() { List<Book> books = new List<Book>() { new Book() { Title = "The Fellowship of the Ring" }, new Book() { Title = "The Two Towers" } }; MockBookListView bookListView = new MockBookListView(); IList<BookDataModel> bookDataModels = new SynchronizingCollection<BookDataModel, Book>(books, b => new BookDataModel(b, dummyCommand)); BookListViewModel bookListViewModel = new BookListViewModel(bookListView) { Books = bookDataModels }; Assert.AreEqual(bookDataModels, bookListViewModel.Books); Assert.IsNull(bookListViewModel.SelectedBook); Assert.IsFalse(bookListViewModel.SelectedBooks.Any()); // Select the first book AssertHelper.PropertyChangedEvent(bookListViewModel, x => x.SelectedBook, () => bookListViewModel.SelectedBook = bookDataModels.First()); Assert.AreEqual(books.First(), bookListViewModel.SelectedBook.Book); bookListViewModel.AddSelectedBook(bookDataModels.First()); Assert.IsTrue(bookListViewModel.SelectedBooks.SequenceEqual(new[] { bookDataModels.First() })); // Select both books bookListViewModel.AddSelectedBook(bookDataModels.Last()); Assert.IsTrue(bookListViewModel.SelectedBooks.SequenceEqual(bookDataModels)); }
public void BookListViewModelBooksTest() { var books = new List <Book>() { new() { Title = "The Fellowship of the Ring" }, new() { Title = "The Two Towers" } }; var bookListView = new MockBookListView(); var bookDataModels = new SynchronizingCollection <BookDataModel, Book>(books, b => new BookDataModel(b, dummyCommand)); var bookListViewModel = new BookListViewModel(bookListView) { Books = bookDataModels }; Assert.AreEqual(bookDataModels, bookListViewModel.Books); Assert.IsNull(bookListViewModel.SelectedBook); Assert.IsFalse(bookListViewModel.SelectedBooks.Any()); // Select the first book AssertHelper.PropertyChangedEvent(bookListViewModel, x => x.SelectedBook, () => bookListViewModel.SelectedBook = bookDataModels[0]); Assert.AreEqual(books[0], bookListViewModel.SelectedBook?.Book); bookListViewModel.AddSelectedBook(bookDataModels[0]); AssertHelper.SequenceEqual(new[] { bookDataModels[0] }, bookListViewModel.SelectedBooks); // Select both books bookListViewModel.AddSelectedBook(bookDataModels[^ 1]);
public void RemoveAndSelection3Test() { Book fellowship = new Book() { Title = "The Fellowship of the Ring" }; Book twoTowers = new Book() { Title = "The Two Towers" }; Book returnKing = new Book() { Title = "The Return of the King" }; IEntityService entityService = Container.GetExportedValue <IEntityService>(); entityService.Books.Add(fellowship); entityService.Books.Add(twoTowers); entityService.Books.Add(returnKing); BookController bookController = Container.GetExportedValue <BookController>(); bookController.Initialize(); MockBookListView bookListView = Container.GetExportedValue <MockBookListView>(); BookListViewModel bookListViewModel = ViewHelper.GetViewModel <BookListViewModel>(bookListView); bookListViewModel.BookCollectionView = bookListViewModel.Books; // Remove all books and check that nothing is selected anymore bookListViewModel.SelectedBook = bookListViewModel.Books.Single(b => b.Book == fellowship); bookListViewModel.AddSelectedBook(bookListViewModel.SelectedBook); bookListViewModel.AddSelectedBook(bookListViewModel.Books.Single(b => b.Book == twoTowers)); bookListViewModel.AddSelectedBook(bookListViewModel.Books.Single(b => b.Book == returnKing)); bookListViewModel.RemoveCommand.Execute(null); Assert.IsFalse(entityService.Books.Any()); Assert.IsNull(bookListViewModel.SelectedBook); }
public void RemoveAndSelection2Test() { Book fellowship = new Book() { Title = "The Fellowship of the Ring" }; Book twoTowers = new Book() { Title = "The Two Towers" }; Book returnKing = new Book() { Title = "The Return of the King" }; IEntityService entityService = Container.GetExportedValue <IEntityService>(); entityService.Books.Add(fellowship); entityService.Books.Add(twoTowers); entityService.Books.Add(returnKing); BookController bookController = Container.GetExportedValue <BookController>(); bookController.Initialize(); MockBookListView bookListView = Container.GetExportedValue <MockBookListView>(); BookListViewModel bookListViewModel = ViewHelper.GetViewModel <BookListViewModel>(bookListView); // Set the sorting to: "The Fell...", "The Retu...", "The Two..." bookListViewModel.BookCollectionView = bookListViewModel.Books.OrderBy(b => b.Book.Title); // Remove the last book and check that the last one is selected again. bookListViewModel.SelectedBook = bookListViewModel.Books.Single(b => b.Book == twoTowers); bookListViewModel.AddSelectedBook(bookListViewModel.SelectedBook); bookListViewModel.RemoveCommand.Execute(null); Assert.IsTrue(entityService.Books.SequenceEqual(new[] { fellowship, returnKing })); Assert.AreEqual(returnKing, bookListViewModel.SelectedBook.Book); }
public void AddAndRemoveDisableTest() { Book fellowship = new Book() { Title = "The Fellowship of the Ring" }; IEntityService entityService = Container.GetExportedValue <IEntityService>(); entityService.Books.Add(fellowship); BookController bookController = Container.GetExportedValue <BookController>(); bookController.Initialize(); BookListViewModel bookListViewModel = Container.GetExportedValue <BookListViewModel>(); bookListViewModel.AddSelectedBook(bookListViewModel.Books.Single()); BookViewModel bookViewModel = Container.GetExportedValue <BookViewModel>(); CheckCommand(bookListViewModel.AddNewCommand, bookListViewModel, bookViewModel); CheckCommand(bookListViewModel.RemoveCommand, bookListViewModel, bookViewModel); }
public void AddAndRemoveTest() { Book fellowship = new Book() { Title = "The Fellowship of the Ring" }; Book twoTowers = new Book() { Title = "The Two Towers" }; IEntityService entityService = Container.GetExportedValue <IEntityService>(); entityService.Books.Add(fellowship); entityService.Books.Add(twoTowers); BookController bookController = Container.GetExportedValue <BookController>(); bookController.Initialize(); MockBookListView bookListView = Container.GetExportedValue <MockBookListView>(); BookListViewModel bookListViewModel = ViewHelper.GetViewModel <BookListViewModel>(bookListView); bookListViewModel.BookCollectionView = bookListViewModel.Books; MockBookView bookView = Container.GetExportedValue <MockBookView>(); BookViewModel bookViewModel = ViewHelper.GetViewModel <BookViewModel>(bookView); // Add a new Book Assert.AreEqual(2, entityService.Books.Count); Assert.IsTrue(bookListViewModel.AddNewCommand.CanExecute(null)); bookListViewModel.AddNewCommand.Execute(null); Assert.AreEqual(3, entityService.Books.Count); // Check that the new Book is selected and the first control gets the focus Assert.AreEqual(entityService.Books.Last(), bookViewModel.Book); Assert.IsTrue(bookListView.FirstCellHasFocus); // Simulate an invalid UI input state => the user can't add more books AssertHelper.CanExecuteChangedEvent(bookListViewModel.AddNewCommand, () => bookViewModel.IsValid = false); Assert.IsFalse(bookListViewModel.AddNewCommand.CanExecute(null)); Assert.IsFalse(bookListViewModel.RemoveCommand.CanExecute(null)); // Remove the last two Books at once and check that the last remaining book is selected. bookViewModel.IsValid = true; bookListView.FirstCellHasFocus = false; bookListViewModel.AddSelectedBook(bookListViewModel.Books.Single(b => b.Book == twoTowers)); bookListViewModel.AddSelectedBook(bookListViewModel.Books.Last()); Assert.IsTrue(bookListViewModel.RemoveCommand.CanExecute(null)); bookListViewModel.RemoveCommand.Execute(null); Assert.IsTrue(entityService.Books.SequenceEqual(new Book[] { fellowship })); Assert.AreEqual(fellowship, bookViewModel.Book); Assert.IsTrue(bookListView.FirstCellHasFocus); // Deselect all Books => the Remove command must be deactivated AssertHelper.CanExecuteChangedEvent(bookListViewModel.RemoveCommand, () => { bookListViewModel.SelectedBooks.ToList().ForEach(x => bookListViewModel.RemoveSelectedBook(x)); bookListViewModel.SelectedBook = null; }); Assert.IsFalse(bookListViewModel.RemoveCommand.CanExecute(null)); }