public void ChangingMealTypeDescriptionMarksDescriptionIsDirtyChanged()
        {
            var mealType = new MealType( Guid.NewGuid(), "Peanut", "Butter Jelly Time", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Description = "with a baseball bat";

             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Description" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
        }
        public void ChangingMealTypeNameMarksNameIsDirtyIsValidChanged()
        {
            var mealType = new MealType( Guid.NewGuid(), "Peanut", "Butter Jelly Time", DateTime.Now, false );
             var viewModel = CreateViewModelForMealType( mealType );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Name = "Jelly";

             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
        public void FoodItemNodeVewModelModifyFoodItem()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             DataRepository dataRepository = new DataRepository( configurationMock.Object );

             FoodItemNodeViewModel foodItemNodeViewModel = new FoodItemNodeViewModel( dataRepository, null );
             Int32 originalChildCount = foodItemNodeViewModel.Children.Count;

             FoodItem foodItem = dataRepository.GetAllFoodItems().ElementAt( 0 );
             String originalName = foodItem.Name;

             ClickableTreeNodeViewModel foodItemInTree = null;
             foreach (ClickableTreeNodeViewModel child in foodItemNodeViewModel.Children)
             {
            child.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
            if ((Guid)child.Parameter == foodItem.ID)
            {
               foodItemInTree = child;
            }
             }

             Assert.IsNotNull( foodItemInTree );
             Assert.AreEqual( originalName, foodItemInTree.Name );

             foodItem.Name += "Modified";
             dataRepository.SaveItem( foodItem );

             Assert.AreEqual( originalName + "Modified", foodItemInTree.Name );
             Assert.AreEqual( 1, propertyChangedHandler.PropertiesChanged.Count );
             Assert.AreEqual( foodItemInTree, propertyChangedHandler.Sender );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
        }
        public void ModifyingMealOnDateUpdatesCaloriesAndFoodGroups()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var loggerMock = new Mock<ILoggerFacade>();
             var dataRepositoryMock = new Mock<IDataRepository>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();
             var navigationServiceMock = new Mock<IRegionNavigationService>();

             var data = new MockData();
             var fullMealList = data.Meals();
             var todaysMeals = (from meal in fullMealList
                            where meal.DateAndTimeOfMeal.Date == DateTime.Today
                            select meal).ToList();
             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) )
            .Returns( new ReadOnlyCollection<Meal>( todaysMeals ) );

             var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             viewModel.CurrentDate = DateTime.Today;
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings
                                              select foodGroupServing.Quantity).Sum();
             Decimal originalCalories = viewModel.Calories;

             var foodGroupServings = viewModel.FoodGroupServings;
             Assert.AreEqual( 5, foodGroupServings.Count );
             var serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 5.0M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 4.75M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 3.5M, serving.Quantity );

             var mealToModify = todaysMeals.Find( x => x.TypeOfMeal.Name == "Breakfast" );
             Assert.IsNotNull( mealToModify );
             mealToModify.FoodItemServings.Remove( mealToModify.FoodItemServings.Find( x => x.Entity.Name == "Orange Juice" ) );
             dataRepositoryMock.Raise( x => x.ItemModified += null, new RepositoryObjectEventArgs( mealToModify ) );

             Assert.AreEqual( originalFoodGroupServings - 2.5M,
                          (from foodGroupServing in viewModel.FoodGroupServings
                           select foodGroupServing.Quantity).Sum() );
             Assert.AreEqual( originalCalories - 250, viewModel.Calories );

             foodGroupServings = viewModel.FoodGroupServings;
             Assert.AreEqual( 4, foodGroupServings.Count );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" );
             Assert.IsNull( serving );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 5.0M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 4.75M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 3.5M, serving.Quantity );

             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
             Assert.AreEqual( viewModel, propertyChangedHandler.Sender );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Calories" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupServings" ) );
        }
        public void ModifyingMealNotOnDateDoesNotUpdateCaloriesAndFoodGroups()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var loggerMock = new Mock<ILoggerFacade>();
             var dataRepositoryMock = new Mock<IDataRepository>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();
             var navigationServiceMock = new Mock<IRegionNavigationService>();

             var data = new MockData();
             var fullMealList = data.Meals();
             var todaysMeals = (from meal in fullMealList
                            where meal.DateAndTimeOfMeal.Date == DateTime.Today
                            select meal).ToList();
             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) )
            .Returns( new ReadOnlyCollection<Meal>( todaysMeals ) );

             var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             viewModel.CurrentDate = DateTime.Today;
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings
                                              select foodGroupServing.Quantity).Sum();
             Decimal originalCalories = viewModel.Calories;

             var mealToModify = new Meal( fullMealList.Find( x => x.TypeOfMeal.Name == "Breakfast" && x.DateAndTimeOfMeal.Date != DateTime.Today ) );
             Assert.IsNotNull( mealToModify );
             mealToModify.FoodItemServings.Remove( mealToModify.FoodItemServings.Find( x => x.Entity.Name == "Orange Juice" ) );
             dataRepositoryMock.Raise( x => x.ItemModified += null, new RepositoryObjectEventArgs( mealToModify ) );

             Assert.AreEqual( originalFoodGroupServings,
                          (from foodGroupServing in viewModel.FoodGroupServings
                           select foodGroupServing.Quantity).Sum() );
             Assert.AreEqual( originalCalories, viewModel.Calories );

             Assert.AreEqual( 0, propertyChangedHandler.PropertiesChanged.Count );
        }
        public void AddingMealForDateToRepositoryUpdatesCaloriesAndFoodGroups()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var loggerMock = new Mock<ILoggerFacade>();
             var dataRepositoryMock = new Mock<IDataRepository>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();
             var navigationServiceMock = new Mock<IRegionNavigationService>();

             var data = new MockData();
             var fullMealList = data.Meals();
             var todaysMeals = (from meal in fullMealList
                            where meal.DateAndTimeOfMeal.Date == DateTime.Today
                            select meal).ToList();
             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) ).Returns(
            new ReadOnlyCollection<Meal>( todaysMeals ) );

             var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             viewModel.CurrentDate = DateTime.Today;
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Int32 originalFoodGroupCount = viewModel.FoodGroupServings.Count;
             Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings
                                              select foodGroupServing.Quantity).Sum();
             Decimal originalCalories = viewModel.Calories;

             var foodGroupServings = viewModel.FoodGroupServings;
             Assert.AreEqual( 5, foodGroupServings.Count );
             var serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 5.0M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 4.75M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 3.5M, serving.Quantity );

             var foodItem = new FoodItem( Guid.NewGuid(), "test", "", 42 );
             foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test1", "" ), 2 ) );
             foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test2", "" ), 1 ) );
             var newMeal = new Meal(
            Guid.NewGuid(), new MealType( Guid.NewGuid(), "Doesn't Matter", "", DateTime.Now, false ), DateTime.Now, "Test Meal", "Just a test" );
             newMeal.FoodItemServings.Add( new Serving<FoodItem>( foodItem, 1 ) );
             todaysMeals.Add( newMeal );
             dataRepositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( newMeal ) );
             Assert.AreEqual( originalCalories + 42, viewModel.Calories );
             Assert.AreEqual( originalFoodGroupCount + 2, viewModel.FoodGroupServings.Count );
             Assert.AreEqual( originalFoodGroupServings + 3, (from foodGroupServing in viewModel.FoodGroupServings
                                                          select foodGroupServing.Quantity).Sum() );

             foodGroupServings = viewModel.FoodGroupServings;
             Assert.AreEqual( 7, foodGroupServings.Count );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Fruit" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Starch" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 5.0M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Dairy" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 4.75M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Meat" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Vegetable" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 3.5M, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Test1" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 2, serving.Quantity );
             serving = foodGroupServings.ToList().Find( x => x.Entity.Name == "Test2" );
             Assert.IsNotNull( serving );
             Assert.AreEqual( 1, serving.Quantity );

             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
             Assert.AreEqual( viewModel, propertyChangedHandler.Sender );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Calories" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupServings" ) );
        }
        public void AddingMealForOtherDateToRepositoryDoesNotUpdateCaloriesAndFoodGroups()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var loggerMock = new Mock<ILoggerFacade>();
             var dataRepositoryMock = new Mock<IDataRepository>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();
             var navigationServiceMock = new Mock<IRegionNavigationService>();

             var data = new MockData();
             var fullMealList = data.Meals();
             var todaysMeals = (from meal in fullMealList
                            where meal.DateAndTimeOfMeal.Date == DateTime.Today
                            select meal).ToList();
             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Today ) ).Returns(
            new ReadOnlyCollection<Meal>( todaysMeals ) );

             var viewModel = new DailyLogViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             viewModel.CurrentDate = DateTime.Today;
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Int32 originalFoodGroupCount = viewModel.FoodGroupServings.Count;
             Decimal originalFoodGroupServings = (from foodGroupServing in viewModel.FoodGroupServings
                                              select foodGroupServing.Quantity).Sum();
             Decimal originalCalories = viewModel.Calories;

             var foodItem = new FoodItem( Guid.NewGuid(), "test", "", 42 );
             foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test1", "" ), 2 ) );
             foodItem.FoodGroupsPerServing.Add( new Serving<FoodGroup>( new FoodGroup( Guid.NewGuid(), "Test2", "" ), 1 ) );
             var newMeal = new Meal(
            Guid.NewGuid(), new MealType( Guid.NewGuid(), "Doesn't Matter", "", DateTime.Now, false ), DateTime.Now.AddDays( 1 ), "Test Meal", "Just a test" );
             newMeal.FoodItemServings.Add( new Serving<FoodItem>( foodItem, 1 ) );
             dataRepositoryMock.Raise( e => e.ItemAdded += null, new RepositoryObjectEventArgs( newMeal ) );
             Assert.AreEqual( originalCalories, viewModel.Calories );
             Assert.AreEqual( originalFoodGroupCount, viewModel.FoodGroupServings.Count );
             Assert.AreEqual( originalFoodGroupServings, (from foodGroupServing in viewModel.FoodGroupServings
                                                      select foodGroupServing.Quantity).Sum() );

             Assert.AreEqual( 0, propertyChangedHandler.PropertiesChanged.Count );
        }
        public void ServingViewModelPropertyChanged()
        {
            MealType pi = new MealType( Guid.NewGuid(), "pi", "not the tasty kind", DateTime.Now, false );
             MealType e = new MealType( Guid.NewGuid(), "e", "exponent", DateTime.Now, false );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             Serving<MealType> serving = new Serving<MealType>( pi, 3.14159M );
             ServingViewModel<MealType> servingViewModel = new ServingViewModel<MealType>( serving );
             servingViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;

             Assert.AreEqual( pi, servingViewModel.Entity );
             Assert.AreEqual( 3.14159M, servingViewModel.Quantity );
             Assert.IsNull( propertyChangedHandler.Sender );
             Assert.AreEqual( 0, propertyChangedHandler.PropertiesChanged.Count );

             servingViewModel.Entity = e;
             Assert.AreEqual( servingViewModel, propertyChangedHandler.Sender );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Entity" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( e, serving.Entity );
             Assert.AreEqual( 3.14159M, serving.Quantity );

             propertyChangedHandler.Reset();
             servingViewModel.Quantity = 2.71828183M;
             Assert.AreEqual( servingViewModel, propertyChangedHandler.Sender );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Quantity" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( e, serving.Entity );
             Assert.AreEqual( 2.71828183M, serving.Quantity );
        }
        public void MealTemplateViewModelName()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             MealTemplate mealTemplate = dataRepository.GetMealTemplate( FullTestData.CheeseBurgerLunchID );
             MealTemplateViewModel testMealTemplateViewModel =
            new MealTemplateViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "MealTemplateView?ID=" + mealTemplate.ID.ToString(), UriKind.Relative ) );
             testMealTemplateViewModel.OnNavigatedTo( navigationContext );
             testMealTemplateViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( mealTemplate.Name, testMealTemplateViewModel.Name );
             Assert.IsFalse( testMealTemplateViewModel.IsDirty );
             Assert.IsTrue( testMealTemplateViewModel.IsValid );

             testMealTemplateViewModel.Name = "Some other name";
             Assert.AreEqual( "Some other name", testMealTemplateViewModel.Name );
             Assert.IsTrue( testMealTemplateViewModel.IsDirty );
             Assert.IsTrue( testMealTemplateViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             testMealTemplateViewModel.Name = mealTemplate.Name;
             Assert.IsFalse( testMealTemplateViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Make the name invalid, show that the error is set properly
             testMealTemplateViewModel.Name = "";
             Assert.IsTrue( String.IsNullOrEmpty( testMealTemplateViewModel.Name ) );
             Assert.IsTrue( testMealTemplateViewModel.IsDirty );
             Assert.IsFalse( testMealTemplateViewModel.IsValid );
             Assert.AreEqual( Messages.Error_No_Name, testMealTemplateViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
        }
        public void FoodItemViewModelName()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();
             FoodItem testFoodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel testFoodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             // Tests:
             //   o Name is the same as the object from the repository
             //   o Changing the name causes appropriate properties to be marked as changed
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + testFoodItem.ID.ToString(), UriKind.Relative ) );
             testFoodItemViewModel.OnNavigatedTo( navigationContext );
             testFoodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( testFoodItem.Name, testFoodItemViewModel.Name );

             testFoodItemViewModel.Name = "Some other name";
             Assert.AreEqual( "Some other name", testFoodItemViewModel.Name );

             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
