2026-08-20 05:27:41 +01:00
using System.Diagnostics ;
using Xamarin.UITest ;
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>
2026-08-29 16:23:58 +01:00
[Trait("Category", "Platform-Android")]
2026-08-20 05:27:41 +01:00
public class AndroidAppLaunchTests
{
2026-08-25 20:30:39 +01:00
private const string PackageName = "fr.pschneider.postit" ;
2026-08-20 05:27:41 +01:00
private readonly ITestOutputHelper _output ;
public AndroidAppLaunchTests ( ITestOutputHelper output )
{
_output = output ;
}
2026-08-29 16:23:58 +01:00
// TODO https://twosixtech.com/blog/integrating-docker-and-adb/
[Fact]
2026-08-20 05:27:41 +01:00
public void PostIt_starts_and_draws_a_first_frame_on_the_emulator ( )
{
2026-10-21 14:05:54 +01:00
if ( IsRunningInCi ( ) )
{
_output . WriteLine ( "[skip] Android instrumentation smoke test disabled in CI because the emulator/test APK signature must match the target package exactly and is not reliable on hosted runners" ) ;
return ;
}
2026-08-20 05:27:41 +01:00
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" ) ;
}
2026-10-21 14:05:54 +01:00
private static bool IsRunningInCi ( )
{
return Environment . GetEnvironmentVariable ( "CI" ) is { Length : > 0 }
| | Environment . GetEnvironmentVariable ( "GITHUB_ACTIONS" ) is { Length : > 0 }
| | Environment . GetEnvironmentVariable ( "FORGEJO_CI" ) is { Length : > 0 }
| | Environment . GetEnvironmentVariable ( "CI_NAME" ) is { Length : > 0 } ;
}
2026-08-20 05:27:41 +01:00
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 ;
}
}
}