Esempio n. 1
0
        private static void UpdateSourceFromTarget(MvxBindingRequest bindingRequest, IMvxSourceBinding sourceBinding, object value)
        {
            try
            {
                if (bindingRequest.Description.Converter != null)
                {
                    value =
                        bindingRequest.Description.Converter.ConvertBack(value,
                                                                         sourceBinding.SourceType,
                                                                         bindingRequest.Description.ConverterParameter,
                                                                         CultureInfo.CurrentUICulture);
                }
                sourceBinding.SetValue(value);
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                MvxBindingTrace.Trace(
                    MvxTraceLevel.Error,

                    "Problem seen during binding execution for {0} - problem {1}",
                    bindingRequest.ToString(),
                    exception.ToLongString());
            }
        }
        // TODO: need to test other things like:
        // - event subscription (hard to do through Moq)
        // - use of valueConverter
        // - one time subscription
        // - fallback values
        // - setup again on DataContext change
        // - dispose mechanism

        private void TestCommon(MvxBindingMode bindingMode, bool expectSourceBinding, bool expectTargetBinding)
        {
            ClearAll();

            var mockSourceBindingFactory = new Mock <IMvxSourceBindingFactory>();

            Ioc.RegisterSingleton(mockSourceBindingFactory.Object);

            var mockTargetBindingFactory = new Mock <IMvxTargetBindingFactory>();

            Ioc.RegisterSingleton(mockTargetBindingFactory.Object);

            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
            {
                Converter          = converter.Object,
                ConverterParameter = converterParameter,
                FallbackValue      = fallbackValue,
                Mode = bindingMode,
                SourcePropertyPath = sourceText,
                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());
        }
        public IMvxUpdateableBinding BindSingle(object source, object target, string targetPropertyName,
                                                string partialBindingDescription)
        {
            var bindingDescription =
                BindingDescriptionParser.ParseSingle(partialBindingDescription);
            if (bindingDescription == null)
                return null;

            bindingDescription.TargetName = targetPropertyName;
            var request = new MvxBindingRequest(source, target, bindingDescription);
            return BindSingle(request);
        }
        public static IMvxBinding BindClickToCommand(this View view, object source, string propertyPath)
        {
            var bindingParameters = new MvxBindingRequest()
            {
                Source      = source,
                Target      = view,
                Description = new MvxBindingDescription()
                {
                    SourcePropertyPath = propertyPath,
                    TargetName         = "Click"
                }
            };

            return(Binder.BindSingle(bindingParameters));
        }
Esempio n. 5
0
        public IMvxUpdateableBinding BindSingle(object source, object target, string targetPropertyName,
                                                string partialBindingDescription)
        {
            var bindingDescription =
                MvxBindingSingletonCache.Instance.BindingDescriptionParser.ParseSingle(partialBindingDescription);

            if (bindingDescription == null)
            {
                return(null);
            }

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

            return(BindSingle(request));
        }
        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;
        }
Esempio n. 7
0
 public IMvxUpdateableBinding BindSingle(MvxBindingRequest bindingRequest)
 {
     return(new MvxFullBinding(bindingRequest));
 }
Esempio n. 8
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);
        }
 public MvxFullBinding(MvxBindingRequest bindingRequest)
 {
     _bindingDescription = bindingRequest.Description;
     CreateTargetBinding(bindingRequest.Target);
     CreateSourceBinding(bindingRequest.Source);
 }
Esempio n. 10
0
        private MvxFullBinding TestSetupCommon(MvxBindingMode mvxBindingMode, MvxBindingMode defaultMode,
                                               out MockSourceBinding mockSource, out MockTargetBinding mockTarget)
        {
            _fixture.ClearAll();
            _fixture.Ioc.RegisterSingleton <IMvxMainThreadDispatcher>(new InlineMockMainThreadDispatcher());

            var mockSourceBindingFactory = new Mock <IMvxSourceBindingFactory>();

            _fixture.Ioc.RegisterSingleton(mockSourceBindingFactory.Object);

            var mockTargetBindingFactory = new Mock <IMvxTargetBindingFactory>();

            _fixture.Ioc.RegisterSingleton(mockTargetBindingFactory.Object);

            var realSourceStepFactory = new MvxSourceStepFactory();

            realSourceStepFactory.AddOrOverwrite(typeof(MvxPathSourceStepDescription), new MvxPathSourceStepFactory());
            _fixture.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 bindingDescription = new MvxBindingDescription
            {
                Source = new MvxPathSourceStepDescription
                {
                    Converter          = null,
                    ConverterParameter = converterParameter,
                    FallbackValue      = fallbackValue,
                    SourcePropertyPath = sourceText,
                },
                Mode       = mvxBindingMode,
                TargetName = targetName
            };

            mockSource = new MockSourceBinding();
            mockTarget = new MockTargetBinding {
                DefaultMode = defaultMode
            };

            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);
        }
Esempio n. 11
0
 public MyBinding(MvxBindingRequest bindingRequest)
     : base(bindingRequest)
 {
 }
 public IMvxUpdateableBinding BindSingle(MvxBindingRequest bindingRequest)
 {
     return new MvxFullBinding(bindingRequest);
 }
Esempio n. 13
0
 public MvxFullBinding(MvxBindingRequest bindingRequest)
 {
     this._bindingDescription = bindingRequest.Description;
     this.CreateTargetBinding(bindingRequest.Target);
     this.CreateSourceBinding(bindingRequest.Source);
 }
        private void TestCommon(MvxBindingMode bindingMode, bool expectSourceBinding, bool expectTargetBinding)
        {
            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 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());
        }
 public MyBinding(MvxBindingRequest bindingRequest)
     : base(bindingRequest)
 {
 }