Skip to content

Commit

Permalink
fix: Align ContentPresenter/ContentControl with WinUI on Skia and Wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Oct 12, 2024
1 parent 5fd8ce0 commit 0936581
Show file tree
Hide file tree
Showing 28 changed files with 1,566 additions and 839 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"profiles": {
"SamplesApp.Windows (Package)": {
"commandName": "MsixPackage"
"commandName": "MsixPackage",
"nativeDebugging": true
},
"SamplesApp.Windows (Unpackaged)": {
"commandName": "Project"
"commandName": "Project",
"nativeDebugging": true
}
}
}
}
22 changes: 22 additions & 0 deletions src/Uno.UI.RuntimeTests/Extensions/ControlExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Runtime.CompilerServices;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.UI.Xaml;

internal static class ControlExtensions
{
#if WINAPPSDK
public static T GetTemplateChild<T>(this Control control, string childName) where T : DependencyObject
{
return UnsafeGetTemplateChild(control, childName) as T;
}

public static DependencyObject GetTemplateChild(this Control control, string childName)
{
return UnsafeGetTemplateChild(control, childName);
}

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "GetTemplateChild")]
extern static DependencyObject UnsafeGetTemplateChild(Control control, string childName);
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.UI.Xaml.Markup;
using Microsoft.UI.Xaml.Media;
using Common;
#if !HAS_UNO_WINUI
#if !HAS_UNO_WINUI && !WINAPPSDK
using Microsoft/* UWP don't rename */.UI.Xaml.Controls;
#endif
using MUXControlsTestApp.Utilities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MUXControlsTestApp.Utilities;
using Uno.Extensions;
using Uno.UI.RuntimeTests.Helpers;
using Uno.UI.Toolkit.Extensions;
using static Private.Infrastructure.TestServices;
using NavigationView = Microsoft/* UWP don't rename */.UI.Xaml.Controls.NavigationView;
using NavigationViewItem = Microsoft/* UWP don't rename */.UI.Xaml.Controls.NavigationViewItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

namespace Microsoft.UI.Xaml.Tests.MUXControls.ApiTests
{
#if !HAS_UNO
// For Uno, there is another partial that has the attribute
// The attribute has AllowMultiple = false so only one partial should have it.
[TestClass]
#endif
public partial class TabViewTests
{
#if HAS_UNO
[TestMethod]
#if __IOS__
[Ignore("Currently fails on iOS")]
Expand Down Expand Up @@ -53,6 +57,5 @@ await RunOnUIThread.ExecuteAsync(async () =>
Assert.AreEqual(1, containerContentChangingCounter);
});
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@
using Microsoft/* UWP don't rename */.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Shapes;

#if !HAS_UNO_WINUI
#if !HAS_UNO_WINUI && !WINAPPSDK
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
#endif

using Private.Infrastructure;
using System.Threading.Tasks;
using Uno.UI.RuntimeTests.Helpers;

#if !WINAPPSDK
using Microsoft.UI.Private.Controls;
#endif

namespace Microsoft.UI.Xaml.Tests.MUXControls.ApiTests;

Expand Down Expand Up @@ -93,6 +96,7 @@ public async Task TeachingTipBackgroundTest()
teachingTip.Background = blueBrush;
Verify.AreSame(blueBrush, teachingTip.Background);
#if !WINAPPSDK
{
var popup = TeachingTipTestHooks.GetPopup(teachingTip);
var child = popup.Child;
Expand Down Expand Up @@ -137,12 +141,14 @@ void VerifyLightDismissTipBackground(Brush brush, string uiPart)
}
}
}
#endif
teachingTip.IsLightDismissEnabled = true;
});

await TestServices.WindowHelper.WaitForIdle();

#if !WINAPPSDK
RunOnUIThread.Execute(() =>
{
Verify.AreEqual(blueBrush.Color, ((SolidColorBrush)teachingTip.Background).Color);
Expand Down Expand Up @@ -174,6 +180,7 @@ void VerifyBackgroundChanged(Brush brush, string uiPart)
}
}
});
#endif
}

[TestMethod]
Expand Down
30 changes: 15 additions & 15 deletions src/Uno.UI.RuntimeTests/MUX/Utilities/RunOnUIThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Windows.UI.Core;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Private.Infrastructure;
using Microsoft.UI.Dispatching;

