Наследование: ValidatableModel, IFormattable
Пример #1
0
        private void AddNewBook()
        {
            Book book = new Book();
            entityService.Books.Add(book);

            bookListViewModel.SelectedBook = bookDataModels.Single(b => b.Book == book);
            bookListViewModel.Focus();
        }
Пример #2
0
        public BookDataModel(Book book, ICommand lendToCommand)
        {
            if (book == null) { throw new ArgumentNullException(nameof(book)); }
            if (lendToCommand == null) { throw new ArgumentNullException(nameof(lendToCommand)); }

            Book = book;
            LendToCommand = lendToCommand;
        }
Пример #3
0
        public void BookLanguagePropertyChangedTest()
        {
            Book book = new Book();
            book.Language = Language.English;

            AssertHelper.PropertyChangedEvent(book, x => x.Language, () => book.Language = Language.German);
            Assert.AreEqual(Language.German, book.Language);
        }
Пример #4
0
        public BookDataModel(Book book, ICommand lendToCommand)
        {
            if (book == null) { throw new ArgumentNullException("book"); }
            if (lendToCommand == null) { throw new ArgumentNullException("lendToCommand"); }

            this.book = book;
            this.lendToCommand = lendToCommand;
        }
Пример #5
0
        public void BookLendToPropertyChangedTest()
        {
            Book book = new Book();
            Assert.IsNull(book.LendTo);

            Person person = new Person();
            AssertHelper.PropertyChangedEvent(book, x => x.LendTo, () => book.LendTo = person);
            Assert.AreEqual(person, book.LendTo);
        }
Пример #6
0
        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));
        }
Пример #7
0
        public void ConstructorTest()
        {
            Book book = new Book();
            ICommand dummyCommand = new DelegateCommand(() => {});
            
            AssertHelper.ExpectedException<ArgumentNullException>(() => new BookDataModel(null, null));
            AssertHelper.ExpectedException<ArgumentNullException>(() => new BookDataModel(book, null));

            BookDataModel bookDataModel = new BookDataModel(book, dummyCommand);
            Assert.AreEqual(book, bookDataModel.Book);
            Assert.AreEqual(dummyCommand, bookDataModel.LendToCommand);
        }
        public void BookControllerLendToTest()
        {
            Book fellowship = new Book() { Title = "The Fellowship of the Ring" };
            Book twoTowers = new Book() { Title = "The Two Towers" };
            Person harry = new Person() { Firstname = "Harry" };
            Person ron = new Person() { Firstname = "Ron" };

            IEntityService entityService = Container.GetExportedValue<IEntityService>();
            entityService.Books.Add(fellowship);
            entityService.Books.Add(twoTowers);
            entityService.Persons.Add(harry);
            entityService.Persons.Add(ron);

            ShellService shellService = Container.GetExportedValue<ShellService>();
            shellService.ShellView = Container.GetExportedValue<IShellView>();

            BookController bookController = Container.GetExportedValue<BookController>();
            bookController.Initialize();

            MockBookListView bookListView = Container.GetExportedValue<MockBookListView>();
            BookListViewModel bookListViewModel = ViewHelper.GetViewModel<BookListViewModel>(bookListView);
            MockBookView bookView = Container.GetExportedValue<MockBookView>();
            BookViewModel bookViewModel = ViewHelper.GetViewModel<BookViewModel>(bookView);

            // Check that the LendTo Button is enabled
            Assert.IsNull(fellowship.LendTo);
            Assert.AreEqual(fellowship, bookViewModel.Book);
            Assert.IsTrue(bookViewModel.IsEnabled);

            // Open the LendTo dialog
            MockLendToView lendToView = Container.GetExportedValue<MockLendToView>();
            lendToView.ShowDialogAction = (view) =>
            {
                Assert.AreEqual(Container.GetExportedValue<IShellView>(), view.Owner);
                Assert.IsTrue(view.IsVisible);
                LendToViewModel viewModel = (LendToViewModel)view.DataContext;
                Assert.AreEqual(fellowship, viewModel.Book);
                Assert.AreEqual(entityService.Persons, viewModel.Persons);

                // Lend the book to Ron
                viewModel.SelectedPerson = ron;
                viewModel.OkCommand.Execute(null);
            };
            bookViewModel.LendToCommand.Execute(fellowship);
            Assert.AreEqual(ron, fellowship.LendTo);

            // Check that the LendTo Button is disabled when no book is selected anymore.
            AssertHelper.CanExecuteChangedEvent(bookViewModel.LendToCommand, () =>
                bookListViewModel.SelectedBook = null);
            Assert.IsNull(bookViewModel.Book);
            Assert.IsFalse(bookViewModel.IsEnabled);
        }
