public void Test_WeakNotify_OneWay()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            int changedCount = 0;

            BindingValueChangedHandler changeHandler = (source, args) =>
                {
                    changedCount++;
                    Assert.IsTrue(args.Data != null);
                    Assert.IsNull(args.OldValue);
                    Assert.IsNull(args.NewValue);
                };

            new WeakNotifyBinding(viewModel, "Age", view, null)
                .Initialize<WeakNotifyBinding>()
                .AttachSourceEvent("TestViewEvent")
                .OfType<WeakNotifyBinding>().SetSourceChanged(changeHandler);

            Assert.AreEqual(0, changedCount);

            GC.Collect();

            view.RaiseTestViewEvent();

            Assert.AreEqual(1, changedCount);
        }
        public void Test_WeakCommandBinding_CanExecuteChangedCallback()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            Assert.IsFalse(view.Enable);
            bool isEnable = false;

            WeakCommandBinding commandBinding = new WeakCommandBinding(view, null, viewModel, "AddAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakCommandBinding>()
                .AddEnableProperty("Enable")
                .Watch("Age")
                .SetCanExecuteChanged((sender, args) => { isEnable = args.CanExecute; });

            Assert.IsTrue(view.Enable);
            Assert.IsTrue(isEnable);

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, viewModel.Age);

            Assert.IsTrue(view.Enable);
            Assert.IsTrue(isEnable);

            commandBinding.SetCanExecuteChanged(null);

            viewModel.Age++;
            Assert.IsFalse(view.Enable);
            Assert.IsTrue(isEnable);
        }
        public void Test_WeakSource_SetBinding()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var ws = new WeakTarget(view);

            ws.SetBinding<WeakPropertyBinding>("Text1", viewModel, "Name");

            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            NotSupportedException exception = null;

            try
            {
                ws.SetBinding<WeakNotifyBinding>("Text1", viewModel, "Name");
            }
            catch(NotSupportedException e)
            {
                exception = e;
            }

            Assert.IsNotNull(exception);
        }
        public void Test_WeakBindEvent_NormalProperty()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var propertyBinding = new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>();

            var weakEvent = new WeakEvent(viewModel);

            Assert.IsFalse(weakEvent.IsAttached);
        }
        public void Test_WeakCommandBinding_Basic()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakCommandBinding(view, null, viewModel, "AddAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent");

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();

            Assert.AreEqual(1, viewModel.Age);
        }
        public void Test_WeakBinding_AttachSourceEvent()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .AttachSourceEvent("TestViewModelEvent");
            viewModel.Name = Name1;

            Assert.AreNotEqual(view.Text1, Name1);

            viewModel.RaiseTestViewModelEvent();
            Assert.AreEqual(view.Text1, Name1);
        }
        public void Test_WeakSource_ClearBinding()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var ws = new WeakTarget(view);

            ws.SetBinding<WeakPropertyBinding>("Text1", viewModel, "Name");

            viewModel.Name = Name1;
            Assert.AreEqual(view.Text1, Name1);

            ws.ClearBinding("Text1", viewModel, "Name");

            viewModel.Name = Name2;
            Assert.AreNotEqual(view.Text2, Name2);
        }
        public void Test_WeakCollectionBinding_CollecionHandler()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakCollectionBinding(view, "CollectionNames", viewModel, "TestViewModelCollection")
                .Initialize<WeakCollectionBinding>()
                .SetTargetCollectionHandler(new TestDataCollectionHandler());

            view.CollectionNames = new List<string>();
            var collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            collections.Add(new TestViewModel2 { Name = Name1 });
            Assert.AreEqual(1, view.CollectionNames.Count);
            Assert.AreEqual(Name1, view.CollectionNames[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(0, view.CollectionNames.Count);

            collections.Add(new TestViewModel2 { Name = Name2 });
            collections.Clear();
            Assert.AreEqual(0, view.CollectionNames.Count);

            viewModel = new TestViewModel();
            view = new TestView();

            new WeakCollectionBinding(viewModel, "TestViewModelCollection", view, "CollectionNames")
                .Initialize<WeakCollectionBinding>()
                .SetMode<WeakCollectionBinding>(BindMode.OneWayToSource)
                .SetSourceCollectionHandler(new TestDataCollectionHandler());

            view.CollectionNames = new List<string>();
            collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            collections.Add(new TestViewModel2 { Name = Name1 });
            Assert.AreEqual(1, view.CollectionNames.Count);
            Assert.AreEqual(Name1, view.CollectionNames[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(0, view.CollectionNames.Count);

            collections.Add(new TestViewModel2 { Name = Name2 });
            collections.Clear();
            Assert.AreEqual(0, view.CollectionNames.Count);
        }
        public void Test_WeakNotify_OneWayToSource()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            int changedCount = 0;

            new WeakNotifyBinding(view, null, viewModel, "Age")
                .Initialize<WeakNotifyBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakNotifyBinding>()
                .SetTargetChanged<WeakNotifyBinding>((source, args) => { changedCount++; });

            Assert.AreEqual(0, changedCount);

            view.RaiseTestViewEvent();

            Assert.AreEqual(1, changedCount);
        }
示例#10
0
        public void Test_WeakSource_ClearAllBindings()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var ws = new WeakTarget(view);

            ws.SetBinding<WeakPropertyBinding>( "Text1", viewModel,"Name");
            ws.SetBinding<WeakPropertyBinding>("ValueInt", viewModel, "Age");

            viewModel.Name = Name1;
            Assert.AreEqual(view.Text1, Name1);
            viewModel.Age = 2;
            Assert.AreEqual(view.ValueInt, 2);

            ws.ClearBindings();
            viewModel.Name = Name2;
            viewModel.Age = 3;

            Assert.AreNotEqual(view.Text1, Name2);
            Assert.AreNotEqual(view.ValueInt, Name2);
        }
        public void Test_WeakNotify_TwoWay()
        {
            var button = new Button();
            var view = new TestView();

            int changedCount = 0;
            int targetCount = 0;

            new WeakNotifyBinding(view, null, button, null)
                .Initialize<WeakNotifyBinding>()
                .AttachTargetEvent("TestViewEvent")
                .AttachSourceEvent("Click")
                .OfType<WeakNotifyBinding>()
                .SetTargetChanged((source, args) => { changedCount++; })
                .SetSourceChanged((source, args) => { targetCount++; });

            Assert.AreEqual(0, changedCount);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, changedCount);

            button.PerformClick();
            Assert.AreEqual(1, targetCount);
        }