#if USING_TAEF
using WEX.TestExecution;
Expand All @@ -26,14 +28,13 @@ public class RunOnUIThread
{
public static void Execute(Action action)
{
Execute(CoreApplication.MainView, action);
Execute(TestServices.WindowHelper.CurrentTestWindow.DispatcherQueue, action);
}

public static void Execute(CoreApplicationView whichView, Action action)
public static void Execute(DispatcherQueue dispatcherQueue, Action action)
{
Exception exception = null;
var dispatcher = whichView.Dispatcher;
if (dispatcher.HasThreadAccess)
if (dispatcherQueue.HasThreadAccess)
{
action();
}
Expand All @@ -47,7 +48,7 @@ public static void Execute(CoreApplicationView whichView, Action action)
#endif
{
// If the Splash screen dismissal happens on the UI thread, run the action right now.
if (dispatcher.HasThreadAccess)
if (dispatcherQueue.HasThreadAccess)
{
try
{
Expand All @@ -66,7 +67,7 @@ public static void Execute(CoreApplicationView whichView, Action action)
else
{
// Otherwise queue the work to the UI thread and then set the completion event on that thread.
var ignore = dispatcher.RunAsync(CoreDispatcherPriority.Normal,
var ignore = dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal,

Check notice on line 70 in src/Uno.UI.RuntimeTests/MUX/Utilities/RunOnUIThread.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RuntimeTests/MUX/Utilities/RunOnUIThread.cs#L70

Remove the unused local variable 'ignore'.
() =>
{
try
Expand Down Expand Up @@ -98,27 +99,26 @@ public static void Execute(CoreApplicationView whichView, Action action)
}

public static async Task ExecuteAsync(Action action) =>
await ExecuteAsync(CoreApplication.MainView, () =>
await ExecuteAsync(TestServices.WindowHelper.CurrentTestWindow.DispatcherQueue, () =>
{
action();
return Task.CompletedTask;
});

public static async Task ExecuteAsync(Func<Task> task) =>
await ExecuteAsync(CoreApplication.MainView, task);
await ExecuteAsync(TestServices.WindowHelper.CurrentTestWindow.DispatcherQueue, task);

public static async Task ExecuteAsync(CoreApplicationView whichView, Action action) =>
await ExecuteAsync(whichView, () =>
public static async Task ExecuteAsync(DispatcherQueue dispatcherQueue, Action action) =>
await ExecuteAsync(dispatcherQueue, () =>
{
action();
return Task.CompletedTask;
});

public static async Task ExecuteAsync(CoreApplicationView whichView, Func<Task> task)
public static async Task ExecuteAsync(DispatcherQueue dispatcherQueue, Func<Task> task)
{
Exception exception = null;
var dispatcher = whichView.Dispatcher;
if (dispatcher.HasThreadAccess)
if (dispatcherQueue.HasThreadAccess)
{
await task();
}
Expand All @@ -132,7 +132,7 @@ public static async Task ExecuteAsync(CoreApplicationView whichView, Func<Task>
#endif
{
// If the Splash screen dismissal happens on the UI thread, run the action right now.
if (dispatcher.HasThreadAccess)
if (dispatcherQueue.HasThreadAccess)
{
try
{
Expand All @@ -151,7 +151,7 @@ public static async Task ExecuteAsync(CoreApplicationView whichView, Func<Task>
else
{
// Otherwise queue the work to the UI thread and then set the completion event on that thread.
await dispatcher.RunAsync(CoreDispatcherPriority.Normal,
dispatcherQueue.TryEnqueue(DispatcherQueuePriority.Normal,
async () =>
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void When_Content_Presenter_Empty()

sut.emptyTestRoot.DataContext = "43";

Assert.AreEqual("43", GetTextBlockText(sut, "emptyTest"));
Assert.AreEqual("", GetTextBlockText(sut, "emptyTest"));
}

[TestMethod]
Expand All @@ -111,16 +111,16 @@ public void When_Content_Presenter_Priority()

TestServices.WindowHelper.WindowContent = sut;

Assert.AreEqual("43", GetTextBlockText(sut, "priorityTest"));
Assert.AreEqual("", GetTextBlockText(sut, "priorityTest"));

sut.priorityTestRoot.DataContext = "44";
Assert.AreEqual("44", GetTextBlockText(sut, "priorityTest"));
Assert.AreEqual("", GetTextBlockText(sut, "priorityTest"));

sut.priorityTestRoot.Content = "45";
Assert.AreEqual("45", GetTextBlockText(sut, "priorityTest"));
Assert.AreEqual("", GetTextBlockText(sut, "priorityTest"));

sut.priorityTestRoot.DataContext = "46";
Assert.AreEqual("46", GetTextBlockText(sut, "priorityTest"));
Assert.AreEqual("", GetTextBlockText(sut, "priorityTest"));
}

[TestMethod]
Expand All @@ -130,7 +130,7 @@ public void When_Content_Presenter_SameValue()

TestServices.WindowHelper.WindowContent = sut;

Assert.AreEqual("42", GetTextBlockText(sut, "sameValueTest"));
Assert.AreEqual("", GetTextBlockText(sut, "sameValueTest"));
}

[TestMethod]
Expand All @@ -140,19 +140,19 @@ public void When_Content_Presenter_Inheritance()

TestServices.WindowHelper.WindowContent = sut;

Assert.AreEqual("DataContext", GetTextBlockText(sut, "inheritanceTest"));
Assert.AreEqual("", GetTextBlockText(sut, "inheritanceTest"));

sut.inheritanceTestRoot.DataContext = "46";
Assert.AreEqual("46", GetTextBlockText(sut, "inheritanceTest"));
Assert.AreEqual("", GetTextBlockText(sut, "inheritanceTest"));

sut.inheritanceTestRoot.DataContext = "47";
Assert.AreEqual("47", GetTextBlockText(sut, "inheritanceTest"));
Assert.AreEqual("", GetTextBlockText(sut, "inheritanceTest"));

sut.inheritanceTestInner.DataContext = "48";
Assert.AreEqual("48", GetTextBlockText(sut, "inheritanceTest"));
Assert.AreEqual("", GetTextBlockText(sut, "inheritanceTest"));

sut.inheritanceTestRoot.DataContext = "49";
Assert.AreEqual("48", GetTextBlockText(sut, "inheritanceTest"));
Assert.AreEqual("", GetTextBlockText(sut, "inheritanceTest"));
}

[TestMethod]
Expand All @@ -167,23 +167,23 @@ public async Task When_Inside_ContentControl_Template()
var border1 = presenter1.FindVisualChildByType<Border>();
var tb1 = presenter1.FindVisualChildByType<TextBlock>();

Assert.AreEqual(new SolidColorBrush(Microsoft.UI.Colors.LightGreen), border1.Background);
Assert.AreEqual(Microsoft.UI.Colors.LightGreen, ((SolidColorBrush)border1.Background).Color);
Assert.AreEqual("Item 1", tb1.Text);

var cc2 = control.FindName("CCWithContentTemplate") as ContentControl;
var presenter2 = cc2.FindVisualChildByType<ContentPresenter>();
var border2 = presenter2.FindVisualChildByType<Border>();
var tb2 = presenter2.FindVisualChildByType<TextBlock>();

Assert.AreEqual(new SolidColorBrush(Microsoft.UI.Colors.LightPink), border2.Background);
Assert.AreEqual(Microsoft.UI.Colors.LightPink, ((SolidColorBrush)border2.Background).Color);
Assert.AreEqual("Item 2", tb2.Text);

var cc3 = control.FindName("CCWithContentTemplateAndContent") as ContentControl;
var presenter3 = cc3.FindVisualChildByType<ContentPresenter>();
var border3 = presenter3.FindVisualChildByType<Border>();
var tb3 = presenter3.FindVisualChildByType<TextBlock>();

Assert.AreEqual(new SolidColorBrush(Microsoft.UI.Colors.LightGreen), border3.Background);
Assert.AreEqual(Microsoft.UI.Colors.LightGreen, ((SolidColorBrush)border3.Background).Color);
Assert.AreEqual("Item 3", tb3.Text);

var cc4 = control.FindName("CCWithContent") as ContentControl;
Expand All @@ -201,7 +201,7 @@ public void When_Content_Presenter_SameValue_Changing()

TestServices.WindowHelper.WindowContent = sut;

Assert.AreEqual("DataContext", GetTextBlockText(sut, "sameValueChangingTest"));
Assert.AreEqual("", GetTextBlockText(sut, "sameValueChangingTest"));
}

[TestMethod]
Expand All @@ -211,7 +211,7 @@ public void When_Content_Presenter_Null_Content_Changed()

TestServices.WindowHelper.WindowContent = sut;

Assert.AreEqual("42", GetTextBlockText(sut, "nullContentChanged"));
Assert.AreEqual("", GetTextBlockText(sut, "nullContentChanged"));
}

static string GetTextBlockText(FrameworkElement sut, string v)
Expand Down Expand Up @@ -344,7 +344,7 @@ public async Task When_Content_Unset_Release()
TestServices.WindowHelper.WindowContent = SUT;

var wref = SetContent();
Assert.AreEqual(wref.Target, SUT.DataContext);
Assert.AreEqual(null, SUT.DataContext);

SUT.Content = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ public async Task Check_Binding()

tbs2.Should().NotBeNull();

// For some reason, the count is 0 in Windows. So this doesn't currently match Windows.
#if WINAPPSDK || UNO_HAS_ENHANCED_LIFECYCLE
tbs2.Should().HaveCount(0);
#else
// For some reason, the count is 0 in Windows. So this doesn't currently match Windows on some platforms.
tbs2.Should().HaveCount(1);
items[1].Content.Should().Be(tbs2.ElementAt(0).Text);
#endif
}

#if !WINAPPSDK // GetTemplateChild is protected in UWP while public in Uno.
Expand Down
Loading

0 comments on commit 0936581

Please sign in to comment.