Пример #9
0
        public void BookIsbnValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.IsNull(book.Isbn);
            Assert.IsFalse(book.GetErrors("Isbn").Any());

            book.Isbn = new string('A', 15);
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.IsbnMaxLength, "Isbn", 14),
                book.GetErrors("Isbn").Single().ErrorMessage);

            book.Isbn = new string('A', 14);
            Assert.IsFalse(book.GetErrors("Isbn").Any());
        }
Пример #10
0
        public void BookPagesValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.AreEqual(0, book.Pages);
            Assert.IsFalse(book.GetErrors("Pages").Any());

            book.Pages = -1;
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.PagesEqualOrLarger, "Pages", 0),
                book.GetErrors("Pages").Single().ErrorMessage);

            book.Pages = 400;
            Assert.IsFalse(book.GetErrors("Pages").Any());
        }
Пример #11
0
        public void BookAuthorValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.AreEqual("", book.Author);
            Assert.AreEqual(Resources.AuthorMandatory, book.GetErrors(nameof(book.Author)).Single().ErrorMessage);

            book.Author = new string('A', 101);
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.AuthorMaxLength, "Author", 100),
                book.GetErrors(nameof(book.Author)).Single().ErrorMessage);

            book.Author = new string('A', 100);
            Assert.IsFalse(book.GetErrors(nameof(book.Author)).Any());
        }
Пример #12
0
        public void BookViewModelBookTest()
        {
            MockBookView bookView = new MockBookView();
            BookViewModel bookViewModel = new BookViewModel(bookView);

            Assert.IsFalse(bookViewModel.IsEnabled);

            Book book = new Book();
            AssertHelper.PropertyChangedEvent(bookViewModel, x => x.Book, () => bookViewModel.Book = book);
            Assert.AreEqual(book, bookViewModel.Book);
            Assert.IsTrue(bookViewModel.IsEnabled);

            AssertHelper.PropertyChangedEvent(bookViewModel, x => x.IsEnabled, () => bookViewModel.Book = null);
            Assert.IsNull(bookViewModel.Book);
            Assert.IsFalse(bookViewModel.IsEnabled);
        }
Пример #13
0
        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 LendToViewModelLendToTest()
        {
            Book book = new Book() { Title = "The Fellowship of the Ring" };

            List<Person> persons = new List<Person>()
            {
                new Person() { Firstname = "Harry" },
                new Person() { Firstname = "Ron" }
            };

            MockLendToView lendToView = new MockLendToView();

            AssertHelper.ExpectedException<ArgumentNullException>(() => new LendToViewModel(lendToView, null, null));
            AssertHelper.ExpectedException<ArgumentNullException>(() => new LendToViewModel(lendToView, book, null));
            LendToViewModel lendToViewModel = new LendToViewModel(lendToView, book, persons);

            Assert.AreEqual(book, lendToViewModel.Book);
            Assert.AreEqual(persons, lendToViewModel.Persons);

            // Show the dialog
            object owner = new object();
            Action<MockLendToView> showDialogAction = (view) =>
            {
                Assert.AreEqual("", LendToViewModel.Title);
                Assert.IsTrue(lendToView.IsVisible);
                Assert.AreEqual(owner, lendToView.Owner);

                // Check the default values
                Assert.IsTrue(lendToViewModel.IsLendTo);
                Assert.IsFalse(lendToViewModel.IsWasReturned);
                Assert.AreEqual(persons.First(), lendToViewModel.SelectedPerson);

                // Select the last person: Lend to Ron
                AssertHelper.PropertyChangedEvent(lendToViewModel, x => x.SelectedPerson, () =>
                    lendToViewModel.SelectedPerson = persons.Last());

                // Press Ok button
                lendToViewModel.OkCommand.Execute(null);
            };

            lendToView.ShowDialogAction = showDialogAction;
            Assert.IsTrue(lendToViewModel.ShowDialog(owner));
            Assert.IsFalse(lendToView.IsVisible);
            Assert.AreEqual(persons.Last(), lendToViewModel.SelectedPerson);
        }
Пример #15
0
        public void GeneralBookTest()
        {
            Book book = new Book();
            Assert.IsNotNull(book.Id);

            book.Title = "Star Wars - Heir to the Empire";
            book.Author = "Timothy Zahn";
            book.Publisher = "Spectra";
            book.PublishDate = new DateTime(1992, 5, 1);
            book.Isbn = "0553296124";
            book.Language = Language.English;
            book.Pages = 416;

            Assert.IsTrue(book.Validate());

            Assert.AreEqual("Star Wars - Heir to the Empire by Timothy Zahn", 
                book.ToString(null, CultureInfo.InvariantCulture));
        }
