|
| 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 | +} |
0 commit comments