[C] Make sure all children are parented (#831)
authorStephane Delcroix <stephane@delcroix.org>
Wed, 22 Mar 2017 16:09:41 +0000 (17:09 +0100)
committerRui Marinho <me@ruimarinho.net>
Wed, 22 Mar 2017 16:19:21 +0000 (16:19 +0000)
* unit test

* [C] make sure all children are properly parented

Xamarin.Forms.Core/Layout.cs
Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381.xaml.cs [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/Issues/Bz53381App.xaml.cs [new file with mode: 0644]
Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj

index 87a3453..f5b71f3 100644 (file)
@@ -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 (file)
index 0000000..a278159
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
+       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+       x:Class="Xamarin.Forms.Xaml.UnitTests.Bz53381">
+       <Grid BackgroundColor="Green">
+               <Label Text="This is my custom control."/>
+       </Grid>
+</ContentView>
\ 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 (file)
index 0000000..95d150f
--- /dev/null
@@ -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 (file)
index 0000000..d25f6b4
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Application xmlns="http://xamarin.com/schemas/2014/forms"
+       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+       xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+       x:Class="Xamarin.Forms.Xaml.UnitTests.Bz53381App">
+       <Application.Resources>
+               <ResourceDictionary>
+                       <Style TargetType="local:Bz53381">
+                               <Setter Property="ControlTemplate">
+                               <Setter.Value>
+                                       <ControlTemplate>
+                                               <StackLayout>
+                                                       <Label Text="This is the control template. A green Grid and Label should be below." />
+                                                       <ContentPresenter  />
+                                               </StackLayout>
+                                       </ControlTemplate>
+                               </Setter.Value>
+                       </Setter>
+                       </Style>
+               </ResourceDictionary>
+       </Application.Resources>
+</Application>
\ 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 (file)
index 0000000..b2dbcbb
--- /dev/null
@@ -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();
+               }
+       }
+}
index 5a02406..45f5b96 100644 (file)
     <Compile Include="Issues\Unreported009.xaml.cs">
       <DependentUpon>Unreported009.xaml</DependentUpon>
     </Compile>
+    <Compile Include="Issues\Bz53318.xaml.cs">
+      <DependentUpon>Bz53318.xaml</DependentUpon>
+    </Compile>
+    <Compile Include="Issues\Bz53381.xaml.cs">
+      <DependentUpon>Bz53381.xaml</DependentUpon>
+    </Compile>
+    <Compile Include="Issues\Bz53381App.xaml.cs">
+      <DependentUpon>Bz53381App.xaml</DependentUpon>
+    </Compile>
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
     <EmbeddedResource Include="Issues\Unreported009.xaml">
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
+    <EmbeddedResource Include="Issues\Bz53318.xaml">
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+    </EmbeddedResource>
+    <EmbeddedResource Include="Issues\Bz53381.xaml">
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+    </EmbeddedResource>
+    <EmbeddedResource Include="Issues\Bz53381App.xaml">
+      <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+    </EmbeddedResource>
   </ItemGroup>
   <ItemGroup>
     <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />