Beispiel #1
0
        public async Task SelectPreviousOnRemove()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            vm.AddTargetCommand.Execute(null);
            vm.AddTargetCommand.Execute(null);
            vm.AddTargetCommand.Execute(null);

            Assume.That(vm.Targets, Is.Not.Empty);
            var newTarget = vm.Targets[1];
            var target    = vm.Targets[2];

            vm.SelectedTarget = target;

            Assume.That(vm.RemoveTargetCommand.CanExecute(null), Is.True);
            vm.RemoveTargetCommand.Execute(null);
            Assume.That(vm.Targets, Does.Not.Contain(target));

            Assert.That(vm.SelectedTarget, Is.SameAs(newTarget));

            vm.RemoveTargetCommand.Execute(null);
            vm.RemoveTargetCommand.Execute(null);

            Assert.That(vm.SelectedTarget, Is.Null);
        }
Beispiel #2
0
        public async Task AddTargetCanAdd()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            Assume.That(vm.SelectedType, Is.Null);
            Assert.That(vm.AddTargetCommand.CanExecute(null), Is.False);

            bool changed = false;

            vm.AddTargetCommand.CanExecuteChanged += (o, e) => { changed = true; };
            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            Assert.That(vm.AddTargetCommand.CanExecute(null), Is.True);
            Assert.That(changed, Is.True, "CanExecuteChanged did not fire");

            changed         = false;
            vm.SelectedType = null;
            Assert.That(vm.AddTargetCommand.CanExecute(null), Is.False);
            Assert.That(changed, Is.True, "CanExecuteChanged did not fire");
        }
Beispiel #3
0
        public async Task ReorderEnablesOnItemAdd()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            vm.AddTargetCommand.Execute(null);

            Assume.That(vm.Targets.Count, Is.EqualTo(1));
            Assume.That(vm.Targets, Is.Not.Empty, "Adding a target failed");
            vm.SelectedTarget = vm.Targets.First();

            Assume.That(vm.MoveUpCommand.CanExecute(null), Is.False);
            Assume.That(vm.MoveDownCommand.CanExecute(null), Is.False);

            bool upChanged = false;

            vm.MoveUpCommand.CanExecuteChanged += (sender, args) => upChanged = true;

            vm.AddTargetCommand.Execute(null);
            Assume.That(vm.SelectedTarget, Is.SameAs(vm.Targets[vm.Targets.Count - 1]), "Selected target not expected");

            Assert.That(upChanged, Is.True);
            Assert.That(vm.MoveUpCommand.CanExecute(null), Is.True);
            Assert.That(vm.MoveDownCommand.CanExecute(null), Is.False);
        }
Beispiel #4
0
        public async Task MoveDownCommand()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            vm.AddTargetCommand.Execute(null);
            vm.AddTargetCommand.Execute(null);
            Assume.That(vm.Targets.Count, Is.EqualTo(2));

            Assume.That(vm.Targets, Is.Not.Empty, "Adding a target failed");
            var target = vm.Targets.First();

            vm.SelectedTarget = target;

            Assume.That(vm.Targets[1], Is.Not.SameAs(target));
            Assert.That(vm.MoveDownCommand.CanExecute(null), Is.True);
            vm.MoveDownCommand.Execute(null);
            Assert.That(vm.MoveDownCommand.CanExecute(null), Is.False);
            Assume.That(vm.SelectedTarget, Is.SameAs(target));
            Assert.That(vm.Targets[1], Is.SameAs(target));
        }
Beispiel #5
0
        public void AddTypeCanceled()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });

            vm.AssignableTypes.Task.Wait();

            var buttonType = GetTypeInfo(typeof(MockWpfButton));

            vm.TypeRequested += (o, e) => {
                e.SelectedType = Task.FromResult(buttonType);
            };

            vm.SelectedType = vm.SuggestedTypes.Last();
            Assume.That(vm.SuggestedTypes, Contains.Item(buttonType));

            buttonType      = null;
            vm.SelectedType = vm.SuggestedTypes.Last();
            Assert.That(vm.SelectedType, Is.EqualTo(vm.SuggestedTypes.First()));
        }
