public void Test_MemoryLeak_WeakPropertyBinding_Getter_Setter()
        {
            var viewModel = new TestViewModel();
            var viewModel2 = new TestViewModel2();

            var viewModel1 = new TestViewModel();
            var viewModel21 = new TestViewModel2();

            var reference = new WeakReference(viewModel1);
            var reference2 = new WeakReference(viewModel21);

            WeakBinding binding = new WeakPropertyBinding(viewModel2, null, viewModel, null)
                .Initialize<WeakPropertyBinding>()
                .SetMode<WeakPropertyBinding>(BindMode.TwoWay)
                .SetTargetPropertyGetter(arg => viewModel1.Name)
                .SetSourcePropertyGetter(arg => viewModel21.Name)
                .SetTargetPropertySetter((data, value) => viewModel1.Name = value.ToStringWithoutException())
                .SetSourcePropertySetter((data, value) => viewModel21.Name = value.ToStringWithoutException())
                .AttachTargetEvent("TestViewModelEvent")
                .AttachSourceEvent("TestViewModelEvent");

            viewModel21.Name = Name1;
            viewModel.RaiseTestViewModelEvent();
            Assert.AreEqual(viewModel1.Name, Name1);

            viewModel1.Name = Name2;
            viewModel2.RaiseTestViewModelEvent();
            Assert.AreEqual(viewModel21.Name, Name2);

            viewModel1 = null;
            GC.Collect();
            Assert.IsFalse(reference.IsAlive);

            viewModel21 = null;
            GC.Collect();
            Assert.IsFalse(reference2.IsAlive);

            GC.KeepAlive(binding);
        }
        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_MemoryLeak_WeakEvent()
        {
            // 1. Normal Event will hold the strong reference which prevent the GC collect.
            var view = new TestView();
            var viewModel = new TestViewModel();
            var reference = new WeakReference(view);

            viewModel.TestViewModelEvent += view.OnEventOccured;
            viewModel.RaiseTestViewModelEvent();
            Assert.AreEqual(1, view.EventCount);
            Assert.AreEqual(1, viewModel.GetTestViewModelEventInvocationCount());

            view = null;
            GC.Collect();
            Assert.IsTrue(reference.IsAlive); // Still live

            viewModel.TestViewModelEvent -= reference.GetTarget<TestView>().OnEventOccured;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            Assert.IsFalse(reference.IsAlive);

            reference = null;
            Assert.IsNull(reference.GetTarget<TestView>());

            // 2. WeakEvent hold the weak reference which will not prevent GC collect.
            var view2 = new TestView();
            var viewModel2 = new TestViewModel();
            var reference2 = new WeakReference(view2);

            var weakEvent = new WeakEvent(view2);
            weakEvent.AttachEvent(viewModel2, null, "TestViewModelEvent", "OnEventOccured");
            viewModel2.RaiseTestViewModelEvent();

            Assert.AreEqual(1, view2.EventCount);

            view2 = null;
            GC.Collect();
            Assert.IsFalse(reference2.IsAlive);

            viewModel2.RaiseTestViewModelEvent();
            Assert.AreEqual(0, viewModel2.GetTestViewModelEventInvocationCount());
        }