public void Re_adding_To_Different_LogicalTree_Should_Recreate_Template()
        {
            using (UnitTestApplication.Start(TestServices.RealStyler))
            {
                TestTemplatedControl target;

                var root = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType <TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                {
                                    Child = new Border(),
                                }))
                            }
                        }
                    },
                    Child = target = new TestTemplatedControl()
                };

                var root2 = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType <TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                {
                                    Child = new Border(),
                                }))
                            }
                        }
                    },
                };

                Assert.NotNull(target.Template);
                target.ApplyTemplate();

                var expected = (Decorator)target.GetVisualChildren().Single();

                root.Child  = null;
                root2.Child = target;
                target.ApplyTemplate();

                var child = target.GetVisualChildren().Single();
                Assert.NotNull(target.Template);
                Assert.NotNull(child);
                Assert.NotSame(expected, child);
            }
        }
        public void Applying_New_Template_Clears_TemplatedParent_Of_Old_Template_Children()
        {
            var target = new TestTemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator
                {
                    Child = new Border(),
                })
            };

            target.ApplyTemplate();

            var decorator = (Decorator)target.GetVisualChildren().Single();
            var border    = (Border)decorator.Child;

            Assert.Equal(target, decorator.TemplatedParent);
            Assert.Equal(target, border.TemplatedParent);

            target.Template = new FuncControlTemplate(_ => new Canvas());

            // Templated children should not be removed here: the control may be re-added
            // somewhere with the same template, so they could still be of use.
            Assert.Same(decorator, target.GetVisualChildren().Single());
            Assert.Equal(target, decorator.TemplatedParent);
            Assert.Equal(target, border.TemplatedParent);

            target.ApplyTemplate();

            Assert.Null(decorator.TemplatedParent);
            Assert.Null(border.TemplatedParent);
        }
        public void Applying_New_Template_Clears_TemplatedParent_Of_Old_Template_Children()
        {
            var target = new TestTemplatedControl
            {
                Template = new FuncControlTemplate(_ => new Decorator
                {
                    Child = new Border(),
                })
            };

            target.ApplyTemplate();

            var decorator = (Decorator)target.GetVisualChildren().Single();
            var border = (Border)decorator.Child;

            Assert.Equal(target, decorator.TemplatedParent);
            Assert.Equal(target, border.TemplatedParent);

            target.Template = new FuncControlTemplate(_ => new Canvas());

            // Templated children should not be removed here: the control may be re-added
            // somewhere with the same template, so they could still be of use.
            Assert.Same(decorator, target.GetVisualChildren().Single());
            Assert.Equal(target, decorator.TemplatedParent);
            Assert.Equal(target, border.TemplatedParent);

            target.ApplyTemplate();

            Assert.Null(decorator.TemplatedParent);
            Assert.Null(border.TemplatedParent);
        }
        public void Removing_From_LogicalTree_Should_Not_Remove_Child()
        {
            using (UnitTestApplication.Start(TestServices.RealStyler))
            {
                Border templateChild = new Border();
                TestTemplatedControl target;
                var root = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType <TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                {
                                    Child = new Border(),
                                }))
                            }
                        }
                    },
                    Child = target = new TestTemplatedControl()
                };

                Assert.NotNull(target.Template);
                target.ApplyTemplate();

                root.Child = null;

                Assert.Null(target.Template);
                Assert.IsType <Decorator>(target.GetVisualChildren().Single());
            }
        }
        public void Moving_To_New_LogicalTree_Should_Detach_Attach_Template_Child()
        {
            using (UnitTestApplication.Start(TestServices.RealStyler))
            {
                TestTemplatedControl target;
                var root = new TestRoot
                {
                    Child = target = new TestTemplatedControl
                    {
                        Template = new FuncControlTemplate(_ => new Decorator()),
                    }
                };

                Assert.NotNull(target.Template);
                target.ApplyTemplate();

                var templateChild = (ILogical)target.GetVisualChildren().Single();
                Assert.True(templateChild.IsAttachedToLogicalTree);

                root.Child = null;
                Assert.False(templateChild.IsAttachedToLogicalTree);

                var newRoot = new TestRoot {
                    Child = target
                };
                Assert.True(templateChild.IsAttachedToLogicalTree);
            }
        }
        public void Re_adding_To_Different_LogicalTree_Should_Recreate_Template()
        {
            using (UnitTestApplication.Start(TestServices.RealStyler))
            {
                TestTemplatedControl target;

                var root = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType<TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                    {
                                        Child = new Border(),
                                    }))
                            }
                        }
                    },
                    Child = target = new TestTemplatedControl()
                };

                var root2 = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType<TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                    {
                                        Child = new Border(),
                                    }))
                            }
                        }
                    },
                };

                Assert.NotNull(target.Template);
                target.ApplyTemplate();

                var expected = (Decorator)target.GetVisualChildren().Single();

                root.Child = null;
                root2.Child = target;
                target.ApplyTemplate();

                var child = target.GetVisualChildren().Single();
                Assert.NotNull(target.Template);
                Assert.NotNull(child);
                Assert.NotSame(expected, child);
            }
        }
        public void Removing_From_LogicalTree_Should_Not_Remove_Child()
        {
            using (UnitTestApplication.Start(TestServices.RealStyler))
            {
                Border templateChild = new Border();
                TestTemplatedControl target;
                var root = new TestRoot
                {
                    Styles = new Styles
                    {
                        new Style(x => x.OfType<TestTemplatedControl>())
                        {
                            Setters = new[]
                            {
                                new Setter(
                                    TemplatedControl.TemplateProperty,
                                    new FuncControlTemplate(_ => new Decorator
                                    {
                                        Child = new Border(),
                                    }))
                            }
                        }
                    },
                    Child = target = new TestTemplatedControl()
                };

                Assert.NotNull(target.Template);
                target.ApplyTemplate();

                root.Child = null;

                Assert.Null(target.Template);
                Assert.IsType<Decorator>(target.GetVisualChildren().Single());
            }
        }