Skip to content

Commit

Permalink
refactor(responsive): remove ResponsiveHelper initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jan 9, 2024
1 parent 130f102 commit 83cdddc
Show file tree
Hide file tree
Showing 9 changed files with 459 additions and 542 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ protected override async void OnLaunched(XamlLaunchActivatedEventArgs e)
#else
_window = XamlWindow.Current;
#endif
var helper = ResponsiveHelper.GetForCurrentView();
helper.HookupEvent(_window);

if (_window.Content is null)
{
Expand Down
352 changes: 160 additions & 192 deletions src/Uno.Toolkit.RuntimeTests/Tests/ResponsiveExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,192 +1,160 @@
// Disabled until fix is implemented for https://github.com/unoplatform/uno/issues/14620

//using System.Threading.Tasks;
//using Microsoft.VisualStudio.TestTools.UnitTesting;
//using Uno.UI.RuntimeTests;
//using Uno.Toolkit.RuntimeTests.Helpers;
//using Uno.Toolkit.UI;
//using Windows.Foundation;

//#if IS_WINUI
//using Microsoft.UI.Xaml.Controls;
//using Microsoft.UI;
//using Microsoft.UI.Xaml.Media;
//#else
//using Windows.UI.Xaml.Controls;
//using Windows.UI;
//using Windows.UI.Xaml.Media;
//#endif

//namespace Uno.Toolkit.RuntimeTests.Tests;

//[TestClass]
//[RunsOnUIThread]
//internal class ResponsiveExtensionsTests
//{
// private static readonly Size NarrowSize = new Size(300, 400);
// private static readonly Size WideSize = new Size(800, 400);

// [TestMethod]
// public async Task ProvideValue_String_InitialValue()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var host = XamlHelper.LoadXaml<TextBlock>("""
// <TextBlock Text="{utu:Responsive Narrow=asd, Wide=qwe}" />
// """);

// await UnitTestUIContentHelperEx.SetContentAndWait(host);

// Assert.AreEqual("asd", host.Text);
// }
// }

//#if !IS_UWP || HAS_UNO
// [TestMethod]
// public async Task ProvideValue_String_SizeChange()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var host = XamlHelper.LoadXaml<TextBlock>("""
// <TextBlock Text="{utu:Responsive Narrow=asd, Wide=qwe}" />
// """);

// await UnitTestUIContentHelperEx.SetContentAndWait(host);

// Assert.AreEqual("asd", host.Text);

// ResponsiveHelper.SetDebugSize(WideSize);

// Assert.AreEqual("qwe", host.Text);
// }
// }
//#endif

// [TestMethod]
// public async Task ProvideValue_Color_InitialValue()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var border = XamlHelper.LoadXaml<Border>("""
// <Border Width="30"
// Height="30">
// <Border.Resources>
// <SolidColorBrush x:Key="BorderRed">Red</SolidColorBrush>
// <SolidColorBrush x:Key="BorderBlue">Blue</SolidColorBrush>
// </Border.Resources>
// <Border.Background>
// <utu:Responsive Narrow="{StaticResource BorderRed}" Wide="{StaticResource BorderBlue}" />
// </Border.Background>
// </Border>
// """);

// await UnitTestUIContentHelperEx.SetContentAndWait(border);

// Assert.AreEqual(Colors.Red, ((SolidColorBrush)border.Background).Color);
// }
// }

//#if !IS_UWP || HAS_UNO
// [TestMethod]
// public async Task ProvideValue_Color_SizeChange()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var border = XamlHelper.LoadXaml<Border>("""
// <Border Width="30"
// Height="30">
// <Border.Resources>
// <SolidColorBrush x:Key="BorderRed">Red</SolidColorBrush>
// <SolidColorBrush x:Key="BorderBlue">Blue</SolidColorBrush>
// </Border.Resources>
// <Border.Background>
// <utu:Responsive Narrow="{StaticResource BorderRed}" Wide="{StaticResource BorderBlue}" />
// </Border.Background>
// </Border>
// """);

// await UnitTestUIContentHelperEx.SetContentAndWait(border);

// Assert.AreEqual(Colors.Red, ((SolidColorBrush)border.Background).Color);

// ResponsiveHelper.SetDebugSize(WideSize);

// Assert.AreEqual(Colors.Blue, ((SolidColorBrush)border.Background).Color);

// }
// }
//#endif

// [TestMethod]
// public async Task ProvideValue_Orientation_InitialValue()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var host = XamlHelper.LoadXaml<StackPanel>("""
// <StackPanel>
// <StackPanel.Resources>
// <Orientation x:Key="NarrowOrientation">Vertical</Orientation>
// <Orientation x:Key="WideOrientation">Horizontal</Orientation>
// </StackPanel.Resources>
// <StackPanel x:Name="MyStackPanel" Orientation="{utu:Responsive Narrow={StaticResource NarrowOrientation}, Wide={StaticResource WideOrientation}}">
// <TextBlock Text="A" />
// <TextBlock Text="B" />
// <TextBlock Text="C" />
// </StackPanel>
// </StackPanel>
// """);

// var stackPanel = (StackPanel)host.FindName("MyStackPanel");

// await UnitTestUIContentHelperEx.SetContentAndWait(host);

// Assert.AreEqual(Orientation.Vertical, stackPanel.Orientation);
// }
// }

//#if !IS_UWP || HAS_UNO
// [TestMethod]
// public async Task ProvideValue_Orientation_SizeChange()
// {
// using (ResponsiveHelper.UsingDebuggableInstance())
// {
// ResponsiveHelper.SetDebugSize(NarrowSize);

// var host = XamlHelper.LoadXaml<StackPanel>("""
// <StackPanel>
// <StackPanel.Resources>
// <Orientation x:Key="NarrowOrientation">Vertical</Orientation>
// <Orientation x:Key="WideOrientation">Horizontal</Orientation>
// </StackPanel.Resources>
// <StackPanel x:Name="MyStackPanel" Orientation="{utu:Responsive Narrow={StaticResource NarrowOrientation}, Wide={StaticResource WideOrientation}}">
// <TextBlock Text="A" />
// <TextBlock Text="B" />
// <TextBlock Text="C" />
// </StackPanel>
// </StackPanel>
// """);

// var stackPanel = (StackPanel)host.FindName("MyStackPanel");

// await UnitTestUIContentHelperEx.SetContentAndWait(host);

// Assert.AreEqual(Orientation.Vertical, stackPanel.Orientation);

// ResponsiveHelper.SetDebugSize(WideSize);

// Assert.AreEqual(Orientation.Horizontal, stackPanel.Orientation);
// }
// }
//#endif

//}
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.Foundation;
using Uno.UI.RuntimeTests;
using Uno.Toolkit.RuntimeTests.Helpers;
using Uno.Toolkit.UI;

#if IS_WINUI
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI;
using Microsoft.UI.Xaml.Media;
#else
using Windows.UI.Xaml.Controls;
using Windows.UI;
using Windows.UI.Xaml.Media;
#endif

namespace Uno.Toolkit.RuntimeTests.Tests;

[TestClass]
#if HAS_UNO
[Ignore("blocked by #14620: dynamically loaded MarkupExtension are not initialized.")] // https://github.com/unoplatform/uno/issues/14620
#elif IS_UWP
[Ignore("ResponsiveExtension is not supported on UWP.")]
#endif
[RunsOnUIThread]
internal class ResponsiveExtensionsTests
{
private static readonly Size NarrowSize = new Size(300, 400);
private static readonly Size WideSize = new Size(800, 400);

[TestMethod]
public async Task ProvideValue_String_Value()
{
var sut = XamlHelper.LoadXaml<TextBlock>("""
<TextBlock Text="{utu:Responsive Narrow=asd, Wide=qwe}" />
""");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Text)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(sut);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual("asd", sut.Text);
}

[TestMethod]
public async Task ProvideValue_String_SizeChange()
{
var sut = XamlHelper.LoadXaml<TextBlock>("""
<TextBlock Text="{utu:Responsive Narrow=asd, Wide=qwe}" />
""");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Text)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(sut);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual("asd", sut.Text);

ext.ForceResponsiveSize(WideSize);
Assert.AreEqual("qwe", sut.Text);
}