예제 #11
0
        public void ChangingMealTypeRaisesPropertyChangedNotIncludingTimeIfTimeWillNotChange()
        {
            var propertyChangedHandler = new PropertyChangedHandler();

             var viewModel = CreateEmptyViewModel();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.TypeOfMeal = new MealType( Guid.NewGuid(), "test", "", DateTime.Now, false );

             Assert.AreEqual( 4, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "TypeOfMeal" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
예제 #12
0
        public void ChangingFoodItemsRaisesPropertyChanged()
        {
            var propertyChangedHandler = new PropertyChangedHandler();

             var viewModel = CreateEmptyViewModel();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.FoodItemServings.Add( new ServingViewModel<FoodItem>( new FoodItem( Guid.NewGuid(), "test", "", 42 ), 2 ) );

             Assert.AreEqual( 5, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodItemServings" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupServings" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Calories" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
예제 #13
0
        public void ChangingDescriptionRaisesPropertyChanged()
        {
            var propertyChangedHandler = new PropertyChangedHandler();

             var viewModel = CreateEmptyViewModel();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Description = "This is a test";

             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Description" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
        }
        public void MealNodeModifiedIfMealModified()
        {
            PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             var dataRepositoryMock = new Mock<IDataRepository>();
             var data = new MockData();
             var allMeals = data.Meals();

             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Now.Date ) ).Returns(
            new ReadOnlyCollection<Meal>( (from meal in allMeals
                                           where meal.DateAndTimeOfMeal.Date == DateTime.Now.Date
                                           select meal).ToList() ) );
             dataRepositoryMock.Setup( x => x.GetAllMealsForDate( DateTime.Now.Date.AddDays( MockData.DaysToAddForFutureMeals ) ) ).Returns(
            new ReadOnlyCollection<Meal>( (from meal in allMeals
                                           where meal.DateAndTimeOfMeal.Date == DateTime.Now.Date.AddDays( MockData.DaysToAddForFutureMeals )
                                           select meal).ToList() ) );

             var mealNodeViewModel = new MealNodeViewModel( dataRepositoryMock.Object, null );
             Assert.AreEqual( 3, mealNodeViewModel.Children.Count );

             var modifiedMeal = allMeals.Find( m => m.DateAndTimeOfMeal.Date == DateTime.Today && m.TypeOfMeal.Name == "Dinner" );
             Assert.IsNotNull( modifiedMeal );
             String originalName = modifiedMeal.Name;

             ClickableTreeNodeViewModel mealInTree = null;
             foreach (ClickableTreeNodeViewModel child in mealNodeViewModel.Children)
             {
            child.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
            if ((Guid)child.Parameter == modifiedMeal.ID)
            {
               mealInTree = child;
            }
             }

             Assert.IsNotNull( mealInTree );
             Assert.AreEqual( originalName, mealInTree.Name );

             modifiedMeal.Name += "Modified";
             dataRepositoryMock.Raise( e => e.ItemModified += null, new RepositoryObjectEventArgs( modifiedMeal ) );

             Assert.AreEqual( originalName + "Modified", mealInTree.Name );
             Assert.AreEqual( 1, propertyChangedHandler.PropertiesChanged.Count );
             Assert.AreEqual( mealInTree, propertyChangedHandler.Sender );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
        }
        public void FoodItemViewModelCaloriesPerServing()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             // Construct a known food group mealType and make sure the food group servings makes sense.
             FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel foodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) );
             foodItemViewModel.OnNavigatedTo( navigationContext );
             foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( foodItem.CaloriesPerServing, foodItemViewModel.CaloriesPerServing );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );

             // Change the calories
             foodItemViewModel.CaloriesPerServing = 700;
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.CaloriesPerServing = 650;
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             propertyChangedHandler.Reset();

             // Make the calories invalid
             foodItemViewModel.CaloriesPerServing = -700;
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_Negative_Calories, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "CaloriesPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
        }
        public void FoodItemViewModelFoodGroupServings()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             // Navigate to a known food group mealType and make sure the food group servings makes sense.
             // NOTE: No need to get too fancy, the model tests makes sure the data loaded properly.
             FoodItem foodItem = dataRepository.GetFoodItem( FullTestData.CheeseBurgerID );
             FoodItemViewModel foodItemViewModel =
            new FoodItemViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "FoodItemView?ID=" + foodItem.ID.ToString(), UriKind.Relative ) );
             foodItemViewModel.OnNavigatedTo( navigationContext );
             foodItemViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );

             // Add a food group
             ServingViewModel<FoodGroup> testFoodServings = new ServingViewModel<FoodGroup>(
            dataRepository.GetFoodGroup( FullTestData.JunkFoodID ), 1 );
             foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings );

             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Delete a food group
             testFoodServings = foodItemViewModel.FoodGroupsPerServing.ElementAt( 0 );
             foodItemViewModel.FoodGroupsPerServing.Remove( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing.Add( testFoodServings );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Modify a food group
             foodItemViewModel.FoodGroupsPerServing[0].Quantity += 1;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Serving<FoodGroup> modelServing = foodItem.FoodGroupsPerServing.Find( f => f.Entity.ID == foodItemViewModel.FoodGroupsPerServing[0].Entity.ID );
             Assert.IsNotNull( modelServing );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing[0].Quantity -= 1;
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Make a serving invalid.
             foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1;
             Assert.AreEqual( foodItem.FoodGroupsPerServing.Count, foodItemViewModel.FoodGroupsPerServing.Count );
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_No_Quantity, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );

             foodItemViewModel.FoodGroupsPerServing[0].Quantity *= -1;
             Assert.IsTrue( foodItemViewModel.IsValid );
             Assert.IsFalse( foodItemViewModel.IsDirty );
             propertyChangedHandler.Reset();

             // Remove all food groups
             foodItemViewModel.FoodGroupsPerServing.Clear();
             Assert.IsTrue( foodItemViewModel.IsDirty );
             Assert.IsFalse( foodItemViewModel.IsValid );
             Assert.AreEqual( Messages.Error_No_FoodGroups, foodItemViewModel.Error );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "FoodGroupsPerServing" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
        }
예제 #17
0
        public void ChangingNameRaisesPropertyChanged()
        {
            var propertyChangedHandler = new PropertyChangedHandler();

             var viewModel = CreateEmptyViewModel();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.Name = "This is a test";

             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Name" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
예제 #18
0
        public void ChangingTimeRaisesPropertyChanged()
        {
            var propertyChangedHandler = new PropertyChangedHandler();

             var viewModel = CreateEmptyViewModel();
             viewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             viewModel.TimeOfMeal = DateTime.Now.AddDays( 2.5 );

             Assert.AreEqual( 3, propertyChangedHandler.PropertiesChanged.Count );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "TimeOfMeal" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsValid" ) );
        }
        public void MealTemplateViewModelDescription()
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var configurationMock = new Mock<IConfiguration>();
             configurationMock.Setup( x => x.DataSource ).Returns( DataSourceType.XMLFile );
             configurationMock.Setup( x => x.FileName ).Returns( FullTestData.DataFileName );

             FullTestData.Reset();
             var dataRepository = new DataRepository( configurationMock.Object );

             PropertyChangedHandler propertyChangedHandler = new PropertyChangedHandler();

             MealTemplate testMealTemplate = dataRepository.GetMealTemplate( FullTestData.CheeseBurgerLunchID );
             MealTemplateViewModel testMealTemplateViewModel =
            new MealTemplateViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             NavigationContext navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "MealTempalte?ID=" + testMealTemplate.ID.ToString(), UriKind.Relative ) );
             testMealTemplateViewModel.OnNavigatedTo( navigationContext );
             testMealTemplateViewModel.PropertyChanged += propertyChangedHandler.OnPropertyChanged;
             Assert.AreEqual( testMealTemplate.Description, testMealTemplateViewModel.Description );
             Assert.IsFalse( testMealTemplateViewModel.IsDirty );
             Assert.IsTrue( testMealTemplateViewModel.IsValid );

             testMealTemplateViewModel.Description = "Some other description";
             Assert.AreEqual( "Some other description", testMealTemplateViewModel.Description );
             Assert.IsTrue( testMealTemplateViewModel.IsDirty );
             Assert.IsTrue( testMealTemplateViewModel.IsValid );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "Description" ) );
             Assert.IsTrue( propertyChangedHandler.PropertiesChanged.Contains( "IsDirty" ) );
             Assert.AreEqual( 2, propertyChangedHandler.PropertiesChanged.Count );
        }