summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-11-15 20:39:48 +0100
committerJason Smith <jason.smith@xamarin.com>2016-11-15 11:39:48 -0800
commita6bbed029c64d2d64b74eeb67e27a099abf70664 (patch)
tree551c3924c055e2d39592b3f1c726cca46924dd73 /Xamarin.Forms.Xaml.UnitTests
parent14e21dcebd4a706aaa5eed384b142957d84df002 (diff)
downloadxamarin-forms-a6bbed029c64d2d64b74eeb67e27a099abf70664.tar.gz
xamarin-forms-a6bbed029c64d2d64b74eeb67e27a099abf70664.tar.bz2
xamarin-forms-a6bbed029c64d2d64b74eeb67e27a099abf70664.zip
[XamlC] TypedBindings, some tests, a compiler, ... (#489)
Diffstat (limited to 'Xamarin.Forms.Xaml.UnitTests')
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml21
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml.cs130
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs2
-rw-r--r--Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj16
4 files changed, 163 insertions, 6 deletions
diff --git a/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml b/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml
new file mode 100644
index 00000000..4d3deeee
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml
@@ -0,0 +1,21 @@
+<?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:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
+ xmlns:sys="clr-namespace:System;assembly=mscorlib"
+
+ x:Class="Xamarin.Forms.Xaml.UnitTests.BindingsCompiler" >
+ <StackLayout>
+ <StackLayout x:DataType="local:MockViewModel">
+ <Label Text="{Binding Text}" x:Name="label0" />
+ <Label Text="{Binding Path=Text}" x:Name="label1" />
+ <Label Text="{Binding Model.Text}" x:Name="label2" />
+ <Label Text="{Binding Model[3]}" x:Name="label3" />
+ <Label Text="{Binding}" x:Name="label4" x:DataType="sys:String"/>
+ <Entry Text="{Binding Text, Mode=TwoWay}" x:Name="entry0"/>
+ </StackLayout>
+
+ <Label Text="{Binding Text}" x:Name="labelWithUncompiledBinding" />
+ </StackLayout>
+</ContentPage> \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml.cs
new file mode 100644
index 00000000..843fe1c2
--- /dev/null
+++ b/Xamarin.Forms.Xaml.UnitTests/BindingsCompiler.xaml.cs
@@ -0,0 +1,130 @@
+using System;
+using System.Collections.Generic;
+
+using Xamarin.Forms;
+using NUnit.Framework;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+using System.Diagnostics;
+using Xamarin.Forms.Core.UnitTests;
+
+namespace Xamarin.Forms.Xaml.UnitTests
+{
+ public partial class BindingsCompiler : ContentPage
+ {
+ public BindingsCompiler()
+ {
+ InitializeComponent();
+ }
+
+ public BindingsCompiler(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;
+ }
+
+ [TestCase(false)]
+ [TestCase(true)]
+ public void Test(bool useCompiledXaml)
+ {
+ var vm = new MockViewModel {
+ Text = "Text0",
+ Model = new MockViewModel {
+ Text = "Text1"
+ },
+ };
+ vm.Model [3] = "TextIndex";
+
+ var layout = new BindingsCompiler(useCompiledXaml);
+ layout.BindingContext = vm;
+ //testing paths
+ Assert.AreEqual("Text0", layout.label0.Text);
+ Assert.AreEqual("Text0", layout.label1.Text);
+ Assert.AreEqual("Text1", layout.label2.Text);
+ Assert.AreEqual("TextIndex", layout.label3.Text);
+
+ //testing selfPath
+ layout.label4.BindingContext = "Self";
+ Assert.AreEqual("Self", layout.label4.Text);
+
+ //testing INPC
+ vm.Text = "Text2";
+ Assert.AreEqual("Text2", layout.label0.Text);
+
+ //testing 2way
+ Assert.AreEqual("Text2", layout.entry0.Text);
+ ((IElementController)layout.entry0).SetValueFromRenderer(Entry.TextProperty, "Text3");
+ Assert.AreEqual("Text3", layout.entry0.Text);
+
+ //testing invalid bindingcontext type
+ layout.BindingContext = new object();
+ Assert.AreEqual(null, layout.label0.Text);
+ }
+ }
+ }
+
+ class MockViewModel : INotifyPropertyChanged
+ {
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ public MockViewModel(string text = null)
+ {
+ _text = text;
+ }
+
+ string _text;
+ public string Text {
+ get { return _text; }
+ set {
+ if (_text == value)
+ return;
+
+ _text = value;
+ OnPropertyChanged();
+ }
+ }
+
+ MockViewModel _model;
+ public MockViewModel Model {
+ get { return _model; }
+ set {
+ if (_model == value)
+ return;
+ _model = value;
+ OnPropertyChanged();
+ }
+ }
+
+ string [] values = new string [5];
+ [IndexerName("Indexer")]
+ public string this [int v] {
+ get { return values [v]; }
+ set {
+ if (values [v] == value)
+ return;
+
+ values [v] = value;
+ OnPropertyChanged("Indexer[" + v + "]");
+ }
+ }
+
+ protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+} \ No newline at end of file
diff --git a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs
index 404169b3..ccbfa20a 100644
--- a/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs
+++ b/Xamarin.Forms.Xaml.UnitTests/MockCompiler.cs
@@ -23,7 +23,7 @@ namespace Xamarin.Forms.Xaml.UnitTests
};
var exceptions = new List<Exception>();
- if (!xamlc.Compile(exceptions) && exceptions.Any())
+ if (!xamlc.Execute(exceptions) && exceptions.Any())
throw exceptions [0];
}
}
diff --git a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
index a5ebbd92..9765c7b9 100644
--- a/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
+++ b/Xamarin.Forms.Xaml.UnitTests/Xamarin.Forms.Xaml.UnitTests.csproj
@@ -368,12 +368,15 @@
<Compile Include="XStaticException.xaml.cs">
<DependentUpon>XStaticException.xaml</DependentUpon>
</Compile>
- <Compile Include="CompiledTypeConverter.xaml.cs">
- <DependentUpon>CompiledTypeConverter.xaml</DependentUpon>
+ <Compile Include="CompiledTypeConverter.xaml.cs">
+ <DependentUpon>CompiledTypeConverter.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="Issues\Bz43301.xaml.cs">
+ <DependentUpon>Bz43301.xaml</DependentUpon>
+ </Compile>
+ <Compile Include="BindingsCompiler.xaml.cs">
+ <DependentUpon>BindingsCompiler.xaml</DependentUpon>
</Compile>
- <Compile Include="Issues\Bz43301.xaml.cs">
- <DependentUpon>Bz43301.xaml</DependentUpon>
- </Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="..\.nuspec\Xamarin.Forms.Debug.targets" />
@@ -665,6 +668,9 @@
<EmbeddedResource Include="Issues\Bz43301.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
+ <EmbeddedResource Include="BindingsCompiler.xaml">
+ <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
+ </EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />