Skip to content

Commit

Permalink
[C] fix specificity comparison (#24891)
Browse files Browse the repository at this point in the history
* test for #24849

* failing test

* [C] OR EQUAL !

- fixes #24849
  • Loading branch information
StephaneDelcroix authored Sep 25, 2024
1 parent a7b43dd commit 7005a68
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Controls/src/Core/Element/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ internal void OnResourcesChanged(IEnumerable<KeyValuePair<string, object>> value
internal override void OnSetDynamicResource(BindableProperty property, string key, SetterSpecificity specificity)
{
base.OnSetDynamicResource(property, key, specificity);
if (!DynamicResources.TryGetValue(property, out var existing) || existing.Item2.CompareTo(specificity) < 0)
if (!DynamicResources.TryGetValue(property, out var existing) || existing.Item2.CompareTo(specificity) <= 0)
DynamicResources[property] = (key, specificity);
if (this.TryGetResource(key, out var value))
OnResourceChanged(property, value, specificity);
Expand Down
26 changes: 26 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/AppResources/Style24849.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Style24849">
<Color x:Key="PrimaryButtonTextColor">White</Color>
<Color x:Key="PrimaryButtonDisabledTextColor">#3c3c3b</Color>
<Style TargetType="Button">
<Setter Property="TextColor" Value="{DynamicResource PrimaryButtonTextColor}" />
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<VisualState.Setters>
<Setter Property="TextColor" Value="{DynamicResource PrimaryButtonTextColor}" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Property="TextColor" Value="{DynamicResource PrimaryButtonDisabledTextColor}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ResourceDictionary>
16 changes: 16 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/AppResources/Style24849.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Core.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public partial class Style24849 : ResourceDictionary
{
public Style24849()
{
InitializeComponent();
}
}
}
7 changes: 7 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui24849.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?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.Maui24849">
<Button x:Name="button" IsEnabled="False" />
</ContentPage>
71 changes: 71 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui24849.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Controls.Shapes;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Dispatching;

using Microsoft.Maui.Graphics;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests;

public partial class Maui24849 : ContentPage
{
public Maui24849()
{
InitializeComponent();
}

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

[TestFixture]
class Test
{
MockDeviceInfo mockDeviceInfo;

[SetUp]
public void Setup()
{
Application.SetCurrentApplication(new MockApplication());
DeviceInfo.SetCurrent(mockDeviceInfo = new MockDeviceInfo());
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
}


[TearDown] public void TearDown()
{
AppInfo.SetCurrent(null);
DeviceInfo.SetCurrent(null);
}

[Test]
public void VSGReturnsToNormal([Values(false, true)] bool useCompiledXaml)
{
var app = new MockApplication();
app.Resources.Add(new Style24849());
var page = new Maui24849(useCompiledXaml);

app.MainPage = page;

Assert.That(page.button.IsEnabled, Is.False);
Assert.That(page.button.TextColor, Is.EqualTo(Color.FromHex("#3c3c3b")));

page.button.IsEnabled = true;
Assert.That(page.button.IsEnabled, Is.True);
Assert.That(page.button.TextColor, Is.EqualTo(Colors.White));

page.button.IsEnabled = false;
Assert.That(page.button.IsEnabled, Is.False);
Assert.That(page.button.TextColor, Is.EqualTo(Color.FromHex("#3c3c3b")));
}
}
}

0 comments on commit 7005a68

Please sign in to comment.