Skip to content

Commit

Permalink
roll back MauiTestUtils upstream update
Browse files Browse the repository at this point in the history
  • Loading branch information
vaind committed Nov 16, 2023
1 parent 1ae3f3e commit bed0abc
Show file tree
Hide file tree
Showing 65 changed files with 2,784 additions and 4,680 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
using Microsoft.CodeAnalysis;

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

[Generator]
public class CodeBehindGenerator : ISourceGenerator
{
[Generator]
public class CodeBehindGenerator : ISourceGenerator
public void Initialize(GeneratorInitializationContext context)
{
public void Initialize(GeneratorInitializationContext context)
{
//#if DEBUG
//if (!System.Diagnostics.Debugger.IsAttached)
// System.Diagnostics.Debugger.Launch();
//#endif
}
//#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,20 +1,19 @@
using System.Diagnostics;
using Microsoft.CodeAnalysis;

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

static class GeneratorDiagnostics
{
static class GeneratorDiagnostics
{
public static readonly DiagnosticDescriptor LoggingMessage = new DiagnosticDescriptor(
id: "TST1001",
title: "Logging Message",
messageFormat: "{0}",
category: "Logging",
DiagnosticSeverity.Info,
isEnabledByDefault: true);
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: 74 additions & 75 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
namespace Microsoft.Maui.TestUtils.DeviceTests.Runners.SourceGen;

public class RunnerGenerator
{
public class RunnerGenerator
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
{
public RunnerGenerator(GeneratorExecutionContext context, string targetFramework)
{
Context = context;
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)
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)
{
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;
}
ContainsSplashScreen = true;
break;
}
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
}
context.Log($"ContainsSplashScreen: {ContainsSplashScreen}");
}

public GeneratorExecutionContext Context { get; }

public string TargetFramework { get; }

public string RootNamespace { get; }
public GeneratorExecutionContext Context { get; }

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

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

public bool ContainsSplashScreen { get; }
public string ApplicationId { get; }

public void Generate()
{
Context.Log($"Generating runners...");
public string ApplicationTitle { get; }

if (TargetFramework.IndexOf("-android", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateAndroidSource();
var name = "TestRunner.Android.sg.cs";
public bool ContainsSplashScreen { get; }

AddSource(name, code);
}
else if (TargetFramework.IndexOf("-ios", StringComparison.OrdinalIgnoreCase) != -1)
{
var code = GenerateIosSource();
var name = "TestRunner.iOS.sg.cs";
public void Generate()
{
Context.Log($"Generating runners...");

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

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

protected void AddSource(string filename, string contents)
AddSource(name, code);
}
else if (TargetFramework.IndexOf("-maccatalyst", StringComparison.OrdinalIgnoreCase) != -1)
{
Context.Log($"AddSource: {filename}");
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
var code = GenerateIosSource();
var name = "TestRunner.MacCatalyst.sg.cs";

AddSource(name, code);
}
}

string GenerateAndroidSource()
{
var mauiProgramName = "MauiProgram";
var mauiProgramFullName = @"global::" + RootNamespace + "." + mauiProgramName;
var splash = ContainsSplashScreen ? @"Theme = ""@style/Maui.SplashTheme""," : "";
protected void AddSource(string filename, string contents)
{
Context.Log($"AddSource: {filename}");
Context.AddSource(filename, SourceText.From(contents, Encoding.UTF8));
}

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

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,6 +219,5 @@ 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: 24 additions & 38 deletions test/MauiTestUtils/DeviceTests.Runners/AppHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,36 @@
using Microsoft.Maui.TestUtils.DeviceTests.Runners.HeadlessRunner;
using Microsoft.Maui.TestUtils.DeviceTests.Runners.VisualRunner;

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

public static class AppHostBuilderExtensions
{
public static class AppHostBuilderExtensions
public static MauiAppBuilder ConfigureTests(this MauiAppBuilder appHostBuilder, TestOptions options)
{
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
appHostBuilder.Services.AddSingleton(options);

return appHostBuilder;
}

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

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

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

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

return appHostBuilder;
}
}
4 changes: 0 additions & 4 deletions test/MauiTestUtils/DeviceTests.Runners/GlobalNamespaces.cs

This file was deleted.

Loading

0 comments on commit bed0abc

Please sign in to comment.