-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for template bindings compilation
- Loading branch information
1 parent
c6e2aec
commit 240f0a3
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
src/Controls/tests/Xaml.UnitTests/TemplateBindingsCompiler.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/Controls/tests/Xaml.UnitTests/TemplateBindingsCompiler.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |