public void ProviderShouldBindIocContainerBindIocContainerTrue()
        {
            Type   typeFrom     = null;
            object item         = null;
            string name         = null;
            var    iocContainer = new IocContainerMock
            {
                BindToConstantFunc = (type, arg2, arg3) =>
                {
                    typeFrom = type;
                    item     = arg2;
                    name     = arg3;
                }
            };
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            provider.BindIocContainer = true;
            var context = new DataContext();

            IViewModel viewModel = provider.GetViewModel(container => new ViewModel(), context);

            typeFrom.ShouldEqual(typeof(IIocContainer));
            item.ShouldEqual(viewModel.IocContainer);
            name.ShouldBeNull();
        }
        public void ProviderShouldCreateIocContainerMixedModeWithoutParentViewModel()
        {
            var child        = new IocContainerMock();
            var iocContainer = new IocContainerMock
            {
                CreateChild = mock => child
            };
            var provider = GetViewModelProvider(iocContainer);
            var context  = new DataContext
            {
                { InitializationConstants.IocContainerCreationMode, IocContainerCreationMode.Mixed }
            };

            int initialize = 0;
            var vm         = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.GetData(InitializationConstants.IocContainer).ShouldEqual(child);
                    ++initialize;
                }
            };

            provider.InitializeViewModel(vm, context);
            initialize.ShouldEqual(1);
        }
Пример #3
0
        public void SetUp()
        {
            ServiceProvider.AttachedValueProvider    = new AttachedValueProvider();
            ServiceProvider.ViewModelSettingsFactory = model => new DefaultViewModelSettings();
            ServiceProvider.ReflectionManager        = new ExpressionReflectionManager();
            CanBeResolvedTypes = new List <Type>
            {
                typeof(IThreadManager),
                typeof(IViewManager),
                typeof(IDisplayNameProvider),
                typeof(IViewModelProvider),
                typeof(OperationCallbackManagerMock),
                typeof(INavigationDispatcher),
                typeof(IViewModelPresenter)
            };
            OperationCallbackManager = new OperationCallbackManagerMock();
            NavigationDispatcher     = new NavigationDispatcherMock();
            ViewManager                   = new ViewManagerMock();
            ThreadManager                 = new ThreadManagerMock();
            ViewModelPresenter            = new ViewModelPresenterMock();
            ServiceProvider.ThreadManager = ThreadManager;
            DisplayNameProvider           = new DisplayNameProviderMock();
            IocContainer                  = new IocContainerMock
            {
                GetFunc            = GetFunc,
                CanResolveDelegate = CanResolve
            };
            Tracer.TraceInformation = true;
            ServiceProvider.Tracer  = new ConsoleTracer();
            ViewModelProvider       = new ViewModelProvider(IocContainer);
            OnInit();
            var app = new UnitTestApp();

            app.Initialize(PlatformInfo.UnitTest, IocContainer, Empty.Array <Assembly>(), DataContext.Empty);
        }
