public void WeakEventManagerShouldRemoveWeakListenersPropertyChanged()
        {
            const int count = 100;
            const string propertyName = "test";

            var model = new BindingSourceModel();
            var listeners = new List<WeakReference>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                weakEventManager.Subscribe(model, propertyName, listenerMock);
                listeners.Add(new WeakReference(listenerMock));
                listenerMock.Handle = (o, o1) => { };
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            model.OnPropertyChanged(propertyName + "1");
            model.OnPropertyChanged(propertyName);
            model.OnPropertyChanged(propertyName);

            listeners.All(reference => reference.Target == null).ShouldBeTrue();
        }
        public void GetValueShouldReturnActualValueDoubleSource()
        {
            const string value = "100";
            bool isInvoked = false;
            var memberMock = new BindingMemberInfoMock();
            var sourceModel = new BindingSourceModel();
            string propertyName1 = GetMemberPath<BindingSourceModel>(model => model.IntProperty);
            string propertyName2 = GetMemberPath<BindingSourceModel>(model => model["test"]);
            var valueAccessor = new MultiBindingSourceAccessor(new[]
            {
                CreateSource(sourceModel, propertyName1),
                CreateSource(sourceModel, propertyName2),
            },
                (context, list) =>
                {
                    list.Count.ShouldEqual(2);
                    isInvoked = true;
                    context.ShouldEqual(EmptyContext);
                    list[0].ShouldEqual(sourceModel.IntProperty);
                    list[1].ShouldEqual(sourceModel["test"]);
                    return value;
                }, EmptyContext);
            valueAccessor.GetValue(memberMock, EmptyContext, true).ShouldEqual(value);
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            sourceModel["test"] = propertyName1;
            sourceModel.IntProperty = int.MaxValue;
            valueAccessor.GetValue(memberMock, EmptyContext, true).ShouldEqual(value);
            isInvoked.ShouldBeTrue();
        }
        public void WeakEventManagerShouldSubscribeAndUnsubscribePropertyChangedEventSeveralSources()
        {
            const int count = 100;
            const string propertyName = "test";

            var model = new BindingSourceModel();
            var listeners = new List<EventListenerMock>();
            var invokedItems = new List<EventListenerMock>();
            IWeakEventManager weakEventManager = CreateWeakEventManager();

            for (int i = 0; i < count; i++)
            {
                var listenerMock = new EventListenerMock();
                var disposable = weakEventManager.Subscribe(model, propertyName, listenerMock);
                listeners.Add(listenerMock);
                listenerMock.Handle = (o, o1) =>
                {
                    invokedItems.Add(listenerMock);
                    disposable.Dispose();
                };
            }
            model.OnPropertyChanged(propertyName + "1");
            model.OnPropertyChanged(propertyName);
            model.OnPropertyChanged(propertyName);

            invokedItems.Count.ShouldEqual(count);
            foreach (var listener in listeners)
                invokedItems.Contains(listener).ShouldBeTrue();
        }
 public void CmdShouldNotAddNotifierWhenCanExecuteNull()
 {
     var notifier = new BindingSourceModel();
     var relayCommand = CreateCommand(NodoAction);
     relayCommand.AddNotifier(notifier).ShouldBeFalse();
     relayCommand.GetNotifiers().ShouldBeEmpty();
 }
 public void GetValueShouldThrowExceptionInvalidValueIfFlagTrue()
 {
     var memberMock = new BindingMemberInfoMock();
     var sourceModel = new BindingSourceModel();
     const string propertyName = "invalid";
     var valueAccessor = new MultiBindingSourceAccessor(new[] { CreateSource(sourceModel, propertyName) },
         (context, list) => null, EmptyContext);
     ShouldThrow(() => valueAccessor.GetValue(memberMock, EmptyContext, true));
 }
        public void ObserverShouldThrowExceptionInvalidPath()
        {
            var model = new BindingSourceModel();
            var observer = CreateObserver(model, "invalid", false, optional: false);

            observer.Source.ShouldEqual(model);
            ShouldThrow(() =>
            {
                var members = observer.GetPathMembers(true);
            });
        }
        public void ObserverShouldUseObjectAsSource()
        {
            var model = new BindingSourceModel();
            var propertyName = GetMemberPath<BindingSourceModel>(sourceModel => sourceModel.StringProperty);
            var observer = CreateObserver(model, propertyName, false);

            observer.Source.ShouldEqual(model);
            var members = observer.GetPathMembers(true);
            members.AllMembersAvailable.ShouldBeTrue();
            members.Members.Single().Type.ShouldEqual(typeof(string));
            observer.Path.Path.ShouldEqual(propertyName);
        }
        public void ObserverShouldNotThrowExceptionInvalidPathOptional()
        {
            var model = new BindingSourceModel();
            var observer = CreateObserver(model, "invalid", false, optional: true);

            observer.Source.ShouldEqual(model);
            var members = observer.GetPathMembers(true);
            members.AllMembersAvailable.ShouldBeFalse();

            members = observer.GetPathMembers(false);
            members.AllMembersAvailable.ShouldBeFalse();
        }
        public void BuilderShouldUseTargetBindingContextForSource3()
        {
            const string sourcePath = "IntProperty";
            var targetObj = new BindingSourceEventNotifierModel();
            var builder = new BindingBuilder();
            builder.Bind(targetObj, () => model => model.ObjectProperty).To(sourcePath);

            IList<Func<IDataContext, IBindingSource>> sources = builder.GetData(BindingBuilderConstants.Sources);
            IBindingSource source = sources.Single().Invoke(builder);
            BindingParserTest.BindingSourceShouldBeValidDataContext(targetObj, source, sourcePath);
            var sourceObj = new BindingSourceModel();
            BindingServiceProvider.ContextManager.GetBindingContext(targetObj).Value = sourceObj;
            BindingParserTest.BindingSourceShouldBeValidDataContext(targetObj, source, sourcePath);
        }
        public void BuilderShouldUseTargetBindingContextForSource2()
        {
            const string targetPath = "Text";
            const string sourcePath = "IntProperty";
            var targetObj = new object();
            var builder = new BindingBuilder();
            builder.Bind(targetObj, targetPath).To<BindingSourceModel>(() => (model, ctx) => model.IntProperty);

            IList<Func<IDataContext, IObserver>> sources = builder.GetData(BindingBuilderConstants.Sources);
            IObserver source = sources.Single().Invoke(builder);
            BindingParserTest.BindingSourceShouldBeValidDataContext(targetObj, source, sourcePath);
            var sourceObj = new BindingSourceModel();
            BindingServiceProvider.ContextManager.GetBindingContext(targetObj).Value = sourceObj;
            BindingParserTest.BindingSourceShouldBeValidDataContext(targetObj, source, sourcePath);
        }
