Skip to content

Commit

Permalink
Merge pull request #103 from unoplatform/dev/dr/hrTests
Browse files Browse the repository at this point in the history
feat: Add ability to test hot-reload scenario
  • Loading branch information
dr1rrb authored Oct 19, 2023
2 parents cbf16c3 + f1b929b commit 3252d70
Show file tree
Hide file tree
Showing 85 changed files with 5,238 additions and 394 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ on:
branches: [ "main" ]

env:
DotNetVersion: '6.0.401'
UnoCheck_Version: '1.5.4'
UnoCheck_Manifest: 'https://raw.githubusercontent.com/unoplatform/uno.check/34b1a60f5c1c51604b47362781969dde46979fd5/manifests/uno.ui.manifest.json'
DotNetVersion: '7.0.401'
UnoCheck_Version: '1.16.0'
UnoCheck_Manifest: 'https://raw.githubusercontent.com/unoplatform/uno.check/519b147a80d92e35cac4b8f97855d9302c91b340/manifests/uno.ui.manifest.json'

jobs:
build:
Expand Down Expand Up @@ -45,7 +45,7 @@ jobs:
- run: |
& dotnet tool update --global uno.check --version $env:UnoCheck_Version --add-source https://api.nuget.org/v3/index.json
& uno-check -v --ci --non-interactive --fix --skip xcode --skip gtk3 --skip vswin --skip vsmac --manifest $env:UnoCheck_Manifest
& uno-check -v --ci --non-interactive --fix --skip xcode --skip gtk3 --skip vswin --skip vswinworkloads --skip vsmac --manifest $env:UnoCheck_Manifest
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
Expand Down
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PropertyGroup>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CS1998</NoWarn><!--Lacks await in async-->
</PropertyGroup>

<PropertyGroup Condition="'$(TF_BUILD)' == 'true' or '$(GITHUB_ACTIONS)'=='true'">
Expand Down
20 changes: 20 additions & 0 deletions src/TestApp/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project ToolsVersion="15.0">
<Import Project="..\Directory.Build.props" />

<PropertyGroup>
<DefineConstants>$(DefineConstants);IS_UNO_RUNTIMETEST_PROJECT</DefineConstants>
</PropertyGroup>

<ItemGroup>
<AssemblyAttribute Include="Uno.UI.RuntimeTests.Engine.RuntimeTestsSourceProjectAttribute">
<_Parameter1>$(MSBuildProjectFullPath)</_Parameter1>
</AssemblyAttribute>

<AssemblyAttribute Include="Uno.UI.RuntimeTests.Engine.RuntimeTestDevServerAttribute" Condition="'$(PkgUno_UI_DevServer)' != ''">
<_Parameter1>$([System.IO.Path]::GetFileName(`$(PkgUno_UI_DevServer)`)) </_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="Uno.UI.RuntimeTests.Engine.RuntimeTestDevServerAttribute" Condition="'$(PkgUno_WinUI_DevServer)' != ''">
<_Parameter1>$([System.IO.Path]::GetFileName(`$(PkgUno_WinUI_DevServer)`)) </_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions src/TestApp/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project ToolsVersion="15.0">
<Import Project="..\Directory.Build.targets" />

<PropertyGroup>
<DefineConstants Condition="'$(PkgUno_UI_DevServer)' != '' OR '$(PkgUno_WinUI_DevServer)' != ''">$(DefineConstants);HAS_UNO_DEVSERVER</DefineConstants>
</PropertyGroup>
</Project>
8 changes: 8 additions & 0 deletions src/TestApp/shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ private static void InitializeLogging()
builder.AddFilter("Windows", LogLevel.Warning);
builder.AddFilter("Microsoft", LogLevel.Warning);

builder.AddFilter("Uno.UI.RuntimeTests.HotReload", LogLevel.Debug);

// Hot reload testing
builder.AddFilter("Uno.UI.RemoteControl", LogLevel.Debug);
builder.AddFilter("Uno.UI.RuntimeTests.Internal.Helpers", LogLevel.Debug); // DevServer and SecondaryApp
builder.AddFilter("Uno.UI.RuntimeTests.HotReloadHelper", LogLevel.Trace); // Helper used by tests in secondary app instances (non debuggable)
builder.AddFilter("Uno.UI.RuntimeTests.UnitTestsControl", LogLevel.Information); // Dumps the runner status

// Generic Xaml events
// builder.AddFilter("Windows.UI.Xaml", LogLevel.Debug );
// builder.AddFilter("Windows.UI.Xaml.VisualStateGroup", LogLevel.Debug );
Expand Down
63 changes: 63 additions & 0 deletions src/TestApp/shared/FilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Uno.UI.RuntimeTests.Engine;

