示例#1
0
        private MvxSourceStepFactory SetupSourceStepFactory()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();

            var autoValueConverters = new MvxAutoValueConverters();

            Ioc.RegisterSingleton <IMvxAutoValueConverters>(autoValueConverters);

            var sourcePropertyParser = new MvxSourcePropertyPathParser();

            Ioc.RegisterSingleton <IMvxSourcePropertyPathParser>(sourcePropertyParser);

            var realSourceBindingFactory = new MvxSourceBindingFactory();

            Ioc.RegisterSingleton <IMvxSourceBindingFactory>(realSourceBindingFactory);

            var sourceStepFactory = new MvxSourceStepFactory();

            sourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory());
            sourceStepFactory.AddOrOverwrite(typeof(MvxLiteralSourceStepDescription), new MvxLiteralSourceStepFactory());
            sourceStepFactory.AddOrOverwrite(typeof(MvxCombinerSourceStepDescription), new MvxCombinerSourceStepFactory());
            Ioc.RegisterSingleton <IMvxSourceStepFactory>(sourceStepFactory);

            var propertySource = new MvxPropertySourceBindingFactoryExtension();

            realSourceBindingFactory.Extensions.Add(propertySource);

            return(sourceStepFactory);
        }
        public void InitializeSingletonCache()
        {
            if (MvxSingletonCache.Instance == null)
            {
                MvxSingletonCache.Initialize();
            }

            if (MvxBindingSingletonCache.Instance == null)
            {
                MvxBindingSingletonCache.Initialize();
            }
        }
示例#3
0
        public void TestEnumValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            Assert.AreEqual(SampleEnum.Defaulto, typeof(SampleEnum).MakeSafeValue(0));
            Assert.AreEqual(SampleEnum.Defaulto, typeof(SampleEnum).MakeSafeValue(null));
            Assert.AreEqual(SampleEnum.Uno, typeof(SampleEnum).MakeSafeValue(1));
            Assert.AreEqual(SampleEnum.Dos, typeof(SampleEnum).MakeSafeValue("Dos"));
            Assert.AreEqual(SampleEnum.Dos, typeof(SampleEnum).MakeSafeValue("dOs"));
        }
示例#4
0
        public void TestStringValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            Assert.AreEqual("0", typeof(string).MakeSafeValue(0.0));
            Assert.AreEqual(null, typeof(string).MakeSafeValue(null));
            Assert.AreEqual("1", typeof(string).MakeSafeValue(1.0));
            Assert.AreEqual("Hello", typeof(string).MakeSafeValue("Hello"));
            Assert.AreEqual("Hello", typeof(string).MakeSafeValue(new MyTest()));
        }
示例#5
0
        public void TestFloatValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            Assert.AreEqual(0.0f, typeof(double).MakeSafeValue(0.0f));
            Assert.AreEqual(0.0f, typeof(double).MakeSafeValue(null));
            Assert.AreEqual(1.0f, typeof(double).MakeSafeValue(1.0f));
            Assert.AreEqual(0.0f, typeof(double?).MakeSafeValue(0.0f));
            Assert.AreEqual(null, typeof(double?).MakeSafeValue(null));
            Assert.AreEqual(1.0f, typeof(double?).MakeSafeValue(1.0f));
            Assert.AreEqual(1.0f, typeof(double).MakeSafeValue(1f));
            Assert.AreEqual(0.0f, typeof(double?).MakeSafeValue(0f));
        }
示例#6
0
        public void TestIntValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            Assert.AreEqual(0, typeof(int).MakeSafeValue(0));
            Assert.AreEqual(0, typeof(int).MakeSafeValue(null));
            Assert.AreEqual(1, typeof(int).MakeSafeValue(1));
            Assert.AreEqual(0, typeof(int?).MakeSafeValue(0));
            Assert.AreEqual(null, typeof(int?).MakeSafeValue(null));
            Assert.AreEqual(1, typeof(int?).MakeSafeValue(1));
            Assert.AreEqual(0, typeof(int?).MakeSafeValue(0.0));
            Assert.AreEqual(1, typeof(int?).MakeSafeValue(1.0));
        }
示例#7
0
        public void TestObjectValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            var foo = new Foo();

            Assert.AreSame(foo, typeof(FooBase).MakeSafeValue(foo));
            var fooBase = new FooBase();

            Assert.AreSame(fooBase, typeof(FooBase).MakeSafeValue(fooBase));
            fooBase = null;
            Assert.IsNull(typeof(FooBase).MakeSafeValue(null));
        }