示例#12
0
        public void Test_WeakBinding_GetBindSource()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            WeakBinding binding = new WeakPropertyBinding(viewModel, "Name", view, "Text1")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.TwoWay)
                .AttachSourceEvent(typeof(TestView), "StaticTestViewEvent");
            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            TestView.RaiseStaticTestViewEvent();
            Assert.AreEqual(viewModel.Name, Name2);

            BindContext context = binding.GetBindContext(viewModel, "Name");
            Assert.AreEqual(binding.BindTarget, context);

            BindContext source2 = binding.GetBindContext(view, "Text1");
            Assert.AreEqual(binding.BindSource, source2);
        }
示例#13
0
        public void Test_WeakBinding_MemoryLeak()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            int changed = 0;

            WeakPropertyBinding binding = new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.OneWay)
                .AttachTargetEvent("TestViewEvent")
                .SetTargetChanged<WeakPropertyBinding>((sender, args) => { changed++; });

            viewModel.Name = Name1;
            Assert.AreEqual(Name1, view.Text1);
            Assert.AreEqual(1, changed);

            viewModel.Name = Name2;
            Assert.AreEqual(Name2, view.Text1);
            Assert.AreEqual(2, changed);

            view = null;
            GC.Collect();
            viewModel.Name = Name1;
            Assert.AreEqual(2, changed);
        }
示例#14
0
        public void Test_WeakBinding_AttachTargetEvent_3()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent(typeof(TestView), "StaticTestViewEvent");
            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            TestView.RaiseStaticTestViewEvent();
            Assert.AreEqual(viewModel.Name, Name2);
        }
示例#15
0
        public void Test_WeakBinding_DeActive()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            WeakBinding binding = new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.OneWay)
                .AttachTargetEvent("TestViewEvent");
            viewModel.Name = Name1;

            binding.DeActivate();
            viewModel.Name = Name2;

            Assert.AreNotEqual(view.Text1, Name2);

            binding.Activate();
            Assert.AreEqual(view.Text1, Name2);
        }
        public void Test_WeakCommandBinding_RemoveEnableProperty_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            Assert.IsFalse(view.Enable);

            WeakCommandBinding weakCommandBinding = new WeakCommandBinding(view, null, viewModel, "AddAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakCommandBinding>()
                .AddEnableProperty<TestView>(i => i.Enable);

            Assert.IsTrue(view.Enable);

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, viewModel.Age);

            Assert.IsTrue(view.Enable);

            weakCommandBinding.RemoveEnableProperty<TestView>(i => i.Enable);

            view.RaiseTestViewEvent();
            Assert.IsTrue(view.Enable);
        }
