Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Properly resubscribe to nested prop changes #622

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions src/Uno.Toolkit.RuntimeTests/Tests/NavigationBarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml;
using Microsoft.UI;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Markup;
Expand Down Expand Up @@ -124,7 +126,7 @@ public async Task MainCommand_In_Popup_With_Page_With_BackStack(MainCommandMode

var shouldGoBack = mainCommandMode == MainCommandMode.Back;
var popup = new Popup { Width = 100, Height = 100, HorizontalOffset = 100, VerticalOffset = 100 };

var content = new Border { Width = 100, Height = 100, Child = popup };
var frame = new Frame() { Width = 400, Height = 400 };

Expand Down Expand Up @@ -206,6 +208,44 @@ public async Task MainCommand_In_Popup_With_Page_With_BackStack(MainCommandMode
}
}

#if __ANDROID__ || __IOS__
[TestMethod]
public async Task NavigationBar_Dynamic_Background()
{
var frame = new Frame() { Width = 400, Height = 400 };
await UnitTestUIContentHelperEx.SetContentAndWait(frame);
await UnitTestsUIContentHelper.WaitForIdle();

var navBar = await frame.NavigateAndGetNavBar<RedNavBarPage>();
navBar!.Content = "Hello";
Assert.IsTrue(navBar!.Background is SolidColorBrush redBrush && redBrush.Color == Colors.Red);
await UnitTestsUIContentHelper.WaitForIdle();

try
{
navBar!.Background = new SolidColorBrush(Colors.Green);
navBar!.Padding = new Thickness(20);
}
catch (Exception ex)
{
Assert.Fail("Expected no exception, but got: " + ex.Message);
}

Assert.IsTrue(navBar!.Background is SolidColorBrush greenBrush && greenBrush.Color == Colors.Green);
}

private sealed partial class RedNavBarPage : Page
{
public RedNavBarPage()
{
Content = new NavigationBar
{
Background = new SolidColorBrush(Colors.Red),
};
}
}
#endif

#if __IOS__
[TestMethod]
public async Task Can_Set_MainCommand_Label_Or_Content()
Expand All @@ -224,7 +264,7 @@ public async Task Can_Set_MainCommand_Label_Or_Content()

// ContentTitlePage
var contentTitleNavBar = await frame.NavigateAndGetNavBar<ContentTitlePage>();
Assert.AreEqual("Content Title", contentTitleNavBar?.GetNativeNavBar()?.BackItem?.BackButtonTitle);
Assert.AreEqual("Content Title", contentTitleNavBar?.GetNativeNavBar()?.BackItem?.BackButtonTitle);
}

[TestMethod]
Expand All @@ -234,7 +274,7 @@ public async Task MainCommand_Use_LeftBarButtonItem_When_No_BackStack()
NavigationBar? secondPageNavBar = null;

var popup = new Popup { Width = 100, Height = 100, HorizontalOffset = 100, VerticalOffset = 100 };

var content = new Border { Width = 100, Height = 100, Child = popup };
var frame = new Frame() { Width = 400, Height = 400 };

Expand All @@ -258,7 +298,7 @@ public async Task MainCommand_Use_LeftBarButtonItem_When_No_BackStack()
var renderedNativeNavItem = firstPageNavBar.GetNativeNavItem();

Assert.IsNotNull(renderedNativeNavItem?.LeftBarButtonItem);

frame.Navigate(typeof(NavBarSecondPage));