Пример #4
0
 public void SetUp()
 {
     ServiceProvider.DesignTimeManager     = DesignTimeManagerImpl.Instance;
     ServiceProvider.AttachedValueProvider = new AttachedValueProvider();
     CanBeResolvedTypes = new List <Type>
     {
         typeof(IThreadManager),
         typeof(IViewModelSettings),
         typeof(IViewManager),
         typeof(IDisplayNameProvider),
         typeof(IViewModelProvider),
         typeof(IVisualStateManager),
         typeof(OperationCallbackManagerMock)
     };
     OperationCallbackManager = new OperationCallbackManagerMock();
     ViewManager   = new ViewManagerMock();
     ThreadManager = new ThreadManagerMock();
     ServiceProvider.ThreadManager = ThreadManager;
     Settings = new ViewModelSettingsMock();
     ApplicationSettings.ViewModelSettings = Settings;
     DisplayNameProvider = new DisplayNameProviderMock();
     IocContainer        = new IocContainerMock
     {
         GetFunc            = GetFunc,
         CanResolveDelegate = CanResolve
     };
     ServiceProvider.Tracer = new ConsoleTracer();
     ServiceProvider.Initialize(IocContainer, PlatformInfo.UnitTest);
     ViewModelProvider = new ViewModelProvider(IocContainer);
     OnInit();
 }
        public void ProviderShouldRestoreViewModelState()
        {
            DataConstant <string> key   = "key";
            const string          value = "value";
            var loadViewModel           = new ViewModel();
            var childIoc     = new IocContainerMock();
            var iocContainer = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    typeof(ViewModel).ShouldEqual(type);
                    return(loadViewModel);
                },
                CreateChild = mock => childIoc
            };

            childIoc.GetFunc = iocContainer.GetFunc;
            var viewModel = new ViewModel();

            viewModel.Settings.State.Add(key, value);
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            var state            = provider.PreserveViewModel(viewModel, DataContext.Empty);
            var restoreViewModel = provider.RestoreViewModel(state, DataContext.Empty, true);

            restoreViewModel.IocContainer.ShouldEqual(childIoc);
            restoreViewModel.ShouldEqual(loadViewModel);
            restoreViewModel.Settings.State.GetData(key).ShouldEqual(value);
        }
        public void GetViewModelShouldUseIocContainerToCreateViewModel()
        {
            var context    = new DataContext();
            int initialize = 0;
            var vm         = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };
            var iocContainer = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    type.ShouldEqual(typeof(ViewModel));
                    return(vm);
                }
            };

            ViewModelProvider provider  = GetViewModelProvider(iocContainer);
            IViewModel        viewModel = provider.GetViewModel(typeof(ViewModel), context);

            viewModel.ShouldEqual(vm);
            initialize.ShouldEqual(1);
        }
