Inheritance: ValidatableModel, IFormattable
Esempio n. 1
0
        public void PersonEmailValidationTest()
        {
            Person person = new Person();
            person.Validate();

            Assert.IsNull(person.Email);
            Assert.IsFalse(person.GetErrors("Email").Any());

            person.Email = "";
            Assert.IsNull(person.Email);
            Assert.IsFalse(person.GetErrors("Email").Any());

            person.Email = new string('A', 92) + "@mail.com";
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.EmailMaxLength, "Email", 100),
                person.GetErrors("Email").Single().ErrorMessage);

            person.Email = "my." + new string('A', 88) + "@mail.com";
            Assert.IsFalse(person.GetErrors("Email").Any());

            person.Email = "harry.potter";
            Assert.AreEqual(Resources.EmailInvalid, person.GetErrors("Email").Single().ErrorMessage);

            person.Email = "harry.potter@hogwarts";
            Assert.AreEqual(Resources.EmailInvalid, person.GetErrors("Email").Single().ErrorMessage);

            person.Email = "*****@*****.**";
            Assert.IsFalse(person.GetErrors("Email").Any());
        }
        public void CreateNewEmailTest()
        {
            Person harry = new Person() { Firstname = "Harry", Email = "*****@*****.**" };
            Person ron = new Person() { Firstname = "Ron", Email = "Wrong Address" };

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

            PersonController personController = Container.GetExportedValue<PersonController>();
            personController.Initialize();

            MockPersonListView personListView = Container.GetExportedValue<MockPersonListView>();
            PersonListViewModel personListViewModel = ViewHelper.GetViewModel<PersonListViewModel>(personListView);
            MockPersonView personView = Container.GetExportedValue<MockPersonView>();
            PersonViewModel personViewModel = ViewHelper.GetViewModel<PersonViewModel>(personView);

            ICommand command = personListViewModel.CreateNewEmailCommand;
            Assert.AreEqual(command, personViewModel.CreateNewEmailCommand);

            MockEmailService emailService = Container.GetExportedValue<MockEmailService>();
            command.Execute(harry);
            Assert.AreEqual(harry.Email, emailService.ToEmailAddress);

            MockMessageService messageService = Container.GetExportedValue<MockMessageService>();
            messageService.Clear();
            emailService.ToEmailAddress = null;
            command.Execute(ron);
            Assert.AreEqual(MessageType.Error, messageService.MessageType);
            Assert.AreEqual(Resources.CorrectEmailAddress, messageService.Message);
            Assert.IsNull(emailService.ToEmailAddress);
        }
Esempio n. 3
0
 private void AddNewPerson()
 {
     Person person = new Person();
     entityService.Persons.Add(person);
     
     personListViewModel.SelectedPerson = person;
     personListViewModel.Focus();
 }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public void AddAndRemoveTest()
        {
            Person harry = new Person() { Firstname = "Harry" };
            Person ron = new Person() { Firstname = "Ron" };

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

            PersonController personController = Container.GetExportedValue<PersonController>();
            personController.Initialize();

            MockPersonListView personListView = Container.GetExportedValue<MockPersonListView>();
            PersonListViewModel personListViewModel = ViewHelper.GetViewModel<PersonListViewModel>(personListView);
            personListViewModel.PersonCollectionView = personListViewModel.Persons;
            MockPersonView personView = Container.GetExportedValue<MockPersonView>();
            PersonViewModel personViewModel = ViewHelper.GetViewModel<PersonViewModel>(personView);

            // Add a new Person
            Assert.AreEqual(2, entityService.Persons.Count);
            Assert.IsTrue(personListViewModel.AddNewCommand.CanExecute(null));
            personListViewModel.AddNewCommand.Execute(null);
            Assert.AreEqual(3, entityService.Persons.Count);

            // Check that the new Person is selected and the first control gets the focus
            Assert.AreEqual(entityService.Persons.Last(), personViewModel.Person);
            Assert.IsTrue(personListView.FirstCellHasFocus);

            // Simulate an invalid UI input state => the user can't add more persons
            AssertHelper.CanExecuteChangedEvent(personListViewModel.AddNewCommand, () =>
                personViewModel.IsValid = false);
            Assert.IsFalse(personListViewModel.AddNewCommand.CanExecute(null));
            Assert.IsFalse(personListViewModel.RemoveCommand.CanExecute(null));

            // Remove the last two Persons at once
            personViewModel.IsValid = true;
            personListView.FirstCellHasFocus = false;
            personListViewModel.AddSelectedPerson(ron);
            personListViewModel.AddSelectedPerson(entityService.Persons.Last());
            Assert.IsTrue(personListViewModel.RemoveCommand.CanExecute(null));
            personListViewModel.RemoveCommand.Execute(null);
            Assert.IsTrue(entityService.Persons.SequenceEqual(new Person[] { harry }));
            Assert.AreEqual(harry, personViewModel.Person);
            Assert.IsTrue(personListView.FirstCellHasFocus);

            // Deselect all Persons => the Remove command must be deactivated
            AssertHelper.CanExecuteChangedEvent(personListViewModel.RemoveCommand, () =>
            {
                personListViewModel.SelectedPersons.ToList().ForEach(x => personListViewModel.RemoveSelectedPerson(x));
                personListViewModel.SelectedPerson = null;
            });
            Assert.IsFalse(personListViewModel.RemoveCommand.CanExecute(null));
        }
        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);
        }
