Skip to content

Commit

Permalink
update maui devicetests from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Nov 16, 2023
1 parent 0d643ff commit 3b0ef43
Show file tree
Hide file tree
Showing 66 changed files with 4,684 additions and 2,788 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
using Microsoft.CodeAnalysis;

namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;

[Generator]
public class CodeBehindGenerator : ISourceGenerator
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
{
public void Initialize(GeneratorInitializationContext context)
[Generator]
public class CodeBehindGenerator : ISourceGenerator
{
//#if DEBUG
//if (!System.Diagnostics.Debugger.IsAttached)
// System.Diagnostics.Debugger.Launch();
//#endif
}
public void Initialize(GeneratorInitializationContext context)
{
//#if DEBUG
//if (!System.Diagnostics.Debugger.IsAttached)
// System.Diagnostics.Debugger.Launch();
//#endif
}

public void Execute(GeneratorExecutionContext context)
{
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.TargetFramework", out var targetFramework))
return;
public void Execute(GeneratorExecutionContext context)
{
if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.TargetFramework", out var targetFramework))
return;

context.Log($"TargetFramework: {targetFramework}");
context.Log($"TargetFramework: {targetFramework}");

var generator = new RunnerGenerator(context, targetFramework);
var generator = new RunnerGenerator(context, targetFramework);

generator?.Generate();
generator?.Generate();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;

namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;

static class GeneratorDiagnostics
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
{
public static readonly DiagnosticDescriptor LoggingMessage = new DiagnosticDescriptor(
id: "TST1001",
title: "Logging Message",
messageFormat: "{0}",
category: "Logging",
DiagnosticSeverity.Info,
isEnabledByDefault: true);
static class GeneratorDiagnostics
{
public static readonly DiagnosticDescriptor LoggingMessage = new DiagnosticDescriptor(
id: "TST1001",
title: "Logging Message",
messageFormat: "{0}",
category: "Logging",
DiagnosticSeverity.Info,
isEnabledByDefault: true);

[Conditional("DEBUG")]
public static void Log(this GeneratorExecutionContext context, string message) =>
context.ReportDiagnostic(Diagnostic.Create(LoggingMessage, Location.None, message));
[Conditional("DEBUG")]
public static void Log(this GeneratorExecutionContext context, string message) =>
context.ReportDiagnostic(Diagnostic.Create(LoggingMessage, Location.None, message));
}
}
149 changes: 75 additions & 74 deletions test/MauiTestUtils/DeviceTests.Runners.SourceGen/RunnerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,99 +3,99 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;

public class RunnerGenerator
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen
{
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
public class RunnerGenerator
{
Context = context;
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
{
Context = context;

TargetFramework = targetFramework;
TargetFramework = targetFramework;

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationId", out var applicationId);
context.Log($"ApplicationId: {applicationId}");
ApplicationId = applicationId ?? throw new Exception("ApplicationId needs to be set.");
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationId", out var applicationId);
context.Log($"ApplicationId: {applicationId}");
ApplicationId = applicationId ?? throw new Exception("ApplicationId needs to be set.");

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationTitle", out var applicationTitle);
context.Log($"ApplicationTitle: {applicationTitle}");
ApplicationTitle = applicationTitle ?? "Tests";
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.ApplicationTitle", out var applicationTitle);
context.Log($"ApplicationTitle: {applicationTitle}");
ApplicationTitle = applicationTitle ?? "Tests";

context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
context.Log($"RootNamespace: {rootNamespace}");
RootNamespace = rootNamespace ?? "TestRunnerNamespace";
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.RootNamespace", out var rootNamespace);
context.Log($"RootNamespace: {rootNamespace}");
RootNamespace = rootNamespace ?? "TestRunnerNamespace";

ContainsSplashScreen = false;
foreach (var file in context.AdditionalFiles)
{
var options = context.AnalyzerConfigOptions.GetOptions(file);
if (options.TryGetValue("build_metadata.AdditionalFiles.IsMauiSplashScreen", out var isMauiSplashScreen) && bool.TryParse(isMauiSplashScreen, out var isSplash) && isSplash)
ContainsSplashScreen = false;
foreach (var file in context.AdditionalFiles)
{
ContainsSplashScreen = true;
break;
var options = context.AnalyzerConfigOptions.GetOptions(file);
if (options.TryGetValue("build_metadata.AdditionalFiles.IsMauiSplashScreen", out var isMauiSplashScreen) && bool.TryParse(isMauiSplashScreen, out var isSplash) && isSplash)
{
ContainsSplashScreen = true;
break;
}
}
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
}
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
}

public GeneratorExecutionContext Context { get; }
public GeneratorExecutionContext Context { get; }

public string TargetFramework { get; }
public string TargetFramework { get; }

public string RootNamespace { get; }
public string RootNamespace { get; }

public string ApplicationId { get; }
public string ApplicationId { get; }

public string ApplicationTitle { get; }
public string ApplicationTitle { get; }

public bool ContainsSplashScreen { get; }
public bool ContainsSplashScreen { get; }

public void Generate()
{
Context.Log($"Generating runners...");

if (TargetFramework.IndexOf("-android", StringComparison.OrdinalIgnoreCase) != -1)
public void Generate()
{
var code = GenerateAndroidSource();
var name = "TestRunner.Android.sg.cs";
Context.Log($"Generating runners...");

AddSource(name, code);
}
else if (TargetFramework.IndexOf("-ios", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateIosSource();
var name = "TestRunner.iOS.sg.cs";
if (TargetFramework.IndexOf("-android", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateAndroidSource();
var name = "TestRunner.Android.sg.cs";

AddSource(name, code);
}
else if (TargetFramework.IndexOf("-maccatalyst", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateIosSource();
var name = "TestRunner.MacCatalyst.sg.cs";
AddSource(name, code);
}
else if (TargetFramework.IndexOf("-ios", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateIosSource();
var name = "TestRunner.iOS.sg.cs";

AddSource(name, code);
AddSource(name, code);
}
else if (TargetFramework.IndexOf("-maccatalyst", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateIosSource();
var name = "TestRunner.MacCatalyst.sg.cs";

AddSource(name, code);
}
}
}

protected void AddSource(string filename, string contents)
{
Context.Log($"AddSource: {filename}");
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
}
protected void AddSource(string filename, string contents)
{
Context.Log($"AddSource: {filename}");
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
}

string GenerateAndroidSource()
{
var mauiProgramName = "MauiProgram";
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "";
string GenerateAndroidSource()
{
var mauiProgramName = "MauiProgram";
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "";

var appName = "MainApplication";
var visualActivityName = "MainActivity";
var appName = "MainApplication";
var visualActivityName = "MainActivity";

var instrumentationName = "TestInstrumentation";
var headlessActivityName = "TestActivity";
var instrumentationName = "TestInstrumentation";
var headlessActivityName = "TestActivity";

return @"
return @"
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_APPLICATION_GENERATION
namespace " + RootNamespace + @"
{
Expand Down Expand Up @@ -162,16 +162,16 @@ public partial class " + headlessActivityName + @" : global::Microsoft.Maui.Test
}
#endif
";
}
}

string GenerateIosSource()
{
var mauiProgramName = "MauiProgram";
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
var visualDelegateName = "VisualRunnerAppDelegate";
var headlessDelegateName = "HeadlessRunnerAppDelegate";
string GenerateIosSource()
{
var mauiProgramName = "MauiProgram";
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
var visualDelegateName = "VisualRunnerAppDelegate";
var headlessDelegateName = "HeadlessRunnerAppDelegate";

return @"
return @"
#if !SKIP_RUNNER_ENTRYPOINT_GENERATION && !SKIP_VISUAL_RUNNER_ENTRYPOINT_GENERATION && !SKIP_RUNNER_PROGRAM_GENERATION
namespace " + RootNamespace + @"
{
Expand Down Expand Up @@ -219,5 +219,6 @@ partial class " + headlessDelegateName + @" : global::Microsoft.Maui.TestUtils.D
}
#endif
";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down
62 changes: 38 additions & 24 deletions test/MauiTestUtils/DeviceTests.Runners/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,50 @@
using Microsoft.Maui.TestUtils.DeviceTests.Runners.HeadlessRunner;
using Microsoft.Maui.TestUtils.DeviceTests.Runners.VisualRunner;

namespace Microsoft.Maui.TestUtils.DeviceTests.Runners;

public static class AppHostBuilderExtensions
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners
{
public static MauiAppBuilder ConfigureTests(this MauiAppBuilder appHostBuilder, TestOptions options)
public static class AppHostBuilderExtensions
{
appHostBuilder.Services.AddSingleton(options);

return appHostBuilder;
}
public static MauiAppBuilder ConfigureTests(this MauiAppBuilder appHostBuilder, TestOptions options)
{
appHostBuilder.Services.AddSingleton(options);

return appHostBuilder;
}

public static MauiAppBuilder UseVisualRunner(this MauiAppBuilder appHostBuilder)
{
appHostBuilder.UseMauiApp(svc => new MauiVisualRunnerApp(
svc.GetRequiredService<TestOptions>(),
svc.GetRequiredService<ILoggerFactory>().CreateLogger("TestRun")));

return appHostBuilder;
}

public static MauiAppBuilder UseHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
{
appHostBuilder.Services.AddSingleton(options);

#if __ANDROID__ || __IOS__ || MACCATALYST || WINDOWS
appHostBuilder.Services.AddTransient(svc => new HeadlessTestRunner(
svc.GetRequiredService<HeadlessRunnerOptions>(),
svc.GetRequiredService<TestOptions>()));
#endif

public static MauiAppBuilder UseVisualRunner(this MauiAppBuilder appHostBuilder)
{
appHostBuilder.UseMauiApp(svc => new MauiVisualRunnerApp(
svc.GetRequiredService<TestOptions>(),
svc.GetRequiredService<ILoggerFactory>().CreateLogger("TestRun")));
return appHostBuilder;
}

return appHostBuilder;
}
#if WINDOWS
public static MauiAppBuilder UseControlsHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
{
appHostBuilder.Services.AddSingleton(options);

public static MauiAppBuilder UseHeadlessRunner(this MauiAppBuilder appHostBuilder, HeadlessRunnerOptions options)
{
appHostBuilder.Services.AddSingleton(options);
appHostBuilder.Services.AddTransient(svc => new ControlsHeadlessTestRunner(
svc.GetRequiredService<HeadlessRunnerOptions>(),
svc.GetRequiredService<TestOptions>()));

#if __ANDROID__ || __IOS__ || MACCATALYST
appHostBuilder.Services.AddTransient(svc => new HeadlessTestRunner(
svc.GetRequiredService<HeadlessRunnerOptions>(),
svc.GetRequiredService<TestOptions>()));
return appHostBuilder;
}
#endif

return appHostBuilder;
}
}
4 changes: 4 additions & 0 deletions test/MauiTestUtils/DeviceTests.Runners/GlobalNamespaces.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using Microsoft.Maui;
global using Microsoft.Maui.Graphics;
global using Microsoft.Maui.Handlers;
global using Microsoft.Maui.Platform;
Loading

0 comments on commit 3b0ef43

Please sign in to comment.