示例#8
0
        public void TestBoolValues()
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Mvx.RegisterSingleton <IMvxAutoValueConverters>(new MockAutoValueConverters());

            Assert.AreEqual(false, typeof(bool).MakeSafeValue(0));
            Assert.AreEqual(false, typeof(bool).MakeSafeValue(null));
            Assert.AreEqual(true, typeof(bool).MakeSafeValue(1));
            Assert.AreEqual(true, typeof(bool).MakeSafeValue(-1.0));
            Assert.AreEqual(true, typeof(bool).MakeSafeValue(1.0));
            Assert.AreEqual(true, typeof(bool).MakeSafeValue("Dos"));
            Assert.AreEqual(true, typeof(bool).MakeSafeValue("dOs"));

            Assert.AreEqual(false, typeof(bool?).MakeSafeValue(0));
            Assert.AreEqual(null, typeof(bool?).MakeSafeValue(null));
            Assert.AreEqual(true, typeof(bool?).MakeSafeValue(1));
            Assert.AreEqual(true, typeof(bool?).MakeSafeValue(-1.0));
            Assert.AreEqual(true, typeof(bool?).MakeSafeValue(1.0));
            Assert.AreEqual(true, typeof(bool?).MakeSafeValue("Dos"));
            Assert.AreEqual(true, typeof(bool?).MakeSafeValue("dOs"));
        }
示例#9
0
        private MvxFullBinding TestSetupCommon(IMvxValueConverter valueConverter, object converterParameter, object fallbackValue,
                                               Type targetType, out MockSourceBinding mockSource, out MockTargetBinding mockTarget)
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();

            var mockSourceBindingFactory = new Mock <IMvxSourceBindingFactory>();

            Ioc.RegisterSingleton(mockSourceBindingFactory.Object);

            var mockTargetBindingFactory = new Mock <IMvxTargetBindingFactory>();

            Ioc.RegisterSingleton(mockTargetBindingFactory.Object);

            var realSourceStepFactory = new MvxSourceStepFactory();

            realSourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory());
            Ioc.RegisterSingleton <IMvxSourceStepFactory>(realSourceStepFactory);

            var sourceText         = "sourceText";
            var targetName         = "targetName";
            var source             = new { Value = 1 };
            var target             = new { Value = 2 };
            var bindingDescription = new MvxBindingDescription
            {
                Source = new MvxPathSourceStepDescription()
                {
                    Converter          = valueConverter,
                    ConverterParameter = converterParameter,
                    FallbackValue      = fallbackValue,
                    SourcePropertyPath = sourceText,
                },
                Mode       = MvxBindingMode.TwoWay,
                TargetName = targetName
            };

            mockSource = new MockSourceBinding();
            mockTarget = new MockTargetBinding()
            {
                TargetType = targetType
            };
            mockTarget.DefaultMode = MvxBindingMode.TwoWay;

            var localSource = mockSource;

            mockSourceBindingFactory
            .Setup(x => x.CreateBinding(It.IsAny <object>(), It.Is <string>(s => s == sourceText)))
            .Returns((object a, string b) => localSource);
            var localTarget = mockTarget;

            mockTargetBindingFactory
            .Setup(x => x.CreateBinding(It.IsAny <object>(), It.Is <string>(s => s == targetName)))
            .Returns((object a, string b) => localTarget);

            mockSource.TryGetValueResult = true;
            mockSource.TryGetValueValue  = "TryGetValueValue";

            var request = new MvxBindingRequest(source, target, bindingDescription);
            var toTest  = new MvxFullBinding(request);

            return(toTest);
        }
