Skip to content

Commit

Permalink
chore: Fix Runtime Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silviuo committed Nov 27, 2023
1 parent 52e87bf commit a241159
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
100 changes: 84 additions & 16 deletions src/Uno.Toolkit.RuntimeTests/Tests/ResponsiveExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ public async Task ProvideValue_String_Initial()
}

#if !IS_UWP || HAS_UNO
[TestMethod]
public async Task ProvideValue_String_SizeChange()
[TestMethod]
public async Task ProvideValue_String_SizeChange()
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
ResponsiveHelper.SetDebugSize(new Size(300, 400));
ResponsiveHelper.SetDebugSize(new Size(300, 400));

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

await UnitTestUIContentHelperEx.SetContentAndWait(host);
await UnitTestUIContentHelperEx.SetContentAndWait(host);

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

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

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

[TestMethod]
Expand All @@ -74,7 +74,7 @@ public async Task ProvideValue_Color_Initial()
<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}}" />
<Border x:Name="MyBorder" Width="30" Height="30" Background="{utu:Responsive Narrow={StaticResource BorderRed}, Wide={StaticResource BorderBlue}}" />
</StackPanel>
""");

Expand All @@ -86,20 +86,52 @@ public async Task ProvideValue_Color_Initial()
}
}

#if !IS_UWP || HAS_UNO
[TestMethod]
public async Task ProvideValue_Orientation_Initial()
public async Task ProvideValue_Color_SizeChange()
{
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 Narrow={StaticResource BorderRed}, Wide={StaticResource BorderBlue}}" />
</StackPanel>
""");

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

await UnitTestUIContentHelperEx.SetContentAndWait(host);

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

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

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

}
}
#endif

[TestMethod]
public async Task ProvideValue_Orientation_Initial()
{
using (ResponsiveHelper.UsingDebuggableInstance())
{
ResponsiveHelper.SetDebugSize(new Size(300, 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}}">
<StackPanel x:Name="MyStackPanel" Orientation="{utu:Responsive Narrow={StaticResource NarrowOrientation}, Wide={StaticResource WideOrientation}}">
<TextBlock Text="A" />
<TextBlock Text="B" />
<TextBlock Text="C" />
Expand All @@ -111,7 +143,43 @@ public async Task ProvideValue_Orientation_Initial()

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

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

}
11 changes: 5 additions & 6 deletions src/Uno.Toolkit.UI/Helpers/ResponsiveHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ public void HookupEvent(Window window)
window.SizeChanged += OnWindowSizeChanged;
}

private void OnWindowSizeChanged(object sender, WindowSizeChangedEventArgs e)
private void OnWindowSizeChanged(object sender, WindowSizeChangedEventArgs e) => OnWindowSizeChanged(e.Size);

private void OnWindowSizeChanged(Size size)
{
WindowSize = e.Size;
WindowSize = size;

// Clean up collected references
_callbacks.RemoveAll(reference => !reference.IsAlive);
Expand Down Expand Up @@ -185,8 +187,5 @@ internal static IDisposable UsingDebuggableInstance()
return Disposable.Create(() => UseDebuggableInstance = false);
}

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

0 comments on commit a241159

Please sign in to comment.