示例#17
0
        public void Test_WeakBinding_AttachTargetEvent_2()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var button = new Button();

            new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent(button, null, "Click");
            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            button.PerformClick();
            Assert.AreEqual(viewModel.Name, Name2);
        }
        public void Test_BindingEngine_GenericBinding()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            BindingEngine.SetBinding<WeakPropertyBinding>(view, "Text1", viewModel, "Name")
                         .SetMode(BindMode.TwoWay)
                         .AttachTargetEvent("TestViewEvent");

            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            view.RaiseTestViewEvent();
            Assert.AreEqual(viewModel.Name, Name2);
        }
        public void Test_BindingEngine_PropertyBinding()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            var binding = BindingEngine.SetPropertyBinding(view, "Text1", viewModel, "Name")
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent("TestViewEvent");

            Assert.AreEqual("Name", binding.SourceProperty);
            Assert.AreEqual("Text1", binding.TargetProperty);

            viewModel.Name = Name1;

            Assert.AreEqual(Name1, view.Text1);

            view.Text1 = Name2;
            Assert.AreNotEqual(Name2, viewModel.Name);

            view.RaiseTestViewEvent();
            Assert.AreEqual(Name2, viewModel.Name);
        }
示例#20
0
        public void Test_WeakBinding_AttachSourceEvent_2()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            var button = new Button();

            WeakBinding binding = new WeakPropertyBinding(viewModel, "Name", view, "Text1")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.OneWay)
                .AttachSourceEvent(button, "Click");

            view.Text1 = Name1;
            Assert.AreNotEqual(viewModel.Name, Name1);

            button.PerformClick();
            Assert.AreEqual(viewModel.Name, Name1);
            binding.Clear();

            viewModel = new TestViewModel();
            view = new TestView();
            button = new Button();

            new WeakPropertyBinding(viewModel, "Name", view, "Text1")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.OneWay)
                .AttachSourceEvent(button, null, "Click");

            view.Text1 = Name1;
            Assert.AreNotEqual(viewModel.Name, Name1);

            button.PerformClick();
            Assert.AreEqual(viewModel.Name, Name1);
        }
        public void Test_WeakCollectionBinding_OneWayToTarget()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakCollectionBinding(viewModel, "TestViewModelCollection", view, "TestViewModelCollections")
                .Initialize<WeakCollectionBinding>()
                .SetMode(BindMode.OneWayToSource);

            view.TestViewModelCollections = new List<TestViewModel2>();
            var collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            var viewModel2 = new TestViewModel2();
            collections.Add(viewModel2);
            collections.Add(new TestViewModel2());

            Assert.AreEqual(2, view.TestViewModelCollections.Count);
            Assert.AreEqual(viewModel2, view.TestViewModelCollections[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(1, view.TestViewModelCollections.Count);

            collections.Clear();
            Assert.AreEqual(0, view.TestViewModelCollections.Count);
        }
        public void Test_WeakCommandBinding_Parameter_2()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            new WeakCommandBinding(view, null, viewModel, "SetAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakCommandBinding>()
                .SetParameter(2, null);

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();

            Assert.AreEqual(2, viewModel.Age);
        }
        public void Test_WeakCommandBinding_UnWatch_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            Assert.IsFalse(view.Enable);

            WeakCommandBinding commandBinding = new WeakCommandBinding(view, null, viewModel, "AddAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakCommandBinding>()
                .AddEnableProperty("Enable")
                .Watch<TestViewModel>(i => i.Age);

            Assert.IsTrue(view.Enable);

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, viewModel.Age);

            Assert.IsTrue(view.Enable);

            viewModel.Age++;
            Assert.IsFalse(view.Enable);

            viewModel.Age--;
            Assert.IsTrue(view.Enable);

            commandBinding.UnWatch<TestViewModel>(null);
            commandBinding.UnWatch(null);

            Exception exception = null;
            try
            {
                commandBinding.UnWatch(null, null);
            }
            catch (ArgumentNullException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            commandBinding.UnWatch<TestViewModel>(o => o.Age);

            viewModel.Age++;
            Assert.IsTrue(view.Enable);
        }
        public void Test_BindingEngine_PropertyBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            BindingEngine.SetPropertyBinding(view, o => o.Text1, viewModel, i => i.Name)
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent("TestViewEvent");

            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            view.RaiseTestViewEvent();
            Assert.AreEqual(viewModel.Name, Name2);
        }
