public void AdapterAssociatesItemsControlWithRegion()
        {
            var control = new ItemsControl();
            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            IRegion region = adapter.Initialize(control);
            Assert.IsNotNull(region);

            Assert.AreSame(control.ItemsSource, region.Views);
        }
        public void ShouldMoveAlreadyExistingContentInControlToRegion()
        {
            var control = new ItemsControl();
            var view = new object();
            control.Items.Add(view);
            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            var region = (MockRegion)adapter.Initialize(control);

            Assert.AreEqual(1, region.MockViews.Count());
            Assert.AreSame(view, region.MockViews.ElementAt(0));
            Assert.AreSame(view, control.Items[0]);
        }
        public async Task AdapterAssociatesItemsControlWithRegion()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new ItemsControl();
                    IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

                    IRegion region = adapter.Initialize(control, "Region1");
                    Assert.IsNotNull(region);

                    Assert.AreSame(control.ItemsSource, region.Views);
                });
        }
        public async Task ShouldMoveAlreadyExistingContentInControlToRegion()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new ItemsControl();
                    var view = new object();
                    control.Items.Add(view);
                    IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

                    var region = (MockPresentationRegion)adapter.Initialize(control, "Region1");

                    Assert.AreEqual(1, region.MockViews.Count());
                    Assert.AreSame(view, region.MockViews.ElementAt(0));
                    Assert.AreSame(view, control.Items[0]);
                });
        }
        public void ControlWithExistingItemSourceThrows()
        {
            TabControl tabControl = new TabControl() { ItemsSource = new List<string>() };

            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            try
            {
                var region = (MockRegion)adapter.Initialize(tabControl);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
            }
        }
        public void ControlWithExistingItemSourceThrows()
        {
            var control = new ItemsControl()
            {
                ItemsSource = new List <string>()
            };

            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            try
            {
                var region = (MockPresentationRegion)adapter.Initialize(control, "Region1");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
            }
        }
        public void ControlWithExistingBindingOnItemsSourceWithNullValueThrows()
        {
            var control = new ItemsControl();
            Binding binding = new Binding("Enumerable");
            binding.Source = new SimpleModel() { Enumerable = null };
            control.SetBinding(ItemsControl.ItemsSourceProperty, binding);

            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            try
            {
                var region = (MockPresentationRegion)adapter.Initialize(control, "Region1");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
            }
        }
        public void ControlWithExistingBindingOnItemsSourceWithNullValueThrows()
        {
            var     control = new ItemsControl();
            Binding binding = new Binding("Enumerable");

            binding.Source = new SimpleModel()
            {
                Enumerable = null
            };
            control.SetBinding(ItemsControl.ItemsSourceProperty, binding);

            IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

            try
            {
                var region = (MockPresentationRegion)adapter.Initialize(control, "Region1");
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
                StringAssert.Contains(ex.Message, "ItemsControl's ItemsSource property is not empty.");
            }
        }
        public async Task ControlWithExistingItemSourceThrows()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new ItemsControl() { ItemsSource = new List<string>() };

                    IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

                    Assert.ThrowsException<InvalidOperationException>(
                        () => (MockPresentationRegion)adapter.Initialize(control, "Region1"),
                        "ItemsControl's ItemsSource property is not empty.");
                });
        }
        public async Task ControlWithExistingBindingOnItemsSourceWithNullValueThrows()
        {
            await ExecuteOnUIThread(() =>
                {
                    var control = new ItemsControl();
                    Binding binding = new Binding
                    {
                        Path = new PropertyPath("Enumerable"),
                        Source = new SimpleModel() { Enumerable = null }
                    };
                    control.SetBinding(ItemsControl.ItemsSourceProperty, binding);

                    IRegionAdapter adapter = new TestableItemsControlRegionAdapter();

                    Assert.ThrowsException<InvalidOperationException>(
                        () => (MockPresentationRegion)adapter.Initialize(control, "Region1"),
                        "ItemsControl's ItemsSource property is not empty.");
                });
        }