public void WhenSelectedItemIsNull_CannotExecute_ShowCommand()
        {
            var target = new MultiReverseCrossRefFieldViewModel();

            target.SelectedItem = null;

            Assert.IsFalse(target.ShowCommand.CanExecute(null));
        }
        public void NotifyPropertyChangedTest()
        {
            var target = new MultiReverseCrossRefFieldViewModel();

            TestsHelper.TestPropertyWithNotifyPropertyChanged(target, () => target.SelectedItem);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(target, () => target.SortDescriptors);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(target, () => target.GroupColumn);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(target, () => target.CurrentPageNumber);
            TestsHelper.TestPropertyWithNotifyPropertyChanged(target, () => target.PageCount);
        }
        public void ShowCommand_Shows_SelectedItem()
        {
            // Arrange.
            const string processName = "TestProcess";
            const int itemId = 111;

            var item = Mock.Create<IReverseCrossReferenceItem>();
            Mock.Arrange(() => item.Id).Returns(itemId);

            var detailsViewModel = Mock.Create<IDetailsViewModel>();
            var parentViewModel = Mock.Create<IDetailsViewModel>();
            var windowManager = Mock.Create<IShell>();

            var target = new MultiReverseCrossRefFieldViewModel();
            target.ReferenceName = processName;
            target.SelectedItem = item;
            target.WindowManager = windowManager;
            target.ParentDetailsVM = parentViewModel;
            target.DetailsViewModelFactory =
                new ExportFactory<IDetailsViewModel>(
                    () => new Tuple<IDetailsViewModel, Action>(detailsViewModel, () => { }));

            // Act.
            target.ShowCommand.Execute(null);

            // Assert.
            Mock.Assert(() => detailsViewModel.Refresh(processName, itemId), Occurs.Once());
            Mock.Assert(() => windowManager.ShowChildWindow(parentViewModel, detailsViewModel, null), Occurs.Once());
        }
        public void WhenSelectedItemIsNotNull_CanExecute_ShowCommand()
        {
            var item = Mock.Create<IReverseCrossReferenceItem>();

            var target = new MultiReverseCrossRefFieldViewModel { SelectedItem = item };

            Assert.IsTrue(target.ShowCommand.CanExecute(null));
        }
        public void PropertiesGetSetTest()
        {
            var target = new MultiReverseCrossRefFieldViewModel();

            TestsHelper.TestPublicPropertiesGetSet(target, x => target.VisibilityRules);
        }
        public void WhenCrossRefHasNoWritePermission_OnlyShowItemButtonCanBeEnabled()
        {
            #region Arrange

            var assemblyRepositoryMock = Mock.Create<IAssemblyRepository>(Behavior.Loose);
            Mock.Arrange(() => assemblyRepositoryMock.IsFullyLoaded(Arg.IsAny<string>())).Returns(true);

            var dtmMock = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            var detailsVmMock = Mock.Create<IDetailsViewModel>(Behavior.Loose);

            var model = SingleCrossRefFieldViewModelTests.MyModel.NewMyModel();
            model.SetIsNew(false);
            model.Name = "test";

            var vm = new MultiReverseCrossRefFieldViewModel();
            var typeMock = Mock.Create<Type>(Behavior.Loose);
            Mock.Arrange(() => dtmMock.GetCrossReferenceListType(Arg.IsAny<string>(), Arg.IsAny<string>())).Returns(typeMock);
            Mock.Arrange(() => dtmMock.GetEditableRootType(Arg.IsAny<string>())).Returns(typeMock);
            vm.TheDynamicTypeManager = dtmMock;
            vm.AssemblyRepository = assemblyRepositoryMock;
            vm.TheIdentityService = Mock.Create<IIdentityService>(Behavior.Loose);

            vm.Property = typeof(SingleCrossRefFieldViewModelTests.MyModel).GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);
            vm.AllowLink = true;
            vm.AllowAddNew = true;
            vm.Init(model, 1, detailsVmMock);

            #endregion Arrange

            #region Act

            model.SetCanWriteProperties(false);

            #endregion Act

            #region Assert

            Assert.IsFalse(vm.LinkCommand.CanExecute(null));
            Assert.IsFalse(vm.AddNewCommand.CanExecute(null));
            Assert.IsFalse(vm.CanEdit);
            Assert.IsFalse(vm.ShowCommand.CanExecute(null));

            #endregion Assert

            #region Act

            vm.SelectedItem = new SingleReverseCrossRefFieldViewModelTests.MyInfo();

            #endregion Act

            #region Assert

            Assert.IsFalse(vm.LinkCommand.CanExecute(null));
            Assert.IsFalse(vm.AddNewCommand.CanExecute(null));
            Assert.IsFalse(vm.CanEdit);
            Assert.IsTrue(vm.ShowCommand.CanExecute(null));

            #endregion Assert
        }
        public void LinkCommand_Initializes_NewSelectViewModel()
        {
            const string processName = "TestProcess";
            const string fieldName = "RCRField";
            const string displayPath = "Field1|Field2|Field3";
            const string crossReferenceFieldName = "CrossReference";
            const int itemId = 123;

            var prop = Mock.Create<PropertyInfo>(Behavior.Loose);
            Mock.Arrange(() => prop.Name).Returns(fieldName);

            var model = Mock.Create<IEditableRoot>(Behavior.Loose);
            Mock.Arrange(() => model.IsNew).Returns(false);
            Mock.Arrange(() => model.ProcessName).Returns(processName);
            Mock.Arrange(() => model.Id).Returns(itemId);
            Mock.Arrange(() => model.CanWriteProperty(prop.Name)).Returns(true);

            var parentViewModel = Mock.Create<IDetailsViewModel>(Behavior.Loose);
            var dtm = Mock.Create<IDynamicTypeManager>(Behavior.Loose);
            var assemblyRepository = Mock.Create<IAssemblyRepository>(Behavior.Loose);
            Mock.Arrange(() => assemblyRepository.IsFullyLoaded(Arg.IsAny<string>())).Returns(true);

            var selectedItems = new ObservableCollection<object>();

            var itemType = Mock.Create<Type>(Behavior.Loose);
            string[] displayFields = null;

            var selectViewModel = Mock.Create<ISelectReverseCrossReferenceViewModel>(Behavior.Loose);
            Mock.Arrange(() => selectViewModel.SelectedItems).Returns(selectedItems);
            Mock.Arrange(() => selectViewModel.ItemsFilter).Returns(new FilterDescriptor("CrossReferenceId", FilterOperator.IsNotEqualTo, itemId));
            Mock.Arrange(() => selectViewModel.Initialize(Arg.IsAny<string>(), Arg.IsAny<string>(), Arg.IsAny<Type>(), Arg.IsAny<IEnumerable<string>>()))
                .DoInstead<string, string, Type, IEnumerable<String>>(
                    (pName, fName, type, fields) =>
                        {
                            displayFields = fields.ToArray();
                        });

            var columnsHelper = Mock.Create<IColumnsHelper>(Behavior.Loose);

            var popup = Mock.Create<PopupBuilder>();
            Mock.Arrange(() => popup.SetCustomViewModel(Arg.IsAny<INotificationViewModel>())).Returns(popup);

            var popupFactory = Mock.Create<PopupFactory>(Behavior.Loose);
            Mock.Arrange(() => popupFactory.Popup()).Returns(popup);

            var target = new MultiReverseCrossRefFieldViewModel
                             {
                                 TheDynamicTypeManager = dtm,
                                 AssemblyRepository = assemblyRepository,
                                 ColumnsHelper = columnsHelper,
                                 PopupFactory = popupFactory,
                                 SelectViewModelFactory =
                                     new ExportFactory<ISelectReverseCrossReferenceViewModel>(
                                     () => new Tuple<ISelectReverseCrossReferenceViewModel, Action>(selectViewModel, () => { }))
                             };

            target.Init(model, null, parentViewModel);
            target.Property = prop;
            target.AllowLink = true;
            target.ItemType = itemType;
            target.DisplayPath = displayPath;
            target.CrossReferenceFieldName = crossReferenceFieldName;
            target.TheIdentityService = Mock.Create<IIdentityService>(Behavior.Loose);

            // Act.
            target.LinkCommand.Execute(null);

            // Assert.
            Mock.Assert(() => selectViewModel.Initialize(processName, fieldName, itemType, Arg.IsAny<IEnumerable<string>>()), Occurs.Once());
            Mock.AssertSet(() => selectViewModel.AllowMultipleSelection = true);
            Mock.Assert(() => selectViewModel.Refresh(), Occurs.Once());
            Assert.IsTrue(new[] { "Field1", "Field2", "Field3" }.SequenceEqual(displayFields));
            
            var itemsFilter = selectViewModel.ItemsFilter;

            Assert.IsNotNull(itemsFilter);
            Assert.AreEqual("CrossReferenceId", itemsFilter.ColumnName);
            Assert.AreEqual(FilterOperator.IsNotEqualTo, itemsFilter.Operator);
            Assert.AreEqual(itemId, itemsFilter.Value);
        }
        public void WhenAllowLinkIsFalse_CannotExecute_LinkCommand()
        {
            var model = Mock.Create<IEditableRoot>();
            Mock.Arrange(() => model.IsNew).Returns(false);

            var parentViewModel = Mock.Create<IDetailsViewModel>();
            var dtm = Mock.Create<IDynamicTypeManager>();
            var assemblyRepository = Mock.Create<IAssemblyRepository>();
            Mock.Arrange(() => assemblyRepository.IsFullyLoaded(Arg.IsAny<string>())).Returns(true);

            var target = new MultiReverseCrossRefFieldViewModel { TheDynamicTypeManager = dtm, AssemblyRepository = assemblyRepository };

            target.Init(model, null, parentViewModel);
            target.AllowLink = false;

            // Assert.
            Assert.IsFalse(target.LinkCommand.CanExecute(null));
        }
        public void WhenParentModelIsNotNew_CanExecute_LinkCommand()
        {
            const string fieldName = "RCRField";

            var prop = Mock.Create<PropertyInfo>();
            Mock.Arrange(() => prop.Name).Returns(fieldName);

            var model = Mock.Create<IEditableRoot>();
            Mock.Arrange(() => model.IsNew).Returns(false);
            Mock.Arrange(() => model.CanWriteProperty(prop.Name)).Returns(true);

            var parentViewModel = Mock.Create<IDetailsViewModel>();
            var dtm = Mock.Create<IDynamicTypeManager>();
            var assemblyRepository = Mock.Create<IAssemblyRepository>();
            Mock.Arrange(() => assemblyRepository.IsFullyLoaded(Arg.IsAny<string>())).Returns(true);

            var target = new MultiReverseCrossRefFieldViewModel { TheDynamicTypeManager = dtm, AssemblyRepository = assemblyRepository, Property = prop };

            target.Init(model, null, parentViewModel);
            target.AllowLink = true;
            target.TheIdentityService = Mock.Create<IIdentityService>();

            // Assert.
            Assert.IsTrue(target.LinkCommand.CanExecute(null));
        }