示例#25
0
        public void Test_WeakBinding_Update()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            WeakBinding binding = new WeakPropertyBinding(view, "Text1", viewModel, "Name")
                .Initialize<WeakPropertyBinding>()
                .SetMode(BindMode.TwoWay)
                .AttachTargetEvent("TestViewEvent");
            viewModel.Name = Name1;

            Assert.AreEqual(view.Text1, Name1);

            view.Text1 = Name2;
            Assert.AreNotEqual(viewModel.Name, Name2);

            view.RaiseTestViewEvent();
            Assert.AreEqual(viewModel.Name, Name2);

            binding.Update("Text2", viewModel, "Name");

            viewModel.Name = Name1;

            Assert.AreNotEqual(view.Text1, Name1);
            Assert.AreEqual(view.Text2, Name1);
        }
        public void Test_BindingEngine_CollectionBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            BindingEngine.SetCollectionBinding(view, o => o.TestViewModelCollections, viewModel,
                i => i.TestViewModelCollection)
                .Initialize<WeakCollectionBinding>();

            view.TestViewModelCollections = new List<TestViewModel2>();
            var collections = new ObservableCollection<TestViewModel2>();
            viewModel.TestViewModelCollection = collections;

            var viewModel2 = new TestViewModel2();
            collections.Add(viewModel2);
            collections.Add(new TestViewModel2());

            Assert.AreEqual(2, view.TestViewModelCollections.Count);
            Assert.AreEqual(viewModel2, view.TestViewModelCollections[0]);

            collections.RemoveAt(0);
            Assert.AreEqual(1, view.TestViewModelCollections.Count);

            collections.Clear();
            Assert.AreEqual(0, view.TestViewModelCollections.Count);
        }
        public void Test_BindingEngine_CommandBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            BindingEngine.SetCommandBinding(view, null, viewModel, i => i.AddAgeCommand)
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent");

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();

            Assert.AreEqual(1, viewModel.Age);
        }
        public void Test_BindingEngine_NotifyBinding_Expression()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();
            int changedCount = 0;

            BindingEngine.SetNotifyBinding(viewModel, "Age", view, null)
                         .Initialize<WeakNotifyBinding>()
                         .AttachSourceEvent("TestViewEvent")
                         .OfType<WeakNotifyBinding>().SetSourceChanged((source, args) => { changedCount++; });

            Assert.AreEqual(0, changedCount);

            view.RaiseTestViewEvent();

            Assert.AreEqual(1, changedCount);
        }
        public void Test_WeakNotify_WeakReference()
        {
            var button = new Button();
            var view = new TestView();

            int changedCount = 0;
            int targetCount = 0;

            BindingValueChangedHandler sourceChanged = (source, args) => { changedCount++; };
            BindingValueChangedHandler targetChanged = (source, args) => { targetCount++; };

            new WeakNotifyBinding(view, null, button, null)
                .Initialize<WeakNotifyBinding>()
                .AttachTargetEvent("TestViewEvent")
                .AttachSourceEvent("Click")
                .OfType<WeakNotifyBinding>()
                .SetTargetChanged(sourceChanged, true)
                .SetSourceChanged(targetChanged, true);

            Assert.AreEqual(0, changedCount);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, changedCount);

            button.PerformClick();
            Assert.AreEqual(1, targetCount);

            sourceChanged = null;
            GC.Collect();
            view.RaiseTestViewEvent();
            Assert.AreEqual(1, changedCount);

            targetChanged = null;
            GC.Collect();
            button.PerformClick();
            Assert.AreEqual(1, targetCount);
        }
        public void Test_WeakCommandBinding_EnableProperty()
        {
            var viewModel = new TestViewModel();
            var view = new TestView();

            Assert.IsFalse(view.Enable);

            new WeakCommandBinding(view, null, viewModel, "AddAgeCommand")
                .Initialize<WeakCommandBinding>()
                .AttachTargetEvent("TestViewEvent")
                .OfType<WeakCommandBinding>()
                .AddEnableProperty("Enable");

            Assert.IsTrue(view.Enable);

            Assert.AreEqual(0, viewModel.Age);

            view.RaiseTestViewEvent();
            Assert.AreEqual(1, viewModel.Age);

            Assert.IsTrue(view.Enable);

            view.RaiseTestViewEvent();
            Assert.AreEqual(2, viewModel.Age);

            Assert.IsFalse(view.Enable);

            view.RaiseTestViewEvent();

            Assert.IsFalse(view.Enable);
            Assert.AreEqual(2, viewModel.Age);
        }