test(postit.android): add Xamarin.UITest smoke test on emulator

Adds AndroidAppLaunchTests to PostIt.Tests, a Xamarin.UITest-based
smoke test that launches the installed com.CompanyName.PostIt app
on the running emulator and waits for the first Avalonia frame
to render. The test skips cleanly when the app is not installed.

Currently FAILS RED on the local emulator: the installed APK has
no Activity declared (am start returns result code=-92, ACTIVITY_NOT_FOUND),
and Xamarin.UITest's test server cannot reach /ping. This is a
guardian test that will turn green once EmbedAssembliesIntoApk=true
is set in PostIt.Android.csproj (follow-up commit).

Also adds a Debug launch config in .vscode/launch.json that runs
the test under vsdbg, enabling breakpoints and object inspection
when investigating the failure.
This commit is contained in:
Paul Schneider 2026-08-20 05:27:41 +01:00
commit 41cc651b7d
Signed by: notazof
GPG key ID: 1DD5D838E5343B06
4 changed files with 91 additions and 1 deletions

View file

@ -0,0 +1,76 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Xamarin.UITest;
using Xamarin.UITest.Android;
using Xunit;
namespace PostIt.Tests;
/// <summary>
/// Smoke test: launches the installed PostIt.Android app on the running
/// emulator and waits for the first Avalonia frame to render. Reveals the
/// "démarrage KO" bug — the test fails if Avalonia never draws a frame
/// within the timeout.
///
/// Skip conditions: the package is not installed on the connected device,
/// or no device is connected via adb.
/// </summary>
public class AndroidAppLaunchTests
{
private const string PackageName = "com.CompanyName.PostIt";
private readonly ITestOutputHelper _output;
public AndroidAppLaunchTests(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void PostIt_starts_and_draws_a_first_frame_on_the_emulator()
{
if (!IsPackageInstalledOnAnyDevice())
{
_output.WriteLine($"[skip] {PackageName} not installed on any device");
return;
}
_output.WriteLine($"[step] configuring app via InstalledApp({PackageName})");
var app = ConfigureApp.Android
.InstalledApp(PackageName)
.StartApp(Xamarin.UITest.Configuration.AppDataMode.DoNotClear);
_output.WriteLine("[step] app.StartApp returned, waiting for first frame");
app.WaitForElement(
e => e.Class("android.view.View"),
timeout: TimeSpan.FromSeconds(30));
_output.WriteLine("[step] first frame observed");
}
private static bool IsPackageInstalledOnAnyDevice()
{
try
{
var startInfo = new ProcessStartInfo("adb", "shell pm list packages")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
using var proc = Process.Start(startInfo);
if (proc is null) return false;
var stdout = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(5000);
return stdout
.Split('\n', StringSplitOptions.RemoveEmptyEntries)
.Any(line => line.Trim().Equals($"package:{PackageName}", StringComparison.Ordinal));
}
catch
{
return false;
}
}
}

View file

@ -1,10 +1,10 @@
<Project>
<!-- Pull in shared package versions from the repository root. -->
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Packages.props', '$(MSBuildThisFileDirectory)../'))" />
<!-- PostIt.Tests-specific versions -->
<ItemGroup>
<PackageVersion Include="Avalonia.Headless" Version="12.0.4" />
<PackageVersion Include="Avalonia.Headless.XUnit" Version="12.0.4" />
<PackageVersion Include="Xamarin.UITest" Version="4.4.2" />
</ItemGroup>
</Project>

View file

@ -13,6 +13,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Xamarin.UITest" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="coverlet.collector" />