Inheritance: IRegionManagerAccessor
        public async Task RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control1 = new MockFrameworkElement();
                    var control2 = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                                       {
                                           GetRegionName = d => d == control1 ? "Region1" : "Region2"
                                       };

                    var adapter = new MockRegionAdapter() { Accessor = accessor };

                    var behavior1 = DelayedRegionCreationBehaviorFixture.GetBehavior(control1, accessor, adapter);
                    var behavior2 = DelayedRegionCreationBehaviorFixture.GetBehavior(control2, accessor, adapter);

                    behavior1.Attach();
                    behavior2.Attach();

                    accessor.UpdateRegions();

                    Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
                    Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
                    Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));
                });
        }
 private static DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor, MockRegionAdapter adapter)
 {
     var mappings = new RegionAdapterMappings();
     mappings.RegisterMapping(control.GetType(), adapter);
     var behavior = new DelayedRegionCreationBehavior(mappings) { RegionManagerAccessor = accessor, TargetElement = control };
     return behavior;
 }
        public void RegionWillNotGetCreatedTwiceWhenThereAreMoreRegions()
        {
            var control1 = new MockFrameworkElement();
            var control2 = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
                               {
                                   GetRegionName = d => d == control1 ? "Region1" : "Region2"
                               };

            var adapter = new MockRegionAdapter();
            adapter.Accessor = accessor;

            var behavior1 = this.GetBehavior(control1, accessor, adapter);
            var behavior2 = this.GetBehavior(control2, accessor, adapter);

            behavior1.Attach();
            behavior2.Attach();

            accessor.UpdateRegions();

            Assert.IsTrue(adapter.CreatedRegions.Contains("Region1"));
            Assert.IsTrue(adapter.CreatedRegions.Contains("Region2"));
            Assert.AreEqual(1, adapter.CreatedRegions.Count((name) => name == "Region2"));

        }
        public void RegionGetsCreatedWhenAccessingRegions()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
                               {
                                   GetRegionName = d => "myRegionName"
                               };

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();

            accessor.UpdateRegions();

            Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
            Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
        }
        public async Task RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                    {
                        GetRegionName = d => "myRegionName"
                    };

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();
                    accessor.UpdateRegions();
                    IRegion region = RegionManager.GetObservableRegion(control).Value;

                    accessor.UpdateRegions();

                    Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
                });
        }
        public async Task RegionGetsCreatedWhenAccessingRegions()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                                       {
                                           GetRegionName = d => "myRegionName"
                                       };

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();

                    accessor.UpdateRegions();

                    Assert.IsNotNull(RegionManager.GetObservableRegion(control).Value);
                    Assert.IsInstanceOfType(RegionManager.GetObservableRegion(control).Value, typeof(IRegion));
                });
        }
 private static DelayedRegionCreationBehavior GetBehavior(DependencyObject control, MockRegionManagerAccessor accessor)
 {
     return GetBehavior(control, accessor, new MockRegionAdapter());
 }
        public async Task ShouldCleanupBehaviorOnceRegionIsCreated()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                    {
                        GetRegionName = d => "myRegionName"
                    };

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    WeakReference behaviorWeakReference = new WeakReference(behavior);
                    behavior.Attach();
                    accessor.UpdateRegions();
                    Assert.IsTrue(behaviorWeakReference.IsAlive);
                    GC.KeepAlive(behavior);

                    behavior = null;
                    GC.Collect();

                    Assert.IsFalse(behaviorWeakReference.IsAlive);
                });
        }
        public async Task BehaviorShouldUnhookEventWhenDetaching()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();

                    var accessor = new MockRegionManagerAccessor
                                       {
                                           GetRegionName = d => "myRegionName",
                                       };
                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();

                    int startingCount = accessor.GetSubscribersCount();

                    behavior.Detach();

                    Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
                });
        }
        public async Task BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new MockFrameworkElement();
                    WeakReference controlWeakReference = new WeakReference(control);

                    var accessor = new MockRegionManagerAccessor
                    {
                        GetRegionName = d => "myRegionName"
                    };

                    var behavior = DelayedRegionCreationBehaviorFixture.GetBehavior(control, accessor);
                    behavior.Attach();
                    accessor.UpdateRegions();

                    Assert.IsTrue(controlWeakReference.IsAlive);
                    GC.KeepAlive(control);

                    control = null;
                    GC.Collect();

                    Assert.IsFalse(controlWeakReference.IsAlive);
                });
        }
        public void RegionDoesNotGetCreatedTwiceWhenUpdatingRegions()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName = d => "myRegionName"
            };

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();
            accessor.UpdateRegions();
            IRegion region = RegionManager.GetObservableRegion(control).Value;

            accessor.UpdateRegions();

            Assert.AreSame(region, RegionManager.GetObservableRegion(control).Value);
        }
        public void ShouldCleanupBehaviorOnceRegionIsCreated()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName = d => "myRegionName"
            };

            var behavior = this.GetBehavior(control, accessor);
            WeakReference behaviorWeakReference = new WeakReference(behavior);
            behavior.Attach();
            accessor.UpdateRegions();
            Assert.IsTrue(behaviorWeakReference.IsAlive);
            GC.KeepAlive(behavior);

            behavior = null;
            GC.Collect();

            Assert.IsFalse(behaviorWeakReference.IsAlive);
        }
        public void BehaviorShouldUnhookEventWhenDetaching()
        {
            var control = new MockFrameworkElement();

            var accessor = new MockRegionManagerAccessor
                               {
                                   GetRegionName = d => "myRegionName",
                               };
            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();

            int startingCount = accessor.GetSubscribersCount();

            behavior.Detach();

            Assert.AreEqual<int>(startingCount - 1, accessor.GetSubscribersCount());
        }
        public void BehaviorDoesNotPreventControlFromBeingGarbageCollectedWhenRegionWasCreated()
        {
            var control = new MockFrameworkElement();
            WeakReference controlWeakReference = new WeakReference(control);

            var accessor = new MockRegionManagerAccessor
            {
                GetRegionName = d => "myRegionName"
            };

            var behavior = this.GetBehavior(control, accessor);
            behavior.Attach();
            accessor.UpdateRegions();
            
            Assert.IsTrue(controlWeakReference.IsAlive);
            GC.KeepAlive(control);

            control = null;
            GC.Collect();

            Assert.IsFalse(controlWeakReference.IsAlive);
        }