Xamarin.Forms/Xamarin.Forms.Core.UnitTests/DataTemplateSelectorTests.cs

158 строки
3.4 KiB
C#
Исходник Постоянная ссылка Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using NUnit.Framework;
using Xamarin.Forms;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class DataTemplateSelectorTests : BaseTestFixture
{
[TearDown]
public override void TearDown()
{
base.TearDown();
Device.PlatformServices = null;
}
[SetUp]
public override void Setup()
{
base.Setup();
Device.PlatformServices = new MockPlatformServices();
}
2016-03-22 23:02:25 +03:00
class TemplateOne : DataTemplate
{
2020-09-29 13:15:44 +03:00
public TemplateOne() : base(typeof(ViewCell))
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
2016-03-22 23:02:25 +03:00
}
}
class TemplateTwo : DataTemplate
{
2020-09-29 13:15:44 +03:00
public TemplateTwo() : base(typeof(EntryCell))
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
2016-03-22 23:02:25 +03:00
}
}
class TestDTS : DataTemplateSelector
{
2020-09-29 13:15:44 +03:00
public TestDTS()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
templateOne = new TemplateOne();
templateTwo = new TemplateTwo();
2016-03-22 23:02:25 +03:00
}
2020-09-29 13:15:44 +03:00
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
2016-03-22 23:02:25 +03:00
{
if (item is double)
return templateOne;
if (item is byte)
2020-09-29 13:15:44 +03:00
return new TestDTS();
2016-03-22 23:02:25 +03:00
return templateTwo;
}
readonly DataTemplate templateOne;
readonly DataTemplate templateTwo;
}
[Test]
2020-09-29 13:15:44 +03:00
public void Constructor()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
var dts = new TestDTS();
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void ReturnsCorrectType()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
var dts = new TestDTS();
Assert.IsInstanceOf<TemplateOne>(dts.SelectTemplate(1d, null));
Assert.IsInstanceOf<TemplateTwo>(dts.SelectTemplate("test", null));
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void ListViewSupport()
2016-03-22 23:02:25 +03:00
{
var listView = new ListView(ListViewCachingStrategy.RecycleElement);
listView.ItemsSource = new object[] { 0d, "test" };
2020-09-29 13:15:44 +03:00
listView.ItemTemplate = new TestDTS();
Assert.IsInstanceOf<ViewCell>(listView.TemplatedItems[0]);
Assert.IsInstanceOf<EntryCell>(listView.TemplatedItems[1]);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void NestingThrowsException()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
var dts = new TestDTS();
Assert.Throws<NotSupportedException>(() => dts.SelectTemplate((byte)0, null));
2016-03-22 23:02:25 +03:00
}
}
[TestFixture]
public class DataTemplateRecycleTests : BaseTestFixture
{
[TearDown]
public override void TearDown()
{
base.TearDown();
Device.PlatformServices = null;
}
[SetUp]
public override void Setup()
{
base.Setup();
Device.PlatformServices = new MockPlatformServices();
}
class TestDataTemplateSelector : DataTemplateSelector
{
readonly DataTemplate declarativeTemplate;
readonly DataTemplate proceduralTemplate;
2020-09-29 13:15:44 +03:00
public TestDataTemplateSelector()
{
declarativeTemplate = new DataTemplate(typeof(ViewCell));
proceduralTemplate = new DataTemplate(() => new EntryCell());
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
Counter++;
if (item is string)
return declarativeTemplate;
return proceduralTemplate;
}
public int Counter = 0;
}
[Test]
2020-09-29 13:15:44 +03:00
public void ListViewSupport()
{
var listView = new ListView(ListViewCachingStrategy.RecycleElementAndDataTemplate);
listView.ItemsSource = new object[] { "foo", "bar", 0 };
2020-09-29 13:15:44 +03:00
Assert.That(listView.CachingStrategy ==
ListViewCachingStrategy.RecycleElementAndDataTemplate);
var selector = new TestDataTemplateSelector();
listView.ItemTemplate = selector;
Assert.That(selector.Counter == 0);
Assert.IsInstanceOf<ViewCell>(listView.TemplatedItems[0]);
Assert.That(selector.Counter == 1);
Assert.IsInstanceOf<ViewCell>(listView.TemplatedItems[1]);
Assert.That(selector.Counter == 1);
Assert.Throws<NotSupportedException>(
() => { var o = listView.TemplatedItems[2]; });
}
}
2016-03-22 23:02:25 +03:00
}