Example #1
0
        public void TwoWayBindWithFuncConvertersSmokeTest()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };
            var fixture = new PropertyBinderImplementation();

            vm.JustADecimal = 123.45m;
            Assert.NotEqual(vm.JustADecimal.ToString(), view.SomeTextBox.Text);

            var disp = fixture.Bind(vm, view, x => x.JustADecimal, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, d => d.ToString(), decimal.Parse);

            Assert.Equal(vm.JustADecimal.ToString(), view.SomeTextBox.Text);
            Assert.Equal(123.45m, vm.JustADecimal);

            view.SomeTextBox.Text = "567.89";
            Assert.Equal(567.89m, vm.JustADecimal);

            disp?.Dispose();
            vm.JustADecimal = 0;

            Assert.Equal(0, vm.JustADecimal);
            Assert.Equal("567.89", view.SomeTextBox.Text);
        }
Example #2
0
        public void TwoWayBindSmokeTest()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };
            var fixture = new PropertyBinderImplementation();

            vm.Property1 = "Foo";
            Assert.NotEqual(vm.Property1, view.SomeTextBox.Text);

            var disp = fixture.Bind(vm, view, x => x.Property1, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, null);

            Assert.Equal(vm.Property1, view.SomeTextBox.Text);
            Assert.Equal("Foo", vm.Property1);

            view.SomeTextBox.Text = "Bar";
            Assert.Equal(vm.Property1, "Bar");

            disp.Dispose();
            vm.Property1 = "Baz";

            Assert.Equal("Baz", vm.Property1);
            Assert.NotEqual(vm.Property1, view.SomeTextBox.Text);
        }
Example #3
0
        public void BindWithFuncShouldWorkAsExtensionMethodSmokeTest()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.Bind(vm, x => x.JustADecimal, x => x.SomeTextBox.Text, d => d.ToString(), decimal.Parse);
        }
Example #4
0
        public void ViewModelIndexerPropertyToView()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(view.ViewModel, x => x.SomeCollectionOfStrings[0].Length, x => x.SomeTextBox.Text);
            Assert.Equal("3", view.SomeTextBox.Text);
        }
Example #5
0
        public void BindingIntoModelObjects()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(view.ViewModel, x => x !.Model !.AnotherThing, x => x.SomeTextBox.Text);
            Assert.Equal("Baz", view.SomeTextBox.Text);
        }
Example #6
0
        public void OneWayBindConverter()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };
            var fixture = new PropertyBinderImplementation();

            fixture.OneWayBind(vm, view, x => x.JustABoolean, x => x.SomeTextBox.IsEnabled, s => s);
            Assert.False(view.SomeTextBox.IsEnabled);
        }
Example #7
0
        public void ItemsControlShouldGetADataTemplate()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            Assert.Null(view.FakeItemsControl.ItemTemplate);
            view.OneWayBind(vm, x => x.SomeCollectionOfStrings, x => x.FakeItemsControl.ItemsSource);

            Assert.NotNull(view.FakeItemsControl.ItemTemplate);
        }
Example #8
0
        public void BindingToItemsControl()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(view.ViewModel, x => x.SomeCollectionOfStrings, x => x.FakeItemsControl.ItemsSource);

            var itemsSourceValue = (IList)view.FakeItemsControl.ItemsSource;

            Assert.True(itemsSourceValue.OfType <string>().Count() > 1);
        }
Example #9
0
        public void BindExpectsConverterFuncsToNotBeNull()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };
            var fixture = new PropertyBinderImplementation();

            Func <string?, string?> nullFunc = null !;

            Assert.Throws <ArgumentNullException>(() => fixture.Bind(vm, view, x => x.Property1, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, nullFunc, s => s));
            Assert.Throws <ArgumentNullException>(() => fixture.Bind(vm, view, x => x.Property1, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, s => s, nullFunc));
        }
Example #10
0
            static (IDisposable?, WeakReference) GetWeakReference()
            {
                var vm   = new PropertyBindViewModel();
                var view = new PropertyBindView {
                    ViewModel = vm
                };
                var weakRef = new WeakReference(vm);
                var disp    = view.OneWayBind(vm, x => x.Property1, x => x.SomeTextBox.Text);

                view.ViewModel = new PropertyBindViewModel();

                return(disp, weakRef);
            }
Example #11
0
        public void OneWayBindWithSelectorAndNonNullStartingValueToNullValue()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(vm, x => x.Model, x => x.SomeTextBox.Text, x => x?.AnotherThing);

            vm.Model = null;

            Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text));
        }
Example #12
0
        public void OneWayBindWithNullStartingValueToNonNullValue()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(vm, x => x.Property1, x => x.SomeTextBox.Text);

            vm.Property1 = "Baz";

            Assert.Equal("Baz", view.SomeTextBox.Text);
        }
Example #13
0
        public void BindToShouldntInitiallySetToNull()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = null
            };

            view.OneWayBind(vm, x => x !.Model !.AnotherThing, x => x.FakeControl.NullHatingString);
            Assert.Equal(string.Empty, view.FakeControl.NullHatingString);

            view.ViewModel = vm;
            Assert.Equal(vm !.Model !.AnotherThing, view.FakeControl.NullHatingString);
        }
Example #14
0
        public void ViewModelIndexerToViewChanges()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.OneWayBind(view.ViewModel, x => x.SomeCollectionOfStrings[0], x => x.SomeTextBox.Text);
            Assert.Equal("Foo", view.SomeTextBox.Text);

            vm.SomeCollectionOfStrings[0] = "Bar";

            Assert.Equal("Bar", view.SomeTextBox.Text);
        }
