Exemplo n.º 1
0
        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 CollectionChangedTest()
        {
            var originalCollection = new ObservableCollection <MyModel>()
            {
                new MyModel()
            };
            var synchronizingCollection = new SynchronizingCollection <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m));

            AssertHelper.SequenceEqual(originalCollection, synchronizingCollection.Select(dm => dm.Model));

            // Check add operation with collection changed event.
            bool handlerCalled = false;
            NotifyCollectionChangedEventHandler handler = (sender, e) =>
            {
                handlerCalled = true;
                Assert.AreEqual(synchronizingCollection, sender);
                Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action);
                Assert.AreEqual(1, e.NewStartingIndex);
                Assert.AreEqual(originalCollection.Last(), e.NewItems.Cast <MyDataModel>().Single().Model);
            };

            synchronizingCollection.CollectionChanged += handler;
            originalCollection.Add(new MyModel());
            Assert.IsTrue(handlerCalled);

            // After dispose the collection does not synchronize anymore
            handlerCalled = false;
            synchronizingCollection.Dispose();
            originalCollection.Add(new MyModel());
            Assert.IsFalse(handlerCalled);
            synchronizingCollection.CollectionChanged -= handler;
        }
Exemplo n.º 3
0
    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]);
Exemplo n.º 4
0
        public void ObservableCollectionTest()
        {
            var originalCollection = new ObservableCollection<MyModel>() { new MyModel() };
            var synchronizingCollection = new SynchronizingCollection<MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m));
            Assert.IsTrue(originalCollection.SequenceEqual(synchronizingCollection.Select(dm => dm.Model)));

            // Check add operation with collection changed event.
            bool handlerCalled = false;
            NotifyCollectionChangedEventHandler handler = (sender, e) =>
            {
                handlerCalled = true;
                Assert.AreEqual(synchronizingCollection, sender);
                Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action);
                Assert.AreEqual(1, e.NewStartingIndex);
                Assert.AreEqual(originalCollection.Last(), e.NewItems.Cast<MyDataModel>().Single().Model);
            };
            synchronizingCollection.CollectionChanged += handler;
            originalCollection.Add(new MyModel());
            Assert.IsTrue(handlerCalled);

            // After dispose the collection does not synchronize anymore
            handlerCalled = false;
            synchronizingCollection.Dispose();
            originalCollection.Add(new MyModel());
            Assert.IsFalse(handlerCalled);
            synchronizingCollection.CollectionChanged -= handler;
        }
Exemplo n.º 5
0
            static WeakReference WeakTest(ObservableCollection <MyModel> originalCollection)
            {
                var synchronizingCollection     = new SynchronizingCollection <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m));
                var weakSynchronizingCollection = new WeakReference(synchronizingCollection);

                originalCollection.Add(new MyModel());
                Assert.IsTrue(weakSynchronizingCollection.IsAlive);
                return(weakSynchronizingCollection);
            }
Exemplo n.º 6
0
 public void Initialize()
 {
     AddWeakEventListener(rateViewModel, RateViewModelPropertyChanged);
     IRateListView rateListView = container.GetExportedValue<IRateListView>();
     rateDataModels = new SynchronizingCollection<RateDataModel, Rate>(entityService.Rates, c => new RateDataModel(c));
     rateListViewModel = new RateListViewModel(rateListView, rateDataModels);
     AddWeakEventListener(rateListViewModel, RateListViewModelPropertyChanged);
     shellService.RateListView = rateListViewModel.View;
     shellService.RateView = rateViewModel.View;
     rateListViewModel.SelectedRate = rateListViewModel.Rates.FirstOrDefault();
 }
        public void WeakEventHandlerTest()
        {
            var originalCollection          = new ObservableCollection <MyModel>();
            var synchronizingCollection     = new SynchronizingCollection <MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m));
            var weakSynchronizingCollection = new WeakReference(synchronizingCollection);

            originalCollection.Add(new MyModel());
            Assert.IsTrue(weakSynchronizingCollection.IsAlive);

            synchronizingCollection = null;
            GC.Collect();
            Assert.IsNotNull(originalCollection);
            Assert.IsFalse(weakSynchronizingCollection.IsAlive);
        }
Exemplo n.º 8
0
        public void BookListViewModelFilterTest()
        {
            var books = new ObservableCollection <Book>()
            {
                new Book()
                {
                    Title = "The Fellowship of the Ring", Author = "J.R.R. Tolkien"
                },
                new Book()
                {
                    Title = "The Two Towers", Author = "J.R.R. Tolkien"
                }
            };
            var bookListView      = new MockBookListView();
            var bookDataModels    = new SynchronizingCollection <BookDataModel, Book>(books, b => new BookDataModel(b, dummyCommand));
            var bookListViewModel = new BookListViewModel(bookListView)
            {
                Books = bookDataModels
            };

            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            AssertHelper.PropertyChangedEvent(bookListViewModel, x => x.FilterText, () => bookListViewModel.FilterText = "J.");
            Assert.AreEqual("J.", bookListViewModel.FilterText);
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "J.R.R. Tolkien";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "Fell";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "Tow";
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "xyz";
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "vol";
            books.Add(new Book());
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[2]));
            books[2].Title = "Serenity, Vol 1: Those Left Behind";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[2]));
        }