[TestMethod]
public async Task ProvideValue_Color_Value()
{
var sut = XamlHelper.LoadXaml<Border>("""
<Border Width="30"
Height="30">
<Border.Resources>
<SolidColorBrush x:Key="BorderRed">Red</SolidColorBrush>
<SolidColorBrush x:Key="BorderBlue">Blue</SolidColorBrush>
</Border.Resources>
<Border.Background>
<utu:Responsive Narrow="{StaticResource BorderRed}" Wide="{StaticResource BorderBlue}" />
</Border.Background>
</Border>
""");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Background)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(sut);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual(Colors.Red, ((SolidColorBrush)sut.Background).Color);
}

[TestMethod]
public async Task ProvideValue_Color_SizeChange()
{
var sut = XamlHelper.LoadXaml<Border>("""
<Border Width="30"
Height="30">
<Border.Resources>
<SolidColorBrush x:Key="BorderRed">Red</SolidColorBrush>
<SolidColorBrush x:Key="BorderBlue">Blue</SolidColorBrush>
</Border.Resources>
<Border.Background>
<utu:Responsive Narrow="{StaticResource BorderRed}" Wide="{StaticResource BorderBlue}" />
</Border.Background>
</Border>
""");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Background)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(sut);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual(Colors.Red, ((SolidColorBrush)sut.Background).Color);

