private MealTypeViewModel CreateViewModelForMealType(
         MealType mealType, Mock<IDataRepository> dataRepositoryMock, Mock<IRegionManager> regionManagerMock, Mock<IInteractionService> interactionServiceMock )
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();

             var viewModel =
            new MealTypeViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             dataRepositoryMock.Setup( x => x.GetMealType( mealType.ID ) ).Returns( mealType );

             var navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "MealTypeView?ID=" + mealType.ID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             return viewModel;
        }
        /// <summary>
        /// Close test template.  All of the close tests follow this based pattern, so they all call this rather than repeating everything
        /// </summary>
        private void RunCloseTest( Boolean makeDirty, Boolean makeInvalid, MessageBoxResult messageResponse, Boolean expectRemove, Boolean expectSave )
        {
            var loggerMock = new Mock<ILoggerFacade>();
             Mock<IDataRepository> dataRepositoryMock = new Mock<IDataRepository>();
             Mock<IRegionNavigationService> regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             Mock<IRegionManager> regionManagerMock = new Mock<IRegionManager>();
             Mock<IRegion> regionWithoutViewMock = new Mock<IRegion>();
             Mock<IRegion> regionMock = new Mock<IRegion>();
             Mock<IInteractionService> interactionServiceMock = new Mock<IInteractionService>();

             MealTypeViewModel viewModel = new MealTypeViewModel(
            dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );
             UserControl view = new UserControl();
             view.DataContext = viewModel;

             // Set up two regions each with their own set of views.
             List<UserControl> views = new List<UserControl>();
             views.Add( new UserControl() );
             views.Add( view );
             views.Add( new UserControl() );
             views.Add( new UserControl() );

             List<UserControl> viewsWithoutView = new List<UserControl>();
             viewsWithoutView.Add( new UserControl() );
             viewsWithoutView.Add( new UserControl() );

             List<IRegion> regions = new List<IRegion>();
             regions.Add( regionMock.Object );

             regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() );
             regionWithoutViewMock.Setup( x => x.Views.GetEnumerator() ).Returns( viewsWithoutView.GetEnumerator() );
             regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() );

             // Setup a mealTemplate type in the mock repository, navigate to it
             MealType mealType = new MealType( Guid.NewGuid(), "Test Meal Type", "Test Meal Type Description", DateTime.Now, false );
             dataRepositoryMock.Setup( x => x.GetMealType( mealType.ID ) ).Returns( mealType );
             dataRepositoryMock.Setup( x => x.Contains( mealType ) ).Returns( true );
             NavigationContext navigationContext =
            new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealTypeView?ID=" + mealType.ID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             if (makeDirty)
             {
            if (makeInvalid)
            {
               interactionServiceMock
                  .Setup( x => x.ShowMessageBox( Messages.Question_MealType_Close, DisplayStrings.CloseCaption, MessageBoxButton.YesNo, MessageBoxImage.Question ) )
                  .Returns( messageResponse );
               viewModel.Name = "";
               Assert.IsTrue( viewModel.IsDirty );
               Assert.IsFalse( viewModel.IsValid );
            }
            else
            {
               interactionServiceMock
                  .Setup( x => x.ShowMessageBox( Messages.Question_MealType_Save, DisplayStrings.SaveChangesCaption, MessageBoxButton.YesNoCancel, MessageBoxImage.Question ) )
                  .Returns( messageResponse );
               viewModel.Name = "Something Else";
               Assert.IsTrue( viewModel.IsDirty );
               Assert.IsTrue( viewModel.IsValid );
            }
             }
             else
             {
            // This will fail if we have passed in the non-sensical makeDirty == false, makeInvalid == true
            Assert.AreEqual( makeDirty, viewModel.IsDirty );
             }

             // Attempt a close.
             viewModel.CloseCommand.Execute( null );

             // If we were dirty, then we need to verify that the correct interaction was done, otherwise, that no interaction was done
             if (makeDirty)
             {
            interactionServiceMock.VerifyAll();
             }
             else
             {
            interactionServiceMock.Verify(
               x => x.ShowMessageBox( It.IsAny<String>(), It.IsAny<String>(), It.IsAny<MessageBoxButton>(), It.IsAny<MessageBoxImage>() ), Times.Never() );
             }

             if (expectRemove)
             {
            regionMock.Verify( x => x.Remove( view ), Times.Exactly( 1 ) );
            regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Exactly( 1 ) );
             }
             else
             {
            regionMock.Verify( x => x.Remove( It.IsAny<UserControl>() ), Times.Never() );
             }

             if (expectSave)
             {
            dataRepositoryMock.Verify( x => x.SaveItem( mealType ), Times.Exactly( 1 ) );
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<MealType>() ), Times.Exactly( 1 ) );
             }
             else
             {
            dataRepositoryMock.Verify( x => x.SaveItem( It.IsAny<MealType>() ), Times.Never() );
             }
        }
        private UserControl CreateViewInRegion( MealTypeViewModel viewModel, Mock<IRegion> regionMock, Mock<IRegionManager> regionManagerMock )
        {
            // Set up region manager so we can determine if the view get removed or not.
             var view = new UserControl();
             view.DataContext = viewModel;

             var views = new List<UserControl>();
             views.Add( new UserControl() );
             views.Add( view );
             views.Add( new UserControl() );
             views.Add( new UserControl() );

             var regions = new List<IRegion>();
             regions.Add( regionMock.Object );

             regionManagerMock.Setup( x => x.Regions.GetEnumerator() ).Returns( regions.GetEnumerator() );
             regionMock.Setup( x => x.Views.GetEnumerator() ).Returns( views.GetEnumerator() );

             return view;
        }
        private MealTypeViewModel CreateEmptyViewModel( Mock<IDataRepository> dataRepositoryMock )
        {
            var loggerMock = new Mock<ILoggerFacade>();
             var regionNavigationServiceMock = new Mock<IRegionNavigationService>();
             var regionManagerMock = new Mock<IRegionManager>();
             var interactionServiceMock = new Mock<IInteractionService>();

             var viewModel =
            new MealTypeViewModel( dataRepositoryMock.Object, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             var navigationContext = new NavigationContext(
            regionNavigationServiceMock.Object, new Uri( "MealTypeView", UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );

             return viewModel;
        }
 private void AssertMealTypeContents( 
  MealTypeViewModel viewModel, String name, String description, DateTime defaultTimeOfMeal, Boolean useDefault, Boolean canUndo, Boolean canRedo, String message )
 {
     Assert.AreEqual( name, viewModel.Name, message );
      Assert.AreEqual( description, viewModel.Description, message );
      Assert.AreEqual( defaultTimeOfMeal, viewModel.DefaultTimeOfMeal, message );
      Assert.AreEqual( useDefault, viewModel.UseDefaultTimeOfMeal, message );
      Assert.AreEqual( canUndo, viewModel.UndoCommand.CanExecute( null ), message );
      Assert.AreEqual( canRedo, viewModel.RedoCommand.CanExecute( null ), message );
 }
        public void NavigateToNewMealType()
        {
            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 );

             var viewModel = new MealTypeViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             var navigationContext = new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealType", UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );
             Assert.AreNotEqual( FullTestData.BreakfastID, viewModel.ID );
             Assert.IsNull( viewModel.Name );
             Assert.IsNull( viewModel.Description );
             Assert.IsFalse( viewModel.IsDirty );
        }
        public void NavigateToExitingMealType()
        {
            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 );

             MealTypeViewModel viewModel = new MealTypeViewModel( dataRepository, regionManagerMock.Object, interactionServiceMock.Object, loggerMock.Object );

             // Navigate to an existing mealTemplate type
             NavigationContext navigationContext =
            new NavigationContext( regionNavigationServiceMock.Object, new Uri( "MealType?ID=" + FullTestData.BreakfastID.ToString(), UriKind.Relative ) );
             viewModel.OnNavigatedTo( navigationContext );
             Assert.AreEqual( FullTestData.BreakfastID, viewModel.ID );
             Assert.AreEqual( "Breakfast", viewModel.Name );
             Assert.AreEqual( "The most important meal of the day.", viewModel.Description );
             Assert.IsFalse( viewModel.IsDirty );
        }