Ejemplo n.º 1
0
        public void Set2WayBindingsWithUpdateSourceEventInBindingObject()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Baz);

            var vm = new MockVMForNativeBinding();

            nativeView.SetBindingContext(vm);

            nativeView.SetBinding("Baz", new Binding("FFoo", mode: BindingMode.TwoWay)
            {
                UpdateSourceEventName = "BazChanged"
            });
            Assert.AreEqual(null, nativeView.Baz);
            Assert.AreEqual(null, vm.FFoo);

            nativeView.Baz = "oof";
            nativeView.FireBazChanged();
            Assert.AreEqual("oof", nativeView.Baz);
            Assert.AreEqual("oof", vm.FFoo);

            vm.FFoo = "foo";
            Assert.AreEqual("foo", nativeView.Baz);
            Assert.AreEqual("foo", vm.FFoo);
        }
Ejemplo n.º 2
0
        public void Set2WayBindings()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);

            var vm = new MockVMForNativeBinding();

            nativeView.SetBindingContext(vm);
            var inpc = new MockINPC();

            nativeView.SetBinding("Foo", new Binding("FFoo", mode: BindingMode.TwoWay), inpc);
            nativeView.SetBinding("Bar", new Binding("BBar", mode: BindingMode.TwoWay), inpc);
            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);
            Assert.AreEqual(null, vm.FFoo);
            Assert.AreEqual(0, vm.BBar);

            nativeView.Foo = "oof";
            inpc.FireINPC(nativeView, "Foo");
            nativeView.Bar = -42;
            inpc.FireINPC(nativeView, "Bar");
            Assert.AreEqual("oof", nativeView.Foo);
            Assert.AreEqual(-42, nativeView.Bar);
            Assert.AreEqual("oof", vm.FFoo);
            Assert.AreEqual(-42, vm.BBar);

            vm.FFoo = "foo";
            vm.BBar = 42;
            Assert.AreEqual("foo", nativeView.Foo);
            Assert.AreEqual(42, nativeView.Bar);
            Assert.AreEqual("foo", vm.FFoo);
            Assert.AreEqual(42, vm.BBar);
        }
Ejemplo n.º 3
0
        public void ThrowsOnMissingProperty()
        {
            var nativeView = new MockNativeView();

            nativeView.SetBinding("Qux", new Binding("Foo"));
            Assert.Throws <InvalidOperationException>(() => nativeView.SetBindingContext(new { Foo = 42 }));
        }
Ejemplo n.º 4
0
        public void NativeViewsAreCollected()
        {
            WeakReference wr = null;

            int    i      = 0;
            Action create = null;

            create = () => {
                if (i++ < 1024)
                {
                    create();
                    return;
                }

                var nativeView = new MockNativeView();
                nativeView.SetBinding("fooBar", new Binding("Foo", BindingMode.TwoWay));
                nativeView.SetBinding("Baz", new Binding("Qux", BindingMode.TwoWay), "BazChanged");

                wr         = new WeakReference(nativeView);
                nativeView = null;
            };

            create();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Assert.False(wr.IsAlive);
        }
Ejemplo n.º 5
0
        public void DoNotApplyNull()
        {
            var native = new MockNativeView();

            Assert.NotNull(native.CantBeNull);
            native.SetBinding("CantBeNull", new Binding("FFoo", BindingMode.TwoWay));
            Assert.NotNull(native.CantBeNull);
            native.SetBindingContext(new { FFoo = "foo" });
            Assert.AreEqual("foo", native.CantBeNull);
        }
Ejemplo n.º 6
0
        public void TestConverterDoesNotThrow()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);
            var vm        = new MockVMForNativeBinding();
            var converter = new MockCustomColorConverter();

            nativeView.SetBinding("SelectedColor", new Binding("CColor", converter: converter));
            Assert.DoesNotThrow(() => nativeView.SetBindingContext(vm));
        }
Ejemplo n.º 7
0
        public void AttachedPropertiesAreTransferredFromTheBackpack()
        {
            var nativeView = new MockNativeView();

            nativeView.SetValue(Grid.ColumnProperty, 3);
            nativeView.SetBinding(Grid.RowProperty, new Binding("foo"));

            var view = nativeView.ToView();

            view.BindingContext = new { foo = 42 };
            Assert.AreEqual(3, view.GetValue(Grid.ColumnProperty));
            Assert.AreEqual(42, view.GetValue(Grid.RowProperty));
        }
Ejemplo n.º 8
0
        public void OneWayToSourceAppliedOnSetBC()
        {
            var nativeView = new MockNativeView {
                Foo = "foobar"
            };

            nativeView.SetBinding("Foo", new Binding("FFoo", BindingMode.OneWayToSource));
            var vm = new MockVMForNativeBinding {
                FFoo = "qux"
            };

            nativeView.SetBindingContext(vm);
            Assert.AreEqual("foobar", vm.FFoo);
        }
Ejemplo n.º 9
0
        public void TestConverterWorks()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);
            var vm = new MockVMForNativeBinding();

            vm.CColor = Color.Red;
            var converter = new MockCustomColorConverter();

            nativeView.SetBinding("SelectedColor", new Binding("CColor", converter: converter));
            nativeView.SetBindingContext(vm);
            Assert.AreEqual(vm.CColor, nativeView.SelectedColor.FormsColor);
        }
Ejemplo n.º 10
0
        public void SetOneWayBinding()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);

            nativeView.SetBinding("Foo", new Binding("FFoo", mode: BindingMode.OneWay));
            nativeView.SetBinding("Bar", new Binding("BBar", mode: BindingMode.OneWay));
            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);

            nativeView.SetBindingContext(new { FFoo = "Foo", BBar = 42 });
            Assert.AreEqual("Foo", nativeView.Foo);
            Assert.AreEqual(42, nativeView.Bar);
        }