Exemplo n.º 9
0
        public void WeakEventHandlerTest()
        {
            var originalCollection = new ObservableCollection<MyModel>();
            var synchronizingCollection = new SynchronizingCollection<MyDataModel, MyModel>(originalCollection, m => new MyDataModel(m));
            WeakReference weakSynchronizingCollection = new WeakReference(synchronizingCollection);

            originalCollection.Add(new MyModel());
            Assert.IsTrue(weakSynchronizingCollection.IsAlive);

            synchronizingCollection = null;
            GC.Collect();
            Assert.IsNotNull(originalCollection);
            Assert.IsFalse(weakSynchronizingCollection.IsAlive);
        }
Exemplo n.º 10
0
        public void Initialize()
        {
            PropertyChangedEventManager.AddHandler(_countryViewModel, CountryViewModel_OnPropertyChanged, "");

            _countryDataModels                  = new SynchronizingCollection <CountryDataModel, CountryEntity>(_entityService.Countries, c => new CountryDataModel(c));
            _countryListViewModel.Items         = _countryDataModels;
            _countryListViewModel.AddCommand    = _addNewCommand;
            _countryListViewModel.RemoveCommand = _removeCommand;
            PropertyChangedEventManager.AddHandler(_countryListViewModel, CountryListViewModel_OnPropertyChanged, "");

            _viewService.CountryListView = (ICountryListView)_countryListViewModel.View;
            _viewService.CountryView     = (ICountryView)_countryViewModel.View;

            _countryListViewModel.SelectedItem = _countryListViewModel.Items.FirstOrDefault();
        }
Exemplo n.º 11
0
        public void Initialize()
        {
            _provinceViewModel.SelectCountryCommand = _selectCountryCommand;
            PropertyChangedEventManager.AddHandler(_provinceViewModel, ProvinceViewModel_OnPropertyChanged, "");

            _provinceDataModels                  = new SynchronizingCollection <ProvinceDataModel, ProvinceEntity>(_entityService.Provinces, c => new ProvinceDataModel(c, _selectCountryCommand));
            _provinceListViewModel.Items         = _provinceDataModels;
            _provinceListViewModel.AddCommand    = _addNewCommand;
            _provinceListViewModel.RemoveCommand = _removeCommand;
            PropertyChangedEventManager.AddHandler(_provinceListViewModel, ProvinceListViewModel_OnPropertyChanged, "");

            _viewService.ProvinceListView = (IProvinceListView)_provinceListViewModel.View;
            _viewService.ProvinceView     = (IProvinceView)_provinceViewModel.View;

            _provinceListViewModel.SelectedItem = _provinceListViewModel.Items.FirstOrDefault();
        }
Exemplo n.º 12
0
        public void Initialize()
        {
            bookViewModel.LendToCommand = lendToCommand;
            PropertyChangedEventManager.AddHandler(bookViewModel, BookViewModelPropertyChanged, "");

            bookDataModels = new SynchronizingCollection<BookDataModel, Book>(entityService.Books, 
                b => new BookDataModel(b, lendToCommand));
            bookListViewModel.Books = bookDataModels;
            bookListViewModel.AddNewCommand = addNewCommand;
            bookListViewModel.RemoveCommand = removeCommand;
            PropertyChangedEventManager.AddHandler(bookListViewModel, BookListViewModelPropertyChanged, "");

            shellService.BookListView = bookListViewModel.View;
            shellService.BookView = bookViewModel.View;

            bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
        }
Exemplo n.º 13
0
        public void Initialize()
        {
            bookViewModel.LendToCommand = lendToCommand;
            PropertyChangedEventManager.AddHandler(bookViewModel, BookViewModelPropertyChanged, "");

            bookDataModels = new SynchronizingCollection <BookDataModel, Book>(entityService.Books,
                                                                               b => new BookDataModel(b, lendToCommand));
            bookListViewModel.Books         = bookDataModels;
            bookListViewModel.AddNewCommand = addNewCommand;
            bookListViewModel.RemoveCommand = removeCommand;
            PropertyChangedEventManager.AddHandler(bookListViewModel, BookListViewModelPropertyChanged, "");

            shellService.BookListView = bookListViewModel.View;
            shellService.BookView     = bookViewModel.View;

            bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
        }