Beispiel #6
0
        public async Task SelectedTargetHoldsAfterCollectionChange()
        {
            var selected = new MockSampleControl();
            var obj      = new TestClass {
                Collection = new ArrayList {
                    new MockSampleControl(),
                    selected
                }
            };

            var property = new Mock <IPropertyInfo> ();

            property.SetupGet(pi => pi.Type).Returns(typeof(IList));
            property.SetupGet(pi => pi.Name).Returns(nameof(TestClass.Collection));
            property.SetupGet(pi => pi.CanWrite).Returns(true);

            var editor = new Mock <IObjectEditor> ();

            editor.SetTarget(obj);
            editor.Setup(e => e.GetAssignableTypesAsync(property.Object, true))
            .ReturnsAsync(new AssignableTypesResult(new[] { typeof(MockSampleControl).ToTypeInfo() }));
            editor.Setup(e => e.Properties).Returns(new[] { property.Object });

            ValueInfo <IList> valueInfo = new ValueInfo <IList> {
                Value  = default(IList),
                Source = ValueSource.Default
            };

            editor.Setup(oe => oe.SetValueAsync(property.Object, It.IsAny <ValueInfo <IList> > (), null))
            .Callback <IPropertyInfo, ValueInfo <IList>, PropertyVariation> ((p, vi, v) => {
                valueInfo = vi;
                editor.Raise(oe => oe.PropertyChanged += null, new EditorPropertyChangedEventArgs(property.Object));
            })
            .Returns(Task.FromResult(true));
            editor.Setup(oe => oe.GetValueAsync <IList> (property.Object, null)).ReturnsAsync(() => valueInfo);

            await editor.Object.SetValueAsync(property.Object, new ValueInfo <IList> {
                Value = obj.Collection, Source = ValueSource.Local
            });

            var provider = new Mock <IEditorProvider> ();

            provider.Setup(ep => ep.GetObjectEditorAsync(obj)).ReturnsAsync(editor.Object);
            provider.Setup(ep => ep.GetObjectEditorAsync(It.IsAny <MockSampleControl> ()))
            .ReturnsAsync((MockSampleControl sample) => new MockObjectEditor(sample));

            var vm = new CollectionPropertyViewModel(new TargetPlatform(provider.Object), property.Object, new[] { editor.Object });
            await vm.AssignableTypes.Task;

            vm.SelectedTarget = vm.Targets.FirstOrDefault(item => item.Item == selected);

            obj.Collection.Insert(0, new MockSampleControl());
            editor.Raise(oe => oe.PropertyChanged += null, new EditorPropertyChangedEventArgs(property.Object, null));

            Assert.That(vm.SelectedTarget?.Item, Is.SameAs(selected), "SelectedTarget didn't hold when collection changed");
        }
Beispiel #7
0
        public async Task SelectedTargetClearsWhenNotPresent()
        {
            var obj = new TestClass {
                Collection = new ArrayList()
            };

            var property = new Mock <IPropertyInfo> ();

            property.SetupGet(pi => pi.Type).Returns(typeof(IList));
            property.SetupGet(pi => pi.Name).Returns(nameof(TestClass.Collection));
            property.SetupGet(pi => pi.CanWrite).Returns(true);

            var editor = new Mock <IObjectEditor> ();

            editor.SetTarget(obj);
            editor.Setup(e => e.GetAssignableTypesAsync(property.Object, true))
            .ReturnsAsync(new AssignableTypesResult(new[] { typeof(MockSampleControl).ToTypeInfo() }));
            editor.Setup(e => e.Properties).Returns(new[] { property.Object });

            ValueInfo <IList> valueInfo = new ValueInfo <IList> {
                Value  = default(IList),
                Source = ValueSource.Default
            };

            editor.Setup(oe => oe.SetValueAsync(property.Object, It.IsAny <ValueInfo <IList> > (), null))
            .Callback <IPropertyInfo, ValueInfo <IList>, PropertyVariation> ((p, vi, v) => {
                valueInfo = vi;
                editor.Raise(oe => oe.PropertyChanged += null, new EditorPropertyChangedEventArgs(property.Object));
            })
            .Returns(Task.FromResult(true));
            editor.Setup(oe => oe.GetValueAsync <IList> (property.Object, null)).ReturnsAsync(() => valueInfo);

            var provider = new Mock <IEditorProvider> ();

            provider.Setup(ep => ep.GetObjectEditorAsync(obj)).ReturnsAsync(editor.Object);
            provider.Setup(ep => ep.GetObjectEditorAsync(It.IsAny <MockSampleControl> ()))
            .ReturnsAsync((MockSampleControl sample) => new MockObjectEditor(sample));
            provider.Setup(ep => ep.CreateObjectAsync(It.IsAny <ITypeInfo> ()))
            .ReturnsAsync((ITypeInfo type) => new MockSampleControl());

            var vm = new CollectionPropertyViewModel(new TargetPlatform(provider.Object), property.Object, new[] { editor.Object });
            await vm.AssignableTypes.Task;

            vm.SelectedType = GetTypeInfo(typeof(MockSampleControl));
            vm.AddTargetCommand.Execute(null);

            Assume.That(vm.Targets, Is.Not.Empty, "Adding a target failed");
            var selected = vm.Targets.Single().Item;

            Assume.That(selected, Is.InstanceOf(typeof(MockSampleControl)));
            Assume.That(vm.SelectedTarget?.Item, Is.SameAs(selected), "Added target wasn't selected");

            editor.Raise(oe => oe.PropertyChanged += null, new EditorPropertyChangedEventArgs(property.Object, null));

            Assert.That(vm.SelectedTarget, Is.Null, "SelectedTarget didn't clear when it was no longer present");
        }