Esempio n. 7
0
        public void GeneralPersonTest()
        {
            Person person = new Person();
            Assert.IsNotNull(person.Id);

            person.Firstname = "Harry";
            person.Lastname = "Potter";
            person.Email = "*****@*****.**";

            Assert.IsTrue(person.Validate());

            Assert.AreEqual("Harry Potter", person.ToString(null, CultureInfo.InvariantCulture));
        }
Esempio n. 8
0
        public void PersonLastnameValidationTest()
        {
            Person person = new Person();
            person.Validate();

            Assert.AreEqual("", person.Lastname);
            Assert.AreEqual(Resources.LastnameMandatory, person.GetErrors("Lastname").Single().ErrorMessage);

            person.Lastname = new string('A', 31);
            Assert.AreEqual(string.Format(CultureInfo.CurrentCulture, Resources.LastnameMaxLength, "Lastname",30),
                person.GetErrors("Lastname").Single().ErrorMessage);

            person.Lastname = new string('A', 30);
            Assert.IsFalse(person.GetErrors("Lastname").Any());
        }
        public void GroupBooksTest()
        {
            Person harryPotter = new Person() { Firstname = "Harry", Lastname = "Potter", Email = "*****@*****.**" };

            IEnumerable<Book> books = new List<Book>()
            {
                new Book() { LendTo = harryPotter, Title = "Serenity, Vol 1: Those Left Behind", Author = "Joss Whedon, Brett Matthews, Will Conrad" },
                new Book() { LendTo = harryPotter, Title = "Star Wars - Heir to the Empire", Author = "Timothy Zahn" },
                new Book() { LendTo = harryPotter, Title = "The Lord of the Rings - The Fellowship of the Ring", Author = "J.R.R. Tolkien" }
            };

            var dataModel = new BorrowedBooksReportDataModel(books);
            Assert.AreEqual(harryPotter, dataModel.GroupedBooks.Single().Key);
            Assert.AreEqual(3, dataModel.GroupedBooks.Single().Count());
        }
Esempio n. 10
0
        public void PersonViewModelPersonTest()
        {
            MockPersonView personView = new MockPersonView();
            PersonViewModel personViewModel = new PersonViewModel(personView);

            Assert.IsFalse(personViewModel.IsEnabled);

            Person person = new Person();
            AssertHelper.PropertyChangedEvent(personViewModel, x => x.Person, () => personViewModel.Person = person);
            Assert.AreEqual(person, personViewModel.Person);
            Assert.IsTrue(personViewModel.IsEnabled);

            AssertHelper.PropertyChangedEvent(personViewModel, x => x.IsEnabled, () => personViewModel.Person = null);
            Assert.IsNull(personViewModel.Person);
            Assert.IsFalse(personViewModel.IsEnabled);
        }