Example #15
0
        public void ItemsControlWithDisplayMemberPathSetShouldNotGetADataTemplate()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.FakeItemsControl.DisplayMemberPath = "Bla";

            Assert.Null(view.FakeItemsControl.ItemTemplate);
            view.OneWayBind(vm, x => x.SomeCollectionOfStrings, x => x.FakeItemsControl.ItemsSource);

            Assert.Null(view.FakeItemsControl.ItemTemplate);
        }
Example #16
0
        public void BindToWithNullStartingValueToNonNullValue()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.WhenAnyValue(x => x.ViewModel !.Property1)
            .BindTo(view, x => x.SomeTextBox.Text);

            vm.Property1 = "Baz";

            Assert.Equal("Baz", view.SomeTextBox.Text);
        }
Example #17
0
        public void OneWayBindWithNonNullStartingValueToNullValue()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            vm.Property1 = "Baz";

            view.OneWayBind(vm, x => x.Property1, x => x.SomeTextBox.Text);

            vm.Property1 = null;

            Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text));
        }
Example #18
0
        public void BindToTypeConversionSmokeTest()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = null
            };

            view.WhenAnyValue(x => x.ViewModel !.JustADouble)
            .BindTo(view, x => x.FakeControl.NullHatingString);

            Assert.Equal(string.Empty, view.FakeControl.NullHatingString);

            view.ViewModel = vm;
            Assert.Equal(vm.JustADouble.ToString(), view.FakeControl.NullHatingString);
        }
Example #19
0
        public void BindToWithNonNullStartingValueToNullValue()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            vm.Property1 = "Baz";

            view.WhenAnyValue(x => x.ViewModel !.Property1)
            .BindTo(view, x => x.SomeTextBox.Text);

            vm.Property1 = null;

            Assert.True(string.IsNullOrEmpty(view.SomeTextBox.Text));
        }
Example #20
0
        public void ItemsControlShouldGetADataTemplateInBindTo()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            Assert.Null(view.FakeItemsControl.ItemTemplate);
            vm.WhenAnyValue(x => x.SomeCollectionOfStrings)
            .BindTo(view, v => v.FakeItemsControl.ItemsSource);

            Assert.NotNull(view.FakeItemsControl.ItemTemplate);

            view.WhenAnyValue(x => x.FakeItemsControl.SelectedItem)
            .BindTo(vm, x => x.Property1);
        }
Example #21
0
        public void ViewModelNullableToViewNullable()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.Bind(view.ViewModel, x => x.NullableDouble, x => x.FakeControl.NullableDouble);
            Assert.Equal(null, vm.NullableDouble);

            view.FakeControl.NullableDouble = 4.0;
            Assert.Equal(4.0, vm.NullableDouble);

            view.FakeControl.NullableDouble = null;
            Assert.Equal(null, vm.NullableDouble);

            view.FakeControl.NullableDouble = 0.0;
            Assert.Equal(0.0, vm.NullableDouble);
        }
Example #22
0
        public void TwoWayBindToSelectedItemOfItemsControl()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };

            view.FakeItemsControl.ItemsSource = new ObservableCollectionExtended <string>(new[] { "aaa", "bbb", "ccc" });

            view.Bind(view.ViewModel, x => x.Property1, x => x.FakeItemsControl.SelectedItem);

            Assert.Null(view.FakeItemsControl.SelectedItem);
            Assert.Null(vm.Property1);

            view.FakeItemsControl.SelectedItem = "aaa";
            Assert.Equal("aaa", vm.Property1); // fail

            vm.Property1 = "bbb";
            Assert.Equal("bbb", view.FakeItemsControl.SelectedItem);
        }
Example #23
0
        public void TypeConvertedTwoWayBindSmokeTest()
        {
            var vm   = new PropertyBindViewModel();
            var view = new PropertyBindView {
                ViewModel = vm
            };
            var fixture = new PropertyBinderImplementation();

            vm.Property2 = 17;
            Assert.NotEqual(vm.Property2.ToString(), view.SomeTextBox.Text);

            var disp = fixture.Bind(vm, view, x => x.Property2, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, null);

            Assert.Equal(vm.Property2.ToString(), view.SomeTextBox.Text);
            Assert.Equal(17, vm.Property2);

            view.SomeTextBox.Text = "42";
            Assert.Equal(42, vm.Property2);

            // Bad formatting error
            view.SomeTextBox.Text = "--";
            Assert.Equal(42, vm.Property2);

            disp.Dispose();
            vm.Property2 = 0;

            Assert.Equal(0, vm.Property2);
            Assert.NotEqual("0", view.SomeTextBox.Text);

            vm.JustADecimal = 17.2m;
            var disp1 = fixture.Bind(vm, view, x => x.JustADecimal, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, null);

            Assert.Equal(vm.JustADecimal.ToString(), view.SomeTextBox.Text);
            Assert.Equal(17.2m, vm.JustADecimal);

            view.SomeTextBox.Text = 42.3m.ToString();
            Assert.Equal(42.3m, vm.JustADecimal);

            // Bad formatting.
            view.SomeTextBox.Text = "--";
            Assert.Equal(42.3m, vm.JustADecimal);

            disp1.Dispose();

            vm.JustADecimal = 0;

            Assert.Equal(0, vm.JustADecimal);
            Assert.NotEqual("0", view.SomeTextBox.Text);

            // Empty test
            vm.JustAInt32 = 12;
            var disp2 = fixture.Bind(vm, view, x => x.JustAInt32, x => x.SomeTextBox.Text, (IObservable <Unit>?)null, null);

            view.SomeTextBox.Text = string.Empty;
            Assert.Equal(12, vm.JustAInt32);

            view.SomeTextBox.Text = "1.2";

            Assert.Equal(12, vm.JustAInt32);

            view.SomeTextBox.Text = "13";
            Assert.Equal(13, vm.JustAInt32);
        }