public virtual void BindingShouldBeRegisteredInBindingManager()
 {
     var target = new object();
     IBindingPath path = BindingPath.Create("test");
     var bindingManager = new BindingManager();
     DataBinding binding = CreateDataBinding(
         new BindingSourceAccessorMock { Source = new BindingSourceMock { GetSource = b => target, Path = path } },
         new BindingSourceAccessorMock(), bindingManager);
     bindingManager.Register(target, path.Path, binding);
     bindingManager.GetBindings(target).Single().ShouldEqual(binding);
     bindingManager.GetBindings(target, path.Path).Single().ShouldEqual(binding);
 }
        public virtual void BindingShouldAddSelfToDataContext()
        {
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            var dataContext = binding.Context;
            dataContext.Count.ShouldEqual(1);
            dataContext.GetData(BindingConstants.Binding).ShouldEqual(binding);
        }
        public virtual void BindingShouldCorrectInitializeProperties()
        {
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            binding.TargetAccessor.ShouldEqual(target);
            binding.SourceAccessor.ShouldEqual(source);
            binding.Behaviors.ShouldBeEmpty();
        }
 public virtual void BindingShouldRaiseExceptionEventWhenUpdateTargetThrowException()
 {
     bool isInvoked = false;
     IBindingPath path = BindingPath.Create("test");
     var bindingManager = new BindingManager();
     var target = new BindingSourceAccessorMock
     {
         Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
     };
     var source = new BindingSourceAccessorMock
     {
         Source = new BindingSourceMock
         {
             GetSource = b => new object(),
             Path = path,
             IsValid = b => true
         }
     };
     DataBinding binding = CreateDataBinding(target, source, bindingManager);
     target.SetValue = (func, context, arg3) => { throw new TestException(); };
     binding.BindingException += (sender, args) =>
     {
         args.Action.ShouldEqual(BindingAction.UpdateTarget);
         args.Exception.InnerException.ShouldBeType<TestException>();
         isInvoked = true;
     };
     binding.UpdateTarget();
     isInvoked.ShouldBeTrue();
 }
 public virtual void BindingShouldUpdateTargetWithBindingContext()
 {
     bool isInvoked = false;
     IBindingPath path = BindingPath.Create("test");
     var bindingManager = new BindingManager();
     var target = new BindingSourceAccessorMock
     {
         Source = new BindingSourceMock
         {
             GetSource = b => new object(),
             Path = path
         }
     };
     var source = new BindingSourceAccessorMock
     {
         Source = new BindingSourceMock
         {
             IsValid = b => true
         }
     };
     DataBinding binding = CreateDataBinding(target, source, bindingManager);
     target.SetValue = (func, context, arg3) =>
     {
         context.ShouldEqual(binding.Context);
         arg3.ShouldBeTrue();
         isInvoked = true;
         return true;
     };
     binding.UpdateTarget();
     isInvoked.ShouldBeTrue();
 }
        public virtual void BindingShouldCallAttachDetachMethodInBehavior()
        {
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            int countAttach = 0;
            int countDetach = 0;
            var first = new BindingBehaviorMock
            {
                Id = Guid.Empty,
                Attach = binding1 =>
                {
                    countAttach++;
                    return true;
                },
                Detach = binding1 => countDetach++
            };
            binding.Behaviors.Add(first);
            countAttach.ShouldEqual(1);
            countDetach.ShouldEqual(0);

            binding.Behaviors.Remove(first);
            countAttach.ShouldEqual(1);
            countDetach.ShouldEqual(1);
        }
        public virtual void BindingShouldNotAddBehaviorIfAttachReturnsFalse()
        {
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            var first = new BindingBehaviorMock
            {
                Id = Guid.Empty,
                Attach = binding1 => false
            };
            binding.Behaviors.Add(first);
            binding.Behaviors.Count.ShouldEqual(0);
            binding.Behaviors.Contains(first).ShouldBeFalse();
        }
        public virtual void BindingShouldNotAddSameBehavior()
        {
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            var first = new BindingBehaviorMock
            {
                Id = Guid.Empty,
                Attach = binding1 => true
            };
            binding.Behaviors.Add(first);
            ShouldThrow(() => binding.Behaviors.Add(first));
        }
        public virtual void BindingShouldRaiseEventWhenUpdateSourceFalse()
        {
            bool isInvoked = false;
            IBindingPath path = BindingPath.Create("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new BindingSourceMock { GetSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();

            DataBinding binding = CreateDataBinding(target, source, bindingManager);
            source.SetValue = (func, context, arg3) => false;
            binding.BindingUpdated += (sender, args) =>
            {
                args.Action.ShouldEqual(BindingAction.UpdateSource);
                args.Result.ShouldBeFalse();
                isInvoked = true;
            };
            binding.UpdateSource();
            isInvoked.ShouldBeTrue();
        }
        public virtual void BindingShouldThrowExceptionDuplicateIdBehavior()
        {
            IBindingPath path = new BindingPath("test");
            var bindingManager = new BindingManager();
            var target = new BindingSourceAccessorMock
            {
                Source = new ObserverMock { GetActualSource = b => new object(), Path = path }
            };
            var source = new BindingSourceAccessorMock();
            DataBinding binding = CreateDataBinding(target, source, bindingManager);

            var first = new BindingBehaviorMock { Id = Guid.Empty, Attach = binding1 => true };
            var second = new BindingBehaviorMock { Id = Guid.Empty, Attach = binding1 => true };
            binding.Behaviors.Add(first);
            ShouldThrow(() => binding.Behaviors.Add(second));
        }