示例#10
0
        private void TestCommon(MvxBindingMode bindingMode, bool expectSourceBinding, bool expectTargetBinding)
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();
            Ioc.RegisterSingleton <IMvxMainThreadDispatcher>(new InlineMockMainThreadDispatcher());

            var mockSourceBindingFactory = new Mock <IMvxSourceBindingFactory>();

            Ioc.RegisterSingleton(mockSourceBindingFactory.Object);

            var mockTargetBindingFactory = new Mock <IMvxTargetBindingFactory>();

            Ioc.RegisterSingleton(mockTargetBindingFactory.Object);

            var realSourceStepFactory = new MvxSourceStepFactory();

            realSourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory());
            Ioc.RegisterSingleton <IMvxSourceStepFactory>(realSourceStepFactory);

            var sourceText         = "sourceText";
            var targetName         = "targetName";
            var source             = new { Value = 1 };
            var target             = new { Value = 2 };
            var converterParameter = new { Value = 3 };
            var fallbackValue      = new { Value = 4 };
            var converter          = new Mock <IMvxValueConverter>();
            var bindingDescription = new MvxBindingDescription
            {
                Source = new MvxPathSourceStepDescription()
                {
                    Converter          = converter.Object,
                    ConverterParameter = converterParameter,
                    FallbackValue      = fallbackValue,
                    SourcePropertyPath = sourceText,
                },
                Mode       = bindingMode,
                TargetName = targetName
            };

            var mockSourceBinding = new Mock <IMvxSourceBinding>();
            var mockTargetBinding = new Mock <IMvxTargetBinding>();

            mockSourceBindingFactory
            .Setup(x => x.CreateBinding(It.Is <object>(s => s == source), It.Is <string>(s => s == sourceText)))
            .Returns((object a, string b) => mockSourceBinding.Object);
            mockTargetBindingFactory
            .Setup(x => x.CreateBinding(It.Is <object>(s => s == target), It.Is <string>(s => s == targetName)))
            .Returns((object a, string b) => mockTargetBinding.Object);

            var request = new MvxBindingRequest(source, target, bindingDescription);

            var toTest = new MvxFullBinding(request);

            //var sourceBindingTimes = expectSourceBinding ? Times.Once() : Times.Never();
            //mockSourceBinding.Verify(x => x.Changed += It.IsAny<EventHandler<MvxSourcePropertyBindingEventArgs>>(), sourceBindingTimes);
            mockSourceBindingFactory
            .Verify(x => x.CreateBinding(It.Is <object>(s => s == source), It.Is <string>(s => s == sourceText)),
                    Times.Once());

            //var targetBindingTimes = expectSourceBinding ? Times.Once() : Times.Never();
            //mockTargetBinding.Verify(x => x.ValueChanged += It.IsAny<EventHandler<MvxTargetChangedEventArgs>>(), targetBindingTimes);
            mockTargetBindingFactory
            .Verify(x => x.CreateBinding(It.Is <object>(s => s == target), It.Is <string>(s => s == targetName)),
                    Times.Once());
        }
        private void DoTest(
            Action <MockBindingContext> action,
            Func <MockBindingContext, object> findTargetObjectFunc,
            MvxBindingDescription expectedDescription)
        {
            ClearAll();
            MvxBindingSingletonCache.Initialize();

            var dataContext = new TestDataContext();

            var bindingContext = new Mock <IMvxBindingContext>();

            bindingContext.Setup(x => x.RegisterBinding(It.IsAny <object>(), It.IsAny <IMvxUpdateableBinding>()));
            bindingContext.SetupGet(x => x.DataContext).Returns(dataContext);

            var callbacksSeen = new List <Callback>();

            var binder = new Mock <IMvxBinder>();

            binder.Setup(
                b => b.Bind(It.IsAny <object>(), It.IsAny <object>(), It.IsAny <IEnumerable <MvxBindingDescription> >()))
            .Callback((object source, object target, IEnumerable <MvxBindingDescription> descriptions) =>
            {
                if (descriptions.Count() != 1)
                {
                    throw new Exception("Unexpected description count");
                }

                callbacksSeen.Add(new Callback
                {
                    Source             = source,
                    Target             = target,
                    BindingDescription = descriptions.First()
                });
            });
            Ioc.RegisterSingleton(binder.Object);

            Ioc.RegisterSingleton <IMvxPropertyExpressionParser>(new MvxPropertyExpressionParser());

            var converterLookup = new MvxValueConverterRegistry();

            converterLookup.AddOrOverwrite("SampleConv", new SampleValueConverter());
            Ioc.RegisterSingleton <IMvxValueConverterLookup>(converterLookup);

            var testTarget = new MockBindingContext
            {
                Target         = new TestTarget(),
                BindingContext = bindingContext.Object
            };

            action(testTarget);

            Assert.AreEqual(1, callbacksSeen.Count);
            var callback       = callbacksSeen[0];
            var expectedTarget = findTargetObjectFunc(testTarget);

            Assert.AreEqual(expectedTarget, callback.Target);
            Assert.AreEqual(dataContext, callback.Source);

            var desc = callback.BindingDescription;

            Assert.IsTrue(expectedDescription.Source is MvxPathSourceStepDescription);
            var path = desc.Source as MvxPathSourceStepDescription;

            Assert.IsTrue(desc.Source is MvxPathSourceStepDescription);
            var expectedPath = expectedDescription.Source as MvxPathSourceStepDescription;

            Assert.AreEqual(expectedPath.ConverterParameter, path.ConverterParameter);
            Assert.AreEqual(expectedPath.FallbackValue, path.FallbackValue);
            Assert.AreEqual(expectedPath.SourcePropertyPath, path.SourcePropertyPath);
            Assert.AreEqual(expectedDescription.Mode, desc.Mode);
            Assert.AreEqual(expectedDescription.TargetName, desc.TargetName);
            if (expectedPath.Converter == null)
            {
                Assert.IsNull(path.Converter);
            }
            else
            {
                Assert.AreEqual(expectedPath.Converter.GetType(), path.Converter.GetType());
            }
        }