Skip to content

Commit

Permalink
chore: Add RuntimeTests
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Nov 24, 2023
1 parent 260a3b1 commit 52e87bf
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
117 changes: 117 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/ResponsiveExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.RuntimeTests;
using Uno.Toolkit.RuntimeTests.Helpers;
using Uno.Toolkit.UI.Helpers;
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
{
[TestMethod]
public async Task ProvideValue_String_Initial()
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
ResponsiveHelper.SetDebugSize(new Size(300, 400));

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(new Size(300, 400));

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

await UnitTestUIContentHelperEx.SetContentAndWait(host);

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

ResponsiveHelper.SetDebugSize(new Size(800, 400));

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

[TestMethod]
public async Task ProvideValue_Color_Initial()
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
ResponsiveHelper.SetDebugSize(new Size(300, 400));

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

var border = (Border)host.FindName("MyBorder");

await UnitTestUIContentHelperEx.SetContentAndWait(host);

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

[TestMethod]
public async Task ProvideValue_Orientation_Initial()
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
ResponsiveHelper.SetDebugSize(new Size(800, 400));

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 Normal={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.Horizontal, stackPanel.Orientation);
}
}
}
20 changes: 17 additions & 3 deletions src/Uno.Toolkit.UI/Helpers/ResponsiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using System;
using System.Collections.Generic;
using Windows.Foundation;

using Uno.Disposables;
#if IS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
#else
using Windows.UI.Xaml;
using Windows.UI.Core;
Expand Down Expand Up @@ -111,6 +110,9 @@ public double Widest
internal class ResponsiveHelper
{
private static readonly Lazy<ResponsiveHelper> _instance = new Lazy<ResponsiveHelper>(() => new ResponsiveHelper());
private static readonly ResponsiveHelper _debugInstance = new();
private static bool UseDebuggableInstance;

private readonly List<WeakReference> _callbacks = new();
#if UNO14502_WORKAROUND
private List<IResponsiveCallback> _hardCallbackReferences = new();
Expand All @@ -119,7 +121,7 @@ internal class ResponsiveHelper
public ResponsiveLayout Layout { get; private set; } = ResponsiveLayout.Create(150, 300, 600, 800, 1080);
public Size WindowSize { get; private set; } = Size.Empty;

public static ResponsiveHelper GetForCurrentView() => _instance.Value;
public static ResponsiveHelper GetForCurrentView() => UseDebuggableInstance ? _debugInstance : _instance.Value;

private ResponsiveHelper() { }

Expand Down Expand Up @@ -175,4 +177,16 @@ internal void Register(IResponsiveCallback host)
var wr = new WeakReference(host);
_callbacks.Add(wr);
}

internal static IDisposable UsingDebuggableInstance()
{
UseDebuggableInstance = true;

return Disposable.Create(() => UseDebuggableInstance = false);
}

internal static void SetDebugSize(Size size)
{
_debugInstance.WindowSize = size;
}
}

0 comments on commit 52e87bf

Please sign in to comment.