From b545d8194a3fc761b4f1c55dabe90c98ac60dff9 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Wed, 22 Mar 2017 17:09:41 +0100 Subject: [PATCH] [C] Make sure all children are parented (#831) * unit test * [C] make sure all children are properly parented --- Xamarin.Forms.Core/Layout.cs | 4 ++ Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml | 8 ++++ .../Issues/Bz53381.xaml.cs | 52 ++++++++++++++++++++++ .../Issues/Bz53381App.xaml | 22 +++++++++ .../Issues/Bz53381App.xaml.cs | 15 +++++++ .../Xamarin.Forms.Xaml.UnitTests.csproj | 18 ++++++++ 6 files changed, 119 insertions(+) create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml.cs create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml.cs diff --git a/Xamarin.Forms.Core/Layout.cs b/Xamarin.Forms.Core/Layout.cs index 87a3453..f5b71f3 100644 --- a/Xamarin.Forms.Core/Layout.cs +++ b/Xamarin.Forms.Core/Layout.cs @@ -70,6 +70,10 @@ namespace Xamarin.Forms protected Layout() { + //if things were added in base ctor (through implicit styles), the items added aren't properly parented + if (InternalChildren.Count > 0) + InternalChildrenOnCollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, InternalChildren)); + InternalChildren.CollectionChanged += InternalChildrenOnCollectionChanged; } diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml new file mode 100644 index 0000000..a278159 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml.cs new file mode 100644 index 0000000..95d150f --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Bz53381 : ContentView + { + public Bz53381() + { + InitializeComponent(); + } + + public Bz53381(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Application.Current = null; + Device.PlatformServices = null; + } + + [TestCase(true)] + [TestCase(false)] + public void ControlTemplateAsImplicitAppLevelStyles(bool useCompiledXaml) + { + Application.Current = new Bz53381App(); + var view = new Bz53381(useCompiledXaml); + Application.Current.MainPage = new ContentPage { Content = view }; + var presenter = ((StackLayout)view.InternalChildren[0]).Children[1] as ContentPresenter; + Assume.That(presenter, Is.Not.Null); + var grid = presenter.Content as Grid; + Assert.That(grid, Is.Not.Null); + Assert.That(grid.BackgroundColor, Is.EqualTo(Color.Green)); + } + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml new file mode 100644 index 0000000..d25f6b4 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml @@ -0,0 +1,22 @@ + + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml.cs new file mode 100644 index 0000000..b2dbcbb --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +using Xamarin.Forms; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class Bz53381App : Application + { + public Bz53381App() + { + InitializeComponent(); + } + } +} diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj index 5a02406..45f5b96 100644 --- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj +++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj @@ -418,6 +418,15 @@ Unreported009.xaml + + Bz53318.xaml + + + Bz53381.xaml + + + Bz53381App.xaml + @@ -757,6 +766,15 @@ MSBuild:UpdateDesignTimeXaml + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + + + MSBuild:UpdateDesignTimeXaml + -- 2.7.4