Пример #16
0
        public void LendToViewModelWasReturnedTest()
        {
            List<Person> persons = new List<Person>()
            {
                new Person() { Firstname = "Harry" },
                new Person() { Firstname = "Ron" }
            };

            Book book = new Book() { Title = "The Fellowship of the Ring", LendTo = persons.First() };

            MockLendToView lendToView = new MockLendToView();
            LendToViewModel lendToViewModel = new LendToViewModel(lendToView) { Book = book, Persons = persons };

            // Show the dialog
            object owner = new object();
            Action<MockLendToView> showDialogAction = (view) =>
            {
                // Check the default values
                Assert.IsFalse(lendToViewModel.IsLendTo);
                Assert.IsTrue(lendToViewModel.IsWasReturned);

                // Change check boxes
                AssertHelper.PropertyChangedEvent(lendToViewModel, x => x.IsLendTo, () =>
                    lendToViewModel.IsLendTo = true);
                Assert.IsTrue(lendToViewModel.IsLendTo);
                Assert.IsFalse(lendToViewModel.IsWasReturned);

                // Restore the original check boxes state
                AssertHelper.PropertyChangedEvent(lendToViewModel, x => x.IsWasReturned, () =>
                    lendToViewModel.IsWasReturned = true);
                Assert.IsFalse(lendToViewModel.IsLendTo);
                Assert.IsTrue(lendToViewModel.IsWasReturned);

                lendToViewModel.OkCommand.Execute(null);
            };

            MockLendToView.ShowDialogAction = showDialogAction;
            Assert.IsNotNull(lendToViewModel.SelectedPerson);
            Assert.IsTrue(lendToViewModel.ShowDialog(owner));
            Assert.IsNull(lendToViewModel.SelectedPerson);

            MockLendToView.ShowDialogAction = null;
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the Books EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToBooks(Book book)
 {
     base.AddObject("Books", book);
 }
Пример #18
0
        public void BookPublishDateValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.IsFalse(book.GetErrors(nameof(book.PublishDate)).Any());

            book.PublishDate = new DateTime(1752, 1, 1);
            Assert.IsTrue(book.GetErrors(nameof(book.PublishDate)).Any());

            book.PublishDate = new DateTime(2000, 1, 1);
            Assert.IsFalse(book.GetErrors(nameof(book.PublishDate)).Any());

            book.PublishDate = new DateTime(1000, 1, 1);
            Assert.IsTrue(book.GetErrors(nameof(book.PublishDate)).Any());
        }
Пример #19
0
 private void LendTo(Book book)
 {
     LendToViewModel lendToViewModel = lendToViewModelFactory.CreateExport().Value;
     lendToViewModel.Book = book;
     lendToViewModel.Persons = entityService.Persons;
     if (lendToViewModel.ShowDialog(shellService.ShellView))
     {
         book.LendTo = lendToViewModel.SelectedPerson;
     }
 }
Пример #20
0
        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);
        }
Пример #21
0
        public void BookTitleValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.AreEqual("", book.Title);
            Assert.AreEqual(Resources.TitleMandatory, book.GetErrors("Title").Single().ErrorMessage);

            book.Title = new string('A', 101);
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.TitleMaxLength, "Title", 100),
                book.GetErrors("Title").Single().ErrorMessage);

            book.Title = new string('A', 100);
            Assert.IsFalse(book.GetErrors("Title").Any());
        }
Пример #22
0
        public void BookPublisherValidationTest()
        {
            Book book = new Book();
            book.Validate();

            Assert.IsNull(book.Publisher);
            Assert.IsFalse(book.GetErrors(nameof(book.Publisher)).Any());

            book.Publisher = new string('A', 101);
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.PublisherMaxLength, "Publisher", 100),
                book.GetErrors(nameof(book.Publisher)).Single().ErrorMessage);

            book.Publisher = new string('A', 100);
            Assert.IsFalse(book.GetErrors(nameof(book.Publisher)).Any());
        }
Пример #23
0
 private void LendTo(Book book)
 {
     LendToViewModel lendToViewModel = new LendToViewModel(container.GetExportedValue<ILendToView>(), book, entityService.Persons);
     if (lendToViewModel.ShowDialog(shellService.ShellView))
     {
         book.LendTo = lendToViewModel.SelectedPerson;
     }
 }
 /// <summary>
 /// Create a new Book object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="title">Initial value of the Title property.</param>
 /// <param name="author">Initial value of the Author property.</param>
 /// <param name="pages">Initial value of the Pages property.</param>
 public static Book CreateBook(global::System.Guid id, global::System.String title, global::System.String author, global::System.Int32 pages)
 {
     Book book = new Book();
     book.Id = id;
     book.Title = title;
     book.Author = author;
     book.Pages = pages;
     return book;
 }
Пример #25
0
        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);
        }