Ejemplo n.º 11
0
        public void Binding2WayWithConvertersDoNotLoop()
        {
            var nativeView = new MockNativeView();
            int count      = 0;

            nativeView.SelectedColorChanged += (o, e) => {
                if (++count > 5)
                {
                    Assert.Fail("Probable loop detected");
                }
            };

            var vm = new MockVMForNativeBinding {
                CColor = Color.Red
            };

            nativeView.SetBinding("SelectedColor", new Binding("CColor", BindingMode.TwoWay, new MockCustomColorConverter()), "SelectedColorChanged");
            nativeView.SetBindingContext(vm);

            Assert.AreEqual(count, 1);
        }
Ejemplo n.º 12
0
        public void SetBindingContextToSubviews()
        {
            var nativeView = new MockNativeView {
                SubViews = new List <MockNativeView> ()
            };
            var nativeViewChild = new MockNativeView();

            nativeViewChild.SetBinding("Foo", new Binding("FFoo", mode: BindingMode.OneWay));
            nativeViewChild.SetBinding("Bar", new Binding("BBar", mode: BindingMode.OneWay));

            nativeView.SubViews.Add(nativeViewChild);

            var vm = new MockVMForNativeBinding();

            nativeView.SetBindingContext(vm, v => v.SubViews);

            Assert.AreEqual(null, nativeViewChild.Foo);
            Assert.AreEqual(0, nativeViewChild.Bar);

            nativeView.SetBindingContext(new { FFoo = "Foo", BBar = 42 }, v => v.SubViews);
            Assert.AreEqual("Foo", nativeViewChild.Foo);
            Assert.AreEqual(42, nativeViewChild.Bar);
        }
Ejemplo n.º 13
0
        public void ProxiesAreCollected()
        {
            WeakReference wr = null;

            int    i      = 0;
            Action create = null;

            create = () => {
                if (i++ < 1024)
                {
                    create();
                    return;
                }

                var nativeView = new MockNativeView();
                nativeView.SetBinding("fooBar", new Binding("Foo", BindingMode.TwoWay));
                nativeView.SetBinding("Baz", new Binding("Qux", BindingMode.TwoWay), "BazChanged");

                NativeBindingHelpers.BindableObjectProxy <MockNativeView> proxy;
                if (!NativeBindingHelpers.BindableObjectProxy <MockNativeView> .BindableObjectProxies.TryGetValue(nativeView, out proxy))
                {
                    Assert.Fail();
                }

                wr         = new WeakReference(proxy);
                nativeView = null;
            };

            create();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            Assert.False(wr.IsAlive);
        }
Ejemplo n.º 14
0
        public void TestConverter2WayWorks()
        {
            var nativeView = new MockNativeView();

            Assert.AreEqual(null, nativeView.Foo);
            Assert.AreEqual(0, nativeView.Bar);
            var inpc = new MockINPC();
            var vm   = new MockVMForNativeBinding();

            vm.CColor = Color.Red;
            var converter = new MockCustomColorConverter();

            nativeView.SetBinding("SelectedColor", new Binding("CColor", BindingMode.TwoWay, converter), inpc);
            nativeView.SetBindingContext(vm);
            Assert.AreEqual(vm.CColor, nativeView.SelectedColor.FormsColor);

            var newFormsColor = Color.Blue;
            var newColor      = new MockNativeColor(newFormsColor);

            nativeView.SelectedColor = newColor;
            inpc.FireINPC(nativeView, nameof(nativeView.SelectedColor));

            Assert.AreEqual(newFormsColor, vm.CColor);
        }
Ejemplo n.º 15
0
        public void ThrowsOnMissingEvent()
        {
            var nativeView = new MockNativeView();

            Assert.Throws <ArgumentException>(() => nativeView.SetBinding("Foo", new Binding("Foo", BindingMode.TwoWay), "missingEvent"));
        }
Ejemplo n.º 16
0
 public static void SetBinding(this MockNativeView target, BindableProperty targetProperty, BindingBase binding)
 {
     NativeBindingHelpers.SetBinding(target, targetProperty, binding);
 }
Ejemplo n.º 17
0
 internal static void SetBinding(this MockNativeView target, string targetProperty, BindingBase binding, INotifyPropertyChanged propertyChanged)
 {
     NativeBindingHelpers.SetBinding(target, targetProperty, binding, propertyChanged);
 }
Ejemplo n.º 18
0
 public MockNativeViewWrapper(MockNativeView nativeView)
 {
     NativeView = nativeView;
     nativeView.TransferbindablePropertiesToWrapper(this);
 }
Ejemplo n.º 19
0
 public static void SetValue(this MockNativeView target, BindableProperty targetProperty, object value)
 {
     NativeBindingHelpers.SetValue(target, targetProperty, value);
 }
Ejemplo n.º 20
0
 public static void SetBindingContext(this MockNativeView target, object bindingContext, Func <MockNativeView, IEnumerable <MockNativeView> > getChild = null)
 {
     NativeBindingHelpers.SetBindingContext(target, bindingContext, getChild);
 }
Ejemplo n.º 21
0
 internal static void TransferbindablePropertiesToWrapper(this MockNativeView target, MockNativeViewWrapper wrapper)
 {
     NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper);
 }
Ejemplo n.º 22
0
 public static View ToView(this MockNativeView nativeView)
 {
     return(new MockNativeViewWrapper(nativeView));
 }
Ejemplo n.º 23
0
 public static void SetBinding(this MockNativeView target, string targetProperty, BindingBase binding, string updateSourceEventName = null)
 {
     NativeBindingHelpers.SetBinding(target, targetProperty, binding, updateSourceEventName);
 }