Пример #7
0
        public void SetUp()
        {
            ServiceProvider.DesignTimeManager     = DesignTimeManagerImpl.Instance;
            ServiceProvider.AttachedValueProvider = new AttachedValueProvider();
            CanBeResolvedTypes = new List <Type>
            {
                typeof(IThreadManager),
                typeof(IViewModelSettings),
                typeof(IViewManager),
                typeof(IDisplayNameProvider),
                typeof(IViewModelProvider),
                typeof(IVisualStateManager),
                typeof(OperationCallbackManagerMock)
            };
            OperationCallbackManager = new OperationCallbackManagerMock();
            ViewManager   = new ViewManagerMock();
            ThreadManager = new ThreadManagerMock();
            ServiceProvider.ThreadManager = ThreadManager;
            Settings            = new ViewModelSettingsMock();
            DisplayNameProvider = new DisplayNameProviderMock();
            IocContainer        = new IocContainerMock
            {
                GetFunc            = GetFunc,
                CanResolveDelegate = CanResolve
            };
            Tracer.TraceInformation = true;
            ServiceProvider.Tracer  = new ConsoleTracer();
            ViewModelProvider       = new ViewModelProvider(IocContainer);
            OnInit();
            var app = new UnitTestApp(this);

            app.Initialize(PlatformInfo.UnitTest, IocContainer, Empty.Array <Assembly>(), DataContext.Empty);
        }
        public void ProviderShouldCallSaveStateMethodOnPreserveViewModel()
        {
            var iocContainer           = new IocContainerMock();
            var viewModel              = new ViewModel();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            provider.PreserveViewModel(viewModel, DataContext.Empty);
            viewModel.SaveStateCount.ShouldEqual(1);
            viewModel.LoadStateCount.ShouldEqual(0);
        }
        public void ProviderShouldCreateIocContainerMixedMode()
        {
            var parentViewModelIoc = new IocContainerMock();
            var child = new IocContainerMock();

            parentViewModelIoc.GetFunc = (type, s, arg3) => parentViewModelIoc;
            child.GetFunc = (type, s, arg3) => child;

            var iocContainer = new IocContainerMock
            {
                CreateChild = mock => child
            };
            var viewModel = new ViewModel
            {
                IocContainer = new IocContainerMock
                {
                    CreateChild = mock => parentViewModelIoc
                }
            };
            var provider = GetViewModelProvider(iocContainer);
            var context  = new DataContext
            {
                { InitializationConstants.IocContainerCreationMode, IocContainerCreationMode.Mixed },
                { InitializationConstants.ParentViewModel, viewModel }
            };

            int initialize = 0;
            var vm         = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    ++initialize;
                }
            };

            provider.InitializeViewModel(vm, context);
            initialize.ShouldEqual(1);

            bool disposed = false;

            vm.IocContainer.Disposed += (sender, args) => disposed = true;

            vm.IocContainer.IsDisposed.ShouldBeFalse();
            vm.IocContainer.Get(typeof(object)).ShouldEqual(parentViewModelIoc);
            parentViewModelIoc.Dispose();

            disposed.ShouldBeFalse();
            vm.IocContainer.IsDisposed.ShouldBeFalse();
            vm.IocContainer.Get(typeof(object)).ShouldEqual(child);

            child.Dispose();
            disposed.ShouldBeTrue();
            vm.IocContainer.IsDisposed.ShouldBeTrue();
        }
        public void WhenInitialazingVmShouldUseObservationModeFromApplicationSettingsNotSpecifiedExplicitly()
        {
            ApplicationSettings.ViewModelObservationMode = ObservationMode.Both;
            var iocContainer           = new IocContainerMock();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);
            var parentViewModel        = new ViewModel();
            var context = new DataContext
            {
                { InitializationConstants.ParentViewModel, parentViewModel }
            };

            var viewModel = (ViewModel)provider.GetViewModel(container => new ViewModel(), context);

            parentViewModel.LocalEventAggregator.GetObservers().Contains(viewModel).ShouldBeTrue();
            viewModel.LocalEventAggregator.GetObservers().Contains(parentViewModel).ShouldBeTrue();
        }
        public void ProviderShouldRestoreIocContainerParentViewModelRestoreChildDuringRestoration()
        {
            var parentIoc = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    type.ShouldEqual(typeof(ViewModel));
                    return(new ViewModel());
                }
            };

            var parentViewModel = new ViewModel {
                IocContainer = new IocContainerMock()
            };
            var provider = GetViewModelProvider(new IocContainerMock {
                GetFunc = parentIoc.GetFunc
            });
            var context = new DataContext
            {
                { InitializationConstants.ParentViewModel, parentViewModel }
            };
            var restoreContext = new DataContext
            {
                { InitializationConstants.IgnoreViewModelCache, true }
            };

            var vm = new ViewModel();

            provider.InitializeViewModel(vm, context);

            var parentState = provider.PreserveViewModel(parentViewModel, DataContext.Empty);
            var state       = provider.PreserveViewModel(vm, DataContext.Empty);

            ViewModel.InitializeViewModelStatic = dataContext =>
            {
                ViewModel.InitializeViewModelStatic = null;
                vm = (ViewModel)provider.RestoreViewModel(state, restoreContext, true);
            };
            parentViewModel = (ViewModel)provider.RestoreViewModel(parentState, restoreContext, true);
            parentViewModel.IocContainer = parentIoc;

            vm.GetParentViewModel().ShouldEqual(parentViewModel);
            vm.Parent.ShouldEqual(parentViewModel);
            parentViewModel.IocContainer.ShouldEqual(parentIoc);
            vm.IocContainer.ShouldEqual(parentIoc);
        }
        public void WhenInitialazingVmShouldUseObservationModeFromDataContextModeBoth()
        {
            const ObservationMode mode = ObservationMode.Both;
            var iocContainer           = new IocContainerMock();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);
            var parentViewModel        = new ViewModel();
            var context = new DataContext
            {
                { InitializationConstants.ObservationMode, mode },
                { InitializationConstants.ParentViewModel, parentViewModel }
            };

            var viewModel = (ViewModel)provider.GetViewModel(container => new ViewModel(), context);

            parentViewModel.LocalEventAggregator.GetObservers().Contains(viewModel).ShouldBeTrue();
            viewModel.LocalEventAggregator.GetObservers().Contains(parentViewModel).ShouldBeTrue();
        }
        public void ProviderShouldUseObservationModeFromDataContextModeNone()
        {
            const ObservationMode mode = ObservationMode.None;
            var iocContainer           = new IocContainerMock();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);
            var parentViewModel        = new ViewModel();
            var context = new DataContext
            {
                { InitializationConstants.ObservationMode, mode },
                { InitializationConstants.ParentViewModel, parentViewModel }
            };

            var viewModel = (ViewModel)provider.GetViewModel(container => new ViewModel(), context);

            viewModel.ViewModelEventAggregator.GetObservers().Contains(viewModel).ShouldBeFalse();
            viewModel.ViewModelEventAggregator.GetObservers().Contains(viewModel).ShouldBeFalse();
        }
        public void ProviderShouldRestoreIocContainerParentViewModelAfterChildViewModelRestore()
        {
            var childIoc  = new IocContainerMock();
            var parentIoc = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    type.ShouldEqual(typeof(ViewModel));
                    return(new ViewModel());
                },
                CreateChild = mock => childIoc
            };

            childIoc.GetFunc = parentIoc.GetFunc;

            var parentViewModel = new ViewModel {
                IocContainer = new IocContainerMock()
            };
            var provider = GetViewModelProvider(new IocContainerMock {
                CreateChild = mock => parentIoc
            });
            var context = new DataContext
            {
                { InitializationConstants.IocContainerCreationMode, IocContainerCreationMode.ParentViewModel },
                { InitializationConstants.ParentViewModel, parentViewModel }
            };

            var vm = new ViewModel();

            provider.InitializeViewModel(vm, context);

            var parentState = provider.PreserveViewModel(parentViewModel, DataContext.Empty);
            var state       = provider.PreserveViewModel(vm, DataContext.Empty);

            vm = (ViewModel)provider.RestoreViewModel(state, DataContext.Empty, true);
            parentViewModel = (ViewModel)provider.RestoreViewModel(parentState, DataContext.Empty, true);

            vm.GetParentViewModel().ShouldEqual(parentViewModel);
            parentViewModel.IocContainer.ShouldEqual(parentIoc);
            vm.IocContainer.ShouldNotEqual(childIoc);

            childIoc.GetFunc = (type, s, arg3) => childIoc;
            vm.IocContainer.Get(typeof(object)).ShouldEqual(childIoc);
        }
        public void ProviderShouldNotBindIocContainerBindIocContainerFalse()
        {
            bool isInvoked    = false;
            var  iocContainer = new IocContainerMock
            {
                BindToConstantFunc = (type, arg2, arg3) =>
                {
                    isInvoked = true;
                }
            };
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            provider.BindIocContainer = false;
            var context = new DataContext();

            IViewModel viewModel = provider.GetViewModel(container => new ViewModel(), context);

            isInvoked.ShouldBeFalse();
        }
        public void ProviderShouldCacheRestoredViewModels()
        {
            var loadViewModel = new ViewModel();
            var childIoc      = new IocContainerMock();
            var iocContainer  = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    typeof(ViewModel).ShouldEqual(type);
                    return(loadViewModel);
                },
                CreateChild = mock => childIoc
            };

            childIoc.GetFunc = iocContainer.GetFunc;
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            var state             = provider.PreserveViewModel(new ViewModel(), DataContext.Empty);
            var restoreViewModel1 = provider.RestoreViewModel(state, DataContext.Empty, true);

            restoreViewModel1.ShouldEqual(loadViewModel);

            loadViewModel = new ViewModel();

            var restoreViewModel2 = provider.RestoreViewModel(state, DataContext.Empty, true);

            restoreViewModel2.ShouldEqual(restoreViewModel1);

            //No cache
            var ctx = new DataContext {
                { InitializationConstants.IgnoreViewModelCache, true }
            };
            var restoreViewModel3 = provider.RestoreViewModel(state, ctx, true);

            restoreViewModel3.ShouldEqual(loadViewModel);

            //Dispose current view model.
            restoreViewModel1.Dispose();
            var restoreViewModel4 = provider.RestoreViewModel(state, DataContext.Empty, true);

            restoreViewModel4.ShouldEqual(loadViewModel);
        }
        public void InitializeViewModelShouldInitializeViewModel()
        {
            var iocContainer = new IocContainerMock();
            var provider     = GetViewModelProvider(iocContainer);
            var context      = new DataContext();

            int initialize = 0;
            var vm         = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };

            provider.InitializeViewModel(vm, context);
            provider.InitializeViewModel(vm, context);
            initialize.ShouldEqual(2);
        }
        public void GetViewModelShouldUseDelegateToCreateViewModel()
        {
            var iocContainer           = new IocContainerMock();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);
            var context = new DataContext();

            int initialize = 0;
            var vm         = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };
            IViewModel viewModel = provider.GetViewModel(container => vm, context);

            viewModel.ShouldEqual(vm);
            initialize.ShouldEqual(1);
        }
        public void ProviderShouldRestoreObservationMode()
        {
            var childIoc  = new IocContainerMock();
            var parentIoc = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    type.ShouldEqual(typeof(ViewModel));
                    return(new ViewModel());
                },
                CreateChild = mock => childIoc
            };

            childIoc.GetFunc = parentIoc.GetFunc;

            var parentViewModel = new ViewModel {
                IocContainer = new IocContainerMock()
            };
            var provider = GetViewModelProvider(new IocContainerMock {
                CreateChild = mock => parentIoc
            });
            var context = new DataContext
            {
                { InitializationConstants.ObservationMode, ObservationMode.Both },
                { InitializationConstants.IocContainerCreationMode, IocContainerCreationMode.Application },
                { InitializationConstants.ParentViewModel, parentViewModel }
            };

            var vm = new ViewModel();

            provider.InitializeViewModel(vm, context);

            var parentState = provider.PreserveViewModel(parentViewModel, DataContext.Empty);
            var state       = provider.PreserveViewModel(vm, DataContext.Empty);

            parentViewModel = (ViewModel)provider.RestoreViewModel(parentState, DataContext.Empty, true);
            vm = (ViewModel)provider.RestoreViewModel(state, DataContext.Empty, true);

            vm.ViewModelEventAggregator.GetObservers().Contains(parentViewModel).ShouldBeTrue();
            parentViewModel.ViewModelEventAggregator.GetObservers().Contains(vm).ShouldBeTrue();
        }
        public void ProviderShouldCallLoadStateMethodOnRestoreViewModel()
        {
            var loadViewModel = new ViewModel();
            var iocContainer  = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    typeof(ViewModel).ShouldEqual(type);
                    return(loadViewModel);
                }
            };
            var viewModel = new ViewModel();
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            var state            = provider.PreserveViewModel(viewModel, DataContext.Empty);
            var restoreViewModel = provider.RestoreViewModel(state, DataContext.Empty, true);

            restoreViewModel.ShouldEqual(loadViewModel);
            loadViewModel.LoadStateCount.ShouldEqual(1);
            loadViewModel.LoadStateContext.ShouldEqual(state);
        }
        public void ProviderShouldRestoreViewModelFromType()
        {
            var loadViewModel = new ViewModel();
            var childIoc      = new IocContainerMock();
            var iocContainer  = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    typeof(ViewModel).ShouldEqual(type);
                    return(loadViewModel);
                },
                CreateChild = mock => childIoc
            };

            childIoc.GetFunc = iocContainer.GetFunc;
            ViewModelProvider provider = GetViewModelProvider(iocContainer);

            var context = new DataContext {
                { InitializationConstants.ViewModelType, typeof(ViewModel) }
            };

            provider.RestoreViewModel(DataContext.Empty, context, true).ShouldEqual(loadViewModel);
        }
        public void GetViewModelShouldUseIocContainerToCreateViewModelWithParameters()
        {
            var          parameters = new IIocParameter[0];
            const string vmName     = "vmName";
            int          initialize = 0;

            var context = new DataContext
            {
                { InitializationConstants.ViewModelBindingName, vmName },
                { InitializationConstants.IocParameters, parameters }
            };
            var vm = new ViewModel
            {
                InitializeViewModel = dataContext =>
                {
                    dataContext.ShouldEqual(context);
                    ++initialize;
                }
            };
            var iocContainer = new IocContainerMock
            {
                GetFunc = (type, s, arg3) =>
                {
                    s.ShouldEqual(vmName);
                    arg3.ShouldEqual(parameters);
                    type.ShouldEqual(typeof(ViewModel));
                    return(vm);
                }
            };

            ViewModelProvider provider  = GetViewModelProvider(iocContainer);
            IViewModel        viewModel = provider.GetViewModel(typeof(ViewModel), context);

            viewModel.ShouldEqual(vm);
            initialize.ShouldEqual(1);
        }