mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2025-04-10 11:15:45 +02:00
Add specific DependencyProperty scenario for NullableBool MarkupExtension tests
(as per original uservoice request for this missing feature)
This commit is contained in:
parent
75f0a91c6b
commit
5ff9578975
UnitTests/Extensions
@ -0,0 +1,31 @@
|
||||
// ******************************************************************
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
|
||||
// ******************************************************************
|
||||
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Data;
|
||||
|
||||
namespace UnitTests.Extensions.Helpers
|
||||
{
|
||||
[Bindable]
|
||||
public class ObjectWithNullableBoolProperty : DependencyObject
|
||||
{
|
||||
public bool? NullableBool
|
||||
{
|
||||
get { return (bool?)GetValue(NullableBoolProperty); }
|
||||
set { SetValue(NullableBoolProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for NullableBool. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty NullableBoolProperty =
|
||||
DependencyProperty.Register(nameof(NullableBool), typeof(bool?), typeof(ObjectWithNullableBoolProperty), new PropertyMetadata(null));
|
||||
}
|
||||
}
|
@ -10,16 +10,14 @@
|
||||
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
|
||||
// ******************************************************************
|
||||
|
||||
using Microsoft.Toolkit.Extensions;
|
||||
using Microsoft.Toolkit.Uwp.UI.Extensions;
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
|
||||
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using UnitTests.Extensions.Helpers;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Controls;
|
||||
using Windows.UI.Xaml.Markup;
|
||||
|
||||
using Assert = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.Assert;
|
||||
|
||||
namespace UnitTests.Extensions
|
||||
@ -117,5 +115,73 @@ public void Test_NullableBool_MarkupExtension_ProvidesNullStillWithFalseValue()
|
||||
|
||||
Assert.AreEqual(null, toggle.IsChecked, "Expected checkbox value to be null.");
|
||||
}
|
||||
|
||||
[TestCategory("NullableBoolMarkupExtension")]
|
||||
[UITestMethod]
|
||||
public void Test_NullableBool_Test_TestObject()
|
||||
{
|
||||
// Just test that we can properly parse our object as-is into our test harness.
|
||||
var treeroot = XamlReader.Load(@"<Page
|
||||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
|
||||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
|
||||
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
|
||||
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
|
||||
<Page.Resources>
|
||||
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject""/>
|
||||
</Page.Resources>
|
||||
</Page>") as FrameworkElement;
|
||||
|
||||
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
|
||||
|
||||
Assert.IsNotNull(obj, "Could not find object in resources.");
|
||||
|
||||
Assert.AreEqual(null, obj.NullableBool, "Expected obj value to be null.");
|
||||
}
|
||||
|
||||
[TestCategory("NullableBoolMarkupExtension")]
|
||||
[UITestMethod]
|
||||
public void Test_NullableBool_DependencyProperty_SystemTrueValueFails()
|
||||
{
|
||||
// This is the failure case in the OS currently which causes us to need
|
||||
// this markup extension.
|
||||
var exception = Assert.ThrowsException<XamlParseException>(
|
||||
() =>
|
||||
{
|
||||
var treeroot = XamlReader.Load(@"<Page
|
||||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
|
||||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
|
||||
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
|
||||
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
|
||||
<Page.Resources>
|
||||
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""True""/>
|
||||
</Page.Resources>
|
||||
</Page>") as FrameworkElement;
|
||||
}, "Expected assignment failure during parsing, OS now supports, update documentation.");
|
||||
|
||||
Assert.IsNotNull(exception);
|
||||
|
||||
Assert.IsTrue(exception.Message.Contains("Failed to create a 'Windows.Foundation.IReference`1<Boolean>' from the text 'True'."));
|
||||
}
|
||||
|
||||
[TestCategory("NullableBoolMarkupExtension")]
|
||||
[UITestMethod]
|
||||
public void Test_NullableBool_DependencyProperty_TrueValue()
|
||||
{
|
||||
var treeroot = XamlReader.Load(@"<Page
|
||||
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
|
||||
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
|
||||
xmlns:ex=""using:Microsoft.Toolkit.Uwp.UI.Extensions""
|
||||
xmlns:helpers=""using:UnitTests.Extensions.Helpers"">
|
||||
<Page.Resources>
|
||||
<helpers:ObjectWithNullableBoolProperty x:Key=""OurObject"" NullableBool=""{ex:NullableBool Value=True}""/>
|
||||
</Page.Resources>
|
||||
</Page>") as FrameworkElement;
|
||||
|
||||
var obj = treeroot.Resources["OurObject"] as ObjectWithNullableBoolProperty;
|
||||
|
||||
Assert.IsNotNull(obj, "Could not find object in resources.");
|
||||
|
||||
Assert.AreEqual(true, obj.NullableBool, "Expected obj value to be true.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user