Example #11
0
        public void ObserverShouldRaiseValueChangedEventWhenPropertyChanged()
        {
            bool isInvoked = false;
            var model = new BindingSourceModel();
            var propertyName = GetMemberPath<BindingSourceModel>(sourceModel => sourceModel.StringProperty);
            var observer = CreateObserver(model, propertyName, false);
            observer.ValueChanged += (sender, args) => isInvoked = true;

            isInvoked = false;
            model.StringProperty = "test";
            isInvoked.ShouldBeTrue();
            isInvoked = false;

            model.IntProperty = 10;
            isInvoked.ShouldBeFalse();
        }
        public void AutoPropertyTest()
        {
            var source = new BindingSourceModel();
            const string path = "path";
            Type type = typeof(string);
            IAttachedBindingMemberInfo<object, object> property = AttachedBindingMember.CreateAutoProperty(path, type);
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(type);
            property.CanRead.ShouldBeTrue();
            property.CanWrite.ShouldBeTrue();
            property.Member.ShouldBeNull();
            property.MemberType.ShouldEqual(BindingMemberType.Attached);

            property.GetValue(source, null).ShouldBeNull();
            property.SetValue(source, new object[] { path });
            property.GetValue(source, null).ShouldEqual(path);
        }
        public void ProviderShouldReturnMemberForProperty()
        {
            var model = new BindingSourceModel();
            var path = GetMemberPath(model, m => m.IntProperty);
            var provider = CreateMemberProvider();

            var member = provider.GetBindingMember(typeof(BindingSourceModel), path, true, true);
            member.CanRead.ShouldBeTrue();
            member.CanWrite.ShouldBeTrue();
            member.Type.ShouldEqual(typeof(int));
            member.MemberType.ShouldEqual(BindingMemberType.Property);
            member.Path.ShouldEqual(path);
            (member.Member is PropertyInfo).ShouldBeTrue();

            member.SetValue(model, new object[] { int.MaxValue });
            member.GetValue(model, null).ShouldEqual(int.MaxValue);
        }
        public void ProviderShouldReturnMemberForField()
        {
            const string value = "value";
            var model = new BindingSourceModel();
            var path = GetMemberPath(model, m => m.PublicField);
            var provider = CreateMemberProvider();

            var member = provider.GetBindingMember(typeof(BindingSourceModel), path, true, true);
            member.CanRead.ShouldBeTrue();
            member.CanWrite.ShouldBeTrue();
            member.Type.ShouldEqual(typeof(string));
            member.MemberType.ShouldEqual(BindingMemberType.Field);
            member.Path.ShouldEqual(path);
            (member.Member is FieldInfo).ShouldBeTrue();

            member.SetValue(model, new object[] { value });
            member.GetValue(model, null).ShouldEqual(value);
        }
        public void ParserShouldUseTargetBindingContextForSource()
        {
            const string targetPath = "Text";
            const string sourcePath = "IntProperty";
            const string binding = "Text IntProperty";
            var target = new object();
            IBindingParser bindingParser = CreateBindingParser();

            var context = new BindingBuilder(bindingParser.Parse(binding, EmptyContext, target, null).Single());
            IBindingPath path = context.GetData(BindingBuilderConstants.TargetPath);
            path.Path.ShouldEqual(targetPath);

            var sources = context.GetData(BindingBuilderConstants.Sources);
            IBindingSource source = sources.Single().Invoke(context);
            BindingSourceShouldBeValidDataContext(target, source, sourcePath);
            var sourceObj = new BindingSourceModel();
            BindingServiceProvider.ContextManager.GetBindingContext(target).Value = sourceObj;
            BindingSourceShouldBeValidDataContext(target, source, sourcePath);
        }
        public void AutoPropertyGenericMemberChangeTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateAutoProperty<BindingSourceModel, string>(path,
                (o, args) =>
                {
                    isInvoked = true;
                    o.ShouldEqual(source);
                    args.OldValue.ShouldBeNull();
                    args.NewValue.ShouldEqual(path);
                });

            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();
        }
        public void WeakEventManagerShouldSubscribeAndUnsubscribePropertyChangedEvent()
        {
            const string propertyName = "test";
            int invokedCount = 0;
            var model = new BindingSourceModel();
            var listenerMock = new EventListenerMock();
            IWeakEventManager weakEventManager = CreateWeakEventManager();
            var disposable = weakEventManager.Subscribe(model, propertyName, listenerMock);

            listenerMock.Handle = (o, o1) => invokedCount++;
            invokedCount.ShouldEqual(0);

            model.OnPropertyChanged(propertyName + "1");
            invokedCount.ShouldEqual(0);
            model.OnPropertyChanged(propertyName);
            invokedCount.ShouldEqual(1);

            disposable.Dispose();

            model.OnPropertyChanged(propertyName);
            invokedCount.ShouldEqual(1);
        }
        public void GetValueShouldReturnActualValue()
        {
            bool isInvoked = false;
            var memberMock = new BindingMemberInfoMock();
            var sourceModel = new BindingSourceModel();
            string propertyName = GetMemberPath<BindingSourceModel>(model => model.IntProperty);
            var valueAccessor = new MultiBindingSourceAccessor(new[] { CreateSource(sourceModel, propertyName) },
                (context, list) =>
                {
                    isInvoked = true;
                    context.ShouldEqual(EmptyContext);
                    list.Single().ShouldEqual(sourceModel.IntProperty);
                    return sourceModel.IntProperty;
                }, EmptyContext);
            valueAccessor.GetValue(memberMock, EmptyContext, true).ShouldEqual(sourceModel.IntProperty);
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            sourceModel.IntProperty = int.MaxValue;
            valueAccessor.GetValue(memberMock, EmptyContext, true).ShouldEqual(sourceModel.IntProperty);
            isInvoked.ShouldBeTrue();
        }
        public void CreateNotifiableMemberGenericMemberAttachTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, (info, o) => null, (info, o, v) => true,
                (model, args) =>
                {
                    model.ShouldEqual(source);
                    args.ShouldNotBeNull();
                    isInvoked = true;
                });

            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeFalse();

            source = new BindingSourceModel();
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();
        }
        public void CreateNotifiableMemberGenericSetterTest()
        {
            object value = null;
            var source = new BindingSourceModel();
            const string path = "path";
            Type type = typeof(string);
            var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, null, (info, o, v) =>
            {
                info.ShouldNotBeNull();
                o.ShouldEqual(source);
                v.ShouldEqual(path);
                value = v;
                return true;
            });
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(type);
            property.CanRead.ShouldBeFalse();
            property.CanWrite.ShouldBeTrue();
            property.MemberType.ShouldEqual(BindingMemberType.Attached);

            property.SetValue(source, new object[] { path });
            value.ShouldEqual(path);
            ShouldThrow(() => property.GetValue(source, null));
        }
 public void CreateNotifiableMemberGenericGetterTest()
 {
     var args = new object[0];
     const string value = "1";
     var source = new BindingSourceModel();
     const string path = "path";
     Type type = typeof(string);
     var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, (info, o) =>
     {
         info.ShouldNotBeNull();
         o.ShouldEqual(source);
         return value;
     }, (info, o, v) => true);
     property.Path.ShouldEqual(path);
     property.Type.ShouldEqual(type);
     property.CanRead.ShouldBeTrue();
     property.CanWrite.ShouldBeTrue();
     property.MemberType.ShouldEqual(BindingMemberType.Attached);
     property.GetValue(source, args).ShouldEqual(value);
 }
        public void CreateNotifiableMemberGenericTest()
        {
            string value = null;
            var source = new BindingSourceModel();
            const string path = "path";
            Type type = typeof(string);
            var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, (info, o) => value,
                (info, o, v) =>
                {
                    value = v;
                    return true;
                }, member: BindingSourceModel.IntPropertyInfo);
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(type);
            property.CanRead.ShouldBeTrue();
            property.CanWrite.ShouldBeTrue();
            property.MemberType.ShouldEqual(BindingMemberType.Attached);
            property.Member.ShouldEqual(BindingSourceModel.IntPropertyInfo);

            property.GetValue(source, null).ShouldBeNull();
            property.SetValue(source, new object[] { path });
            property.GetValue(source, null).ShouldEqual(path);
            value.ShouldEqual(path);
        }
        public void CreateEventGenericSetterTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            IEventListener listener = new EventListenerMock();
            const string path = "path";
            var property = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) =>
            {
                info.ShouldNotBeNull();
                o.ShouldEqual(source);
                arg3.ShouldEqual(listener);
                isInvoked = true;
                return null;
            });
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(typeof(Delegate));
            property.CanRead.ShouldBeTrue();
            property.CanWrite.ShouldBeTrue();
            property.MemberType.ShouldEqual(BindingMemberType.Event);

            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();
        }
        public void CreateMemberMemberAttachTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateMember(path, typeof(string), (info, o) => null, (info, o, v) => { }, memberAttachedHandler:
                (model, args) =>
                {
                    model.ShouldEqual(source);
                    args.ShouldNotBeNull();
                    isInvoked = true;
                });

            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeFalse();

            source = new BindingSourceModel();
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();
        }
        public void CreateMemberGetterTest()
        {
            var args = new object[0];
            const string value = "1";
            var source = new BindingSourceModel();
            const string path = "path";
            Type type = typeof(string);
            var property = AttachedBindingMember.CreateMember(path, type, (info, o, arg3) =>
            {
                info.ShouldNotBeNull();
                o.ShouldEqual(source);
                arg3.ShouldEqual(args);
                return value;
            });
            property.Path.ShouldEqual(path);
            property.Type.ShouldEqual(type);
            property.CanRead.ShouldBeTrue();
            property.CanWrite.ShouldBeFalse();
            property.MemberType.ShouldEqual(BindingMemberType.Attached);

            property.GetValue(source, args).ShouldEqual(value);
            ShouldThrow(() => property.SetValue(source, new object[] { path }));
        }
        public void CreateNotifiableMemberGenericObserveTest()
        {
            bool isInvoked = false;
            bool raiseEvent = true;
            var listenerMock = new EventListenerMock
            {
                Handle = (o, o1) => isInvoked = true
            };
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateNotifiableMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => raiseEvent);

            IDisposable subscriber = property.TryObserve(source, listenerMock);
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeTrue();
            subscriber.ShouldNotBeNull();

            raiseEvent = false;
            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();

            raiseEvent = true;
            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeTrue();

            subscriber.Dispose();
            isInvoked = false;
            property.SetValue(source, new object[] { path });
            isInvoked.ShouldBeFalse();
        }
        public void CreateEventGenericTest()
        {
            IEventListener listener = new EventListenerMock();
            var source = new BindingSourceModel();
            const string path = "path";
            var @event = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) => null);
            @event.Path.ShouldEqual(path);
            @event.Type.ShouldEqual(typeof(Delegate));
            @event.CanRead.ShouldBeTrue();
            @event.CanWrite.ShouldBeTrue();
            @event.Member.ShouldBeNull();
            @event.MemberType.ShouldEqual(BindingMemberType.Event);

            @event.GetValue(source, null).ShouldBeType<BindingActionValue>();
            @event.SetValue(source, new object[] { listener });
            var value = (BindingActionValue)@event.GetValue(source, null);
            value.Member.ShouldEqual(@event);
            value.MemberSource.Target.ShouldEqual(source);
        }
        public void CreateMemberGenericObserveEventTest()
        {
            bool isInvoked = false;
            var listenerMock = new EventListenerMock
            {
                Handle = (o, o1) => isInvoked = true
            };
            var source = new BindingSourceModel();
            const string path = "path";
            source.RaiseEvent();
            var property = AttachedBindingMember.CreateMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => { }, BindingSourceModel.EventName);

            IDisposable subscriber = property.TryObserve(source, listenerMock);
            source.RaiseEvent();
            isInvoked.ShouldBeTrue();
            subscriber.ShouldNotBeNull();

            isInvoked = false;
            source.RaiseEvent();
            isInvoked.ShouldBeTrue();

            subscriber.Dispose();
            isInvoked = false;
            source.RaiseEvent();
            isInvoked.ShouldBeFalse();
        }
        public void CreateEventGenericMemberAttachTest()
        {
            bool isInvoked = false;
            var source = new BindingSourceModel();
            IEventListener listener = new EventListenerMock();
            const string path = "path";
            var property = AttachedBindingMember.CreateEvent<BindingSourceModel>(path, (info, o, arg3) => null,
                (model, args) =>
                {
                    model.ShouldEqual(source);
                    args.ShouldNotBeNull();
                    isInvoked = true;
                });

            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            property.SetValue(source, new object[] { null });
            isInvoked.ShouldBeFalse();

            source = new BindingSourceModel();
            property.SetValue(source, new object[] { listener });
            isInvoked.ShouldBeTrue();
        }
        public void CreateMemberGenericObserveTest()
        {
            bool isInvoked = false;
            IDisposable result = null;
            var listenerMock = new EventListenerMock();
            var source = new BindingSourceModel();
            const string path = "path";
            var property = AttachedBindingMember.CreateMember<BindingSourceModel, string>(path, (info, o) => null,
                (info, o, v) => { },
                (info, o, arg3) =>
                {
                    isInvoked = true;
                    info.ShouldNotBeNull();
                    o.ShouldEqual(source);
                    arg3.ShouldEqual(listenerMock);
                    return result;
                });

            property.TryObserve(source, listenerMock).ShouldBeNull();
            isInvoked.ShouldBeTrue();

            isInvoked = false;
            result = new ActionToken(() => { });
            property.TryObserve(source, listenerMock).ShouldEqual(result);
            isInvoked.ShouldBeTrue();
        }