using NUnit.Framework; using Xamarin.Forms.Core.UnitTests; namespace Xamarin.Forms.Xaml.UnitTests { [TestFixture] public class Issue1545 { [SetUp] public void Setup() { Device.PlatformServices = new MockPlatformServices { RuntimePlatform = Device.iOS }; } [TearDown] public void TearDown() { Device.PlatformServices = null; } [Test] public void BindingCanNotBeReused() { string xaml = @" "; ContentPage page = new ContentPage().LoadFromXaml (xaml); var items = new[] { "Fu", "Bar" }; page.BindingContext = items; ListView lv = page.FindByName ("List"); TextCell cell = (TextCell)lv.TemplatedItems.GetOrCreateContent (0, items[0]); Assert.That (cell.Text, Is.EqualTo ("Fu")); cell = (TextCell)lv.TemplatedItems.GetOrCreateContent (1, items[1]); Assert.That (cell.Text, Is.EqualTo ("Bar")); } [Test] public void ElementsCanNotBeReused() { string xaml = @" #ff00aa "; ContentPage page = new ContentPage().LoadFromXaml (xaml); var items = new[] { "Fu", "Bar" }; page.BindingContext = items; ListView lv = page.FindByName ("List"); ViewCell cell0 = (ViewCell)lv.TemplatedItems.GetOrCreateContent (0, items[0]); Assert.That (((Label)((StackLayout)cell0.View).Children[0]).Text, Is.EqualTo ("Fu")); ViewCell cell1 = (ViewCell)lv.TemplatedItems.GetOrCreateContent (1, items[1]); Assert.That (((Label)((StackLayout)cell1.View).Children[0]).Text, Is.EqualTo ("Bar")); Assert.AreNotSame (cell0, cell1); Assert.AreNotSame (cell0.View, cell1.View); Assert.AreNotSame (((StackLayout)cell0.View).Children [0], ((StackLayout)cell1.View).Children [0]); Assert.AreEqual (Color.FromHex ("ff00aa"), ((StackLayout)cell1.View).Children [0].BackgroundColor); } [Test] public void ElementsFromCollectionsAreNotReused () { var xaml = @" "; var listview = new ListView (); var items = new [] { "Foo", "Bar", "Baz" }; listview.BindingContext = items; listview.LoadFromXaml (xaml); var cell0 = (ViewCellWithCollection)listview.TemplatedItems.GetOrCreateContent (0, items[0]); var cell1 = (ViewCellWithCollection)listview.TemplatedItems.GetOrCreateContent (1, items[1]); Assert.AreNotSame (cell0, cell1); Assert.AreNotSame (cell0.Children, cell1.Children); Assert.AreNotSame (cell0.Children[0], cell1.Children[0]); } [Test] public void ResourcesDeclaredInDataTemplatesAreNotShared () { var xaml = @" "; var listview = new ListView (); var items = new [] { "Foo", "Bar", "Baz" }; listview.BindingContext = items; listview.LoadFromXaml (xaml); var cell0 = (ViewCell)listview.TemplatedItems.GetOrCreateContent (0, items[0]); var cell1 = (ViewCell)listview.TemplatedItems.GetOrCreateContent (1, items[1]); Assert.AreNotSame (cell0, cell1); var label0 = (Label)cell0.View; var label1 = (Label)cell1.View; Assert.AreNotSame (label0, label1); Assert.AreEqual ("Foo", label0.Text); Assert.AreEqual ("Bar", label1.Text); var res0 = label0.Resources; var res1 = label1.Resources; Assert.AreNotSame (res0, res1); var obj0 = res0 ["object"]; var obj1 = res1 ["object"]; Assert.NotNull (obj0); Assert.NotNull (obj1); Assert.AreNotSame (obj0, obj1); } } public class ViewCellWithCollection : ViewCell { public ViewList Children { get; set; } } }