summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-09-08 20:45:43 +0200
committerJason Smith <jason.smith@xamarin.com>2016-09-08 11:45:43 -0700
commit3b7d798fdda51a669683ed7d5c3770ebf3adfa77 (patch)
tree8e4d16d91e9a6cb5a49a8aecf8514a8c84f36b97 /Xamarin.Forms.Xaml.UnitTests
parent85426c5d9495eb1d55b3128bf97e50c68a73b53f (diff)
downloadxamarin-forms-3b7d798fdda51a669683ed7d5c3770ebf3adfa77.tar.gz
xamarin-forms-3b7d798fdda51a669683ed7d5c3770ebf3adfa77.tar.bz2
xamarin-forms-3b7d798fdda51a669683ed7d5c3770ebf3adfa77.zip
[Xaml] support native views and native bindings (#266)
Allows including Native views directly in xaml. Support for ios, android, UWP
Diffstat (limited to 'Xamarin.Forms.Xaml.UnitTests')
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/BPNotResolvedOnSubClass.xaml.cs13
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Issues/Issue1497.cs16
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml13
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml.cs293
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj6
5 files changed, 339 insertions, 2 deletions
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/BPNotResolvedOnSubClass.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/BPNotResolvedOnSubClass.xaml.cs
index 65bb73f1..c6913949 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Issues/BPNotResolvedOnSubClass.xaml.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/BPNotResolvedOnSubClass.xaml.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using Xamarin.Forms;
using NUnit.Framework;
+using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
@@ -29,6 +30,18 @@ namespace Xamarin.Forms.Xaml.UnitTests
[TestFixture]
class Tests
{
+ [SetUp]
+ public void Setup()
+ {
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ Device.PlatformServices = null;
+ }
+
[TestCase(true)]
[TestCase(false)]
public void CorrectlyResolveBPOnSubClasses (bool useCompiledXaml)
diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Issue1497.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Issue1497.cs
index 3813f73a..b35339d5 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Issues/Issue1497.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Issue1497.cs
@@ -1,11 +1,24 @@
using System;
using NUnit.Framework;
+using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
[TestFixture]
public class Issue1497
{
+ [SetUp]
+ public void Setup()
+ {
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ Device.PlatformServices = null;
+ }
+
[Test]
public void BPCollectionsWithSingleElement ()
{
@@ -23,5 +36,4 @@ namespace Xamarin.Forms.Xaml.UnitTests
Assert.True (grid.ColumnDefinitions [0].Width.IsStar);
}
}
-}
-
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml b/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml
new file mode 100644
index 00000000..a35f8e50
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
+ xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+ xmlns:ios="clr-namespace:Xamarin.Forms.Xaml.UnitTests;targetPlatform=iOS"
+ xmlns:android="clr-namespace:Xamarin.Forms.Xaml.UnitTests;targetPlatform=Android"
+ x:Class="Xamarin.Forms.Xaml.UnitTests.NativeViewsAndBindings">
+ <StackLayout>
+ <ContentView x:Name="view0">
+ <ios:MockUIView Foo="foo" Bar="42" Baz="{Binding Baz}" View.HorizontalOptions="End" View.VerticalOptions="{Binding VerticalOption}" />
+ <android:MockAndroidView Foo="foo" Bar="42" Baz="{Binding Baz}" View.HorizontalOptions="End" View.VerticalOptions="{Binding VerticalOption}" />
+ </ContentView>
+ </StackLayout>
+</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml.cs
new file mode 100644
index 00000000..ce02d63b
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/NativeViewsAndBindings.xaml.cs
@@ -0,0 +1,293 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using NUnit.Framework;
+using Xamarin.Forms;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ public abstract class MockNativeView
+ {
+ public string Foo { get; set; }
+ public int Bar { get; set; }
+ public string Baz { get; set; }
+ }
+
+ public class MockUIView : MockNativeView
+ {
+ public IList<MockUIView> SubViews { get; set; }
+ }
+
+ class MockUIViewWrapper : View
+ {
+ public MockUIView NativeView { get; }
+
+ public MockUIViewWrapper(MockUIView nativeView)
+ {
+ NativeView = nativeView;
+ nativeView.TransferbindablePropertiesToWrapper(this);
+ }
+
+ protected override void OnBindingContextChanged()
+ {
+ NativeView.SetBindingContext(BindingContext, nv => nv.SubViews);
+ base.OnBindingContextChanged();
+ }
+ }
+
+ public class MockAndroidView : MockNativeView
+ {
+ public IList<MockAndroidView> SubViews { get; set; }
+ }
+
+ class MockAndroidViewWrapper : View
+ {
+ public MockAndroidView NativeView { get; }
+
+ public MockAndroidViewWrapper(MockAndroidView nativeView)
+ {
+ NativeView = nativeView;
+ nativeView.TransferbindablePropertiesToWrapper(this);
+ }
+
+ protected override void OnBindingContextChanged()
+ {
+ NativeView.SetBindingContext(BindingContext, nv => nv.SubViews);
+ base.OnBindingContextChanged();
+ }
+ }
+
+ public static class MockNativeViewExtensions
+ {
+ public static View ToView(this MockUIView nativeView)
+ {
+ return new MockUIViewWrapper(nativeView);
+ }
+
+ public static void SetBinding(this MockUIView target, string targetProperty, BindingBase binding, string updateSourceEventName = null)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding, updateSourceEventName);
+ }
+
+ internal static void SetBinding(this MockUIView target, string targetProperty, BindingBase binding, INotifyPropertyChanged propertyChanged)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding, propertyChanged);
+ }
+
+ public static void SetBinding(this MockUIView target, BindableProperty targetProperty, BindingBase binding)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding);
+ }
+
+ public static void SetValue(this MockUIView target, BindableProperty targetProperty, object value)
+ {
+ NativeBindingHelpers.SetValue(target, targetProperty, value);
+ }
+
+ public static void SetBindingContext(this MockUIView target, object bindingContext, Func<MockUIView, IEnumerable<MockUIView>> getChild = null)
+ {
+ NativeBindingHelpers.SetBindingContext(target, bindingContext, getChild);
+ }
+
+ internal static void TransferbindablePropertiesToWrapper(this MockUIView target, MockUIViewWrapper wrapper)
+ {
+ NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper);
+ }
+
+ public static View ToView(this MockAndroidView nativeView)
+ {
+ return new MockAndroidViewWrapper(nativeView);
+ }
+
+ public static void SetBinding(this MockAndroidView target, string targetProperty, BindingBase binding, string updateSourceEventName = null)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding, updateSourceEventName);
+ }
+
+ internal static void SetBinding(this MockAndroidView target, string targetProperty, BindingBase binding, INotifyPropertyChanged propertyChanged)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding, propertyChanged);
+ }
+
+ public static void SetBinding(this MockAndroidView target, BindableProperty targetProperty, BindingBase binding)
+ {
+ NativeBindingHelpers.SetBinding(target, targetProperty, binding);
+ }
+
+ public static void SetValue(this MockAndroidView target, BindableProperty targetProperty, object value)
+ {
+ NativeBindingHelpers.SetValue(target, targetProperty, value);
+ }
+
+ public static void SetBindingContext(this MockAndroidView target, object bindingContext, Func<MockAndroidView, IEnumerable<MockAndroidView>> getChild = null)
+ {
+ NativeBindingHelpers.SetBindingContext(target, bindingContext, getChild);
+ }
+
+ internal static void TransferbindablePropertiesToWrapper(this MockAndroidView target, MockAndroidViewWrapper wrapper)
+ {
+ NativeBindingHelpers.TransferBindablePropertiesToWrapper(target, wrapper);
+ }
+ }
+
+ public class MockIosNativeValueConverterService : INativeValueConverterService
+ {
+ public bool ConvertTo(object value, Type toType, out object nativeValue)
+ {
+ nativeValue = null;
+ if (typeof(MockUIView).IsInstanceOfType(value) && toType.IsAssignableFrom(typeof(View))) {
+ nativeValue = ((MockUIView)value).ToView();
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public class MockAndroidNativeValueConverterService : INativeValueConverterService
+ {
+ public bool ConvertTo(object value, Type toType, out object nativeValue)
+ {
+ nativeValue = null;
+ if (typeof(MockAndroidView).IsInstanceOfType(value) && toType.IsAssignableFrom(typeof(View))) {
+ nativeValue = ((MockAndroidView)value).ToView();
+ return true;
+ }
+ return false;
+ }
+ }
+
+ public class MockIosNativeBindingService : INativeBindingService
+ {
+ public bool TrySetBinding(object target, string propertyName, BindingBase binding)
+ {
+ var view = target as MockUIView;
+ if (view == null)
+ return false;
+ if (target.GetType().GetProperty(propertyName)?.GetMethod == null)
+ return false;
+ view.SetBinding(propertyName, binding);
+ return true;
+ }
+
+ public bool TrySetBinding(object target, BindableProperty property, BindingBase binding)
+ {
+ var view = target as MockUIView;
+ if (view == null)
+ return false;
+ view.SetBinding(property, binding);
+ return true;
+ }
+
+ public bool TrySetValue(object target, BindableProperty property, object value)
+ {
+ var view = target as MockUIView;
+ if (view == null)
+ return false;
+ view.SetValue(property, value);
+ return true;
+ }
+ }
+
+ public class MockAndroidNativeBindingService : INativeBindingService
+ {
+ public bool TrySetBinding(object target, string propertyName, BindingBase binding)
+ {
+ var view = target as MockAndroidView;
+ if (view == null)
+ return false;
+ view.SetBinding(propertyName, binding);
+ return true;
+ }
+
+ public bool TrySetBinding(object target, BindableProperty property, BindingBase binding)
+ {
+ var view = target as MockAndroidView;
+ if (view == null)
+ return false;
+ view.SetBinding(property, binding);
+ return true;
+ }
+
+ public bool TrySetValue(object target, BindableProperty property, object value)
+ {
+ var view = target as MockAndroidView;
+ if (view == null)
+ return false;
+ view.SetValue(property, value);
+ return true;
+ }
+ }
+
+ public partial class NativeViewsAndBindings : ContentPage
+ {
+ public NativeViewsAndBindings()
+ {
+ InitializeComponent();
+ }
+
+ public NativeViewsAndBindings(bool useCompiledXaml)
+ {
+ //this stub will be replaced at compile time
+ }
+
+ [TestFixture]
+ public class Tests
+ {
+ [SetUp]
+ public void SetUp()
+ {
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TearDown]
+ public void TearDown()
+ {
+ Device.PlatformServices = null;
+ }
+
+ void SetUpPlatform(TargetPlatform platform)
+ {
+ Device.OS = platform;
+ if (platform == TargetPlatform.iOS) {
+ DependencyService.Register<INativeValueConverterService, MockIosNativeValueConverterService>();
+ DependencyService.Register<INativeBindingService, MockIosNativeBindingService>();
+ } else if (platform == TargetPlatform.Android) {
+ DependencyService.Register<INativeValueConverterService, MockAndroidNativeValueConverterService>();
+ DependencyService.Register<INativeBindingService, MockAndroidNativeBindingService>();
+ }
+ }
+
+ [TestCase(false, TargetPlatform.iOS)]
+ [TestCase(false, TargetPlatform.Android)]
+ //[TestCase(true)]
+ public void NativeInContentView(bool useCompiledXaml, TargetPlatform platform)
+ {
+ SetUpPlatform(platform);
+ var layout = new NativeViewsAndBindings(useCompiledXaml);
+ layout.BindingContext = new {
+ Baz = "Bound Value",
+ VerticalOption=LayoutOptions.EndAndExpand
+ };
+ var view = layout.view0;
+ Assert.NotNull(view.Content);
+ MockNativeView nativeView = null;
+ if (platform == TargetPlatform.iOS) {
+ Assert.That(view.Content, Is.TypeOf<MockUIViewWrapper>());
+ Assert.That(((MockUIViewWrapper)view.Content).NativeView, Is.TypeOf<MockUIView>());
+ nativeView = ((MockUIViewWrapper)view.Content).NativeView;
+ } else if (platform == TargetPlatform.Android) {
+ Assert.That(view.Content, Is.TypeOf<MockAndroidViewWrapper>());
+ Assert.That(((MockAndroidViewWrapper)view.Content).NativeView, Is.TypeOf<MockAndroidView>());
+ nativeView = ((MockAndroidViewWrapper)view.Content).NativeView;
+ }
+
+ Assert.AreEqual("foo", nativeView.Foo);
+ Assert.AreEqual(42, nativeView.Bar);
+ Assert.AreEqual("Bound Value", nativeView.Baz);
+ Assert.AreEqual(LayoutOptions.End, view.Content.GetValue(View.HorizontalOptionsProperty));
+ Assert.AreEqual(LayoutOptions.EndAndExpand, view.Content.GetValue(View.VerticalOptionsProperty));
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
index 86e2eeb0..26bf6d41 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
+++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
@@ -359,6 +359,9 @@
<Compile Include="TypeExtension.xaml.cs">
<DependentUpon>TypeExtension.xaml</DependentUpon>
</Compile>
+ <Compile Include="NativeViewsAndBindings.xaml.cs">
+ <DependentUpon>NativeViewsAndBindings.xaml</DependentUpon>
+ </Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
@@ -638,6 +641,9 @@
<EmbeddedResource Include="TypeExtension.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
+ <EmbeddedResource Include="NativeViewsAndBindings.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />