Skip to content

Commit

Permalink
Add test for template bindings compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrozsival committed Apr 23, 2024
1 parent c6e2aec commit 240f0a3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/TemplateBindingsCompiler.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.TemplateBindingsCompiler" >
<ContentPage.Resources>
<ControlTemplate x:Key="MyCardTemplate">
<Label
x:Name="CardTitleLabel"
Text="{TemplateBinding CardTitle, x:DataType=local:TemplateBindingCompilerTestCardView}" />
</ControlTemplate>
</ContentPage.Resources>

<local:TemplateBindingCompilerTestCardView
x:Name="ContentView"
CardTitle="The title"
ControlTemplate="{StaticResource MyCardTemplate}" />
</ContentPage>
61 changes: 61 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/TemplateBindingsCompiler.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Dispatching;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public partial class TemplateBindingsCompiler : ContentPage
{
public TemplateBindingsCompiler()
{
InitializeComponent();
}

public TemplateBindingsCompiler(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
public class Tests
{
[SetUp] public void Setup() => DispatcherProvider.SetCurrent(new DispatcherProviderStub());
[TearDown] public void TearDown() => DispatcherProvider.SetCurrent(null);

[TestCase(false)]
[TestCase(true)]
public void Test(bool useCompiledXaml)
{
var page = new TemplateBindingsCompiler(useCompiledXaml);
var label = (Label)page.ContentView.GetTemplateChild("CardTitleLabel");
Assert.AreEqual("The title", label?.Text);

if (useCompiledXaml)
{
var binding = label.GetContext(Label.TextProperty).Bindings.GetValue();
Assert.That(binding, Is.TypeOf<TypedBinding<TemplateBindingCompilerTestCardView, string>>());
}
}
}
}

public class TemplateBindingCompilerTestCardView : ContentView
{
public static readonly BindableProperty CardTitleProperty =
BindableProperty.Create(nameof(CardTitle), typeof(string), typeof(TemplateBindingCompilerTestCardView), string.Empty);

public string CardTitle
{
get => (string)GetValue(CardTitleProperty);
set => SetValue(CardTitleProperty, value);
}
}
}

0 comments on commit 240f0a3

Please sign in to comment.