public virtual void BindingShouldBeRegisteredInBindingManager()
 {
     var target = new object();
     IBindingPath path = new BindingPath("test");
     var bindingManager = new BindingManager();
     DataBinding binding = CreateDataBinding(
         new BindingSourceAccessorMock { Source = new ObserverMock { GetActualSource = 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);
 }
Example #2
0
 public static IBindingPath Create([NotNull] string path)
 {
     lock (Cache)
     {
         IBindingPath value;
         if (!Cache.TryGetValue(path, out value))
         {
             value       = new BindingPath(path);
             Cache[path] = value;
         }
         return(value);
     }
 }
        public virtual void BindingShouldAddSelfToDataContext()
        {
            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 dataContext = binding.Context;
            dataContext.Count.ShouldEqual(1);
            dataContext.GetData(BindingConstants.Binding).ShouldEqual(binding);
        }
        public virtual void BindingShouldCorrectInitializeProperties()
        {
            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);

            binding.TargetAccessor.ShouldEqual(target);
            binding.SourceAccessor.ShouldEqual(source);
            binding.Behaviors.ShouldBeEmpty();
        }
Example #5
0
 public static IBindingPath Create([NotNull] string path, bool useCache = true)
 {
     if (!useCache)
     {
         return(new BindingPath(path));
     }
     lock (Cache)
     {
         IBindingPath value;
         if (!Cache.TryGetValue(path, out value))
         {
             value = new BindingPath(path);
             if (Cache.Count > 300)
             {
                 while (Cache.Count != 250)
                 {
                     Cache.Remove(Cache.FirstOrDefault().Key);
                 }
             }
             Cache[path] = value;
         }
         return(value);
     }
 }
 private static IBindingPath BindingPathFactoryImpl(string path)
 {
     lock (BindingPathCache)
     {
         IBindingPath value;
         if (!BindingPathCache.TryGetValue(path, out value))
         {
             value = new BindingPath(path);
             BindingPathCache[path] = value;
         }
         return value;
     }
 }
Example #7
0
 public static IBindingPath Create([NotNull]string path, bool useCache = true)
 {
     if (!useCache)
         return new BindingPath(path);
     lock (Cache)
     {
         IBindingPath value;
         if (!Cache.TryGetValue(path, out value))
         {
             value = new BindingPath(path);
             if (Cache.Count > 300)
             {
                 while (Cache.Count != 250)
                     Cache.Remove(Cache.FirstOrDefault().Key);
             }
             Cache[path] = value;
         }
         return value;
     }
 }
 public virtual void BindingShouldRaiseExceptionEventWhenUpdateTargetThrowException()
 {
     bool isInvoked = false;
     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
     {
         Source = new ObserverMock
         {
             GetActualSource = b => new object(),
             Path = path,
             IsValid = b => true
         }
     };
     DataBinding binding = CreateDataBinding(target, source, bindingManager);
     target.SetValue = (func, context, arg3) => { throw new TestException(); };
     binding.BindingUpdated += (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 = new BindingPath("test");
     var bindingManager = new BindingManager();
     var target = new BindingSourceAccessorMock
     {
         Source = new ObserverMock
         {
             GetActualSource = b => new object(),
             Path = path
         }
     };
     var source = new BindingSourceAccessorMock
     {
         Source = new ObserverMock
         {
             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 = 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);

            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 = 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 => false
            };
            binding.Behaviors.Add(first);
            binding.Behaviors.Count.ShouldEqual(0);
            binding.Behaviors.Contains(first).ShouldBeFalse();
        }
        public virtual void BindingShouldNotAddSameBehavior()
        {
            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
            };
            binding.Behaviors.Add(first);
            ShouldThrow(() => binding.Behaviors.Add(first));
        }
        public virtual void BindingShouldRaiseEventWhenUpdateSourceFalse()
        {
            bool isInvoked = false;
            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);
            source.SetValue = (func, context, arg3) => false;
            binding.BindingUpdated += (sender, args) =>
            {
                args.Action.ShouldEqual(BindingAction.UpdateSource);
                args.Result.ShouldBeFalse();
                isInvoked = true;
            };
            binding.UpdateSource();
            isInvoked.ShouldBeTrue();
        }
 protected virtual IObserver CreateObserver(object source, string path, bool ignoreContext,
     IBindingMemberProvider memberProvider = null, IBindingContextManager contextManager = null, bool hasStablePath = false, bool observable = true, bool optional = false)
 {
     if (memberProvider != null)
         BindingServiceProvider.MemberProvider = memberProvider;
     if (contextManager != null)
         BindingServiceProvider.ContextManager = contextManager;
     var bindingPath = new BindingPath(path);
     if (bindingPath.IsEmpty)
         return new EmptyPathObserver(source, bindingPath);
     if (bindingPath.IsSingle)
         return new SinglePathObserver(source, bindingPath, ignoreContext, hasStablePath, observable, optional);
     return new MultiPathObserver(source, bindingPath, ignoreContext, hasStablePath, observable, optional);
 }