public IocContainerMock(IocContainerMock iocContainerMock = null)
 {
     _count = Interlocked.Increment(ref _countSt);
     Parent = iocContainerMock;
     Container = this;
     if (iocContainerMock == null) return;
     GetFunc = iocContainerMock.GetFunc;
     BindToConstantFunc = iocContainerMock.BindToConstantFunc;
     BindFunc = iocContainerMock.BindFunc;
     UnbindFunc = iocContainerMock.UnbindFunc;
 }
Example #2
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="IocContainerMock" /> class.
 /// </summary>
 public IocContainerMock(IocContainerMock iocContainerMock = null)
 {
     _count    = Interlocked.Increment(ref _countSt);
     Parent    = iocContainerMock;
     Container = this;
     if (iocContainerMock == null)
     {
         return;
     }
     GetFunc            = iocContainerMock.GetFunc;
     BindToConstantFunc = iocContainerMock.BindToConstantFunc;
     BindFunc           = iocContainerMock.BindFunc;
     UnbindFunc         = iocContainerMock.UnbindFunc;
 }
        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 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 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 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 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 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 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);
        }
        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 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);
        }
        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 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 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 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 ProviderShouldRestoreObservationMode()
        {
            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.ObservationMode, ObservationMode.Both},
                {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);

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

            vm.LocalEventAggregator.GetObservers().Contains(parentViewModel).ShouldBeTrue();
            parentViewModel.LocalEventAggregator.GetObservers().Contains(vm).ShouldBeTrue();
        }
        public void ProviderShouldRestorePreserveViewModelState()
        {
            bool restoringCalled = false;
            bool restoredCalled = false;
            bool preservingCalled = false;
            bool preservedCalled = false;
            var preserveCtx = new DataContext();
            var restoreCtx = new DataContext();
            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);

            provider.Preserving += (sender, args) =>
            {
                sender.ShouldEqual(provider);
                args.Context.ShouldEqual(preserveCtx);
                args.ViewModel.ShouldEqual(viewModel);
                preservingCalled = true;
            };
            ViewModelPreservedEventArgs preservedEventArgs = null;
            provider.Preserved += (sender, args) =>
            {
                sender.ShouldEqual(provider);
                args.Context.ShouldEqual(preserveCtx);
                args.ViewModel.ShouldEqual(viewModel);
                preservedEventArgs = args;
                preservedCalled = true;
            };

            var state = provider.PreserveViewModel(viewModel, preserveCtx);
            state.ShouldEqual(preservedEventArgs.State);
            provider.Restoring += (sender, args) =>
            {
                sender.ShouldEqual(provider);
                args.Context.ShouldEqual(restoreCtx);
                args.ViewModelState.ShouldEqual(state);
                restoringCalled = true;
            };
            ViewModelRestoredEventArgs restoredEventArgs = null;
            provider.Restored += (sender, args) =>
            {
                sender.ShouldEqual(provider);
                args.Context.ShouldEqual(restoreCtx);
                args.ViewModelState.ShouldEqual(state);
                restoredEventArgs = args;
                restoredCalled = true;
            };

            var restoreViewModel = provider.RestoreViewModel(state, restoreCtx, true);
            restoreViewModel.ShouldEqual(restoredEventArgs.ViewModel);
            restoreViewModel.IocContainer.ShouldEqual(childIoc);
            restoreViewModel.ShouldEqual(loadViewModel);
            restoreViewModel.Settings.State.GetData(key).ShouldEqual(value);
            restoringCalled.ShouldBeTrue();
            restoredCalled.ShouldBeTrue();
            preservingCalled.ShouldBeTrue();
            preservedCalled.ShouldBeTrue();
        }
        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 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);
        }
        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();
        }