Exemplo n.º 1
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);
        }
Exemplo 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);
        }
Exemplo n.º 3
0
        public void ThrowsOnMissingProperty()
        {
            var nativeView = new MockNativeView();

            nativeView.SetBinding("Qux", new Binding("Foo"));
            Assert.Throws <InvalidOperationException>(() => nativeView.SetBindingContext(new { Foo = 42 }));
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
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));
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
0
        public void TestConverterWorks()
        {
            var nativeView = new MockNativeView();

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

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

            nativeView.SetBinding("SelectedColor", new Binding("CColor", converter: converter));
            nativeView.SetBindingContext(vm);
            Assert.AreEqual(vm.CColor, nativeView.SelectedColor.FormsColor);
        }
Exemplo n.º 13
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 = Colors.Red
            };

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

            Assert.AreEqual(count, 1);
        }
Exemplo 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 = Colors.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 = Colors.Blue;
            var newColor      = new MockNativeColor(newFormsColor);

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

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

            Assert.Throws <ArgumentException>(() => nativeView.SetBinding("Foo", new Binding("Foo", BindingMode.TwoWay), "missingEvent"));
        }