ext.ForceResponsiveSize(WideSize);
Assert.AreEqual(Colors.Blue, ((SolidColorBrush)sut.Background).Color);
}

[TestMethod]
public async Task ProvideValue_Orientation_Value()
{
var container = XamlHelper.LoadXaml<Border>("""
<Border>
<Border.Resources>
<Orientation x:Key="NarrowOrientation">Vertical</Orientation>
<Orientation x:Key="WideOrientation">Horizontal</Orientation>
</Border.Resources>
<StackPanel Orientation="{utu:Responsive Narrow={StaticResource NarrowOrientation}, Wide={StaticResource WideOrientation}}">
<TextBlock Text="A" />
<TextBlock Text="B" />
<TextBlock Text="C" />
</StackPanel>
</Border>
""");
var sut = container.Child as StackPanel ?? throw new InvalidOperationException("Failed to resolve the SUT");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Orientation)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(container);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual(Orientation.Vertical, (container.Child as StackPanel)?.Orientation);
}

[TestMethod]
public async Task ProvideValue_Orientation_SizeChange()
{
var container = XamlHelper.LoadXaml<Border>("""
<Border>
<Border.Resources>
<Orientation x:Key="NarrowOrientation">Vertical</Orientation>
<Orientation x:Key="WideOrientation">Horizontal</Orientation>
</Border.Resources>
<StackPanel Orientation="{utu:Responsive Narrow={StaticResource NarrowOrientation}, Wide={StaticResource WideOrientation}}">
<TextBlock Text="A" />
<TextBlock Text="B" />
<TextBlock Text="C" />
</StackPanel>
</Border>
""");
var sut = container.Child as StackPanel ?? throw new InvalidOperationException("Failed to resolve the SUT");
var ext = ResponsiveExtension.GetInstanceFor(sut, nameof(sut.Orientation)) ?? throw new InvalidOperationException("Failed to resolve the markup extension.");
await UnitTestUIContentHelperEx.SetContentAndWait(container);

ext.ForceResponsiveSize(NarrowSize);
Assert.AreEqual(Orientation.Vertical, sut.Orientation);

ext.ForceResponsiveSize(WideSize);
Assert.AreEqual(Orientation.Horizontal, sut.Orientation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ResponsiveHelperTests
public void When_Resolving_AllLayout()
{
var layout = DefaultLayout;
var options = Enum.GetValues<Layout>();
var options = new[] { Layout.Narrowest, Layout.Narrow, Layout.Normal, Layout.Wide, Layout.Widest };

Assert.AreEqual(Layout.Narrowest, ResponsiveHelper.ResolveLayoutCore(layout, 149, options), "149");
Assert.AreEqual(Layout.Narrowest, ResponsiveHelper.ResolveLayoutCore(layout, 150, options), "150"); // breakpoint=Narrowest
Expand Down
Loading

0 comments on commit 83cdddc

Please sign in to comment.