Exemplo n.º 14
0
    public void Initialize()
    {
        bookViewModel.LendToCommand    = lendToCommand;
        bookViewModel.PropertyChanged += BookViewModelPropertyChanged;

        bookDataModels                     = new SynchronizingCollection <BookDataModel, Book>(entityService.Books, b => new BookDataModel(b, lendToCommand));
        BooksView                          = new ObservableListView <BookDataModel>(bookDataModels, null, bookListViewModel.Filter, null);
        bookListViewModel.Books            = BooksView;
        bookListViewModel.AddNewCommand    = addNewCommand;
        bookListViewModel.RemoveCommand    = removeCommand;
        bookListViewModel.PropertyChanged += BookListViewModelPropertyChanged;

        shellService.BookListView = bookListViewModel.View;
        shellService.BookView     = bookViewModel.View;

        bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
    }
Exemplo n.º 15
0
        public void Initialize()
        {
            bookViewModel.LendToCommand = lendToCommand;
            AddWeakEventListener(bookViewModel, BookViewModelPropertyChanged);

            IBookListView bookListView = container.GetExportedValue<IBookListView>();
            bookDataModels = new SynchronizingCollection<BookDataModel, Book>(entityService.Books, 
                b => new BookDataModel(b, lendToCommand));
            bookListViewModel = new BookListViewModel(bookListView, bookDataModels);
            bookListViewModel.AddNewCommand = addNewCommand;
            bookListViewModel.RemoveCommand = removeCommand;
            AddWeakEventListener(bookListViewModel, BookListViewModelPropertyChanged);

            shellService.BookListView = bookListViewModel.View;
            shellService.BookView = bookViewModel.View;

            bookListViewModel.SelectedBook = bookListViewModel.Books.FirstOrDefault();
        }
        public void DisposeTest()
        {
            var  originalCollection      = new ObservableCollection <MyModel>();
            bool factoryCalled           = false;
            var  synchronizingCollection = new SynchronizingCollection <MyDataModel, MyModel>(originalCollection, m =>
            {
                factoryCalled = true;
                return(new MyDataModel(m));
            });

            originalCollection.Add(new MyModel());
            Assert.IsTrue(factoryCalled);

            // Calling dispose twice must not throw an exception.
            synchronizingCollection.Dispose();
            synchronizingCollection.Dispose();
            factoryCalled = false;
            originalCollection.Add(new MyModel());
            Assert.IsFalse(factoryCalled);
        }
Exemplo n.º 17
0
        public void BookListViewModelFilterTest()
        {
            IList<Book> books = new ObservableCollection<Book>()
            {
                new Book() { Title = "The Fellowship of the Ring", Author = "J.R.R. Tolkien" },
                new Book() { Title = "The Two Towers", Author = "J.R.R. Tolkien" }
            };

            MockBookListView bookListView = new MockBookListView();
            var bookDataModels = new SynchronizingCollection<BookDataModel, Book>(books, b => new BookDataModel(b, dummyCommand));
            BookListViewModel bookListViewModel = new BookListViewModel(bookListView) { Books = bookDataModels };

            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            AssertHelper.PropertyChangedEvent(bookListViewModel, x => x.FilterText, () => bookListViewModel.FilterText = "J.");
            Assert.AreEqual("J.", bookListViewModel.FilterText);
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "J.R.R. Tolkien";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "Fell";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "Tow";
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[1]));

            bookListViewModel.FilterText = "xyz";
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[0]));
            Assert.IsFalse(bookListViewModel.Filter(bookDataModels[1]));

            books.Add(new Book());
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[2]));
            books[2].Title = "Serenity, Vol 1: Those Left Behind";
            Assert.IsTrue(bookListViewModel.Filter(bookDataModels[2]));
        }
Exemplo n.º 18
0
 public void Initialize(IEnumerable <MusicFile> musicFiles)
 {
     musicFileDataModels = new SynchronizingCollection <MusicFileDataModel, MusicFile>(musicFiles, x => new MusicFileDataModel(x));
 }
Exemplo n.º 19
0
 public void Initialize(IEnumerable<MusicFile> musicFiles)
 {
     musicFileDataModels = new SynchronizingCollection<MusicFileDataModel, MusicFile>(musicFiles, x => new MusicFileDataModel(x));
 }
Exemplo n.º 20
0
 // assigning the second collection as synchronized
 public void SetSynchronizedCollection(SynchronizingCollection <T> synchronized)
 {
     this._synchronizedCollection = synchronized;
     this._synchronizedCollection.CollectionChanged += new NotifyCollectionChangedEventHandler(_synchronized_CollectionChanged);
     this.CollectionChanged += new NotifyCollectionChangedEventHandler(this_CollectionChanged);
 }
Exemplo n.º 21
0
 public void Initialize(IEnumerable <KinectStream> streams)
 {
     _streams = new SynchronizingCollection <KinectStream, KinectStream>(streams, x => x);
 }