8000 Add Resource Dictionary Tests (#10753) · dotnet/wpf@cac41ae · GitHub
[go: up one dir, main page]

Skip to content

Commit cac41ae

Browse files
authored
Add Re 8000 source Dictionary Tests (#10753)
1 parent 03fa924 commit cac41ae

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed

src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/GlobalUsings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55

66
global using System.Collections.Generic;
77
global using System.Globalization;
8+
9+
#pragma warning disable IDE0005 // Using directive is unnecessary.
10+
global using FluentAssertions;
11+
#pragma warning restore IDE0005 // Using directive is unnecessary.

src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationFramework.Tests/PresentationFramework.Tests.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<NoWarn>$(NoWarn)</NoWarn>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<TargetFramework Condition="!$(TargetFramework.Contains('windows'))">$(TargetFramework)-windows</TargetFramework>
13+
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
14+
<GenerateProgramFile>true</GenerateProgramFile>
1315
</PropertyGroup>
1416

1517
<ItemGroup>
@@ -21,6 +23,7 @@
2123
<ProjectReference Include="$(WpfSourceDir)WindowsBase\WindowsBase.csproj" />
2224
<ProjectReference Include="$(WpfSourceDir)System.Xaml\System.Xaml.csproj" />
2325
<ProjectReference Include="$(WpfSourceDir)Extensions\PresentationFramework-SystemDrawing\PresentationFramework-SystemDrawing.csproj" />
26+
<ProjectReference Include="$(WpfSourceDir)UIAutomation\UIAutomationTypes\UIAutomationTypes.csproj" />
2427
</ItemGroup>
2528

2629
<ItemGroup>
@@ -36,6 +39,15 @@
3639
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="$(SystemRuntimeSerializationFormattersPackageVersion)" />
3740
<PackageReference Include="$(SystemDrawingCommonPackage)" Version="$(SystemDrawingCommonVersion)" />
3841
<PackageReference Include="System.Private.Windows.Core.TestUtilities" Version="$(SystemPrivateWindowsCoreTestUtilitiesVersion)" />
42+
<PackageReference Include="System.Windows.Extensions" Version="$(SystemWindowsExtensionsPackageVersion)" />
43+
</ItemGroup>
44+
45+
<ItemGroup>
46+
<None Remove="System\Windows\Resources\SampleResourceDictionary.xaml" />
47+
</ItemGroup>
48+
49+
<ItemGroup>
50+
<Page Include="System\Windows\Resources\SampleResourceDictionary.xaml" Generator="MSBuild:Compile" CopyToOutputDirectory="Always" />
3951
</ItemGroup>
4052

4153
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Linq;
5+
using System.Windows.Controls;
6+
using System.Windows.Media;
7+
using System.Windows.Threading;
8+
9+
namespace System.Windows;
10+
11+
public class ResourceDictionaryTests
12+
{
13+
private readonly ResourceDictionary _dictionary;
14+
private string SampleDictionaryPath => "/PresentationFramework.Tests;component/System/Windows/Resources/SampleResourceDictionary.xaml";
15+
16+
public ResourceDictionaryTests()
17+
{
18+
_dictionary = (ResourceDictionary)Application.LoadComponent(new Uri(SampleDictionaryPath, UriKind.Relative));
19+
}
20+
21+
[Fact]
22+
public void Dictionary_ShouldContainStaticResource()
23+
{
24+
_dictionary.Contains("StaticBrush").Should().BeTrue();
25+
_dictionary["StaticBrush"].Should().BeOfType<SolidColorBrush>();
26+
}
27+
28+
[Fact]
29+
public void Dictionary_ShouldContainDynamicResource()
30+
{
31+
_dictionary.Contains("DynamicBrush").Should().BeTrue();
32+
_dictionary["DynamicBrush"].Should().BeAssignableTo<Brush>();
33+
}
34+
35+
[Fact]
36+
public void Dictionary_ShouldContainGradientBrush()
37+
{
38+
_dictionary.Contains("GradientBackground").Should().BeTrue();
39+
_dictionary["GradientBackground"].Should().BeOfType<LinearGradientBrush>();
40+
}
41+
42+
[Fact]
43+
public void Dictionary_ShouldContainStyleForButton()
44+
{
45+
_dictionary.Contains("PrimaryButtonStyle").Should().BeTrue();
46+
47+
var style = _dictionary["PrimaryButtonStyle"] as Style;
48+
49+
style.Should().NotBeNull();
50+
51+
style!.TargetType.Should().Be(typeof(Button));
52+
style.Setters.Should().NotBeEmpty();
53+
}
54+
55+
[Fact]
56+
public void Dictionary_StyleShouldContainTrigger()
57+
{
58+
var style = _dictionary["PrimaryButtonStyle"] as Style;
59+
60+
style.Should().NotBeNull();
61+
62+
#pragma warning disable IDE0002 // Name should be simplified
63+
style!.Triggers.Should().NotBeNull();
64+
style.Triggers.OfType<Trigger>().Should().ContainSingle(t =>
65+
t.Property == Button.IsMouseOverProperty &&
66+
t.Value.Equals(true));
67+
#pragma warning restore IDE0002 // Name should be simplified
68+
}
69+
70+
[Fact]
71+
public void Dictionary_ShouldContainDataTemplate()
72+
{
73+
_dictionary.Contains("ItemTemplate").Should().BeTrue();
74+
_dictionary["ItemTemplate"].Should().BeOfType<DataTemplate>();
75+
}
76+
77+
[Fact]
78+
public void Dictionary_ShouldContainControlTemplate()
79+
{
80+
_dictionary.Contains("CustomControlTemplate").Should().BeTrue();
81+
_dictionary["CustomControlTemplate"].Should().BeOfType<ControlTemplate>();
82+
}
83+
84+
[Fact]
85+
public void Dictionary_ShouldContainStyleWithBasedOn()
86+
{
87+
var baseStyle = _dictionary["BaseTextBlockStyle"] as Style;
88+
var derivedStyle = _dictionary["DerivedTextBlockStyle"] as Style;
89+
90+
baseStyle.Should().NotBeNull();
91+
derivedStyle.Should().NotBeNull();
92+
derivedStyle!.BasedOn.Should().Be(baseStyle);
93+
}
94+
95+
[Fact]
96+
public void Dictionary_StyleShouldHaveMultiTrigger()
97+
{
98+
var style = _dictionary["MultiTriggerStyle"] as Style;
99+
100+
style.Should().NotBeNull();
101+
style!.Triggers.OfType<MultiTrigger>().Should().Contain(mt =>
102+
mt.Conditions.Count == 2);
103+
}
104+
105+
[WpfFact]
106+
public void DynamicResource_ShouldResolve_FromLocalResourceDictionary()
107+
{
108+
var key = "DynamicBrush";
109+
110+
var brush = new SolidColorBrush(Colors.Green);
111+
var textBlock = new TextBlock();
112+
113+
textBlock.Resources[key] = brush;
114+
textBlock.SetResourceReference(TextBlock.ForegroundProperty, key);
115+
116+
textBlock.Foreground.Should().BeSameAs(brush);
117+
}
118+
119+
[WpfFact]
120+
public void DynamicResource_ShouldUpdate_WhenLocalResourceChanges()
121+
{
122+
var key = "DynamicBrush";
123+
var initialBrush = new SolidColorBrush(Colors.Green);
124+
var updatedBrush = new SolidColorBrush(Colors.Red);
125+
126+
var textBlock = new TextBlock();
127+
textBlock.Resources[key] = initialBrush;
128+
textBlock.SetResourceReference(TextBlock.ForegroundProperty, key);
129+
130+
textBlock.Foreground.Should().BeSameAs(initialBrush);
131+
132+
// Update local resource
133+
textBlock.Resources[key] = updatedBrush;
134+
135+
// Force UI changes
136+
textBlock.Dispatcher.Invoke(() => { }, DispatcherPriority.Background);
137+
138+
textBlock.Foreground.Should().BeSameAs(updatedBrush);
139+
}
140+
141+
[WpfFact]
142+
public void DynamicResource_ShouldRemainLastValue_WhenLocalResourceRemoved()
143+
{
144+
var key = "DynamicBrush";
145+
var initialBrush = new SolidColorBrush(Colors.Green);
146+
147+
var textBlock = new TextBlock();
148+
textBlock.SetResourceReference(TextBlock.ForegroundProperty, key);
149+
150+
textBlock.Resources[key] = initialBrush;
151+
textBlock.Foreground.Should().BeSameAs(initialBrush);
152+
153+
// Remove the key from resources
154+
textBlock.Resources.Remove(key);
155+
156+
// Force UI Changes
157+
textBlock.Dispatcher.Invoke(() => { }, DispatcherPriority.Background);
158+
159+
// The default foreground color of textBlock (as in aero2)
160+
textBlock.Foreground.Should().BeSameAs(Brushes.Black);
161+
}
162+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:sys="clr-namespace:System;assembly=mscorlib">
4+
5+
<SolidColorBrush x:Key="StaticBrush" Color="Blue"/>
6+
7+
<SolidColorBrush x:Key="DynamicBrush" Color="Red"/>
8+
9+
<LinearGradientBrush x:Key="GradientBackground" StartPoint="0,0" EndPoint="1,1">
10+
<GradientStop Color="LightBlue" Offset="0"/>
11+
<GradientStop Color="DarkBlue" Offset="1"/>
12+
</LinearGradientBrush>
13+
14+
<Style x:Key="BaseTextBlockStyle" TargetType="TextBlock">
15+
<Setter Property="FontSize" Value="14"/>
16+
<Setter Property="Foreground" Value="Gray"/>
17+
</Style>
18+
19+
<Style x:Key="DerivedTextBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource BaseTextBlockStyle}">
20+
<Setter Property="FontWeight" Value="Bold"/>
21+
</Style>
22+
23+
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
24+
<Setter Property="Background" Value="{StaticResource StaticBrush}"/>
25+
<Setter Property="Foreground" Value="White"/>
26+
<Style.Triggers>
27+
<Trigger Property="IsMouseOver" Value="True">
28+
<Setter Property="Background" Value="DarkBlue"/>
29+
</Trigger>
30+
</Style.Triggers>
31+
</Style>
32+
33+
<Style x:Key="MultiTriggerStyle" TargetType="ToggleButton">
34+
<Setter Property="Background" Value="LightGray"/>
35+
<Style.Triggers>
36+
<MultiTrigger>
37+
<MultiTrigger.Conditions>
38+
<Condition Property="IsMouseOver" Value="True"/>
39+
<Condition Property="IsChecked" Value="True"/>
40+
</MultiTrigger.Conditions>
41+
<Setter Property="Background" Value="Green"/>
42+
</MultiTrigger>
43+
</Style.Triggers>
44+
</Style>
45+
46+
<Style x:Key="DataTriggerStyle" TargetType="TextBox">
47+
<Setter Property="Background" Value="White"/>
48+
<Style.Triggers>
49+
<DataTrigger Binding="{Binding IsReadOnly}" Value="True">
50+
<Setter Property="Background" Value="LightGray"/>
51+
</DataTrigger>
52+
</Style.Triggers>
53+
</Style>
54+
55+
<ControlTemplate x:Key="CustomControlTemplate" TargetType="Button">
56+
<Border x:Name="Root" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
57+
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
58+
</Border>
59+
<ControlTemplate.Triggers>
60+
<Trigger Property="IsPressed" Value="True">
61+
<Setter TargetName="Root" Property="Background" Value="Gray"/>
62+
</Trigger>
63+
</ControlTemplate.Triggers>
64+
</ControlTemplate>
65+
66+
<DataTemplate x:Key="ItemTemplate">
67+
<TextBlock Text="{Binding Name}" Style="{StaticResource DerivedTextBlockStyle}"/>
68+
</DataTemplate>
69+
</ResourceDictionary>

0 commit comments

Comments
 (0)
0