Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrozsival authored and rmarinho committed Aug 24, 2024
1 parent 4051892 commit 4c49356
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/Controls/tests/Xaml.UnitTests/BindingsCompiler.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:cmp="clr-namespace:Microsoft.Maui.Controls.Compatibility;assembly=Microsoft.Maui.Controls"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.BindingsCompiler" >
<cmp:StackLayout>
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.BindingsCompiler"
x:Name="page"
x:DataType="local:GlobalViewModel">
<cmp:StackLayout x:Name="stack" x:DataType="{x:Null}">
<cmp:StackLayout x:DataType="local:MockViewModel">
<Label Text="{Binding Text}" x:Name="label0" />
<Label Text="{Binding Path=Text}" x:Name="label1" />
Expand All @@ -23,6 +25,7 @@
<Label Text="{Binding StructModel.Model.Text, Mode=TwoWay}" x:Name="label10" />
<Label Text="Text for label12" x:Name="label11" />
<Label Text="{Binding Text, x:DataType=Label, Source={x:Reference label11}}" x:Name="label12" />
<Label Text="{Binding BindingContext.GlobalText, Source={x:Reference page}, x:DataType=ContentPage}" x:Name="label13" />

<Picker
ItemsSource="{Binding Items}"
Expand Down
13 changes: 11 additions & 2 deletions src/Controls/tests/Xaml.UnitTests/BindingsCompiler.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public void TestCompiledBindings([Values(false, true)]bool useCompiledXaml)

var layout = new BindingsCompiler(useCompiledXaml)
{
BindingContext = vm
BindingContext = new GlobalViewModel(),
};
layout.stack.BindingContext = vm;
layout.label6.BindingContext = new MockStructViewModel
{
Model = new MockViewModel
Expand Down Expand Up @@ -121,11 +122,14 @@ public void TestCompiledBindings([Values(false, true)]bool useCompiledXaml)
}

//testing invalid bindingcontext type
layout.BindingContext = new object();
layout.stack.BindingContext = new object();
Assert.AreEqual(null, layout.label0.Text);

//testing source
Assert.That(layout.label12.Text, Is.EqualTo("Text for label12"));

//testing binding with path that cannot be statically compiled (we don't support casts in the Path)
Assert.That(layout.label13.Text, Is.EqualTo("Global Text"));
}

[Test]
Expand All @@ -148,6 +152,11 @@ public string Text
public MockViewModel Model { get; set; }
}

class GlobalViewModel
{
public string GlobalText { get; set; } = "Global Text";
}

class MockViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
Expand Down

0 comments on commit 4c49356

Please sign in to comment.