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 );
        }
        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 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 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 );
        }