await UnitTestsUIContentHelper.WaitForIdle();
Expand Down Expand Up @@ -292,7 +332,7 @@ public async Task NavigationBar_Does_Render()
[TestMethod]
public async Task NavigationBar_Does_Render_Within_AutoLayout()
{
var frame = new Frame { Width = 200, Height = 200 };;
var frame = new Frame { Width = 200, Height = 200 };
await UnitTestUIContentHelperEx.SetContentAndWait(frame);

frame.Navigate(typeof(NavBarAutoLayoutPage));
Expand All @@ -305,7 +345,7 @@ public async Task NavigationBar_Does_Render_Within_AutoLayout()
[TestMethod]
public async Task NavigationBar_Renders_BackItem_Within_AutoLayout()
{
var frame = new Frame { Width = 600, Height = 200 };;
var frame = new Frame { Width = 600, Height = 200 };
await UnitTestUIContentHelperEx.SetContentAndWait(frame);

var firstNavBar = await frame.NavigateAndGetNavBar<NavBarAutoLayoutPage>();
Expand Down Expand Up @@ -334,7 +374,7 @@ private static void AssertNavigationBar(Frame frame)
Assert.AreSame(renderedNativeNavBar, presenter.NavigationController.NavigationBar);
}



private sealed partial class FirstPage : Page
{
Expand Down Expand Up @@ -391,17 +431,18 @@ public NavBarTestPage()
#endif
}

#if __IOS__
#if __IOS__ || __ANDROID__
public static class NavigationBarTestHelper
{
#if __IOS__
public static UINavigationBar? GetNativeNavBar(this NavigationBar? navBar) => navBar
kazo0 marked this conversation as resolved.
Show resolved Hide resolved
?.TryGetRenderer<NavigationBar, NavigationBarRenderer>()
?.Native;

public static UINavigationItem? GetNativeNavItem(this NavigationBar? navBar) => navBar
?.TryGetRenderer<NavigationBar, NavigationBarNavigationItemRenderer>()
?.Native;

#endif
public static async Task<NavigationBar?> NavigateAndGetNavBar<TPage>(this Frame frame) where TPage : Page
{
frame.Navigate(typeof(TPage));
Expand All @@ -410,8 +451,7 @@ public static class NavigationBarTestHelper
var page = frame.Content as TPage;
await UnitTestsUIContentHelper.WaitForLoaded(page!);
return page?.FindChild<NavigationBar>();
}
}
}
#endif
}

42 changes: 33 additions & 9 deletions src/Uno.Toolkit.UI/Extensions/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Uno.Disposables;
using Uno.Extensions;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Uno.Logging;

#if IS_WINUI
using Microsoft.UI.Xaml.Controls.Primitives;
Expand Down Expand Up @@ -57,16 +57,23 @@ internal static IDisposable RegisterDisposableNestedPropertyChangedCallback(this

var childDisposable = new SerialDisposable();

childDisposable.Disposable = (instance.GetValue(property) as DependencyObject)?.RegisterDisposableNestedPropertyChangedCallback(callback, subProperties);

var disposable = instance.RegisterDisposablePropertyChangedCallback(property, (s, e) =>
if (instance.TryGetValue(property, out var dpValue))
{
callback(s, e);
childDisposable.Disposable = dpValue?.RegisterDisposableNestedPropertyChangedCallback(callback, subProperties);

var disposable = instance.RegisterDisposablePropertyChangedCallback(property, (s, e) =>
{
callback(s, e);
if (s is { } && s.TryGetValue(property, out var dpValue))
{
childDisposable.Disposable = dpValue?.RegisterDisposableNestedPropertyChangedCallback(callback, subProperties);
}
});

childDisposable.Disposable = s?.RegisterDisposableNestedPropertyChangedCallback(callback, subProperties);
});
return new CompositeDisposable(disposable, childDisposable);
}

return new CompositeDisposable(disposable, childDisposable);
return Disposable.Empty;
});

return new CompositeDisposable(disposables);
Expand Down Expand Up @@ -206,5 +213,22 @@ internal static void SetParent(this DependencyObject dependencyObject, object? p

return property;
}

public static bool TryGetValue(this DependencyObject dependencyObject, DependencyProperty dependencyProperty, out DependencyObject? value)
{
value = default;

try
{
value = dependencyObject.GetValue(dependencyProperty) as DependencyObject;
}
catch (Exception e)
{
typeof(DependencyObjectExtensions).Log().WarnIfEnabled(() => $"Error attempting to read property:", e);
return false;
}

return true;
}
}
}
Loading