Beispiel #8
0
        public void AddTarget()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });

            vm.AssignableTypes.Task.Wait();

            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            vm.AddTargetCommand.Execute(null);

            Assert.That(vm.Targets, Is.Not.Empty, "Adding a target failed");
            Assume.That(vm.Targets.Single().Item, Is.InstanceOf(typeof(MockWpfButton)));
        }
Beispiel #9
0
        public async Task SuggestedSelected()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var collectionProperty = new Mock <IPropertyInfo> ();

            collectionProperty.SetupGet(pi => pi.Type).Returns(typeof(IList));

            ITypeInfo objType = GetTypeInfo(typeof(object));

            var editor = new Mock <IObjectEditor> ();

            editor.Setup(e => e.GetAssignableTypesAsync(collectionProperty.Object, true)).ReturnsAsync(new AssignableTypesResult(new[] { objType }, new[] { objType }));
            editor.Setup(e => e.Properties).Returns(new[] { collectionProperty.Object });

            var vm = new CollectionPropertyViewModel(platform, collectionProperty.Object, new[] { editor.Object });
            await vm.AssignableTypes.Task;

            Assert.That(vm.SelectedType, Is.EqualTo(objType));
        }
Beispiel #10
0
        public async Task RemoveEditor()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            vm.Editors.Remove(editor);
            await vm.AssignableTypes.Task;

            Assert.That(vm.AssignableTypes.Value, Is.Empty);
            Assert.That(vm.SuggestedTypes, Is.Empty);
            Assert.That(vm.SelectedType, Is.Null);
        }
        public CollectionEditorWindow(IHostResourceProvider hostResources, CollectionPropertyViewModel viewModel)
            : base(new CGRect(0, 0, 500, 400), NSWindowStyle.Titled | NSWindowStyle.Closable | NSWindowStyle.Resizable, NSBackingStore.Buffered, true)
        {
            Delegate = new ModalDialogDelegate();
            Title    = String.Format(Properties.Resources.CollectionEditorTitle, viewModel.Property.Name);

            this.collectionEditor = new CollectionEditorControl(hostResources)
            {
                ViewModel = viewModel,
                TranslatesAutoresizingMaskIntoConstraints = false
            };

            ContentView.AddSubview(this.collectionEditor);

            this.ok = NSButton.CreateButton(Properties.Resources.OK, OnOked);
            this.ok.TranslatesAutoresizingMaskIntoConstraints = false;
            //this.ok.KeyEquivalent = "\r"; // FIXME: The type selector popup doesn't eat this key, so it ends up closing both.
            ContentView.AddSubview(this.ok);

            this.cancel = NSButton.CreateButton(Properties.Resources.Cancel, OnCanceled);
            this.cancel.TranslatesAutoresizingMaskIntoConstraints = false;
            ContentView.AddSubview(this.cancel);

            ContentView.AddConstraints(new[] {
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Width, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Width, 1, -40),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 20),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.CenterX, 1, 0),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Top, 1, -20),

                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Bottom, 1, -20),
                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.collectionEditor, NSLayoutAttribute.Right, 1, 0),
                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.cancel, NSLayoutAttribute.Width, 1, 0),

                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Left, 1, -10),
                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Bottom, 1, 0),
                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1, 80)
            });
        }