[TestClass]
public class FilterTests
{
[TestMethod]
[DataRow("abc & ghi", "abc", false)]
[DataRow("abc & ghi", "ghi", false)]
[DataRow("abc & ghi", "abc.ghi", true)]
[DataRow("abc & (ghi)", "abc", false)]
[DataRow("abc & (ghi)", "ghi", false)]
[DataRow("abc & (ghi)", "abc.ghi", true)]
[DataRow("a.b.c & ghi", "a.b", false)]
[DataRow("a.b.c & ghi", "a.b.c.def.ghi", true)]
[DataRow("a.b.c & (ghi)", "a.b", false)]
[DataRow("a.b.c & (ghi)", "a.b.c.def.ghi", true)]
[DataRow("abc & g.h.i", "g.h", false)]
[DataRow("abc & g.h.i", "abc.def.g.h.i", true)]

[DataRow("abc | ghi", "abc", true)]
[DataRow("abc | ghi", "ghi", true)]
[DataRow("abc | ghi", "abc.ghi", true)]
[DataRow("abc | (ghi)", "abc", true)]
[DataRow("abc | (ghi)", "ghi", true)]
[DataRow("abc | (ghi)", "abc.ghi", true)]
[DataRow("a.b.c | ghi", "a.b", false)]
[DataRow("a.b.c | ghi", "a.b.c", true)]
[DataRow("a.b.c | ghi", "a.b.c.def.ghi", true)]
[DataRow("a.b.c | (ghi)", "a.b", false)]
[DataRow("a.b.c | (ghi)", "a.b.c", true)]
[DataRow("a.b.c | (ghi)", "a.b.c.def.ghi", true)]
[DataRow("abc | g.h.i", "g.h", false)]
[DataRow("abc | g.h.i", "g.h.i", true)]
[DataRow("abc | g.h.i", "abc.def.g.h.i", true)]

[DataRow("abc ; ghi", "abc", true)]
[DataRow("abc ; ghi", "ghi", true)]
[DataRow("abc ; ghi", "abc.ghi", true)]
[DataRow("abc ; (ghi)", "abc", true)]
[DataRow("abc ; (ghi)", "ghi", true)]
[DataRow("abc ; (ghi)", "abc.ghi", true)]
[DataRow("a.b.c ; ghi", "a.b", false)]
[DataRow("a.b.c ; ghi", "a.b.c", true)]
[DataRow("a.b.c ; ghi", "a.b.c.def.ghi", true)]
[DataRow("a.b.c ; (ghi)", "a.b", false)]
[DataRow("a.b.c ; (ghi)", "a.b.c", true)]
[DataRow("a.b.c ; (ghi)", "a.b.c.def.ghi", true)]
[DataRow("abc ; g.h.i", "g.h", false)]
[DataRow("abc ; g.h.i", "g.h.i", true)]
[DataRow("abc ; g.h.i", "abc.def.g.h.i", true)]
public void When_ParseAndMatch(string filter, string method, bool expectedResult)
{
UnitTestFilter sut = filter;
var result = sut.IsMatch(method);

Assert.AreEqual(expectedResult, result);
}
}
12 changes: 12 additions & 0 deletions src/TestApp/shared/HotReloadTest_SimpleSubject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma warning disable

using System;
using System.Collections.Generic;
using System.Text;

namespace Uno.UI.RuntimeTests.Engine;

public class HotReloadTest_SimpleSubject
{
public string Value => "42";
}
92 changes: 92 additions & 0 deletions src/TestApp/shared/HotReloadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.Extensions;

#if HAS_UNO_WINUI || WINDOWS_WINUI
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#endif

namespace Uno.UI.RuntimeTests.Engine;

[TestClass]
public class HotReloadSanity
{
[TestMethod]
public void Is_HotReload_Supported()
{
#if __SKIA__ // DevServer should be referenced also in release
Assert.IsTrue(HotReloadHelper.IsSupported);
#else
Assert.IsFalse(HotReloadHelper.IsSupported);
#endif
}
}

[TestClass]
[RunsInSecondaryApp]
public class HotReloadTests
{
[TestMethod]
#if !__SKIA__
[Ignore("This tests assume directs access to the file system which not possible on this platform.")]
#endif
public async Task Is_SourcesEditable(CancellationToken ct)
{
var sutPath = "../../shared/HotReloadTests_Subject.xaml";
var dir = Path.GetDirectoryName(typeof(HotReloadHelper).Assembly.GetCustomAttribute<RuntimeTestsSourceProjectAttribute>()!.ProjectFullPath)!;
var file = Path.Combine(dir, sutPath);

Assert.IsTrue(File.Exists(file));
Assert.IsTrue(File.ReadAllText(file).Contains("Original text"));

await using var _ = await HotReloadHelper.UpdateSourceFile(sutPath, "Original text", "Updated text from Can_Edit_File", waitForMetadataUpdate: false, ct);

await TestHelper.WaitFor(() => File.ReadAllText(file).Contains("Updated text from Can_Edit_File"), ct);

Assert.IsTrue(File.ReadAllText(file).Contains("Updated text from Can_Edit_File"));
}

[TestMethod]
public async Task Is_CodeHotReload_Enabled(CancellationToken ct)
{
if (!HotReloadHelper.IsSupported)
{
Assert.Inconclusive("Hot reload testing is not supported on this platform.");
}

var sut = new HotReloadTest_SimpleSubject();

Debug.Assert(sut.Value == "42");

await using var _ = await HotReloadHelper.UpdateSourceFile("../../shared/HotReloadTest_SimpleSubject.cs", "42", "43", ct);

Debug.Assert(sut.Value == "43");
}

[TestMethod]
public async Task Is_UIHotReload_Enabled(CancellationToken ct)
{
if (!HotReloadHelper.IsSupported)
{
Assert.Inconclusive("Hot reload testing is not supported on this platform.");
}

await UIHelper.Load(new HotReloadTests_Subject(), ct);

Assert.AreEqual("Original text", UIHelper.GetChild<TextBlock>().Text);

await using var _ = await HotReloadHelper.UpdateSourceFile<HotReloadTests_Subject>("Original text", "Updated text", ct);

await AsyncAssert.AreEqual("Updated text", () => UIHelper.GetChild<TextBlock>().Text, ct);
}
}
14 changes: 14 additions & 0 deletions src/TestApp/shared/HotReloadTests_Subject.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Page
x:Class="Uno.UI.RuntimeTests.Engine.HotReloadTests_Subject"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Engine"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<TextBlock Text="Original text" />
</Grid>
</Page>
20 changes: 20 additions & 0 deletions src/TestApp/shared/HotReloadTests_Subject.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#if HAS_UNO_WINUI || WINDOWS_WINUI
using Microsoft.UI.Xaml.Controls;
#else
using Windows.UI.Xaml.Controls;
#endif

namespace Uno.UI.RuntimeTests.Engine
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class HotReloadTests_Subject : Page
{
public HotReloadTests_Subject()
{
this.InitializeComponent();
}
}
}
75 changes: 75 additions & 0 deletions src/TestApp/shared/SecondaryAppTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#pragma warning disable

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Uno.UI.RuntimeTests.Internal.Helpers;

namespace Uno.UI.RuntimeTests.Engine;

[TestClass]
public class SecondaryAppSanity
{
[TestMethod]
public void Is_SecondaryApp_Supported()
{
#if __SKIA__
Assert.IsTrue(SecondaryApp.IsSupported);
#else
Assert.IsFalse(SecondaryApp.IsSupported);
#endif
}
}

[TestClass]
[RunsInSecondaryApp(ignoreIfNotSupported: true)]
public class SecondaryAppTests
{
[TestMethod]
public void Is_From_A_Secondary_App()
{
Assert.IsTrue(Environment.GetEnvironmentVariable("UNO_RUNTIME_TESTS_IS_SECONDARY_APP") is "true");
}

[TestMethod]
public async Task Is_DevServer_Connected()
{
#if HAS_UNO_DEVSERVER
Assert.IsNotNull(Uno.UI.RemoteControl.RemoteControlClient.Instance);

var connected = Uno.UI.RemoteControl.RemoteControlClient.Instance.WaitForConnection(CancellationToken.None);
var timeout = Task.Delay(500);

Assert.AreEqual(connected, await Task.WhenAny(connected, timeout));
#else
Assert.Inconclusive("Dev server in not supported on this platform.");
#endif
}

#if DEBUG
[TestMethod]
public async Task No_Longer_Sane() // expected to fail
{
await Task.Delay(2000);

throw new Exception("Great works require a touch of insanity.");
}

[TestMethod, Ignore]
public void Is_An_Illusion() // expected to be ignored
{
}
#endif
}

[TestClass]
public class NotSecondaryAppTests
{
[TestMethod]
public void Is_From_A_Secondary_App()
{
Assert.IsFalse(Environment.GetEnvironmentVariable("UNO_RUNTIME_TESTS_IS_SECONDARY_APP") is "true");
}
}
10 changes: 10 additions & 0 deletions src/TestApp/shared/Uno.UI.RuntimeTests.Engine.Shared.shproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@
<Import Project="Uno.UI.RuntimeTests.Engine.Shared.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
<ItemGroup>
<UpToDateCheckInput Remove="HotReloadTests_Subject.xaml" />
</ItemGroup>
<ItemGroup>
<_Globbed_Compile Remove="FilterTests.cs" />
<_Globbed_Compile Remove="HotReloadTests.cs" />
<_Globbed_Compile Remove="HotReloadTests_Subject.xaml.cs" />
<_Globbed_Compile Remove="HotReloadTest_SimpleSubject.cs" />
<_Globbed_Compile Remove="MetaAttributes.cs" />
</ItemGroup>
<ItemGroup>
<_Globbled_Page Remove="HotReloadTests_Subject.xaml" />
</ItemGroup>
</Project>
Loading

0 comments on commit 3252d70

Please sign in to comment.