Esempio n. 11
0
        public void AddAndRemoveDisableTest()
        {
            var harry = new Person() { Firstname = "Harry" };

            var entityService = Container.GetExportedValue<IEntityService>();
            entityService.Persons.Add(harry);

            var personController = Container.GetExportedValue<PersonController>();
            personController.Initialize();

            var personListViewModel = Container.GetExportedValue<PersonListViewModel>();
            personListViewModel.AddSelectedPerson(personListViewModel.Persons.Single());
            var personViewModel = Container.GetExportedValue<PersonViewModel>();

            CheckCommand(personListViewModel.AddNewCommand, personListViewModel, personViewModel);
            CheckCommand(personListViewModel.RemoveCommand, personListViewModel, personViewModel);
        }
 /// <summary>
 /// Create a new Person object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="firstname">Initial value of the Firstname property.</param>
 /// <param name="lastname">Initial value of the Lastname property.</param>
 public static Person CreatePerson(global::System.Guid id, global::System.String firstname, global::System.String lastname)
 {
     Person person = new Person();
     person.Id = id;
     person.Firstname = firstname;
     person.Lastname = lastname;
     return person;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Persons EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToPersons(Person person)
 {
     base.AddObject("Persons", person);
 }
        public void RemoveAndSelection3Test()
        {
            Person harry = new Person() { Firstname = "Harry" };
            Person ron = new Person() { Firstname = "Ron" };
            Person ginny = new Person() { Firstname = "Ginny" };

            IEntityService entityService = Container.GetExportedValue<IEntityService>();
            entityService.Persons.Add(harry);
            entityService.Persons.Add(ron);
            entityService.Persons.Add(ginny);

            PersonController personController = Container.GetExportedValue<PersonController>();
            personController.Initialize();

            MockPersonListView personListView = Container.GetExportedValue<MockPersonListView>();
            PersonListViewModel personListViewModel = ViewHelper.GetViewModel<PersonListViewModel>(personListView);
            personListViewModel.PersonCollectionView = personListViewModel.Persons;

            // Remove all persons and check that nothing is selected anymore
            personListViewModel.SelectedPerson = harry;
            personListViewModel.SelectedPersons.Add(harry);
            personListViewModel.SelectedPersons.Add(ron);
            personListViewModel.SelectedPersons.Add(ginny);
            personListViewModel.RemoveCommand.Execute(null);
            Assert.IsFalse(entityService.Persons.Any());
            Assert.IsNull(personListViewModel.SelectedPerson);
        }
        public void RemoveAndSelection2Test()
        {
            Person harry = new Person() { Firstname = "Harry" };
            Person ron = new Person() { Firstname = "Ron" };
            Person ginny = new Person() { Firstname = "Ginny" };

            IEntityService entityService = Container.GetExportedValue<IEntityService>();
            entityService.Persons.Add(harry);
            entityService.Persons.Add(ron);
            entityService.Persons.Add(ginny);

            PersonController personController = Container.GetExportedValue<PersonController>();
            personController.Initialize();

            MockPersonListView personListView = Container.GetExportedValue<MockPersonListView>();
            PersonListViewModel personListViewModel = ViewHelper.GetViewModel<PersonListViewModel>(personListView);
            // Set the sorting to: "Ginny", "Harry", "Ron"
            personListViewModel.PersonCollectionView = personListViewModel.Persons.OrderBy(p => p.Firstname);

            // Remove the last person and check that the last one is selected again.
            personListViewModel.SelectedPerson = ron;
            personListViewModel.SelectedPersons.Add(personListViewModel.SelectedPerson);
            personListViewModel.RemoveCommand.Execute(null);
            Assert.IsTrue(entityService.Persons.SequenceEqual(new[] { harry, ginny }));
            Assert.AreEqual(harry, personListViewModel.SelectedPerson);
        }