Beispiel #12
0
        public async Task AddAfterSelected()
        {
            TargetPlatform platform = new TargetPlatform(new MockEditorProvider());

            var obj = new {
                Collection = new ArrayList()
            };

            var editor = new ReflectionObjectEditor(obj);

            var vm = new CollectionPropertyViewModel(platform, editor.Properties.First(), new[] { editor });
            await vm.AssignableTypes.Task;

            vm.SelectedType = GetTypeInfo(typeof(MockWpfButton));
            vm.AddTargetCommand.Execute(null);
            vm.AddTargetCommand.Execute(null);
            vm.AddTargetCommand.Execute(null);
            Assume.That(vm.Targets.Count, Is.EqualTo(3), "Dummy items were not added");

            bool changed = false;
            var  incc    = (INotifyCollectionChanged)vm.Targets;

            incc.CollectionChanged += (sender, args) => {
                changed = true;
                Assert.That(args.Action, Is.EqualTo(NotifyCollectionChangedAction.Add));
                Assert.That(args.NewStartingIndex, Is.EqualTo(2));
            };

            vm.SelectedTarget = vm.Targets[1];
            vm.AddTargetCommand.Execute(null);

            Assert.That(changed, Is.True);
            Assert.That(vm.Targets[1].Row, Is.EqualTo(1));
            Assert.That(vm.Targets[2].Row, Is.EqualTo(2));
            Assert.That(vm.Targets[3].Row, Is.EqualTo(3));
        }
        public CollectionEditorWindow(IHostResourceProvider hostResources, CollectionPropertyViewModel viewModel)
            : base(new CGRect(0, 0, 500, 400), NSWindowStyle.Titled | NSWindowStyle.Closable | NSWindowStyle.Resizable, NSBackingStore.Buffered, true)
        {
            if (hostResources == null)
            {
                throw new ArgumentNullException(nameof(hostResources));
            }
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            Delegate = new ModalWindowCloseDelegate();
            Title    = String.Format(Properties.Resources.CollectionEditorTitle, viewModel.Property.Name);

            this.collectionEditor = new CollectionEditorControl(hostResources)
            {
                ViewModel = viewModel,
                TranslatesAutoresizingMaskIntoConstraints = false
            };

            ContentView.AddSubview(this.collectionEditor);

            this.ok = new FocusableButton {
                AccessibilityEnabled = true,
                AccessibilityTitle   = Properties.Resources.AccessibilityCollectionOKButton,
                BezelStyle           = NSBezelStyle.Rounded,
                ControlSize          = NSControlSize.Regular,
                Highlighted          = true,
                //KeyEquivalent = "\r", // FIXME: The type selector popup doesn't eat this key, so it ends up closing both.Sw
                Title = Properties.Resources.OK,
                TranslatesAutoresizingMaskIntoConstraints = false
            };
            this.ok.Activated += OnOked;
            ContentView.AddSubview(this.ok);

            this.cancel = new FocusableButton {
                AccessibilityEnabled = true,
                AccessibilityTitle   = Properties.Resources.AccessibilityCollectionCancelButton,
                BezelStyle           = NSBezelStyle.Rounded,
                ControlSize          = NSControlSize.Regular,
                Title = Properties.Resources.Cancel,
                TranslatesAutoresizingMaskIntoConstraints = false
            };
            this.cancel.Activated += OnCanceled;
            ContentView.AddSubview(this.cancel);

            ContentView.AddConstraints(new[] {
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Width, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Width, 1, -40),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Top, 1, 20),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.CenterX, 1, 0),
                NSLayoutConstraint.Create(this.collectionEditor, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Top, 1, -20),

                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, ContentView, NSLayoutAttribute.Bottom, 1, -20),
                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.collectionEditor, NSLayoutAttribute.Right, 1, 0),
                NSLayoutConstraint.Create(this.ok, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this.cancel, NSLayoutAttribute.Width, 1, 0),

                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Left, 1, -10),
                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this.ok, NSLayoutAttribute.Bottom, 1, 0),
                NSLayoutConstraint.Create(this.cancel, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1, 80)
            });
        }
        public static void EditCollection(NSAppearance appearance, IHostResourceProvider hostResources, CollectionPropertyViewModel collectionVm)
        {
            var w = new CollectionEditorWindow(hostResources, collectionVm)
            {
                Appearance = appearance
            };

            var result = (NSModalResponse)(int)NSApplication.SharedApplication.RunModalForWindow(w);

            if (result != NSModalResponse.OK)
            {
                collectionVm.CancelCommand.Execute(null);
                return;
            }

            collectionVm.CommitCommand.Execute(null);
        }
 public CollectionTableSource(IHostResourceProvider hostResources, CollectionPropertyViewModel vm)
 {
     this.viewModel     = vm;
     this.hostResources = hostResources;
 }